raspberry-pi.patch 3.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454744557445674457744587445974460744617446274463744647446574466744677446874469744707447174472744737447474475744767447774478744797448074481744827448374484744857448674487744887448974490744917449274493744947449574496744977449874499745007450174502745037450474505745067450774508745097451074511745127451374514745157451674517745187451974520745217452274523745247452574526745277452874529745307453174532745337453474535745367453774538745397454074541745427454374544745457454674547745487454974550745517455274553745547455574556745577455874559745607456174562745637456474565745667456774568745697457074571745727457374574745757457674577745787457974580745817458274583745847458574586745877458874589745907459174592745937459474595745967459774598745997460074601746027460374604746057460674607746087460974610746117461274613746147461574616746177461874619746207462174622746237462474625746267462774628746297463074631746327463374634746357463674637746387463974640746417464274643746447464574646746477464874649746507465174652746537465474655746567465774658746597466074661746627466374664746657466674667746687466974670746717467274673746747467574676746777467874679746807468174682746837468474685746867468774688746897469074691746927469374694746957469674697746987469974700747017470274703747047470574706747077470874709747107471174712747137471474715747167471774718747197472074721747227472374724747257472674727747287472974730747317473274733747347473574736747377473874739747407474174742747437474474745747467474774748747497475074751747527475374754747557475674757747587475974760747617476274763747647476574766747677476874769747707477174772747737477474775747767477774778747797478074781747827478374784747857478674787747887478974790747917479274793747947479574796747977479874799748007480174802748037480474805748067480774808748097481074811748127481374814748157481674817748187481974820748217482274823748247482574826748277482874829748307483174832748337483474835748367483774838748397484074841748427484374844748457484674847748487484974850748517485274853748547485574856748577485874859748607486174862748637486474865748667486774868748697487074871748727487374874748757487674877748787487974880748817488274883748847488574886748877488874889748907489174892748937489474895748967489774898748997490074901749027490374904749057490674907749087490974910749117491274913749147491574916749177491874919749207492174922749237492474925749267492774928749297493074931749327493374934749357493674937749387493974940749417494274943749447494574946749477494874949749507495174952749537495474955749567495774958749597496074961749627496374964749657496674967749687496974970749717497274973749747497574976749777497874979749807498174982749837498474985749867498774988749897499074991749927499374994749957499674997749987499975000750017500275003750047500575006750077500875009750107501175012750137501475015750167501775018750197502075021750227502375024750257502675027750287502975030750317503275033750347503575036750377503875039750407504175042750437504475045750467504775048750497505075051750527505375054750557505675057750587505975060750617506275063750647506575066750677506875069750707507175072750737507475075750767507775078750797508075081750827508375084750857508675087750887508975090750917509275093750947509575096750977509875099751007510175102751037510475105751067510775108751097511075111751127511375114751157511675117751187511975120751217512275123751247512575126751277512875129751307513175132751337513475135751367513775138751397514075141751427514375144751457514675147751487514975150751517515275153751547515575156751577515875159751607516175162751637516475165751667516775168751697517075171751727517375174751757517675177751787517975180751817518275183751847518575186751877518875189751907519175192751937519475195751967519775198751997520075201752027520375204752057520675207752087520975210752117521275213752147521575216752177521875219752207522175222752237522475225752267522775228752297523075231752327523375234752357523675237752387523975240752417524275243752447524575246752477524875249752507525175252752537525475255752567525775258752597526075261752627526375264752657526675267752687526975270752717527275273752747527575276752777527875279752807528175282752837528475285752867528775288752897529075291752927529375294752957529675297752987529975300753017530275303753047530575306753077530875309753107531175312753137531475315753167531775318753197532075321753227532375324753257532675327753287532975330753317533275333753347533575336753377533875339753407534175342753437534475345753467534775348753497535075351753527535375354753557535675357753587535975360753617536275363753647536575366753677536875369753707537175372753737537475375753767537775378753797538075381753827538375384753857538675387753887538975390753917539275393753947539575396753977539875399754007540175402754037540475405754067540775408754097541075411754127541375414754157541675417754187541975420754217542275423754247542575426754277542875429754307543175432754337543475435754367543775438754397544075441754427544375444754457544675447754487544975450754517545275453754547545575456754577545875459754607546175462754637546475465754667546775468754697547075471754727547375474754757547675477754787547975480754817548275483754847548575486754877548875489754907549175492754937549475495754967549775498754997550075501755027550375504755057550675507755087550975510755117551275513755147551575516755177551875519755207552175522755237552475525755267552775528755297553075531755327553375534755357553675537755387553975540755417554275543755447554575546755477554875549755507555175552755537555475555755567555775558755597556075561755627556375564755657556675567755687556975570755717557275573755747557575576755777557875579755807558175582755837558475585755867558775588755897559075591755927559375594755957559675597755987559975600756017560275603756047560575606756077560875609756107561175612756137561475615756167561775618756197562075621756227562375624756257562675627756287562975630756317563275633756347563575636756377563875639756407564175642756437564475645756467564775648756497565075651756527565375654756557565675657756587565975660756617566275663756647566575666756677566875669756707567175672756737567475675756767567775678756797568075681756827568375684756857568675687756887568975690756917569275693756947569575696756977569875699757007570175702757037570475705757067570775708757097571075711757127571375714757157571675717757187571975720757217572275723757247572575726757277572875729757307573175732757337573475735757367573775738757397574075741757427574375744757457574675747757487574975750757517575275753757547575575756757577575875759757607576175762757637576475765757667576775768757697577075771757727577375774757757577675777757787577975780757817578275783757847578575786757877578875789757907579175792757937579475795757967579775798757997580075801758027580375804758057580675807758087580975810758117581275813758147581575816758177581875819758207582175822758237582475825758267582775828758297583075831758327583375834758357583675837758387583975840758417584275843758447584575846758477584875849758507585175852758537585475855758567585775858758597586075861758627586375864758657586675867758687586975870758717587275873758747587575876758777587875879758807588175882758837588475885758867588775888758897589075891758927589375894758957589675897758987589975900759017590275903759047590575906759077590875909759107591175912759137591475915759167591775918759197592075921759227592375924759257592675927759287592975930759317593275933759347593575936759377593875939759407594175942759437594475945759467594775948759497595075951759527595375954759557595675957759587595975960759617596275963759647596575966759677596875969759707597175972759737597475975759767597775978759797598075981759827598375984759857598675987759887598975990759917599275993759947599575996759977599875999760007600176002760037600476005760067600776008760097601076011760127601376014760157601676017760187601976020760217602276023760247602576026760277602876029760307603176032760337603476035760367603776038760397604076041760427604376044760457604676047760487604976050760517605276053760547605576056760577605876059760607606176062760637606476065760667606776068760697607076071760727607376074760757607676077760787607976080760817608276083760847608576086760877608876089760907609176092760937609476095760967609776098760997610076101761027610376104761057610676107761087610976110761117611276113761147611576116761177611876119761207612176122761237612476125761267612776128761297613076131761327613376134761357613676137761387613976140761417614276143761447614576146761477614876149761507615176152761537615476155761567615776158761597616076161761627616376164761657616676167761687616976170761717617276173761747617576176761777617876179761807618176182761837618476185761867618776188761897619076191761927619376194761957619676197761987619976200762017620276203762047620576206762077620876209762107621176212762137621476215762167621776218762197622076221762227622376224762257622676227762287622976230762317623276233762347623576236762377623876239762407624176242762437624476245762467624776248762497625076251762527625376254762557625676257762587625976260762617626276263762647626576266762677626876269762707627176272762737627476275762767627776278762797628076281762827628376284762857628676287762887628976290762917629276293762947629576296762977629876299763007630176302763037630476305763067630776308763097631076311763127631376314763157631676317763187631976320763217632276323763247632576326763277632876329763307633176332763337633476335763367633776338763397634076341763427634376344763457634676347763487634976350763517635276353763547635576356763577635876359763607636176362763637636476365763667636776368763697637076371763727637376374763757637676377763787637976380763817638276383763847638576386763877638876389763907639176392763937639476395763967639776398763997640076401764027640376404764057640676407764087640976410764117641276413764147641576416764177641876419764207642176422764237642476425764267642776428764297643076431764327643376434764357643676437764387643976440764417644276443764447644576446764477644876449764507645176452764537645476455764567645776458764597646076461764627646376464764657646676467764687646976470764717647276473764747647576476764777647876479764807648176482764837648476485764867648776488764897649076491764927649376494764957649676497764987649976500765017650276503765047650576506765077650876509765107651176512765137651476515765167651776518765197652076521765227652376524765257652676527765287652976530765317653276533765347653576536765377653876539765407654176542765437654476545765467654776548765497655076551765527655376554765557655676557765587655976560765617656276563765647656576566765677656876569765707657176572765737657476575765767657776578765797658076581765827658376584765857658676587765887658976590765917659276593765947659576596765977659876599766007660176602766037660476605766067660776608766097661076611766127661376614766157661676617766187661976620766217662276623766247662576626766277662876629766307663176632766337663476635766367663776638766397664076641766427664376644766457664676647766487664976650766517665276653766547665576656766577665876659766607666176662766637666476665766667666776668766697667076671766727667376674766757667676677766787667976680766817668276683766847668576686766877668876689766907669176692766937669476695766967669776698766997670076701767027670376704767057670676707767087670976710767117671276713767147671576716767177671876719767207672176722767237672476725767267672776728767297673076731767327673376734767357673676737767387673976740767417674276743767447674576746767477674876749767507675176752767537675476755767567675776758767597676076761767627676376764767657676676767767687676976770767717677276773767747677576776767777677876779767807678176782767837678476785767867678776788767897679076791767927679376794767957679676797767987679976800768017680276803768047680576806768077680876809768107681176812768137681476815768167681776818768197682076821768227682376824768257682676827768287682976830768317683276833768347683576836768377683876839768407684176842768437684476845768467684776848768497685076851768527685376854768557685676857768587685976860768617686276863768647686576866768677686876869768707687176872768737687476875768767687776878768797688076881768827688376884768857688676887768887688976890768917689276893768947689576896768977689876899769007690176902769037690476905769067690776908769097691076911769127691376914769157691676917769187691976920769217692276923769247692576926769277692876929769307693176932769337693476935769367693776938769397694076941769427694376944769457694676947769487694976950769517695276953769547695576956769577695876959769607696176962769637696476965769667696776968769697697076971769727697376974769757697676977769787697976980769817698276983769847698576986769877698876989769907699176992769937699476995769967699776998769997700077001770027700377004770057700677007770087700977010770117701277013770147701577016770177701877019770207702177022770237702477025770267702777028770297703077031770327703377034770357703677037770387703977040770417704277043770447704577046770477704877049770507705177052770537705477055770567705777058770597706077061770627706377064770657706677067770687706977070770717707277073770747707577076770777707877079770807708177082770837708477085770867708777088770897709077091770927709377094770957709677097770987709977100771017710277103771047710577106771077710877109771107711177112771137711477115771167711777118771197712077121771227712377124771257712677127771287712977130771317713277133771347713577136771377713877139771407714177142771437714477145771467714777148771497715077151771527715377154771557715677157771587715977160771617716277163771647716577166771677716877169771707717177172771737717477175771767717777178771797718077181771827718377184771857718677187771887718977190771917719277193771947719577196771977719877199772007720177202772037720477205772067720777208772097721077211772127721377214772157721677217772187721977220772217722277223772247722577226772277722877229772307723177232772337723477235772367723777238772397724077241772427724377244772457724677247772487724977250772517725277253772547725577256772577725877259772607726177262772637726477265772667726777268772697727077271772727727377274772757727677277772787727977280772817728277283772847728577286772877728877289772907729177292772937729477295772967729777298772997730077301773027730377304773057730677307773087730977310773117731277313773147731577316773177731877319773207732177322773237732477325773267732777328773297733077331773327733377334773357733677337773387733977340773417734277343773447734577346773477734877349773507735177352773537735477355773567735777358773597736077361773627736377364773657736677367773687736977370773717737277373773747737577376773777737877379773807738177382773837738477385773867738777388773897739077391773927739377394773957739677397773987739977400774017740277403774047740577406774077740877409774107741177412774137741477415774167741777418774197742077421774227742377424774257742677427774287742977430774317743277433774347743577436774377743877439774407744177442774437744477445774467744777448774497745077451774527745377454774557745677457774587745977460774617746277463774647746577466774677746877469774707747177472774737747477475774767747777478774797748077481774827748377484774857748677487774887748977490774917749277493774947749577496774977749877499775007750177502775037750477505775067750777508775097751077511775127751377514775157751677517775187751977520775217752277523775247752577526775277752877529775307753177532775337753477535775367753777538775397754077541775427754377544775457754677547775487754977550775517755277553775547755577556775577755877559775607756177562775637756477565775667756777568775697757077571775727757377574775757757677577775787757977580775817758277583775847758577586775877758877589775907759177592775937759477595775967759777598775997760077601776027760377604776057760677607776087760977610776117761277613776147761577616776177761877619776207762177622776237762477625776267762777628776297763077631776327763377634776357763677637776387763977640776417764277643776447764577646776477764877649776507765177652776537765477655776567765777658776597766077661776627766377664776657766677667776687766977670776717767277673776747767577676776777767877679776807768177682776837768477685776867768777688776897769077691776927769377694776957769677697776987769977700777017770277703777047770577706777077770877709777107771177712777137771477715777167771777718777197772077721777227772377724777257772677727777287772977730777317773277733777347773577736777377773877739777407774177742777437774477745777467774777748777497775077751777527775377754777557775677757777587775977760777617776277763777647776577766777677776877769777707777177772777737777477775777767777777778777797778077781777827778377784777857778677787777887778977790777917779277793777947779577796777977779877799778007780177802778037780477805778067780777808778097781077811778127781377814778157781677817778187781977820778217782277823778247782577826778277782877829778307783177832778337783477835778367783777838778397784077841778427784377844778457784677847778487784977850778517785277853778547785577856778577785877859778607786177862778637786477865778667786777868778697787077871778727787377874778757787677877778787787977880778817788277883778847788577886778877788877889778907789177892778937789477895778967789777898778997790077901779027790377904779057790677907779087790977910779117791277913779147791577916779177791877919779207792177922779237792477925779267792777928779297793077931779327793377934779357793677937779387793977940779417794277943779447794577946779477794877949779507795177952779537795477955779567795777958779597796077961779627796377964779657796677967779687796977970779717797277973779747797577976779777797877979779807798177982779837798477985779867798777988779897799077991779927799377994779957799677997779987799978000780017800278003780047800578006780077800878009780107801178012780137801478015780167801778018780197802078021780227802378024780257802678027780287802978030780317803278033780347803578036780377803878039780407804178042780437804478045780467804778048780497805078051780527805378054780557805678057780587805978060780617806278063780647806578066780677806878069780707807178072780737807478075780767807778078780797808078081780827808378084780857808678087780887808978090780917809278093780947809578096780977809878099781007810178102781037810478105781067810778108781097811078111781127811378114781157811678117781187811978120781217812278123781247812578126781277812878129781307813178132781337813478135781367813778138781397814078141781427814378144781457814678147781487814978150781517815278153781547815578156781577815878159781607816178162781637816478165781667816778168781697817078171781727817378174781757817678177781787817978180781817818278183781847818578186781877818878189781907819178192781937819478195781967819778198781997820078201782027820378204782057820678207782087820978210782117821278213782147821578216782177821878219782207822178222782237822478225782267822778228782297823078231782327823378234782357823678237782387823978240782417824278243782447824578246782477824878249782507825178252782537825478255782567825778258782597826078261782627826378264782657826678267782687826978270782717827278273782747827578276782777827878279782807828178282782837828478285782867828778288782897829078291782927829378294782957829678297782987829978300783017830278303783047830578306783077830878309783107831178312783137831478315783167831778318783197832078321783227832378324783257832678327783287832978330783317833278333783347833578336783377833878339783407834178342783437834478345783467834778348783497835078351783527835378354783557835678357783587835978360783617836278363783647836578366783677836878369783707837178372783737837478375783767837778378783797838078381783827838378384783857838678387783887838978390783917839278393783947839578396783977839878399784007840178402784037840478405784067840778408784097841078411784127841378414784157841678417784187841978420784217842278423784247842578426784277842878429784307843178432784337843478435784367843778438784397844078441784427844378444784457844678447784487844978450784517845278453784547845578456784577845878459784607846178462784637846478465784667846778468784697847078471784727847378474784757847678477784787847978480784817848278483784847848578486784877848878489784907849178492784937849478495784967849778498784997850078501785027850378504785057850678507785087850978510785117851278513785147851578516785177851878519785207852178522785237852478525785267852778528785297853078531785327853378534785357853678537785387853978540785417854278543785447854578546785477854878549785507855178552785537855478555785567855778558785597856078561785627856378564785657856678567785687856978570785717857278573785747857578576785777857878579785807858178582785837858478585785867858778588785897859078591785927859378594785957859678597785987859978600786017860278603786047860578606786077860878609786107861178612786137861478615786167861778618786197862078621786227862378624786257862678627786287862978630786317863278633786347863578636786377863878639786407864178642786437864478645786467864778648786497865078651786527865378654786557865678657786587865978660786617866278663786647866578666786677866878669786707867178672786737867478675786767867778678786797868078681786827868378684786857868678687786887868978690786917869278693786947869578696786977869878699787007870178702787037870478705787067870778708787097871078711787127871378714787157871678717787187871978720787217872278723787247872578726787277872878729787307873178732787337873478735787367873778738787397874078741787427874378744787457874678747787487874978750787517875278753787547875578756787577875878759787607876178762787637876478765787667876778768787697877078771787727877378774787757877678777787787877978780787817878278783787847878578786787877878878789787907879178792787937879478795787967879778798787997880078801788027880378804788057880678807788087880978810788117881278813788147881578816788177881878819788207882178822788237882478825788267882778828788297883078831788327883378834788357883678837788387883978840788417884278843788447884578846788477884878849788507885178852788537885478855788567885778858788597886078861788627886378864788657886678867788687886978870788717887278873788747887578876788777887878879788807888178882788837888478885788867888778888788897889078891788927889378894788957889678897788987889978900789017890278903789047890578906789077890878909789107891178912789137891478915789167891778918789197892078921789227892378924789257892678927789287892978930789317893278933789347893578936789377893878939789407894178942789437894478945789467894778948789497895078951789527895378954789557895678957789587895978960789617896278963789647896578966789677896878969789707897178972789737897478975789767897778978789797898078981789827898378984789857898678987789887898978990789917899278993789947899578996789977899878999790007900179002790037900479005790067900779008790097901079011790127901379014790157901679017790187901979020790217902279023790247902579026790277902879029790307903179032790337903479035790367903779038790397904079041790427904379044790457904679047790487904979050790517905279053790547905579056790577905879059790607906179062790637906479065790667906779068790697907079071790727907379074790757907679077790787907979080790817908279083790847908579086790877908879089790907909179092790937909479095790967909779098790997910079101791027910379104791057910679107791087910979110791117911279113791147911579116791177911879119791207912179122791237912479125791267912779128791297913079131791327913379134791357913679137791387913979140791417914279143791447914579146791477914879149791507915179152791537915479155791567915779158791597916079161791627916379164791657916679167791687916979170791717917279173791747917579176791777917879179791807918179182791837918479185791867918779188791897919079191791927919379194791957919679197791987919979200792017920279203792047920579206792077920879209792107921179212792137921479215792167921779218792197922079221792227922379224792257922679227792287922979230792317923279233792347923579236792377923879239792407924179242792437924479245792467924779248792497925079251792527925379254792557925679257792587925979260792617926279263792647926579266792677926879269792707927179272792737927479275792767927779278792797928079281792827928379284792857928679287792887928979290792917929279293792947929579296792977929879299793007930179302793037930479305793067930779308793097931079311793127931379314793157931679317793187931979320793217932279323793247932579326793277932879329793307933179332793337933479335793367933779338793397934079341793427934379344793457934679347793487934979350793517935279353793547935579356793577935879359793607936179362793637936479365793667936779368793697937079371793727937379374793757937679377793787937979380793817938279383793847938579386793877938879389793907939179392793937939479395793967939779398793997940079401794027940379404794057940679407794087940979410794117941279413794147941579416794177941879419794207942179422794237942479425794267942779428794297943079431794327943379434794357943679437794387943979440794417944279443794447944579446794477944879449794507945179452794537945479455794567945779458794597946079461794627946379464794657946679467794687946979470794717947279473794747947579476794777947879479794807948179482794837948479485794867948779488794897949079491794927949379494794957949679497794987949979500795017950279503795047950579506795077950879509795107951179512795137951479515795167951779518795197952079521795227952379524795257952679527795287952979530795317953279533795347953579536795377953879539795407954179542795437954479545795467954779548795497955079551795527955379554795557955679557795587955979560795617956279563795647956579566795677956879569795707957179572795737957479575795767957779578795797958079581795827958379584795857958679587795887958979590795917959279593795947959579596795977959879599796007960179602796037960479605796067960779608796097961079611796127961379614796157961679617796187961979620796217962279623796247962579626796277962879629796307963179632796337963479635796367963779638796397964079641796427964379644796457964679647796487964979650796517965279653796547965579656796577965879659796607966179662796637966479665796667966779668796697967079671796727967379674796757967679677796787967979680796817968279683796847968579686796877968879689796907969179692796937969479695796967969779698796997970079701797027970379704797057970679707797087970979710797117971279713797147971579716797177971879719797207972179722797237972479725797267972779728797297973079731797327973379734797357973679737797387973979740797417974279743797447974579746797477974879749797507975179752797537975479755797567975779758797597976079761797627976379764797657976679767797687976979770797717977279773797747977579776797777977879779797807978179782797837978479785797867978779788797897979079791797927979379794797957979679797797987979979800798017980279803798047980579806798077980879809798107981179812798137981479815798167981779818798197982079821798227982379824798257982679827798287982979830798317983279833798347983579836798377983879839798407984179842798437984479845798467984779848798497985079851798527985379854798557985679857798587985979860798617986279863798647986579866798677986879869798707987179872798737987479875798767987779878798797988079881798827988379884798857988679887798887988979890798917989279893798947989579896798977989879899799007990179902799037990479905799067990779908799097991079911799127991379914799157991679917799187991979920799217992279923799247992579926799277992879929799307993179932799337993479935799367993779938799397994079941799427994379944799457994679947799487994979950799517995279953799547995579956799577995879959799607996179962799637996479965799667996779968799697997079971799727997379974799757997679977799787997979980799817998279983799847998579986799877998879989799907999179992799937999479995799967999779998799998000080001800028000380004800058000680007800088000980010800118001280013800148001580016800178001880019800208002180022800238002480025800268002780028800298003080031800328003380034800358003680037800388003980040800418004280043800448004580046800478004880049800508005180052800538005480055800568005780058800598006080061800628006380064800658006680067800688006980070800718007280073800748007580076800778007880079800808008180082800838008480085800868008780088800898009080091800928009380094800958009680097800988009980100801018010280103801048010580106801078010880109801108011180112801138011480115801168011780118801198012080121801228012380124801258012680127801288012980130801318013280133801348013580136801378013880139801408014180142801438014480145801468014780148801498015080151801528015380154801558015680157801588015980160801618016280163801648016580166801678016880169801708017180172801738017480175801768017780178801798018080181801828018380184801858018680187801888018980190801918019280193801948019580196801978019880199802008020180202802038020480205802068020780208802098021080211802128021380214802158021680217802188021980220802218022280223802248022580226802278022880229802308023180232802338023480235802368023780238802398024080241802428024380244802458024680247802488024980250802518025280253802548025580256802578025880259802608026180262802638026480265802668026780268802698027080271802728027380274802758027680277802788027980280802818028280283802848028580286802878028880289802908029180292802938029480295802968029780298802998030080301803028030380304803058030680307803088030980310803118031280313803148031580316803178031880319803208032180322803238032480325803268032780328803298033080331803328033380334803358033680337803388033980340803418034280343803448034580346803478034880349803508035180352803538035480355803568035780358803598036080361803628036380364803658036680367803688036980370803718037280373803748037580376803778037880379803808038180382803838038480385803868038780388803898039080391803928039380394803958039680397803988039980400804018040280403804048040580406804078040880409804108041180412804138041480415804168041780418804198042080421804228042380424804258042680427804288042980430804318043280433804348043580436804378043880439804408044180442804438044480445804468044780448804498045080451804528045380454804558045680457804588045980460804618046280463804648046580466804678046880469804708047180472804738047480475804768047780478804798048080481804828048380484804858048680487804888048980490804918049280493804948049580496804978049880499805008050180502805038050480505805068050780508805098051080511805128051380514805158051680517805188051980520805218052280523805248052580526805278052880529805308053180532805338053480535805368053780538805398054080541805428054380544805458054680547805488054980550805518055280553805548055580556805578055880559805608056180562805638056480565805668056780568805698057080571805728057380574805758057680577805788057980580805818058280583805848058580586805878058880589805908059180592805938059480595805968059780598805998060080601806028060380604806058060680607806088060980610806118061280613806148061580616806178061880619806208062180622806238062480625806268062780628806298063080631806328063380634806358063680637806388063980640806418064280643806448064580646806478064880649806508065180652806538065480655806568065780658806598066080661806628066380664806658066680667806688066980670806718067280673806748067580676806778067880679806808068180682806838068480685806868068780688806898069080691806928069380694806958069680697806988069980700807018070280703807048070580706807078070880709807108071180712807138071480715807168071780718807198072080721807228072380724807258072680727807288072980730807318073280733807348073580736807378073880739807408074180742807438074480745807468074780748807498075080751807528075380754807558075680757807588075980760807618076280763807648076580766807678076880769807708077180772807738077480775807768077780778807798078080781807828078380784807858078680787807888078980790807918079280793807948079580796807978079880799808008080180802808038080480805808068080780808808098081080811808128081380814808158081680817808188081980820808218082280823808248082580826808278082880829808308083180832808338083480835808368083780838808398084080841808428084380844808458084680847808488084980850808518085280853808548085580856808578085880859808608086180862808638086480865808668086780868808698087080871808728087380874808758087680877808788087980880808818088280883808848088580886808878088880889808908089180892808938089480895808968089780898808998090080901809028090380904809058090680907809088090980910809118091280913809148091580916809178091880919809208092180922809238092480925809268092780928809298093080931809328093380934809358093680937809388093980940809418094280943809448094580946809478094880949809508095180952809538095480955809568095780958809598096080961809628096380964809658096680967809688096980970809718097280973809748097580976809778097880979809808098180982809838098480985809868098780988809898099080991809928099380994809958099680997809988099981000810018100281003810048100581006810078100881009810108101181012810138101481015810168101781018810198102081021810228102381024810258102681027810288102981030810318103281033810348103581036810378103881039810408104181042810438104481045810468104781048810498105081051810528105381054810558105681057810588105981060810618106281063810648106581066810678106881069810708107181072810738107481075810768107781078810798108081081810828108381084810858108681087810888108981090810918109281093810948109581096810978109881099811008110181102811038110481105811068110781108811098111081111811128111381114811158111681117811188111981120811218112281123811248112581126811278112881129811308113181132811338113481135811368113781138811398114081141811428114381144811458114681147811488114981150811518115281153811548115581156811578115881159811608116181162811638116481165811668116781168811698117081171811728117381174811758117681177811788117981180811818118281183811848118581186811878118881189811908119181192811938119481195811968119781198811998120081201812028120381204812058120681207812088120981210812118121281213812148121581216812178121881219812208122181222812238122481225812268122781228812298123081231812328123381234812358123681237812388123981240812418124281243812448124581246812478124881249812508125181252812538125481255812568125781258812598126081261812628126381264812658126681267812688126981270812718127281273812748127581276812778127881279812808128181282812838128481285812868128781288812898129081291812928129381294812958129681297812988129981300813018130281303813048130581306813078130881309813108131181312813138131481315813168131781318813198132081321813228132381324813258132681327813288132981330813318133281333813348133581336813378133881339813408134181342813438134481345813468134781348813498135081351813528135381354813558135681357813588135981360813618136281363813648136581366813678136881369813708137181372813738137481375813768137781378813798138081381813828138381384813858138681387813888138981390813918139281393813948139581396813978139881399814008140181402814038140481405814068140781408814098141081411814128141381414814158141681417814188141981420814218142281423814248142581426814278142881429814308143181432814338143481435814368143781438814398144081441814428144381444814458144681447814488144981450814518145281453814548145581456814578145881459814608146181462814638146481465814668146781468814698147081471814728147381474814758147681477814788147981480814818148281483814848148581486814878148881489814908149181492814938149481495814968149781498814998150081501815028150381504815058150681507815088150981510815118151281513815148151581516815178151881519815208152181522815238152481525815268152781528815298153081531815328153381534815358153681537815388153981540815418154281543815448154581546815478154881549815508155181552815538155481555815568155781558815598156081561815628156381564815658156681567815688156981570815718157281573815748157581576815778157881579815808158181582815838158481585815868158781588815898159081591815928159381594815958159681597815988159981600816018160281603816048160581606816078160881609816108161181612816138161481615816168161781618816198162081621816228162381624816258162681627816288162981630816318163281633816348163581636816378163881639816408164181642816438164481645816468164781648816498165081651816528165381654816558165681657816588165981660816618166281663816648166581666816678166881669816708167181672816738167481675816768167781678816798168081681816828168381684816858168681687816888168981690816918169281693816948169581696816978169881699817008170181702817038170481705817068170781708817098171081711817128171381714817158171681717817188171981720817218172281723817248172581726817278172881729817308173181732817338173481735817368173781738817398174081741817428174381744817458174681747817488174981750817518175281753817548175581756817578175881759817608176181762817638176481765817668176781768817698177081771817728177381774817758177681777817788177981780817818178281783817848178581786817878178881789817908179181792817938179481795817968179781798817998180081801818028180381804818058180681807818088180981810818118181281813818148181581816818178181881819818208182181822818238182481825818268182781828818298183081831818328183381834818358183681837818388183981840818418184281843818448184581846818478184881849818508185181852818538185481855818568185781858818598186081861818628186381864818658186681867818688186981870818718187281873818748187581876818778187881879818808188181882818838188481885818868188781888818898189081891818928189381894818958189681897818988189981900819018190281903819048190581906819078190881909819108191181912819138191481915819168191781918819198192081921819228192381924819258192681927819288192981930819318193281933819348193581936819378193881939819408194181942819438194481945819468194781948819498195081951819528195381954819558195681957819588195981960819618196281963819648196581966819678196881969819708197181972819738197481975819768197781978819798198081981819828198381984819858198681987819888198981990819918199281993819948199581996819978199881999820008200182002820038200482005820068200782008820098201082011820128201382014820158201682017820188201982020820218202282023820248202582026820278202882029820308203182032820338203482035820368203782038820398204082041820428204382044820458204682047820488204982050820518205282053820548205582056820578205882059820608206182062820638206482065820668206782068820698207082071820728207382074820758207682077820788207982080820818208282083820848208582086820878208882089820908209182092820938209482095820968209782098820998210082101821028210382104821058210682107821088210982110821118211282113821148211582116821178211882119821208212182122821238212482125821268212782128821298213082131821328213382134821358213682137821388213982140821418214282143821448214582146821478214882149821508215182152821538215482155821568215782158821598216082161821628216382164821658216682167821688216982170821718217282173821748217582176821778217882179821808218182182821838218482185821868218782188821898219082191821928219382194821958219682197821988219982200822018220282203822048220582206822078220882209822108221182212822138221482215822168221782218822198222082221822228222382224822258222682227822288222982230822318223282233822348223582236822378223882239822408224182242822438224482245822468224782248822498225082251822528225382254822558225682257822588225982260822618226282263822648226582266822678226882269822708227182272822738227482275822768227782278822798228082281822828228382284822858228682287822888228982290822918229282293822948229582296822978229882299823008230182302823038230482305823068230782308823098231082311823128231382314823158231682317823188231982320823218232282323823248232582326823278232882329823308233182332823338233482335823368233782338823398234082341823428234382344823458234682347823488234982350823518235282353823548235582356823578235882359823608236182362823638236482365823668236782368823698237082371823728237382374823758237682377823788237982380823818238282383823848238582386823878238882389823908239182392823938239482395823968239782398823998240082401824028240382404824058240682407824088240982410824118241282413824148241582416824178241882419824208242182422824238242482425824268242782428824298243082431824328243382434824358243682437824388243982440824418244282443824448244582446824478244882449824508245182452824538245482455824568245782458824598246082461824628246382464824658246682467824688246982470824718247282473824748247582476824778247882479824808248182482824838248482485824868248782488824898249082491824928249382494824958249682497824988249982500825018250282503825048250582506825078250882509825108251182512825138251482515825168251782518825198252082521825228252382524825258252682527825288252982530825318253282533825348253582536825378253882539825408254182542825438254482545825468254782548825498255082551825528255382554825558255682557825588255982560825618256282563825648256582566825678256882569825708257182572825738257482575825768257782578825798258082581825828258382584825858258682587825888258982590825918259282593825948259582596825978259882599826008260182602826038260482605826068260782608826098261082611826128261382614826158261682617826188261982620826218262282623826248262582626826278262882629826308263182632826338263482635826368263782638826398264082641826428264382644826458264682647826488264982650826518265282653826548265582656826578265882659826608266182662826638266482665826668266782668826698267082671826728267382674826758267682677826788267982680826818268282683826848268582686826878268882689826908269182692826938269482695826968269782698826998270082701827028270382704827058270682707827088270982710827118271282713827148271582716827178271882719827208272182722827238272482725827268272782728827298273082731827328273382734827358273682737827388273982740827418274282743827448274582746827478274882749827508275182752827538275482755827568275782758827598276082761827628276382764827658276682767827688276982770827718277282773827748277582776827778277882779827808278182782827838278482785827868278782788827898279082791827928279382794827958279682797827988279982800828018280282803828048280582806828078280882809828108281182812828138281482815828168281782818828198282082821828228282382824828258282682827828288282982830828318283282833828348283582836828378283882839828408284182842828438284482845828468284782848828498285082851828528285382854828558285682857828588285982860828618286282863828648286582866828678286882869828708287182872828738287482875828768287782878828798288082881828828288382884828858288682887828888288982890828918289282893828948289582896828978289882899829008290182902829038290482905829068290782908829098291082911829128291382914829158291682917829188291982920829218292282923829248292582926829278292882929829308293182932829338293482935829368293782938829398294082941829428294382944829458294682947829488294982950829518295282953829548295582956829578295882959829608296182962829638296482965829668296782968829698297082971829728297382974829758297682977829788297982980829818298282983829848298582986829878298882989829908299182992829938299482995829968299782998829998300083001830028300383004830058300683007830088300983010830118301283013830148301583016830178301883019830208302183022830238302483025830268302783028830298303083031830328303383034830358303683037830388303983040830418304283043830448304583046830478304883049830508305183052830538305483055830568305783058830598306083061830628306383064830658306683067830688306983070830718307283073830748307583076830778307883079830808308183082830838308483085830868308783088830898309083091830928309383094830958309683097830988309983100831018310283103831048310583106831078310883109831108311183112831138311483115831168311783118831198312083121831228312383124831258312683127831288312983130831318313283133831348313583136831378313883139831408314183142831438314483145831468314783148831498315083151831528315383154831558315683157831588315983160831618316283163831648316583166831678316883169831708317183172831738317483175831768317783178831798318083181831828318383184831858318683187831888318983190831918319283193831948319583196831978319883199832008320183202832038320483205832068320783208832098321083211832128321383214832158321683217832188321983220832218322283223832248322583226832278322883229832308323183232832338323483235832368323783238832398324083241832428324383244832458324683247832488324983250832518325283253832548325583256832578325883259832608326183262832638326483265832668326783268832698327083271832728327383274832758327683277832788327983280832818328283283832848328583286832878328883289832908329183292832938329483295832968329783298832998330083301833028330383304833058330683307833088330983310833118331283313833148331583316833178331883319833208332183322833238332483325833268332783328833298333083331833328333383334833358333683337833388333983340833418334283343833448334583346833478334883349833508335183352833538335483355833568335783358833598336083361833628336383364833658336683367833688336983370833718337283373833748337583376833778337883379833808338183382833838338483385833868338783388833898339083391833928339383394833958339683397833988339983400834018340283403834048340583406834078340883409834108341183412834138341483415834168341783418834198342083421834228342383424834258342683427834288342983430834318343283433834348343583436834378343883439834408344183442834438344483445834468344783448834498345083451834528345383454834558345683457834588345983460834618346283463834648346583466834678346883469834708347183472834738347483475834768347783478834798348083481834828348383484834858348683487834888348983490834918349283493834948349583496834978349883499835008350183502835038350483505835068350783508835098351083511835128351383514835158351683517835188351983520835218352283523835248352583526835278352883529835308353183532835338353483535835368353783538835398354083541835428354383544835458354683547835488354983550835518355283553835548355583556835578355883559835608356183562835638356483565835668356783568835698357083571835728357383574835758357683577835788357983580835818358283583835848358583586835878358883589835908359183592835938359483595835968359783598835998360083601836028360383604836058360683607836088360983610836118361283613836148361583616836178361883619836208362183622836238362483625836268362783628836298363083631836328363383634836358363683637836388363983640836418364283643836448364583646836478364883649836508365183652836538365483655836568365783658836598366083661836628366383664836658366683667836688366983670836718367283673836748367583676836778367883679836808368183682836838368483685836868368783688836898369083691836928369383694836958369683697836988369983700837018370283703837048370583706837078370883709837108371183712837138371483715837168371783718837198372083721837228372383724837258372683727837288372983730837318373283733837348373583736837378373883739837408374183742837438374483745837468374783748837498375083751837528375383754837558375683757837588375983760837618376283763837648376583766837678376883769837708377183772837738377483775837768377783778837798378083781837828378383784837858378683787837888378983790837918379283793837948379583796837978379883799838008380183802838038380483805838068380783808838098381083811838128381383814838158381683817838188381983820838218382283823838248382583826838278382883829838308383183832838338383483835838368383783838838398384083841838428384383844838458384683847838488384983850838518385283853838548385583856838578385883859838608386183862838638386483865838668386783868838698387083871838728387383874838758387683877838788387983880838818388283883838848388583886838878388883889838908389183892838938389483895838968389783898838998390083901839028390383904839058390683907839088390983910839118391283913839148391583916839178391883919839208392183922839238392483925839268392783928839298393083931839328393383934839358393683937839388393983940839418394283943839448394583946839478394883949839508395183952839538395483955839568395783958839598396083961839628396383964839658396683967839688396983970839718397283973839748397583976839778397883979839808398183982839838398483985839868398783988839898399083991839928399383994839958399683997839988399984000840018400284003840048400584006840078400884009840108401184012840138401484015840168401784018840198402084021840228402384024840258402684027840288402984030840318403284033840348403584036840378403884039840408404184042840438404484045840468404784048840498405084051840528405384054840558405684057840588405984060840618406284063840648406584066840678406884069840708407184072840738407484075840768407784078840798408084081840828408384084840858408684087840888408984090840918409284093840948409584096840978409884099841008410184102841038410484105841068410784108841098411084111841128411384114841158411684117841188411984120841218412284123841248412584126841278412884129841308413184132841338413484135841368413784138841398414084141841428414384144841458414684147841488414984150841518415284153841548415584156841578415884159841608416184162841638416484165841668416784168841698417084171841728417384174841758417684177841788417984180841818418284183841848418584186841878418884189841908419184192841938419484195841968419784198841998420084201842028420384204842058420684207842088420984210842118421284213842148421584216842178421884219842208422184222842238422484225842268422784228842298423084231842328423384234842358423684237842388423984240842418424284243842448424584246842478424884249842508425184252842538425484255842568425784258842598426084261842628426384264842658426684267842688426984270842718427284273842748427584276842778427884279842808428184282842838428484285842868428784288842898429084291842928429384294842958429684297842988429984300843018430284303843048430584306843078430884309843108431184312843138431484315843168431784318843198432084321843228432384324843258432684327843288432984330843318433284333843348433584336843378433884339843408434184342843438434484345843468434784348843498435084351843528435384354843558435684357843588435984360843618436284363843648436584366843678436884369843708437184372843738437484375843768437784378843798438084381843828438384384843858438684387843888438984390843918439284393843948439584396843978439884399844008440184402844038440484405844068440784408844098441084411844128441384414844158441684417844188441984420844218442284423844248442584426844278442884429844308443184432844338443484435844368443784438844398444084441844428444384444844458444684447844488444984450844518445284453844548445584456844578445884459844608446184462844638446484465844668446784468844698447084471844728447384474844758447684477844788447984480844818448284483844848448584486844878448884489844908449184492844938449484495844968449784498844998450084501845028450384504845058450684507845088450984510845118451284513845148451584516845178451884519845208452184522845238452484525845268452784528845298453084531845328453384534845358453684537845388453984540845418454284543845448454584546845478454884549845508455184552845538455484555845568455784558845598456084561845628456384564845658456684567845688456984570845718457284573845748457584576845778457884579845808458184582845838458484585845868458784588845898459084591845928459384594845958459684597845988459984600846018460284603846048460584606846078460884609846108461184612846138461484615846168461784618846198462084621846228462384624846258462684627846288462984630846318463284633846348463584636846378463884639846408464184642846438464484645846468464784648846498465084651846528465384654846558465684657846588465984660846618466284663846648466584666846678466884669846708467184672846738467484675846768467784678846798468084681846828468384684846858468684687846888468984690846918469284693846948469584696846978469884699847008470184702847038470484705847068470784708847098471084711847128471384714847158471684717847188471984720847218472284723847248472584726847278472884729847308473184732847338473484735847368473784738847398474084741847428474384744847458474684747847488474984750847518475284753847548475584756847578475884759847608476184762847638476484765847668476784768847698477084771847728477384774847758477684777847788477984780847818478284783847848478584786847878478884789847908479184792847938479484795847968479784798847998480084801848028480384804848058480684807848088480984810848118481284813848148481584816848178481884819848208482184822848238482484825848268482784828848298483084831848328483384834848358483684837848388483984840848418484284843848448484584846848478484884849848508485184852848538485484855848568485784858848598486084861848628486384864848658486684867848688486984870848718487284873848748487584876848778487884879848808488184882848838488484885848868488784888848898489084891848928489384894848958489684897848988489984900849018490284903849048490584906849078490884909849108491184912849138491484915849168491784918849198492084921849228492384924849258492684927849288492984930849318493284933849348493584936849378493884939849408494184942849438494484945849468494784948849498495084951849528495384954849558495684957849588495984960849618496284963849648496584966849678496884969849708497184972849738497484975849768497784978849798498084981849828498384984849858498684987849888498984990849918499284993849948499584996849978499884999850008500185002850038500485005850068500785008850098501085011850128501385014850158501685017850188501985020850218502285023850248502585026850278502885029850308503185032850338503485035850368503785038850398504085041850428504385044850458504685047850488504985050850518505285053850548505585056850578505885059850608506185062850638506485065850668506785068850698507085071850728507385074850758507685077850788507985080850818508285083850848508585086850878508885089850908509185092850938509485095850968509785098850998510085101851028510385104851058510685107851088510985110851118511285113851148511585116851178511885119851208512185122851238512485125851268512785128851298513085131851328513385134851358513685137851388513985140851418514285143851448514585146851478514885149851508515185152851538515485155851568515785158851598516085161851628516385164851658516685167851688516985170851718517285173851748517585176851778517885179851808518185182851838518485185851868518785188851898519085191851928519385194851958519685197851988519985200852018520285203852048520585206852078520885209852108521185212852138521485215852168521785218852198522085221852228522385224852258522685227852288522985230852318523285233852348523585236852378523885239852408524185242852438524485245852468524785248852498525085251852528525385254852558525685257852588525985260852618526285263852648526585266852678526885269852708527185272852738527485275852768527785278852798528085281852828528385284852858528685287852888528985290852918529285293852948529585296852978529885299853008530185302853038530485305853068530785308853098531085311853128531385314853158531685317853188531985320853218532285323853248532585326853278532885329853308533185332853338533485335853368533785338853398534085341853428534385344853458534685347853488534985350853518535285353853548535585356853578535885359853608536185362853638536485365853668536785368853698537085371853728537385374853758537685377853788537985380853818538285383853848538585386853878538885389853908539185392853938539485395853968539785398853998540085401854028540385404854058540685407854088540985410854118541285413854148541585416854178541885419854208542185422854238542485425854268542785428854298543085431854328543385434854358543685437854388543985440854418544285443854448544585446854478544885449854508545185452854538545485455854568545785458854598546085461854628546385464854658546685467854688546985470854718547285473854748547585476854778547885479854808548185482854838548485485854868548785488854898549085491854928549385494854958549685497854988549985500855018550285503855048550585506855078550885509855108551185512855138551485515855168551785518855198552085521855228552385524855258552685527855288552985530855318553285533855348553585536855378553885539855408554185542855438554485545855468554785548855498555085551855528555385554855558555685557855588555985560855618556285563855648556585566855678556885569855708557185572855738557485575855768557785578855798558085581855828558385584855858558685587855888558985590855918559285593855948559585596855978559885599856008560185602856038560485605856068560785608856098561085611856128561385614856158561685617856188561985620856218562285623856248562585626856278562885629856308563185632856338563485635856368563785638856398564085641856428564385644856458564685647856488564985650856518565285653856548565585656856578565885659856608566185662856638566485665856668566785668856698567085671856728567385674856758567685677856788567985680856818568285683856848568585686856878568885689856908569185692856938569485695856968569785698856998570085701857028570385704857058570685707857088570985710857118571285713857148571585716857178571885719857208572185722857238572485725857268572785728857298573085731857328573385734857358573685737857388573985740857418574285743857448574585746857478574885749857508575185752857538575485755857568575785758857598576085761857628576385764857658576685767857688576985770857718577285773857748577585776857778577885779857808578185782857838578485785857868578785788857898579085791857928579385794857958579685797857988579985800858018580285803858048580585806858078580885809858108581185812858138581485815858168581785818858198582085821858228582385824858258582685827858288582985830858318583285833858348583585836858378583885839858408584185842858438584485845858468584785848858498585085851858528585385854858558585685857858588585985860858618586285863858648586585866858678586885869858708587185872858738587485875858768587785878858798588085881858828588385884858858588685887858888588985890858918589285893858948589585896858978589885899859008590185902859038590485905859068590785908859098591085911859128591385914859158591685917859188591985920859218592285923859248592585926859278592885929859308593185932859338593485935859368593785938859398594085941859428594385944859458594685947859488594985950859518595285953859548595585956859578595885959859608596185962859638596485965859668596785968859698597085971859728597385974859758597685977859788597985980859818598285983859848598585986859878598885989859908599185992859938599485995859968599785998859998600086001860028600386004860058600686007860088600986010860118601286013860148601586016860178601886019860208602186022860238602486025860268602786028860298603086031860328603386034860358603686037860388603986040860418604286043860448604586046860478604886049860508605186052860538605486055860568605786058860598606086061860628606386064860658606686067860688606986070860718607286073860748607586076860778607886079860808608186082860838608486085860868608786088860898609086091860928609386094860958609686097860988609986100861018610286103861048610586106861078610886109861108611186112861138611486115861168611786118861198612086121861228612386124861258612686127861288612986130861318613286133861348613586136861378613886139861408614186142861438614486145861468614786148861498615086151861528615386154861558615686157861588615986160861618616286163861648616586166861678616886169861708617186172861738617486175861768617786178861798618086181861828618386184861858618686187861888618986190861918619286193861948619586196861978619886199862008620186202862038620486205862068620786208862098621086211862128621386214862158621686217862188621986220862218622286223862248622586226862278622886229862308623186232862338623486235862368623786238862398624086241862428624386244862458624686247862488624986250862518625286253862548625586256862578625886259862608626186262862638626486265862668626786268862698627086271862728627386274862758627686277862788627986280862818628286283862848628586286862878628886289862908629186292862938629486295862968629786298862998630086301863028630386304863058630686307863088630986310863118631286313863148631586316863178631886319863208632186322863238632486325863268632786328863298633086331863328633386334863358633686337863388633986340863418634286343863448634586346863478634886349863508635186352863538635486355863568635786358863598636086361863628636386364863658636686367863688636986370863718637286373863748637586376863778637886379863808638186382863838638486385863868638786388863898639086391863928639386394863958639686397863988639986400864018640286403864048640586406864078640886409864108641186412864138641486415864168641786418864198642086421864228642386424864258642686427864288642986430864318643286433864348643586436864378643886439864408644186442864438644486445864468644786448864498645086451864528645386454864558645686457864588645986460864618646286463864648646586466864678646886469864708647186472864738647486475864768647786478864798648086481864828648386484864858648686487864888648986490864918649286493864948649586496864978649886499865008650186502865038650486505865068650786508865098651086511865128651386514865158651686517865188651986520865218652286523865248652586526865278652886529865308653186532865338653486535865368653786538865398654086541865428654386544865458654686547865488654986550865518655286553865548655586556865578655886559865608656186562865638656486565865668656786568865698657086571865728657386574865758657686577865788657986580865818658286583865848658586586865878658886589865908659186592865938659486595865968659786598865998660086601866028660386604866058660686607866088660986610866118661286613866148661586616866178661886619866208662186622866238662486625866268662786628866298663086631866328663386634866358663686637866388663986640866418664286643866448664586646866478664886649866508665186652866538665486655866568665786658866598666086661866628666386664866658666686667866688666986670866718667286673866748667586676866778667886679866808668186682866838668486685866868668786688866898669086691866928669386694866958669686697866988669986700867018670286703867048670586706867078670886709867108671186712867138671486715867168671786718867198672086721867228672386724867258672686727867288672986730867318673286733867348673586736867378673886739867408674186742867438674486745867468674786748867498675086751867528675386754867558675686757867588675986760867618676286763867648676586766867678676886769867708677186772867738677486775867768677786778867798678086781867828678386784867858678686787867888678986790867918679286793867948679586796867978679886799868008680186802868038680486805868068680786808868098681086811868128681386814868158681686817868188681986820868218682286823868248682586826868278682886829868308683186832868338683486835868368683786838868398684086841868428684386844868458684686847868488684986850868518685286853868548685586856868578685886859868608686186862868638686486865868668686786868868698687086871868728687386874868758687686877868788687986880868818688286883868848688586886868878688886889868908689186892868938689486895868968689786898868998690086901869028690386904869058690686907869088690986910869118691286913869148691586916869178691886919869208692186922869238692486925869268692786928869298693086931869328693386934869358693686937869388693986940869418694286943869448694586946869478694886949869508695186952869538695486955869568695786958869598696086961869628696386964869658696686967869688696986970869718697286973869748697586976869778697886979869808698186982869838698486985869868698786988869898699086991869928699386994869958699686997869988699987000870018700287003870048700587006870078700887009870108701187012870138701487015870168701787018870198702087021870228702387024870258702687027870288702987030870318703287033870348703587036870378703887039870408704187042870438704487045870468704787048870498705087051870528705387054870558705687057870588705987060870618706287063870648706587066870678706887069870708707187072870738707487075870768707787078870798708087081870828708387084870858708687087870888708987090870918709287093870948709587096870978709887099871008710187102871038710487105871068710787108871098711087111871128711387114871158711687117871188711987120871218712287123871248712587126871278712887129871308713187132871338713487135871368713787138871398714087141871428714387144871458714687147871488714987150871518715287153871548715587156871578715887159871608716187162871638716487165871668716787168871698717087171871728717387174871758717687177871788717987180871818718287183871848718587186871878718887189871908719187192871938719487195871968719787198871998720087201872028720387204872058720687207872088720987210872118721287213872148721587216872178721887219872208722187222872238722487225872268722787228872298723087231872328723387234872358723687237872388723987240872418724287243872448724587246872478724887249872508725187252872538725487255872568725787258872598726087261872628726387264872658726687267872688726987270872718727287273872748727587276872778727887279872808728187282872838728487285872868728787288872898729087291872928729387294872958729687297872988729987300873018730287303873048730587306873078730887309873108731187312873138731487315873168731787318873198732087321873228732387324873258732687327873288732987330873318733287333873348733587336873378733887339873408734187342873438734487345873468734787348873498735087351873528735387354873558735687357873588735987360873618736287363873648736587366873678736887369873708737187372873738737487375873768737787378873798738087381873828738387384873858738687387873888738987390873918739287393873948739587396873978739887399874008740187402874038740487405874068740787408874098741087411874128741387414874158741687417874188741987420874218742287423874248742587426874278742887429874308743187432874338743487435874368743787438874398744087441874428744387444874458744687447874488744987450874518745287453874548745587456874578745887459874608746187462874638746487465874668746787468874698747087471874728747387474874758747687477874788747987480874818748287483874848748587486874878748887489874908749187492874938749487495874968749787498874998750087501875028750387504875058750687507875088750987510875118751287513875148751587516875178751887519875208752187522875238752487525875268752787528875298753087531875328753387534875358753687537875388753987540875418754287543875448754587546875478754887549875508755187552875538755487555875568755787558875598756087561875628756387564875658756687567875688756987570875718757287573875748757587576875778757887579875808758187582875838758487585875868758787588875898759087591875928759387594875958759687597875988759987600876018760287603876048760587606876078760887609876108761187612876138761487615876168761787618876198762087621876228762387624876258762687627876288762987630876318763287633876348763587636876378763887639876408764187642876438764487645876468764787648876498765087651876528765387654876558765687657876588765987660876618766287663876648766587666876678766887669876708767187672876738767487675876768767787678876798768087681876828768387684876858768687687876888768987690876918769287693876948769587696876978769887699877008770187702877038770487705877068770787708877098771087711877128771387714877158771687717877188771987720877218772287723877248772587726877278772887729877308773187732877338773487735877368773787738877398774087741877428774387744877458774687747877488774987750877518775287753877548775587756877578775887759877608776187762877638776487765877668776787768877698777087771877728777387774877758777687777877788777987780877818778287783877848778587786877878778887789877908779187792877938779487795877968779787798877998780087801878028780387804878058780687807878088780987810878118781287813878148781587816878178781887819878208782187822878238782487825878268782787828878298783087831878328783387834878358783687837878388783987840878418784287843878448784587846878478784887849878508785187852878538785487855878568785787858878598786087861878628786387864878658786687867878688786987870878718787287873878748787587876878778787887879878808788187882878838788487885878868788787888878898789087891878928789387894878958789687897878988789987900879018790287903879048790587906879078790887909879108791187912879138791487915879168791787918879198792087921879228792387924879258792687927879288792987930879318793287933879348793587936879378793887939879408794187942879438794487945879468794787948879498795087951879528795387954879558795687957879588795987960879618796287963879648796587966879678796887969879708797187972879738797487975879768797787978879798798087981879828798387984879858798687987879888798987990879918799287993879948799587996879978799887999880008800188002880038800488005880068800788008880098801088011880128801388014880158801688017880188801988020880218802288023880248802588026880278802888029880308803188032880338803488035880368803788038880398804088041880428804388044880458804688047880488804988050880518805288053880548805588056880578805888059880608806188062880638806488065880668806788068880698807088071880728807388074880758807688077880788807988080880818808288083880848808588086880878808888089880908809188092880938809488095880968809788098880998810088101881028810388104881058810688107881088810988110881118811288113881148811588116881178811888119881208812188122881238812488125881268812788128881298813088131881328813388134881358813688137881388813988140881418814288143881448814588146881478814888149881508815188152881538815488155881568815788158881598816088161881628816388164881658816688167881688816988170881718817288173881748817588176881778817888179881808818188182881838818488185881868818788188881898819088191881928819388194881958819688197881988819988200882018820288203882048820588206882078820888209882108821188212882138821488215882168821788218882198822088221882228822388224882258822688227882288822988230882318823288233882348823588236882378823888239882408824188242882438824488245882468824788248882498825088251882528825388254882558825688257882588825988260882618826288263882648826588266882678826888269882708827188272882738827488275882768827788278882798828088281882828828388284882858828688287882888828988290882918829288293882948829588296882978829888299883008830188302883038830488305883068830788308883098831088311883128831388314883158831688317883188831988320883218832288323883248832588326883278832888329883308833188332883338833488335883368833788338883398834088341883428834388344883458834688347883488834988350883518835288353883548835588356883578835888359883608836188362883638836488365883668836788368883698837088371883728837388374883758837688377883788837988380883818838288383883848838588386883878838888389883908839188392883938839488395883968839788398883998840088401884028840388404884058840688407884088840988410884118841288413884148841588416884178841888419884208842188422884238842488425884268842788428884298843088431884328843388434884358843688437884388843988440884418844288443884448844588446884478844888449884508845188452884538845488455884568845788458884598846088461884628846388464884658846688467884688846988470884718847288473884748847588476884778847888479884808848188482884838848488485884868848788488884898849088491884928849388494884958849688497884988849988500885018850288503885048850588506885078850888509885108851188512885138851488515885168851788518885198852088521885228852388524885258852688527885288852988530885318853288533885348853588536885378853888539885408854188542885438854488545885468854788548885498855088551885528855388554885558855688557885588855988560885618856288563885648856588566885678856888569885708857188572885738857488575885768857788578885798858088581885828858388584885858858688587885888858988590885918859288593885948859588596885978859888599886008860188602886038860488605886068860788608886098861088611886128861388614886158861688617886188861988620886218862288623886248862588626886278862888629886308863188632886338863488635886368863788638886398864088641886428864388644886458864688647886488864988650886518865288653886548865588656886578865888659886608866188662886638866488665886668866788668886698867088671886728867388674886758867688677886788867988680886818868288683886848868588686886878868888689886908869188692886938869488695886968869788698886998870088701887028870388704887058870688707887088870988710887118871288713887148871588716887178871888719887208872188722887238872488725887268872788728887298873088731887328873388734887358873688737887388873988740887418874288743887448874588746887478874888749887508875188752887538875488755887568875788758887598876088761887628876388764887658876688767887688876988770887718877288773887748877588776887778877888779887808878188782887838878488785887868878788788887898879088791887928879388794887958879688797887988879988800888018880288803888048880588806888078880888809888108881188812888138881488815888168881788818888198882088821888228882388824888258882688827888288882988830888318883288833888348883588836888378883888839888408884188842888438884488845888468884788848888498885088851888528885388854888558885688857888588885988860888618886288863888648886588866888678886888869888708887188872888738887488875888768887788878888798888088881888828888388884888858888688887888888888988890888918889288893888948889588896888978889888899889008890188902889038890488905889068890788908889098891088911889128891388914889158891688917889188891988920889218892288923889248892588926889278892888929889308893188932889338893488935889368893788938889398894088941889428894388944889458894688947889488894988950889518895288953889548895588956889578895888959889608896188962889638896488965889668896788968889698897088971889728897388974889758897688977889788897988980889818898288983889848898588986889878898888989889908899188992889938899488995889968899788998889998900089001890028900389004890058900689007890088900989010890118901289013890148901589016890178901889019890208902189022890238902489025890268902789028890298903089031890328903389034890358903689037890388903989040890418904289043890448904589046890478904889049890508905189052890538905489055890568905789058890598906089061890628906389064890658906689067890688906989070890718907289073890748907589076890778907889079890808908189082890838908489085890868908789088890898909089091890928909389094890958909689097890988909989100891018910289103891048910589106891078910889109891108911189112891138911489115891168911789118891198912089121891228912389124891258912689127891288912989130891318913289133891348913589136891378913889139891408914189142891438914489145891468914789148891498915089151891528915389154891558915689157891588915989160891618916289163891648916589166891678916889169891708917189172891738917489175891768917789178891798918089181891828918389184891858918689187891888918989190891918919289193891948919589196891978919889199892008920189202892038920489205892068920789208892098921089211892128921389214892158921689217892188921989220892218922289223892248922589226892278922889229892308923189232892338923489235892368923789238892398924089241892428924389244892458924689247892488924989250892518925289253892548925589256892578925889259892608926189262892638926489265892668926789268892698927089271892728927389274892758927689277892788927989280892818928289283892848928589286892878928889289892908929189292892938929489295892968929789298892998930089301893028930389304893058930689307893088930989310893118931289313893148931589316893178931889319893208932189322893238932489325893268932789328893298933089331893328933389334893358933689337893388933989340893418934289343893448934589346893478934889349893508935189352893538935489355893568935789358893598936089361893628936389364893658936689367893688936989370893718937289373893748937589376893778937889379893808938189382893838938489385893868938789388893898939089391893928939389394893958939689397893988939989400894018940289403894048940589406894078940889409894108941189412894138941489415894168941789418894198942089421894228942389424894258942689427894288942989430894318943289433894348943589436894378943889439894408944189442894438944489445894468944789448894498945089451894528945389454894558945689457894588945989460894618946289463894648946589466894678946889469894708947189472894738947489475894768947789478894798948089481894828948389484894858948689487894888948989490894918949289493894948949589496894978949889499895008950189502895038950489505895068950789508895098951089511895128951389514895158951689517895188951989520895218952289523895248952589526895278952889529895308953189532895338953489535895368953789538895398954089541895428954389544895458954689547895488954989550895518955289553895548955589556895578955889559895608956189562895638956489565895668956789568895698957089571895728957389574895758957689577895788957989580895818958289583895848958589586895878958889589895908959189592895938959489595895968959789598895998960089601896028960389604896058960689607896088960989610896118961289613896148961589616896178961889619896208962189622896238962489625896268962789628896298963089631896328963389634896358963689637896388963989640896418964289643896448964589646896478964889649896508965189652896538965489655896568965789658896598966089661896628966389664896658966689667896688966989670896718967289673896748967589676896778967889679896808968189682896838968489685896868968789688896898969089691896928969389694896958969689697896988969989700897018970289703897048970589706897078970889709897108971189712897138971489715897168971789718897198972089721897228972389724897258972689727897288972989730897318973289733897348973589736897378973889739897408974189742897438974489745897468974789748897498975089751897528975389754897558975689757897588975989760897618976289763897648976589766897678976889769897708977189772897738977489775897768977789778897798978089781897828978389784897858978689787897888978989790897918979289793897948979589796897978979889799898008980189802898038980489805898068980789808898098981089811898128981389814898158981689817898188981989820898218982289823898248982589826898278982889829898308983189832898338983489835898368983789838898398984089841898428984389844898458984689847898488984989850898518985289853898548985589856898578985889859898608986189862898638986489865898668986789868898698987089871898728987389874898758987689877898788987989880898818988289883898848988589886898878988889889898908989189892898938989489895898968989789898898998990089901899028990389904899058990689907899088990989910899118991289913899148991589916899178991889919899208992189922899238992489925899268992789928899298993089931899328993389934899358993689937899388993989940899418994289943899448994589946899478994889949899508995189952899538995489955899568995789958899598996089961899628996389964899658996689967899688996989970899718997289973899748997589976899778997889979899808998189982899838998489985899868998789988899898999089991899928999389994899958999689997899988999990000900019000290003900049000590006900079000890009900109001190012900139001490015900169001790018900199002090021900229002390024900259002690027900289002990030900319003290033900349003590036900379003890039900409004190042900439004490045900469004790048900499005090051900529005390054900559005690057900589005990060900619006290063900649006590066900679006890069900709007190072900739007490075900769007790078900799008090081900829008390084900859008690087900889008990090900919009290093900949009590096900979009890099901009010190102901039010490105901069010790108901099011090111901129011390114901159011690117901189011990120901219012290123901249012590126901279012890129901309013190132901339013490135901369013790138901399014090141901429014390144901459014690147901489014990150901519015290153901549015590156901579015890159901609016190162901639016490165901669016790168901699017090171901729017390174901759017690177901789017990180901819018290183901849018590186901879018890189901909019190192901939019490195901969019790198901999020090201902029020390204902059020690207902089020990210902119021290213902149021590216902179021890219902209022190222902239022490225902269022790228902299023090231902329023390234902359023690237902389023990240902419024290243902449024590246902479024890249902509025190252902539025490255902569025790258902599026090261902629026390264902659026690267902689026990270902719027290273902749027590276902779027890279902809028190282902839028490285902869028790288902899029090291902929029390294902959029690297902989029990300903019030290303903049030590306903079030890309903109031190312903139031490315903169031790318903199032090321903229032390324903259032690327903289032990330903319033290333903349033590336903379033890339903409034190342903439034490345903469034790348903499035090351903529035390354903559035690357903589035990360903619036290363903649036590366903679036890369903709037190372903739037490375903769037790378903799038090381903829038390384903859038690387903889038990390903919039290393903949039590396903979039890399904009040190402904039040490405904069040790408904099041090411904129041390414904159041690417904189041990420904219042290423904249042590426904279042890429904309043190432904339043490435904369043790438904399044090441904429044390444904459044690447904489044990450904519045290453904549045590456904579045890459904609046190462904639046490465904669046790468904699047090471904729047390474904759047690477904789047990480904819048290483904849048590486904879048890489904909049190492904939049490495904969049790498904999050090501905029050390504905059050690507905089050990510905119051290513905149051590516905179051890519905209052190522905239052490525905269052790528905299053090531905329053390534905359053690537905389053990540905419054290543905449054590546905479054890549905509055190552905539055490555905569055790558905599056090561905629056390564905659056690567905689056990570905719057290573905749057590576905779057890579905809058190582905839058490585905869058790588905899059090591905929059390594905959059690597905989059990600906019060290603906049060590606906079060890609906109061190612906139061490615906169061790618906199062090621906229062390624906259062690627906289062990630906319063290633906349063590636906379063890639906409064190642906439064490645906469064790648906499065090651906529065390654906559065690657906589065990660906619066290663906649066590666906679066890669906709067190672906739067490675906769067790678906799068090681906829068390684906859068690687906889068990690906919069290693906949069590696906979069890699907009070190702907039070490705907069070790708907099071090711907129071390714907159071690717907189071990720907219072290723907249072590726907279072890729907309073190732907339073490735907369073790738907399074090741907429074390744907459074690747907489074990750907519075290753907549075590756907579075890759907609076190762907639076490765907669076790768907699077090771907729077390774907759077690777907789077990780907819078290783907849078590786907879078890789907909079190792907939079490795907969079790798907999080090801908029080390804908059080690807908089080990810908119081290813908149081590816908179081890819908209082190822908239082490825908269082790828908299083090831908329083390834908359083690837908389083990840908419084290843908449084590846908479084890849908509085190852908539085490855908569085790858908599086090861908629086390864908659086690867908689086990870908719087290873908749087590876908779087890879908809088190882908839088490885908869088790888908899089090891908929089390894908959089690897908989089990900909019090290903909049090590906909079090890909909109091190912909139091490915909169091790918909199092090921909229092390924909259092690927909289092990930909319093290933909349093590936909379093890939909409094190942909439094490945909469094790948909499095090951909529095390954909559095690957909589095990960909619096290963909649096590966909679096890969909709097190972909739097490975909769097790978909799098090981909829098390984909859098690987909889098990990909919099290993909949099590996909979099890999910009100191002910039100491005910069100791008910099101091011910129101391014910159101691017910189101991020910219102291023910249102591026910279102891029910309103191032910339103491035910369103791038910399104091041910429104391044910459104691047910489104991050910519105291053910549105591056910579105891059910609106191062910639106491065910669106791068910699107091071910729107391074910759107691077910789107991080910819108291083910849108591086910879108891089910909109191092910939109491095910969109791098910999110091101911029110391104911059110691107911089110991110911119111291113911149111591116911179111891119911209112191122911239112491125911269112791128911299113091131911329113391134911359113691137911389113991140911419114291143911449114591146911479114891149911509115191152911539115491155911569115791158911599116091161911629116391164911659116691167911689116991170911719117291173911749117591176911779117891179911809118191182911839118491185911869118791188911899119091191911929119391194911959119691197911989119991200912019120291203912049120591206912079120891209912109121191212912139121491215912169121791218912199122091221912229122391224912259122691227912289122991230912319123291233912349123591236912379123891239912409124191242912439124491245912469124791248912499125091251912529125391254912559125691257912589125991260912619126291263912649126591266912679126891269912709127191272912739127491275912769127791278912799128091281912829128391284912859128691287912889128991290912919129291293912949129591296912979129891299913009130191302913039130491305913069130791308913099131091311913129131391314913159131691317913189131991320913219132291323913249132591326913279132891329913309133191332913339133491335913369133791338913399134091341913429134391344913459134691347913489134991350913519135291353913549135591356913579135891359913609136191362913639136491365913669136791368913699137091371913729137391374913759137691377913789137991380913819138291383913849138591386913879138891389913909139191392913939139491395913969139791398913999140091401914029140391404914059140691407914089140991410914119141291413914149141591416914179141891419914209142191422914239142491425914269142791428914299143091431914329143391434914359143691437914389143991440914419144291443914449144591446914479144891449914509145191452914539145491455914569145791458914599146091461914629146391464914659146691467914689146991470914719147291473914749147591476914779147891479914809148191482914839148491485914869148791488914899149091491914929149391494914959149691497914989149991500915019150291503915049150591506915079150891509915109151191512915139151491515915169151791518915199152091521915229152391524915259152691527915289152991530915319153291533915349153591536915379153891539915409154191542915439154491545915469154791548915499155091551915529155391554915559155691557915589155991560915619156291563915649156591566915679156891569915709157191572915739157491575915769157791578915799158091581915829158391584915859158691587915889158991590915919159291593915949159591596915979159891599916009160191602916039160491605916069160791608916099161091611916129161391614916159161691617916189161991620916219162291623916249162591626916279162891629916309163191632916339163491635916369163791638916399164091641916429164391644916459164691647916489164991650916519165291653916549165591656916579165891659916609166191662916639166491665916669166791668916699167091671916729167391674916759167691677916789167991680916819168291683916849168591686916879168891689916909169191692916939169491695916969169791698916999170091701917029170391704917059170691707917089170991710917119171291713917149171591716917179171891719917209172191722917239172491725917269172791728917299173091731917329173391734917359173691737917389173991740917419174291743917449174591746917479174891749917509175191752917539175491755917569175791758917599176091761917629176391764917659176691767917689176991770917719177291773917749177591776917779177891779917809178191782917839178491785917869178791788917899179091791917929179391794917959179691797917989179991800918019180291803918049180591806918079180891809918109181191812918139181491815918169181791818918199182091821918229182391824918259182691827918289182991830918319183291833918349183591836918379183891839918409184191842918439184491845918469184791848918499185091851918529185391854918559185691857918589185991860918619186291863918649186591866918679186891869918709187191872918739187491875918769187791878918799188091881918829188391884918859188691887918889188991890918919189291893918949189591896918979189891899919009190191902919039190491905919069190791908919099191091911919129191391914919159191691917919189191991920919219192291923919249192591926919279192891929919309193191932919339193491935919369193791938919399194091941919429194391944919459194691947919489194991950919519195291953919549195591956919579195891959919609196191962919639196491965919669196791968919699197091971919729197391974919759197691977919789197991980919819198291983919849198591986919879198891989919909199191992919939199491995919969199791998919999200092001920029200392004920059200692007920089200992010920119201292013920149201592016920179201892019920209202192022920239202492025920269202792028920299203092031920329203392034920359203692037920389203992040920419204292043920449204592046920479204892049920509205192052920539205492055920569205792058920599206092061920629206392064920659206692067920689206992070920719207292073920749207592076920779207892079920809208192082920839208492085920869208792088920899209092091920929209392094920959209692097920989209992100921019210292103921049210592106921079210892109921109211192112921139211492115921169211792118921199212092121921229212392124921259212692127921289212992130921319213292133921349213592136921379213892139921409214192142921439214492145921469214792148921499215092151921529215392154921559215692157921589215992160921619216292163921649216592166921679216892169921709217192172921739217492175921769217792178921799218092181921829218392184921859218692187921889218992190921919219292193921949219592196921979219892199922009220192202922039220492205922069220792208922099221092211922129221392214922159221692217922189221992220922219222292223922249222592226922279222892229922309223192232922339223492235922369223792238922399224092241922429224392244922459224692247922489224992250922519225292253922549225592256922579225892259922609226192262922639226492265922669226792268922699227092271922729227392274922759227692277922789227992280922819228292283922849228592286922879228892289922909229192292922939229492295922969229792298922999230092301923029230392304923059230692307923089230992310923119231292313923149231592316923179231892319923209232192322923239232492325923269232792328923299233092331923329233392334923359233692337923389233992340923419234292343923449234592346923479234892349923509235192352923539235492355923569235792358923599236092361923629236392364923659236692367923689236992370923719237292373923749237592376923779237892379923809238192382923839238492385923869238792388923899239092391923929239392394923959239692397923989239992400924019240292403924049240592406924079240892409924109241192412924139241492415924169241792418924199242092421924229242392424924259242692427924289242992430924319243292433924349243592436924379243892439924409244192442924439244492445924469244792448924499245092451924529245392454924559245692457924589245992460924619246292463924649246592466924679246892469924709247192472924739247492475924769247792478924799248092481924829248392484924859248692487924889248992490924919249292493924949249592496924979249892499925009250192502925039250492505925069250792508925099251092511925129251392514925159251692517925189251992520925219252292523925249252592526925279252892529925309253192532925339253492535925369253792538925399254092541925429254392544925459254692547925489254992550925519255292553925549255592556925579255892559925609256192562925639256492565925669256792568925699257092571925729257392574925759257692577925789257992580925819258292583925849258592586925879258892589925909259192592925939259492595925969259792598925999260092601926029260392604926059260692607926089260992610926119261292613926149261592616926179261892619926209262192622926239262492625926269262792628926299263092631926329263392634926359263692637926389263992640926419264292643926449264592646926479264892649926509265192652926539265492655926569265792658926599266092661926629266392664926659266692667926689266992670926719267292673926749267592676926779267892679926809268192682926839268492685926869268792688926899269092691926929269392694926959269692697926989269992700927019270292703927049270592706927079270892709927109271192712927139271492715927169271792718927199272092721927229272392724927259272692727927289272992730927319273292733927349273592736927379273892739927409274192742927439274492745927469274792748927499275092751927529275392754927559275692757927589275992760927619276292763927649276592766927679276892769927709277192772927739277492775927769277792778927799278092781927829278392784927859278692787927889278992790927919279292793927949279592796927979279892799928009280192802928039280492805928069280792808928099281092811928129281392814928159281692817928189281992820928219282292823928249282592826928279282892829928309283192832928339283492835928369283792838928399284092841928429284392844928459284692847928489284992850928519285292853928549285592856928579285892859928609286192862928639286492865928669286792868928699287092871928729287392874928759287692877928789287992880928819288292883928849288592886928879288892889928909289192892928939289492895928969289792898928999290092901929029290392904929059290692907929089290992910929119291292913929149291592916929179291892919929209292192922929239292492925929269292792928929299293092931929329293392934929359293692937929389293992940929419294292943929449294592946929479294892949929509295192952929539295492955929569295792958929599296092961929629296392964929659296692967929689296992970929719297292973929749297592976929779297892979929809298192982929839298492985929869298792988929899299092991929929299392994929959299692997929989299993000930019300293003930049300593006930079300893009930109301193012930139301493015930169301793018930199302093021930229302393024930259302693027930289302993030930319303293033930349303593036930379303893039930409304193042930439304493045930469304793048930499305093051930529305393054930559305693057930589305993060930619306293063930649306593066930679306893069930709307193072930739307493075930769307793078930799308093081930829308393084930859308693087930889308993090930919309293093930949309593096930979309893099931009310193102931039310493105931069310793108931099311093111931129311393114931159311693117931189311993120931219312293123931249312593126931279312893129931309313193132931339313493135931369313793138931399314093141931429314393144931459314693147931489314993150931519315293153931549315593156931579315893159931609316193162931639316493165931669316793168931699317093171931729317393174931759317693177931789317993180931819318293183931849318593186931879318893189931909319193192931939319493195931969319793198931999320093201932029320393204932059320693207932089320993210932119321293213932149321593216932179321893219932209322193222932239322493225932269322793228932299323093231932329323393234932359323693237932389323993240932419324293243932449324593246932479324893249932509325193252932539325493255932569325793258932599326093261932629326393264932659326693267932689326993270932719327293273932749327593276932779327893279932809328193282932839328493285932869328793288932899329093291932929329393294932959329693297932989329993300933019330293303933049330593306933079330893309933109331193312933139331493315933169331793318933199332093321933229332393324933259332693327933289332993330933319333293333933349333593336933379333893339933409334193342933439334493345933469334793348933499335093351933529335393354933559335693357933589335993360933619336293363933649336593366933679336893369933709337193372933739337493375933769337793378933799338093381933829338393384933859338693387933889338993390933919339293393933949339593396933979339893399934009340193402934039340493405934069340793408934099341093411934129341393414934159341693417934189341993420934219342293423934249342593426934279342893429934309343193432934339343493435934369343793438934399344093441934429344393444934459344693447934489344993450934519345293453934549345593456934579345893459934609346193462934639346493465934669346793468934699347093471934729347393474934759347693477934789347993480934819348293483934849348593486934879348893489934909349193492934939349493495934969349793498934999350093501935029350393504935059350693507935089350993510935119351293513935149351593516935179351893519935209352193522935239352493525935269352793528935299353093531935329353393534935359353693537935389353993540935419354293543935449354593546935479354893549935509355193552935539355493555935569355793558935599356093561935629356393564935659356693567935689356993570935719357293573935749357593576935779357893579935809358193582935839358493585935869358793588935899359093591935929359393594935959359693597935989359993600936019360293603936049360593606936079360893609936109361193612936139361493615936169361793618936199362093621936229362393624936259362693627936289362993630936319363293633936349363593636936379363893639936409364193642936439364493645936469364793648936499365093651936529365393654936559365693657936589365993660936619366293663936649366593666936679366893669936709367193672936739367493675936769367793678936799368093681936829368393684936859368693687936889368993690936919369293693936949369593696936979369893699937009370193702937039370493705937069370793708937099371093711937129371393714937159371693717937189371993720937219372293723937249372593726937279372893729937309373193732937339373493735937369373793738937399374093741937429374393744937459374693747937489374993750937519375293753937549375593756937579375893759937609376193762937639376493765937669376793768937699377093771937729377393774937759377693777937789377993780937819378293783937849378593786937879378893789937909379193792937939379493795937969379793798937999380093801938029380393804938059380693807938089380993810938119381293813938149381593816938179381893819938209382193822938239382493825938269382793828938299383093831938329383393834938359383693837938389383993840938419384293843938449384593846938479384893849938509385193852938539385493855938569385793858938599386093861938629386393864938659386693867938689386993870938719387293873938749387593876938779387893879938809388193882938839388493885938869388793888938899389093891938929389393894938959389693897938989389993900939019390293903939049390593906939079390893909939109391193912939139391493915939169391793918939199392093921939229392393924939259392693927939289392993930939319393293933939349393593936939379393893939939409394193942939439394493945939469394793948939499395093951939529395393954939559395693957939589395993960939619396293963939649396593966939679396893969939709397193972939739397493975939769397793978939799398093981939829398393984939859398693987939889398993990939919399293993939949399593996939979399893999940009400194002940039400494005940069400794008940099401094011940129401394014940159401694017940189401994020940219402294023940249402594026940279402894029940309403194032940339403494035940369403794038940399404094041940429404394044940459404694047940489404994050940519405294053940549405594056940579405894059940609406194062940639406494065940669406794068940699407094071940729407394074940759407694077940789407994080940819408294083940849408594086940879408894089940909409194092940939409494095940969409794098940999410094101941029410394104941059410694107941089410994110941119411294113941149411594116941179411894119941209412194122941239412494125941269412794128941299413094131941329413394134941359413694137941389413994140941419414294143941449414594146941479414894149941509415194152941539415494155941569415794158941599416094161941629416394164941659416694167941689416994170941719417294173941749417594176941779417894179941809418194182941839418494185941869418794188941899419094191941929419394194941959419694197941989419994200942019420294203942049420594206942079420894209942109421194212942139421494215942169421794218942199422094221942229422394224942259422694227942289422994230942319423294233942349423594236942379423894239942409424194242942439424494245942469424794248942499425094251942529425394254942559425694257942589425994260942619426294263942649426594266942679426894269942709427194272942739427494275942769427794278942799428094281942829428394284942859428694287942889428994290942919429294293942949429594296942979429894299943009430194302943039430494305943069430794308943099431094311943129431394314943159431694317943189431994320943219432294323943249432594326943279432894329943309433194332943339433494335943369433794338943399434094341943429434394344943459434694347943489434994350943519435294353943549435594356943579435894359943609436194362943639436494365943669436794368943699437094371943729437394374943759437694377943789437994380943819438294383943849438594386943879438894389943909439194392943939439494395943969439794398943999440094401944029440394404944059440694407944089440994410944119441294413944149441594416944179441894419944209442194422944239442494425944269442794428944299443094431944329443394434944359443694437944389443994440944419444294443944449444594446944479444894449944509445194452944539445494455944569445794458944599446094461944629446394464944659446694467944689446994470944719447294473944749447594476944779447894479944809448194482944839448494485944869448794488944899449094491944929449394494944959449694497944989449994500945019450294503945049450594506945079450894509945109451194512945139451494515945169451794518945199452094521945229452394524945259452694527945289452994530945319453294533945349453594536945379453894539945409454194542945439454494545945469454794548945499455094551945529455394554945559455694557945589455994560945619456294563945649456594566945679456894569945709457194572945739457494575945769457794578945799458094581945829458394584945859458694587945889458994590945919459294593945949459594596945979459894599946009460194602946039460494605946069460794608946099461094611946129461394614946159461694617946189461994620946219462294623946249462594626946279462894629946309463194632946339463494635946369463794638946399464094641946429464394644946459464694647946489464994650946519465294653946549465594656946579465894659946609466194662946639466494665946669466794668946699467094671946729467394674946759467694677946789467994680946819468294683946849468594686946879468894689946909469194692946939469494695946969469794698946999470094701947029470394704947059470694707947089470994710947119471294713947149471594716947179471894719947209472194722947239472494725947269472794728947299473094731947329473394734947359473694737947389473994740947419474294743947449474594746947479474894749947509475194752947539475494755947569475794758947599476094761947629476394764947659476694767947689476994770947719477294773947749477594776947779477894779947809478194782947839478494785947869478794788947899479094791947929479394794947959479694797947989479994800948019480294803948049480594806948079480894809948109481194812948139481494815948169481794818948199482094821948229482394824948259482694827948289482994830948319483294833948349483594836948379483894839948409484194842948439484494845948469484794848948499485094851948529485394854948559485694857948589485994860948619486294863948649486594866948679486894869948709487194872948739487494875948769487794878948799488094881948829488394884948859488694887948889488994890948919489294893948949489594896948979489894899949009490194902949039490494905949069490794908949099491094911949129491394914949159491694917949189491994920949219492294923949249492594926949279492894929949309493194932949339493494935949369493794938949399494094941949429494394944949459494694947949489494994950949519495294953949549495594956949579495894959949609496194962949639496494965949669496794968949699497094971949729497394974949759497694977949789497994980949819498294983949849498594986949879498894989949909499194992949939499494995949969499794998949999500095001950029500395004950059500695007950089500995010950119501295013950149501595016950179501895019950209502195022950239502495025950269502795028950299503095031950329503395034950359503695037950389503995040950419504295043950449504595046950479504895049950509505195052950539505495055950569505795058950599506095061950629506395064950659506695067950689506995070950719507295073950749507595076950779507895079950809508195082950839508495085950869508795088950899509095091950929509395094950959509695097950989509995100951019510295103951049510595106951079510895109951109511195112951139511495115951169511795118951199512095121951229512395124951259512695127951289512995130951319513295133951349513595136951379513895139951409514195142951439514495145951469514795148951499515095151951529515395154951559515695157951589515995160951619516295163951649516595166951679516895169951709517195172951739517495175951769517795178951799518095181951829518395184951859518695187951889518995190951919519295193951949519595196951979519895199952009520195202952039520495205952069520795208952099521095211952129521395214952159521695217952189521995220952219522295223952249522595226952279522895229952309523195232952339523495235952369523795238952399524095241952429524395244952459524695247952489524995250952519525295253952549525595256952579525895259952609526195262952639526495265952669526795268952699527095271952729527395274952759527695277952789527995280952819528295283952849528595286952879528895289952909529195292952939529495295952969529795298952999530095301953029530395304953059530695307953089530995310953119531295313953149531595316953179531895319953209532195322953239532495325953269532795328953299533095331953329533395334953359533695337953389533995340953419534295343953449534595346953479534895349953509535195352953539535495355953569535795358953599536095361953629536395364953659536695367953689536995370953719537295373953749537595376953779537895379953809538195382953839538495385953869538795388953899539095391953929539395394953959539695397953989539995400954019540295403954049540595406954079540895409954109541195412954139541495415954169541795418954199542095421954229542395424954259542695427954289542995430954319543295433954349543595436954379543895439954409544195442954439544495445954469544795448954499545095451954529545395454954559545695457954589545995460954619546295463954649546595466954679546895469954709547195472954739547495475954769547795478954799548095481954829548395484954859548695487954889548995490954919549295493954949549595496954979549895499955009550195502955039550495505955069550795508955099551095511955129551395514955159551695517955189551995520955219552295523955249552595526955279552895529955309553195532955339553495535955369553795538955399554095541955429554395544955459554695547955489554995550955519555295553955549555595556955579555895559955609556195562955639556495565955669556795568955699557095571955729557395574955759557695577955789557995580955819558295583955849558595586955879558895589955909559195592955939559495595955969559795598955999560095601956029560395604956059560695607956089560995610956119561295613956149561595616956179561895619956209562195622956239562495625956269562795628956299563095631956329563395634956359563695637956389563995640956419564295643956449564595646956479564895649956509565195652956539565495655956569565795658956599566095661956629566395664956659566695667956689566995670956719567295673956749567595676956779567895679956809568195682956839568495685956869568795688956899569095691956929569395694956959569695697956989569995700957019570295703957049570595706957079570895709957109571195712957139571495715957169571795718957199572095721957229572395724957259572695727957289572995730957319573295733957349573595736957379573895739957409574195742957439574495745957469574795748957499575095751957529575395754957559575695757957589575995760957619576295763957649576595766957679576895769957709577195772957739577495775957769577795778957799578095781957829578395784957859578695787957889578995790957919579295793957949579595796957979579895799958009580195802958039580495805958069580795808958099581095811958129581395814958159581695817958189581995820958219582295823958249582595826958279582895829958309583195832958339583495835958369583795838958399584095841958429584395844958459584695847958489584995850958519585295853958549585595856958579585895859958609586195862958639586495865958669586795868958699587095871958729587395874958759587695877958789587995880958819588295883958849588595886958879588895889958909589195892958939589495895958969589795898958999590095901959029590395904959059590695907959089590995910959119591295913959149591595916959179591895919959209592195922959239592495925959269592795928959299593095931959329593395934959359593695937959389593995940959419594295943959449594595946959479594895949959509595195952959539595495955959569595795958959599596095961959629596395964959659596695967959689596995970959719597295973959749597595976959779597895979959809598195982959839598495985959869598795988959899599095991959929599395994959959599695997959989599996000960019600296003960049600596006960079600896009960109601196012960139601496015960169601796018960199602096021960229602396024960259602696027960289602996030960319603296033960349603596036960379603896039960409604196042960439604496045960469604796048960499605096051960529605396054960559605696057960589605996060960619606296063960649606596066960679606896069960709607196072960739607496075960769607796078960799608096081960829608396084960859608696087960889608996090960919609296093960949609596096960979609896099961009610196102961039610496105961069610796108961099611096111961129611396114961159611696117961189611996120961219612296123961249612596126961279612896129961309613196132961339613496135961369613796138961399614096141961429614396144961459614696147961489614996150961519615296153961549615596156961579615896159961609616196162961639616496165961669616796168961699617096171961729617396174961759617696177961789617996180961819618296183961849618596186961879618896189961909619196192961939619496195961969619796198961999620096201962029620396204962059620696207962089620996210962119621296213962149621596216962179621896219962209622196222962239622496225962269622796228962299623096231962329623396234962359623696237962389623996240962419624296243962449624596246962479624896249962509625196252962539625496255962569625796258962599626096261962629626396264962659626696267962689626996270962719627296273962749627596276962779627896279962809628196282962839628496285962869628796288962899629096291962929629396294962959629696297962989629996300963019630296303963049630596306963079630896309963109631196312963139631496315963169631796318963199632096321963229632396324963259632696327963289632996330963319633296333963349633596336963379633896339963409634196342963439634496345963469634796348963499635096351963529635396354963559635696357963589635996360963619636296363963649636596366963679636896369963709637196372963739637496375963769637796378963799638096381963829638396384963859638696387963889638996390963919639296393963949639596396963979639896399964009640196402964039640496405964069640796408964099641096411964129641396414964159641696417964189641996420964219642296423964249642596426964279642896429964309643196432964339643496435964369643796438964399644096441964429644396444964459644696447964489644996450964519645296453964549645596456964579645896459964609646196462964639646496465964669646796468964699647096471964729647396474964759647696477964789647996480964819648296483964849648596486964879648896489964909649196492964939649496495964969649796498964999650096501965029650396504965059650696507965089650996510965119651296513965149651596516965179651896519965209652196522965239652496525965269652796528965299653096531965329653396534965359653696537965389653996540965419654296543965449654596546965479654896549965509655196552965539655496555965569655796558965599656096561965629656396564965659656696567965689656996570965719657296573965749657596576965779657896579965809658196582965839658496585965869658796588965899659096591965929659396594965959659696597965989659996600966019660296603966049660596606966079660896609966109661196612966139661496615966169661796618966199662096621966229662396624966259662696627966289662996630966319663296633966349663596636966379663896639966409664196642966439664496645966469664796648966499665096651966529665396654966559665696657966589665996660966619666296663966649666596666966679666896669966709667196672966739667496675966769667796678966799668096681966829668396684966859668696687966889668996690966919669296693966949669596696966979669896699967009670196702967039670496705967069670796708967099671096711967129671396714967159671696717967189671996720967219672296723967249672596726967279672896729967309673196732967339673496735967369673796738967399674096741967429674396744967459674696747967489674996750967519675296753967549675596756967579675896759967609676196762967639676496765967669676796768967699677096771967729677396774967759677696777967789677996780967819678296783967849678596786967879678896789967909679196792967939679496795967969679796798967999680096801968029680396804968059680696807968089680996810968119681296813968149681596816968179681896819968209682196822968239682496825968269682796828968299683096831968329683396834968359683696837968389683996840968419684296843968449684596846968479684896849968509685196852968539685496855968569685796858968599686096861968629686396864968659686696867968689686996870968719687296873968749687596876968779687896879968809688196882968839688496885968869688796888968899689096891968929689396894968959689696897968989689996900969019690296903969049690596906969079690896909969109691196912969139691496915969169691796918969199692096921969229692396924969259692696927969289692996930969319693296933969349693596936969379693896939969409694196942969439694496945969469694796948969499695096951969529695396954969559695696957969589695996960969619696296963969649696596966969679696896969969709697196972969739697496975969769697796978969799698096981969829698396984969859698696987969889698996990969919699296993969949699596996969979699896999970009700197002970039700497005970069700797008970099701097011970129701397014970159701697017970189701997020970219702297023970249702597026970279702897029970309703197032970339703497035970369703797038970399704097041970429704397044970459704697047970489704997050970519705297053970549705597056970579705897059970609706197062970639706497065970669706797068970699707097071970729707397074970759707697077970789707997080970819708297083970849708597086970879708897089970909709197092970939709497095970969709797098970999710097101971029710397104971059710697107971089710997110971119711297113971149711597116971179711897119971209712197122971239712497125971269712797128971299713097131971329713397134971359713697137971389713997140971419714297143971449714597146971479714897149971509715197152971539715497155971569715797158971599716097161971629716397164971659716697167971689716997170971719717297173971749717597176971779717897179971809718197182971839718497185971869718797188971899719097191971929719397194971959719697197971989719997200972019720297203972049720597206972079720897209972109721197212972139721497215972169721797218972199722097221972229722397224972259722697227972289722997230972319723297233972349723597236972379723897239972409724197242972439724497245972469724797248972499725097251972529725397254972559725697257972589725997260972619726297263972649726597266972679726897269972709727197272972739727497275972769727797278972799728097281972829728397284972859728697287972889728997290972919729297293972949729597296972979729897299973009730197302973039730497305973069730797308973099731097311973129731397314973159731697317973189731997320973219732297323973249732597326973279732897329973309733197332973339733497335973369733797338973399734097341973429734397344973459734697347973489734997350973519735297353973549735597356973579735897359973609736197362973639736497365973669736797368973699737097371973729737397374973759737697377973789737997380973819738297383973849738597386973879738897389973909739197392973939739497395973969739797398973999740097401974029740397404974059740697407974089740997410974119741297413974149741597416974179741897419974209742197422974239742497425974269742797428974299743097431974329743397434974359743697437974389743997440974419744297443974449744597446974479744897449974509745197452974539745497455974569745797458974599746097461974629746397464974659746697467974689746997470974719747297473974749747597476974779747897479974809748197482974839748497485974869748797488974899749097491974929749397494974959749697497974989749997500975019750297503975049750597506975079750897509975109751197512975139751497515975169751797518975199752097521975229752397524975259752697527975289752997530975319753297533975349753597536975379753897539975409754197542975439754497545975469754797548975499755097551975529755397554975559755697557975589755997560975619756297563975649756597566975679756897569975709757197572975739757497575975769757797578975799758097581975829758397584975859758697587975889758997590975919759297593975949759597596975979759897599976009760197602976039760497605976069760797608976099761097611976129761397614976159761697617976189761997620976219762297623976249762597626976279762897629976309763197632976339763497635976369763797638976399764097641976429764397644976459764697647976489764997650976519765297653976549765597656976579765897659976609766197662976639766497665976669766797668976699767097671976729767397674976759767697677976789767997680976819768297683976849768597686976879768897689976909769197692976939769497695976969769797698976999770097701977029770397704977059770697707977089770997710977119771297713977149771597716977179771897719977209772197722977239772497725977269772797728977299773097731977329773397734977359773697737977389773997740977419774297743977449774597746977479774897749977509775197752977539775497755977569775797758977599776097761977629776397764977659776697767977689776997770977719777297773977749777597776977779777897779977809778197782977839778497785977869778797788977899779097791977929779397794977959779697797977989779997800978019780297803978049780597806978079780897809978109781197812978139781497815978169781797818978199782097821978229782397824978259782697827978289782997830978319783297833978349783597836978379783897839978409784197842978439784497845978469784797848978499785097851978529785397854978559785697857978589785997860978619786297863978649786597866978679786897869978709787197872978739787497875978769787797878978799788097881978829788397884978859788697887978889788997890978919789297893978949789597896978979789897899979009790197902979039790497905979069790797908979099791097911979129791397914979159791697917979189791997920979219792297923979249792597926979279792897929979309793197932979339793497935979369793797938979399794097941979429794397944979459794697947979489794997950979519795297953979549795597956979579795897959979609796197962979639796497965979669796797968979699797097971979729797397974979759797697977979789797997980979819798297983979849798597986979879798897989979909799197992979939799497995979969799797998979999800098001980029800398004980059800698007980089800998010980119801298013980149801598016980179801898019980209802198022980239802498025980269802798028980299803098031980329803398034980359803698037980389803998040980419804298043980449804598046980479804898049980509805198052980539805498055980569805798058980599806098061980629806398064980659806698067980689806998070980719807298073980749807598076980779807898079980809808198082980839808498085980869808798088980899809098091980929809398094980959809698097980989809998100981019810298103981049810598106981079810898109981109811198112981139811498115981169811798118981199812098121981229812398124981259812698127981289812998130981319813298133981349813598136981379813898139981409814198142981439814498145981469814798148981499815098151981529815398154981559815698157981589815998160981619816298163981649816598166981679816898169981709817198172981739817498175981769817798178981799818098181981829818398184981859818698187981889818998190981919819298193981949819598196981979819898199982009820198202982039820498205982069820798208982099821098211982129821398214982159821698217982189821998220982219822298223982249822598226982279822898229982309823198232982339823498235982369823798238982399824098241982429824398244982459824698247982489824998250982519825298253982549825598256982579825898259982609826198262982639826498265982669826798268982699827098271982729827398274982759827698277982789827998280982819828298283982849828598286982879828898289982909829198292982939829498295982969829798298982999830098301983029830398304983059830698307983089830998310983119831298313983149831598316983179831898319983209832198322983239832498325983269832798328983299833098331983329833398334983359833698337983389833998340983419834298343983449834598346983479834898349983509835198352983539835498355983569835798358983599836098361983629836398364983659836698367983689836998370983719837298373983749837598376983779837898379983809838198382983839838498385983869838798388983899839098391983929839398394983959839698397983989839998400984019840298403984049840598406984079840898409984109841198412984139841498415984169841798418984199842098421984229842398424984259842698427984289842998430984319843298433984349843598436984379843898439984409844198442984439844498445984469844798448984499845098451984529845398454984559845698457984589845998460984619846298463984649846598466984679846898469984709847198472984739847498475984769847798478984799848098481984829848398484984859848698487984889848998490984919849298493984949849598496984979849898499985009850198502985039850498505985069850798508985099851098511985129851398514985159851698517985189851998520985219852298523985249852598526985279852898529985309853198532985339853498535985369853798538985399854098541985429854398544985459854698547985489854998550985519855298553985549855598556985579855898559985609856198562985639856498565985669856798568985699857098571985729857398574985759857698577985789857998580985819858298583985849858598586985879858898589985909859198592985939859498595985969859798598985999860098601986029860398604986059860698607986089860998610986119861298613986149861598616986179861898619986209862198622986239862498625986269862798628986299863098631986329863398634986359863698637986389863998640986419864298643986449864598646986479864898649986509865198652986539865498655986569865798658986599866098661986629866398664986659866698667986689866998670986719867298673986749867598676986779867898679986809868198682986839868498685986869868798688986899869098691986929869398694986959869698697986989869998700987019870298703987049870598706987079870898709987109871198712987139871498715987169871798718987199872098721987229872398724987259872698727987289872998730987319873298733987349873598736987379873898739987409874198742987439874498745987469874798748987499875098751987529875398754987559875698757987589875998760987619876298763987649876598766987679876898769987709877198772987739877498775987769877798778987799878098781987829878398784987859878698787987889878998790987919879298793987949879598796987979879898799988009880198802988039880498805988069880798808988099881098811988129881398814988159881698817988189881998820988219882298823988249882598826988279882898829988309883198832988339883498835988369883798838988399884098841988429884398844988459884698847988489884998850988519885298853988549885598856988579885898859988609886198862988639886498865988669886798868988699887098871988729887398874988759887698877988789887998880988819888298883988849888598886988879888898889988909889198892988939889498895988969889798898988999890098901989029890398904989059890698907989089890998910989119891298913989149891598916989179891898919989209892198922989239892498925989269892798928989299893098931989329893398934989359893698937989389893998940989419894298943989449894598946989479894898949989509895198952989539895498955989569895798958989599896098961989629896398964989659896698967989689896998970989719897298973989749897598976989779897898979989809898198982989839898498985989869898798988989899899098991989929899398994989959899698997989989899999000990019900299003990049900599006990079900899009990109901199012990139901499015990169901799018990199902099021990229902399024990259902699027990289902999030990319903299033990349903599036990379903899039990409904199042990439904499045990469904799048990499905099051990529905399054990559905699057990589905999060990619906299063990649906599066990679906899069990709907199072990739907499075990769907799078990799908099081990829908399084990859908699087990889908999090990919909299093990949909599096990979909899099991009910199102991039910499105991069910799108991099911099111991129911399114991159911699117991189911999120991219912299123991249912599126991279912899129991309913199132991339913499135991369913799138991399914099141991429914399144991459914699147991489914999150991519915299153991549915599156991579915899159991609916199162991639916499165991669916799168991699917099171991729917399174991759917699177991789917999180991819918299183991849918599186991879918899189991909919199192991939919499195991969919799198991999920099201992029920399204992059920699207992089920999210992119921299213992149921599216992179921899219992209922199222992239922499225992269922799228992299923099231992329923399234992359923699237992389923999240992419924299243992449924599246992479924899249992509925199252992539925499255992569925799258992599926099261992629926399264992659926699267992689926999270992719927299273992749927599276992779927899279992809928199282992839928499285992869928799288992899929099291992929929399294992959929699297992989929999300993019930299303993049930599306993079930899309993109931199312993139931499315993169931799318993199932099321993229932399324993259932699327993289932999330993319933299333993349933599336993379933899339993409934199342993439934499345993469934799348993499935099351993529935399354993559935699357993589935999360993619936299363993649936599366993679936899369993709937199372993739937499375993769937799378993799938099381993829938399384993859938699387993889938999390993919939299393993949939599396993979939899399994009940199402994039940499405994069940799408994099941099411994129941399414994159941699417994189941999420994219942299423994249942599426994279942899429994309943199432994339943499435994369943799438994399944099441994429944399444994459944699447994489944999450994519945299453994549945599456994579945899459994609946199462994639946499465994669946799468994699947099471994729947399474994759947699477994789947999480994819948299483994849948599486994879948899489994909949199492994939949499495994969949799498994999950099501995029950399504995059950699507995089950999510995119951299513995149951599516995179951899519995209952199522995239952499525995269952799528995299953099531995329953399534995359953699537995389953999540995419954299543995449954599546995479954899549995509955199552995539955499555995569955799558995599956099561995629956399564995659956699567995689956999570995719957299573995749957599576995779957899579995809958199582995839958499585995869958799588995899959099591995929959399594995959959699597995989959999600996019960299603996049960599606996079960899609996109961199612996139961499615996169961799618996199962099621996229962399624996259962699627996289962999630996319963299633996349963599636996379963899639996409964199642996439964499645996469964799648996499965099651996529965399654996559965699657996589965999660996619966299663996649966599666996679966899669996709967199672996739967499675996769967799678996799968099681996829968399684996859968699687996889968999690996919969299693996949969599696996979969899699997009970199702997039970499705997069970799708997099971099711997129971399714997159971699717997189971999720997219972299723997249972599726997279972899729997309973199732997339973499735997369973799738997399974099741997429974399744997459974699747997489974999750997519975299753997549975599756997579975899759997609976199762997639976499765997669976799768997699977099771997729977399774997759977699777997789977999780997819978299783997849978599786997879978899789997909979199792997939979499795997969979799798997999980099801998029980399804998059980699807998089980999810998119981299813998149981599816998179981899819998209982199822998239982499825998269982799828998299983099831998329983399834998359983699837998389983999840998419984299843998449984599846998479984899849998509985199852998539985499855998569985799858998599986099861998629986399864998659986699867998689986999870998719987299873998749987599876998779987899879998809988199882998839988499885998869988799888998899989099891998929989399894998959989699897998989989999900999019990299903999049990599906999079990899909999109991199912999139991499915999169991799918999199992099921999229992399924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999100000100001100002100003100004100005100006100007100008100009100010100011100012100013100014100015100016100017100018100019100020100021100022100023100024100025100026100027100028100029100030100031100032100033100034100035100036100037100038100039100040100041100042100043100044100045100046100047100048100049100050100051100052100053100054100055100056100057100058100059100060100061100062100063100064100065100066100067100068100069100070100071100072100073100074100075100076100077100078100079100080100081100082100083100084100085100086100087100088100089100090100091100092100093100094100095100096100097100098100099100100100101100102100103100104100105100106100107100108100109100110100111100112100113100114100115100116100117100118100119100120100121100122100123100124100125100126100127100128100129100130100131100132100133100134100135100136100137100138100139100140100141100142100143100144100145100146100147100148100149100150100151100152100153100154100155100156100157100158100159100160100161100162100163100164100165100166100167100168100169100170100171100172100173100174100175100176100177100178100179100180100181100182100183100184100185100186100187100188100189100190100191100192100193100194100195100196100197100198100199100200100201100202100203100204100205100206100207100208100209100210100211100212100213100214100215100216100217100218100219100220100221100222100223100224100225100226100227100228100229100230100231100232100233100234100235100236100237100238100239100240100241100242100243100244100245100246100247100248100249100250100251100252100253100254100255100256100257100258100259100260100261100262100263100264100265100266100267100268100269100270100271100272100273100274100275100276100277100278100279100280100281100282100283100284100285100286100287100288100289100290100291100292100293100294100295100296100297100298100299100300100301100302100303100304100305100306100307100308100309100310100311100312100313100314100315100316100317100318100319100320100321100322100323100324100325100326100327100328100329100330100331100332100333100334100335100336100337100338100339100340100341100342100343100344100345100346100347100348100349100350100351100352100353100354100355100356100357100358100359100360100361100362100363100364100365100366100367100368100369100370100371100372100373100374100375100376100377100378100379100380100381100382100383100384100385100386100387100388100389100390100391100392100393100394100395100396100397100398100399100400100401100402100403100404100405100406100407100408100409100410100411100412100413100414100415100416100417100418100419100420100421100422100423100424100425100426100427100428100429100430100431100432100433100434100435100436100437100438100439100440100441100442100443100444100445100446100447100448100449100450100451100452100453100454100455100456100457100458100459100460100461100462100463100464100465100466100467100468100469100470100471100472100473100474100475100476100477100478100479100480100481100482100483100484100485100486100487100488100489100490100491100492100493100494100495100496100497100498100499100500100501100502100503100504100505100506100507100508100509100510100511100512100513100514100515100516100517100518100519100520100521100522100523100524100525100526100527100528100529100530100531100532100533100534100535100536100537100538100539100540100541100542100543100544100545100546100547100548100549100550100551100552100553100554100555100556100557100558100559100560100561100562100563100564100565100566100567100568100569100570100571100572100573100574100575100576100577100578100579100580100581100582100583100584100585100586100587100588100589100590100591100592100593100594100595100596100597100598100599100600100601100602100603100604100605100606100607100608100609100610100611100612100613100614100615100616100617100618100619100620100621100622100623100624100625100626100627100628100629100630100631100632100633100634100635100636100637100638100639100640100641100642100643100644100645100646100647100648100649100650100651100652100653100654100655100656100657100658100659100660100661100662100663100664100665100666100667100668100669100670100671100672100673100674100675100676100677100678100679100680100681100682100683100684100685100686100687100688100689100690100691100692100693100694100695100696100697100698100699100700100701100702100703100704100705100706100707100708100709100710100711100712100713100714100715100716100717100718100719100720100721100722100723100724100725100726100727100728100729100730100731100732100733100734100735100736100737100738100739100740100741100742100743100744100745100746100747100748100749100750100751100752100753100754100755100756100757100758100759100760100761100762100763100764100765100766100767100768100769100770100771100772100773100774100775100776100777100778100779100780100781100782100783100784100785100786100787100788100789100790100791100792100793100794100795100796100797100798100799100800100801100802100803100804100805100806100807100808100809100810100811100812100813100814100815100816100817100818100819100820100821100822100823100824100825100826100827100828100829100830100831100832100833100834100835100836100837100838100839100840100841100842100843100844100845100846100847100848100849100850100851100852100853100854100855100856100857100858100859100860100861100862100863100864100865100866100867100868100869100870100871100872100873100874100875100876100877100878100879100880100881100882100883100884100885100886100887100888100889100890100891100892100893100894100895100896100897100898100899100900100901100902100903100904100905100906100907100908100909100910100911100912100913100914100915100916100917100918100919100920100921100922100923100924100925100926100927100928100929100930100931100932100933100934100935100936100937100938100939100940100941100942100943100944100945100946100947100948100949100950100951100952100953100954100955100956100957100958100959100960100961100962100963100964100965100966100967100968100969100970100971100972100973100974100975100976100977100978100979100980100981100982100983100984100985100986100987100988100989100990100991100992100993100994100995100996100997100998100999101000101001101002101003101004101005101006101007101008101009101010101011101012101013101014101015101016101017101018101019101020101021101022101023101024101025101026101027101028101029101030101031101032101033101034101035101036101037101038101039101040101041101042101043101044101045101046101047101048101049101050101051101052101053101054101055101056101057101058101059101060101061101062101063101064101065101066101067101068101069101070101071101072101073101074101075101076101077101078101079101080101081101082101083101084101085101086101087101088101089101090101091101092101093101094101095101096101097101098101099101100101101101102101103101104101105101106101107101108101109101110101111101112101113101114101115101116101117101118101119101120101121101122101123101124101125101126101127101128101129101130101131101132101133101134101135101136101137101138101139101140101141101142101143101144101145101146101147101148101149101150101151101152101153101154101155101156101157101158101159101160101161101162101163101164101165101166101167101168101169101170101171101172101173101174101175101176101177101178101179101180101181101182101183101184101185101186101187101188101189101190101191101192101193101194101195101196101197101198101199101200101201101202101203101204101205101206101207101208101209101210101211101212101213101214101215101216101217101218101219101220101221101222101223101224101225101226101227101228101229101230101231101232101233101234101235101236101237101238101239101240101241101242101243101244101245101246101247101248101249101250101251101252101253101254101255101256101257101258101259101260101261101262101263101264101265101266101267101268101269101270101271101272101273101274101275101276101277101278101279101280101281101282101283101284101285101286101287101288101289101290101291101292101293101294101295101296101297101298101299101300101301101302101303101304101305101306101307101308101309101310101311101312101313101314101315101316101317101318101319101320101321101322101323101324101325101326101327101328101329101330101331101332101333101334101335101336101337101338101339101340101341101342101343101344101345101346101347101348101349101350101351101352101353101354101355101356101357101358101359101360101361101362101363101364101365101366101367101368101369101370101371101372101373101374101375101376101377101378101379101380101381101382101383101384101385101386101387101388101389101390101391101392101393101394101395101396101397101398101399101400101401101402101403101404101405101406101407101408101409101410101411101412101413101414101415101416101417101418101419101420101421101422101423101424101425101426101427101428101429101430101431101432101433101434101435101436101437101438101439101440101441101442101443101444101445101446101447101448101449101450101451101452101453101454101455101456101457101458101459101460101461101462101463101464101465101466101467101468101469101470101471101472101473101474101475101476101477101478101479101480101481101482101483101484101485101486101487101488101489101490101491101492101493101494101495101496101497101498101499101500101501101502101503101504101505101506101507101508101509101510101511101512101513101514101515101516101517101518101519101520101521101522101523101524101525101526101527101528101529101530101531101532101533101534101535101536101537101538101539101540101541101542101543101544101545101546101547101548101549101550101551101552101553101554101555101556101557101558101559101560101561101562101563101564101565101566101567101568101569101570101571101572101573101574101575101576101577101578101579101580101581101582101583101584101585101586101587101588101589101590101591101592101593101594101595101596101597101598101599101600101601101602101603101604101605101606101607101608101609101610101611101612101613101614101615101616101617101618101619101620101621101622101623101624101625101626101627101628101629101630101631101632101633101634101635101636101637101638101639101640101641101642101643101644101645101646101647101648101649101650101651101652101653101654101655101656101657101658101659101660101661101662101663101664101665101666101667101668101669101670101671101672101673101674101675101676101677101678101679101680101681101682101683101684101685101686101687101688101689101690101691101692101693101694101695101696101697101698101699101700101701101702101703101704101705101706101707101708101709101710101711101712101713101714101715101716101717101718101719101720101721101722101723101724101725101726101727101728101729101730101731101732101733101734101735101736101737101738101739101740101741101742101743101744101745101746101747101748101749101750101751101752101753101754101755101756101757101758101759101760101761101762101763101764101765101766101767101768101769101770101771101772101773101774101775101776101777101778101779101780101781101782101783101784101785101786101787101788101789101790101791101792101793101794101795101796101797101798101799101800101801101802101803101804101805101806101807101808101809101810101811101812101813101814101815101816101817101818101819101820101821101822101823101824101825101826101827101828101829101830101831101832101833101834101835101836101837101838101839101840101841101842101843101844101845101846101847101848101849101850101851101852101853101854101855101856101857101858101859101860101861101862101863101864101865101866101867101868101869101870101871101872101873101874101875101876101877101878101879101880101881101882101883101884101885101886101887101888101889101890101891101892101893101894101895101896101897101898101899101900101901101902101903101904101905101906101907101908101909101910101911101912101913101914101915101916101917101918101919101920101921101922101923101924101925101926101927101928101929101930101931101932101933101934101935101936101937101938101939101940101941101942101943101944101945101946101947101948101949101950101951101952101953101954101955101956101957101958101959101960101961101962101963101964101965101966101967101968101969101970101971101972101973101974101975101976101977101978101979101980101981101982101983101984101985101986101987101988101989101990101991101992101993101994101995101996101997101998101999102000102001102002102003102004102005102006102007102008102009102010102011102012102013102014102015102016102017102018102019102020102021102022102023102024102025102026102027102028102029102030102031102032102033102034102035102036102037102038102039102040102041102042102043102044102045102046102047102048102049102050102051
  1. diff -Nur linux-3.10.37/arch/arm/configs/bcmrpi_cutdown_defconfig linux-rpi/arch/arm/configs/bcmrpi_cutdown_defconfig
  2. --- linux-3.10.37/arch/arm/configs/bcmrpi_cutdown_defconfig 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-rpi/arch/arm/configs/bcmrpi_cutdown_defconfig 2014-04-24 15:35:00.717527267 +0200
  4. @@ -0,0 +1,504 @@
  5. +CONFIG_EXPERIMENTAL=y
  6. +# CONFIG_LOCALVERSION_AUTO is not set
  7. +CONFIG_SYSVIPC=y
  8. +CONFIG_POSIX_MQUEUE=y
  9. +CONFIG_IKCONFIG=y
  10. +CONFIG_IKCONFIG_PROC=y
  11. +# CONFIG_UID16 is not set
  12. +# CONFIG_KALLSYMS is not set
  13. +CONFIG_EMBEDDED=y
  14. +# CONFIG_VM_EVENT_COUNTERS is not set
  15. +# CONFIG_COMPAT_BRK is not set
  16. +CONFIG_SLAB=y
  17. +CONFIG_MODULES=y
  18. +CONFIG_MODULE_UNLOAD=y
  19. +CONFIG_MODVERSIONS=y
  20. +CONFIG_MODULE_SRCVERSION_ALL=y
  21. +# CONFIG_BLK_DEV_BSG is not set
  22. +CONFIG_ARCH_BCM2708=y
  23. +CONFIG_NO_HZ=y
  24. +CONFIG_HIGH_RES_TIMERS=y
  25. +CONFIG_AEABI=y
  26. +CONFIG_ZBOOT_ROM_TEXT=0x0
  27. +CONFIG_ZBOOT_ROM_BSS=0x0
  28. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  29. +CONFIG_CPU_IDLE=y
  30. +CONFIG_VFP=y
  31. +CONFIG_BINFMT_MISC=m
  32. +CONFIG_NET=y
  33. +CONFIG_PACKET=y
  34. +CONFIG_UNIX=y
  35. +CONFIG_XFRM_USER=y
  36. +CONFIG_NET_KEY=m
  37. +CONFIG_INET=y
  38. +CONFIG_IP_MULTICAST=y
  39. +CONFIG_IP_PNP=y
  40. +CONFIG_IP_PNP_DHCP=y
  41. +CONFIG_IP_PNP_RARP=y
  42. +CONFIG_SYN_COOKIES=y
  43. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  44. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  45. +# CONFIG_INET_XFRM_MODE_BEET is not set
  46. +# CONFIG_INET_LRO is not set
  47. +# CONFIG_INET_DIAG is not set
  48. +# CONFIG_IPV6 is not set
  49. +CONFIG_NET_PKTGEN=m
  50. +CONFIG_IRDA=m
  51. +CONFIG_IRLAN=m
  52. +CONFIG_IRCOMM=m
  53. +CONFIG_IRDA_ULTRA=y
  54. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  55. +CONFIG_IRDA_FAST_RR=y
  56. +CONFIG_IRTTY_SIR=m
  57. +CONFIG_KINGSUN_DONGLE=m
  58. +CONFIG_KSDAZZLE_DONGLE=m
  59. +CONFIG_KS959_DONGLE=m
  60. +CONFIG_USB_IRDA=m
  61. +CONFIG_SIGMATEL_FIR=m
  62. +CONFIG_MCS_FIR=m
  63. +CONFIG_BT=m
  64. +CONFIG_BT_L2CAP=y
  65. +CONFIG_BT_SCO=y
  66. +CONFIG_BT_RFCOMM=m
  67. +CONFIG_BT_RFCOMM_TTY=y
  68. +CONFIG_BT_BNEP=m
  69. +CONFIG_BT_BNEP_MC_FILTER=y
  70. +CONFIG_BT_BNEP_PROTO_FILTER=y
  71. +CONFIG_BT_HIDP=m
  72. +CONFIG_BT_HCIBTUSB=m
  73. +CONFIG_BT_HCIBCM203X=m
  74. +CONFIG_BT_HCIBPA10X=m
  75. +CONFIG_BT_HCIBFUSB=m
  76. +CONFIG_BT_HCIVHCI=m
  77. +CONFIG_BT_MRVL=m
  78. +CONFIG_BT_MRVL_SDIO=m
  79. +CONFIG_BT_ATH3K=m
  80. +CONFIG_CFG80211=m
  81. +CONFIG_MAC80211=m
  82. +CONFIG_MAC80211_RC_PID=y
  83. +CONFIG_MAC80211_MESH=y
  84. +CONFIG_WIMAX=m
  85. +CONFIG_NET_9P=m
  86. +CONFIG_NFC=m
  87. +CONFIG_NFC_PN533=m
  88. +CONFIG_DEVTMPFS=y
  89. +CONFIG_BLK_DEV_LOOP=y
  90. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  91. +CONFIG_BLK_DEV_NBD=m
  92. +CONFIG_BLK_DEV_RAM=y
  93. +CONFIG_CDROM_PKTCDVD=m
  94. +CONFIG_MISC_DEVICES=y
  95. +CONFIG_SCSI=y
  96. +# CONFIG_SCSI_PROC_FS is not set
  97. +CONFIG_BLK_DEV_SD=m
  98. +CONFIG_BLK_DEV_SR=m
  99. +CONFIG_SCSI_MULTI_LUN=y
  100. +# CONFIG_SCSI_LOWLEVEL is not set
  101. +CONFIG_NETDEVICES=y
  102. +CONFIG_TUN=m
  103. +CONFIG_PHYLIB=m
  104. +CONFIG_MDIO_BITBANG=m
  105. +CONFIG_NET_ETHERNET=y
  106. +# CONFIG_NETDEV_1000 is not set
  107. +# CONFIG_NETDEV_10000 is not set
  108. +CONFIG_LIBERTAS_THINFIRM=m
  109. +CONFIG_LIBERTAS_THINFIRM_USB=m
  110. +CONFIG_AT76C50X_USB=m
  111. +CONFIG_USB_ZD1201=m
  112. +CONFIG_USB_NET_RNDIS_WLAN=m
  113. +CONFIG_RTL8187=m
  114. +CONFIG_MAC80211_HWSIM=m
  115. +CONFIG_ATH_COMMON=m
  116. +CONFIG_ATH9K=m
  117. +CONFIG_ATH9K_HTC=m
  118. +CONFIG_CARL9170=m
  119. +CONFIG_B43=m
  120. +CONFIG_B43LEGACY=m
  121. +CONFIG_HOSTAP=m
  122. +CONFIG_IWM=m
  123. +CONFIG_LIBERTAS=m
  124. +CONFIG_LIBERTAS_USB=m
  125. +CONFIG_LIBERTAS_SDIO=m
  126. +CONFIG_P54_COMMON=m
  127. +CONFIG_P54_USB=m
  128. +CONFIG_RT2X00=m
  129. +CONFIG_RT2500USB=m
  130. +CONFIG_RT73USB=m
  131. +CONFIG_RT2800USB=m
  132. +CONFIG_RT2800USB_RT53XX=y
  133. +CONFIG_RTL8192CU=m
  134. +CONFIG_WL1251=m
  135. +CONFIG_WL12XX_MENU=m
  136. +CONFIG_ZD1211RW=m
  137. +CONFIG_MWIFIEX=m
  138. +CONFIG_MWIFIEX_SDIO=m
  139. +CONFIG_WIMAX_I2400M_USB=m
  140. +CONFIG_USB_CATC=m
  141. +CONFIG_USB_KAWETH=m
  142. +CONFIG_USB_PEGASUS=m
  143. +CONFIG_USB_RTL8150=m
  144. +CONFIG_USB_USBNET=y
  145. +CONFIG_USB_NET_AX8817X=m
  146. +CONFIG_USB_NET_CDCETHER=m
  147. +CONFIG_USB_NET_CDC_EEM=m
  148. +CONFIG_USB_NET_DM9601=m
  149. +CONFIG_USB_NET_SMSC75XX=m
  150. +CONFIG_USB_NET_SMSC95XX=y
  151. +CONFIG_USB_NET_GL620A=m
  152. +CONFIG_USB_NET_NET1080=m
  153. +CONFIG_USB_NET_PLUSB=m
  154. +CONFIG_USB_NET_MCS7830=m
  155. +CONFIG_USB_NET_CDC_SUBSET=m
  156. +CONFIG_USB_ALI_M5632=y
  157. +CONFIG_USB_AN2720=y
  158. +CONFIG_USB_KC2190=y
  159. +# CONFIG_USB_NET_ZAURUS is not set
  160. +CONFIG_USB_NET_CX82310_ETH=m
  161. +CONFIG_USB_NET_KALMIA=m
  162. +CONFIG_USB_NET_INT51X1=m
  163. +CONFIG_USB_IPHETH=m
  164. +CONFIG_USB_SIERRA_NET=m
  165. +CONFIG_USB_VL600=m
  166. +CONFIG_PPP=m
  167. +CONFIG_PPP_ASYNC=m
  168. +CONFIG_PPP_SYNC_TTY=m
  169. +CONFIG_PPP_DEFLATE=m
  170. +CONFIG_PPP_BSDCOMP=m
  171. +CONFIG_SLIP=m
  172. +CONFIG_SLIP_COMPRESSED=y
  173. +CONFIG_NETCONSOLE=m
  174. +CONFIG_INPUT_POLLDEV=m
  175. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  176. +CONFIG_INPUT_JOYDEV=m
  177. +CONFIG_INPUT_EVDEV=m
  178. +# CONFIG_INPUT_KEYBOARD is not set
  179. +# CONFIG_INPUT_MOUSE is not set
  180. +CONFIG_INPUT_MISC=y
  181. +CONFIG_INPUT_AD714X=m
  182. +CONFIG_INPUT_ATI_REMOTE=m
  183. +CONFIG_INPUT_ATI_REMOTE2=m
  184. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  185. +CONFIG_INPUT_POWERMATE=m
  186. +CONFIG_INPUT_YEALINK=m
  187. +CONFIG_INPUT_CM109=m
  188. +CONFIG_INPUT_UINPUT=m
  189. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  190. +CONFIG_INPUT_ADXL34X=m
  191. +CONFIG_INPUT_CMA3000=m
  192. +CONFIG_SERIO=m
  193. +CONFIG_SERIO_RAW=m
  194. +CONFIG_GAMEPORT=m
  195. +CONFIG_GAMEPORT_NS558=m
  196. +CONFIG_GAMEPORT_L4=m
  197. +CONFIG_VT_HW_CONSOLE_BINDING=y
  198. +# CONFIG_LEGACY_PTYS is not set
  199. +# CONFIG_DEVKMEM is not set
  200. +CONFIG_SERIAL_AMBA_PL011=y
  201. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  202. +# CONFIG_HW_RANDOM is not set
  203. +CONFIG_RAW_DRIVER=y
  204. +CONFIG_GPIO_SYSFS=y
  205. +# CONFIG_HWMON is not set
  206. +CONFIG_WATCHDOG=y
  207. +CONFIG_BCM2708_WDT=m
  208. +# CONFIG_MFD_SUPPORT is not set
  209. +CONFIG_FB=y
  210. +CONFIG_FB_BCM2708=y
  211. +CONFIG_FRAMEBUFFER_CONSOLE=y
  212. +CONFIG_LOGO=y
  213. +# CONFIG_LOGO_LINUX_MONO is not set
  214. +# CONFIG_LOGO_LINUX_VGA16 is not set
  215. +CONFIG_SOUND=y
  216. +CONFIG_SND=m
  217. +CONFIG_SND_SEQUENCER=m
  218. +CONFIG_SND_SEQ_DUMMY=m
  219. +CONFIG_SND_MIXER_OSS=m
  220. +CONFIG_SND_PCM_OSS=m
  221. +CONFIG_SND_SEQUENCER_OSS=y
  222. +CONFIG_SND_HRTIMER=m
  223. +CONFIG_SND_DUMMY=m
  224. +CONFIG_SND_ALOOP=m
  225. +CONFIG_SND_VIRMIDI=m
  226. +CONFIG_SND_MTPAV=m
  227. +CONFIG_SND_SERIAL_U16550=m
  228. +CONFIG_SND_MPU401=m
  229. +CONFIG_SND_BCM2835=m
  230. +CONFIG_SND_USB_AUDIO=m
  231. +CONFIG_SND_USB_UA101=m
  232. +CONFIG_SND_USB_CAIAQ=m
  233. +CONFIG_SND_USB_6FIRE=m
  234. +CONFIG_SOUND_PRIME=m
  235. +CONFIG_HID_PID=y
  236. +CONFIG_USB_HIDDEV=y
  237. +CONFIG_HID_A4TECH=m
  238. +CONFIG_HID_ACRUX=m
  239. +CONFIG_HID_APPLE=m
  240. +CONFIG_HID_BELKIN=m
  241. +CONFIG_HID_CHERRY=m
  242. +CONFIG_HID_CHICONY=m
  243. +CONFIG_HID_CYPRESS=m
  244. +CONFIG_HID_DRAGONRISE=m
  245. +CONFIG_HID_EMS_FF=m
  246. +CONFIG_HID_ELECOM=m
  247. +CONFIG_HID_EZKEY=m
  248. +CONFIG_HID_HOLTEK=m
  249. +CONFIG_HID_KEYTOUCH=m
  250. +CONFIG_HID_KYE=m
  251. +CONFIG_HID_UCLOGIC=m
  252. +CONFIG_HID_WALTOP=m
  253. +CONFIG_HID_GYRATION=m
  254. +CONFIG_HID_TWINHAN=m
  255. +CONFIG_HID_KENSINGTON=m
  256. +CONFIG_HID_LCPOWER=m
  257. +CONFIG_HID_LOGITECH=m
  258. +CONFIG_HID_MAGICMOUSE=m
  259. +CONFIG_HID_MICROSOFT=m
  260. +CONFIG_HID_MONTEREY=m
  261. +CONFIG_HID_MULTITOUCH=m
  262. +CONFIG_HID_NTRIG=m
  263. +CONFIG_HID_ORTEK=m
  264. +CONFIG_HID_PANTHERLORD=m
  265. +CONFIG_HID_PETALYNX=m
  266. +CONFIG_HID_PICOLCD=m
  267. +CONFIG_HID_QUANTA=m
  268. +CONFIG_HID_ROCCAT=m
  269. +CONFIG_HID_SAMSUNG=m
  270. +CONFIG_HID_SONY=m
  271. +CONFIG_HID_SPEEDLINK=m
  272. +CONFIG_HID_SUNPLUS=m
  273. +CONFIG_HID_GREENASIA=m
  274. +CONFIG_HID_SMARTJOYPLUS=m
  275. +CONFIG_HID_TOPSEED=m
  276. +CONFIG_HID_THRUSTMASTER=m
  277. +CONFIG_HID_WACOM=m
  278. +CONFIG_HID_WIIMOTE=m
  279. +CONFIG_HID_ZEROPLUS=m
  280. +CONFIG_HID_ZYDACRON=m
  281. +CONFIG_USB=y
  282. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  283. +CONFIG_USB_MON=m
  284. +CONFIG_USB_DWCOTG=y
  285. +CONFIG_USB_STORAGE=y
  286. +CONFIG_USB_STORAGE_REALTEK=m
  287. +CONFIG_USB_STORAGE_DATAFAB=m
  288. +CONFIG_USB_STORAGE_FREECOM=m
  289. +CONFIG_USB_STORAGE_ISD200=m
  290. +CONFIG_USB_STORAGE_USBAT=m
  291. +CONFIG_USB_STORAGE_SDDR09=m
  292. +CONFIG_USB_STORAGE_SDDR55=m
  293. +CONFIG_USB_STORAGE_JUMPSHOT=m
  294. +CONFIG_USB_STORAGE_ALAUDA=m
  295. +CONFIG_USB_STORAGE_ONETOUCH=m
  296. +CONFIG_USB_STORAGE_KARMA=m
  297. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  298. +CONFIG_USB_STORAGE_ENE_UB6250=m
  299. +CONFIG_USB_UAS=m
  300. +CONFIG_USB_LIBUSUAL=y
  301. +CONFIG_USB_MDC800=m
  302. +CONFIG_USB_MICROTEK=m
  303. +CONFIG_USB_SERIAL=m
  304. +CONFIG_USB_SERIAL_GENERIC=y
  305. +CONFIG_USB_SERIAL_AIRCABLE=m
  306. +CONFIG_USB_SERIAL_ARK3116=m
  307. +CONFIG_USB_SERIAL_BELKIN=m
  308. +CONFIG_USB_SERIAL_CH341=m
  309. +CONFIG_USB_SERIAL_WHITEHEAT=m
  310. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  311. +CONFIG_USB_SERIAL_CP210X=m
  312. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  313. +CONFIG_USB_SERIAL_EMPEG=m
  314. +CONFIG_USB_SERIAL_FTDI_SIO=m
  315. +CONFIG_USB_SERIAL_FUNSOFT=m
  316. +CONFIG_USB_SERIAL_VISOR=m
  317. +CONFIG_USB_SERIAL_IPAQ=m
  318. +CONFIG_USB_SERIAL_IR=m
  319. +CONFIG_USB_SERIAL_EDGEPORT=m
  320. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  321. +CONFIG_USB_SERIAL_GARMIN=m
  322. +CONFIG_USB_SERIAL_IPW=m
  323. +CONFIG_USB_SERIAL_IUU=m
  324. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  325. +CONFIG_USB_SERIAL_KEYSPAN=m
  326. +CONFIG_USB_SERIAL_KLSI=m
  327. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  328. +CONFIG_USB_SERIAL_MCT_U232=m
  329. +CONFIG_USB_SERIAL_MOS7720=m
  330. +CONFIG_USB_SERIAL_MOS7840=m
  331. +CONFIG_USB_SERIAL_MOTOROLA=m
  332. +CONFIG_USB_SERIAL_NAVMAN=m
  333. +CONFIG_USB_SERIAL_PL2303=m
  334. +CONFIG_USB_SERIAL_OTI6858=m
  335. +CONFIG_USB_SERIAL_QCAUX=m
  336. +CONFIG_USB_SERIAL_QUALCOMM=m
  337. +CONFIG_USB_SERIAL_SPCP8X5=m
  338. +CONFIG_USB_SERIAL_HP4X=m
  339. +CONFIG_USB_SERIAL_SAFE=m
  340. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  341. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  342. +CONFIG_USB_SERIAL_SYMBOL=m
  343. +CONFIG_USB_SERIAL_TI=m
  344. +CONFIG_USB_SERIAL_CYBERJACK=m
  345. +CONFIG_USB_SERIAL_XIRCOM=m
  346. +CONFIG_USB_SERIAL_OPTION=m
  347. +CONFIG_USB_SERIAL_OMNINET=m
  348. +CONFIG_USB_SERIAL_OPTICON=m
  349. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  350. +CONFIG_USB_SERIAL_ZIO=m
  351. +CONFIG_USB_SERIAL_SSU100=m
  352. +CONFIG_USB_SERIAL_DEBUG=m
  353. +CONFIG_USB_EMI62=m
  354. +CONFIG_USB_EMI26=m
  355. +CONFIG_USB_ADUTUX=m
  356. +CONFIG_USB_SEVSEG=m
  357. +CONFIG_USB_RIO500=m
  358. +CONFIG_USB_LEGOTOWER=m
  359. +CONFIG_USB_LCD=m
  360. +CONFIG_USB_LED=m
  361. +CONFIG_USB_CYPRESS_CY7C63=m
  362. +CONFIG_USB_CYTHERM=m
  363. +CONFIG_USB_IDMOUSE=m
  364. +CONFIG_USB_FTDI_ELAN=m
  365. +CONFIG_USB_APPLEDISPLAY=m
  366. +CONFIG_USB_LD=m
  367. +CONFIG_USB_TRANCEVIBRATOR=m
  368. +CONFIG_USB_IOWARRIOR=m
  369. +CONFIG_USB_TEST=m
  370. +CONFIG_USB_ISIGHTFW=m
  371. +CONFIG_USB_YUREX=m
  372. +CONFIG_MMC=y
  373. +CONFIG_MMC_SDHCI=y
  374. +CONFIG_MMC_SDHCI_PLTFM=y
  375. +CONFIG_MMC_SDHCI_BCM2708=y
  376. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  377. +CONFIG_LEDS_GPIO=y
  378. +CONFIG_LEDS_TRIGGER_TIMER=m
  379. +CONFIG_LEDS_TRIGGER_HEARTBEAT=m
  380. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=m
  381. +CONFIG_UIO=m
  382. +CONFIG_UIO_PDRV=m
  383. +CONFIG_UIO_PDRV_GENIRQ=m
  384. +# CONFIG_IOMMU_SUPPORT is not set
  385. +CONFIG_EXT4_FS=y
  386. +CONFIG_EXT4_FS_POSIX_ACL=y
  387. +CONFIG_EXT4_FS_SECURITY=y
  388. +CONFIG_REISERFS_FS=m
  389. +CONFIG_REISERFS_FS_XATTR=y
  390. +CONFIG_REISERFS_FS_POSIX_ACL=y
  391. +CONFIG_REISERFS_FS_SECURITY=y
  392. +CONFIG_JFS_FS=m
  393. +CONFIG_JFS_POSIX_ACL=y
  394. +CONFIG_JFS_SECURITY=y
  395. +CONFIG_XFS_FS=m
  396. +CONFIG_XFS_QUOTA=y
  397. +CONFIG_XFS_POSIX_ACL=y
  398. +CONFIG_XFS_RT=y
  399. +CONFIG_GFS2_FS=m
  400. +CONFIG_OCFS2_FS=m
  401. +CONFIG_BTRFS_FS=m
  402. +CONFIG_BTRFS_FS_POSIX_ACL=y
  403. +CONFIG_NILFS2_FS=m
  404. +CONFIG_AUTOFS4_FS=y
  405. +CONFIG_FUSE_FS=m
  406. +CONFIG_CUSE=m
  407. +CONFIG_FSCACHE=y
  408. +CONFIG_CACHEFILES=y
  409. +CONFIG_ISO9660_FS=m
  410. +CONFIG_JOLIET=y
  411. +CONFIG_ZISOFS=y
  412. +CONFIG_UDF_FS=m
  413. +CONFIG_MSDOS_FS=y
  414. +CONFIG_VFAT_FS=y
  415. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  416. +CONFIG_NTFS_FS=m
  417. +CONFIG_TMPFS=y
  418. +CONFIG_TMPFS_POSIX_ACL=y
  419. +CONFIG_CONFIGFS_FS=y
  420. +CONFIG_SQUASHFS=m
  421. +CONFIG_SQUASHFS_XATTR=y
  422. +CONFIG_SQUASHFS_LZO=y
  423. +CONFIG_SQUASHFS_XZ=y
  424. +CONFIG_NFS_FS=y
  425. +CONFIG_NFS_V3=y
  426. +CONFIG_NFS_V3_ACL=y
  427. +CONFIG_NFS_V4=y
  428. +CONFIG_ROOT_NFS=y
  429. +CONFIG_NFS_FSCACHE=y
  430. +CONFIG_CIFS=m
  431. +CONFIG_CIFS_WEAK_PW_HASH=y
  432. +CONFIG_CIFS_XATTR=y
  433. +CONFIG_CIFS_POSIX=y
  434. +CONFIG_9P_FS=m
  435. +CONFIG_PARTITION_ADVANCED=y
  436. +CONFIG_MAC_PARTITION=y
  437. +CONFIG_EFI_PARTITION=y
  438. +CONFIG_NLS_DEFAULT="utf8"
  439. +CONFIG_NLS_CODEPAGE_437=y
  440. +CONFIG_NLS_CODEPAGE_737=m
  441. +CONFIG_NLS_CODEPAGE_775=m
  442. +CONFIG_NLS_CODEPAGE_850=m
  443. +CONFIG_NLS_CODEPAGE_852=m
  444. +CONFIG_NLS_CODEPAGE_855=m
  445. +CONFIG_NLS_CODEPAGE_857=m
  446. +CONFIG_NLS_CODEPAGE_860=m
  447. +CONFIG_NLS_CODEPAGE_861=m
  448. +CONFIG_NLS_CODEPAGE_862=m
  449. +CONFIG_NLS_CODEPAGE_863=m
  450. +CONFIG_NLS_CODEPAGE_864=m
  451. +CONFIG_NLS_CODEPAGE_865=m
  452. +CONFIG_NLS_CODEPAGE_866=m
  453. +CONFIG_NLS_CODEPAGE_869=m
  454. +CONFIG_NLS_CODEPAGE_936=m
  455. +CONFIG_NLS_CODEPAGE_950=m
  456. +CONFIG_NLS_CODEPAGE_932=m
  457. +CONFIG_NLS_CODEPAGE_949=m
  458. +CONFIG_NLS_CODEPAGE_874=m
  459. +CONFIG_NLS_ISO8859_8=m
  460. +CONFIG_NLS_CODEPAGE_1250=m
  461. +CONFIG_NLS_CODEPAGE_1251=m
  462. +CONFIG_NLS_ASCII=y
  463. +CONFIG_NLS_ISO8859_1=m
  464. +CONFIG_NLS_ISO8859_2=m
  465. +CONFIG_NLS_ISO8859_3=m
  466. +CONFIG_NLS_ISO8859_4=m
  467. +CONFIG_NLS_ISO8859_5=m
  468. +CONFIG_NLS_ISO8859_6=m
  469. +CONFIG_NLS_ISO8859_7=m
  470. +CONFIG_NLS_ISO8859_9=m
  471. +CONFIG_NLS_ISO8859_13=m
  472. +CONFIG_NLS_ISO8859_14=m
  473. +CONFIG_NLS_ISO8859_15=m
  474. +CONFIG_NLS_KOI8_R=m
  475. +CONFIG_NLS_KOI8_U=m
  476. +CONFIG_NLS_UTF8=m
  477. +# CONFIG_SCHED_DEBUG is not set
  478. +# CONFIG_DEBUG_BUGVERBOSE is not set
  479. +# CONFIG_FTRACE is not set
  480. +# CONFIG_ARM_UNWIND is not set
  481. +CONFIG_CRYPTO_AUTHENC=m
  482. +CONFIG_CRYPTO_SEQIV=m
  483. +CONFIG_CRYPTO_CBC=y
  484. +CONFIG_CRYPTO_HMAC=y
  485. +CONFIG_CRYPTO_XCBC=m
  486. +CONFIG_CRYPTO_MD5=y
  487. +CONFIG_CRYPTO_SHA1=y
  488. +CONFIG_CRYPTO_SHA256=m
  489. +CONFIG_CRYPTO_SHA512=m
  490. +CONFIG_CRYPTO_TGR192=m
  491. +CONFIG_CRYPTO_WP512=m
  492. +CONFIG_CRYPTO_CAST5=m
  493. +CONFIG_CRYPTO_DES=y
  494. +CONFIG_CRYPTO_DEFLATE=m
  495. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  496. +# CONFIG_CRYPTO_HW is not set
  497. +CONFIG_CRC_ITU_T=y
  498. +CONFIG_LIBCRC32C=y
  499. +CONFIG_I2C=y
  500. +CONFIG_I2C_BOARDINFO=y
  501. +CONFIG_I2C_COMPAT=y
  502. +CONFIG_I2C_CHARDEV=m
  503. +CONFIG_I2C_HELPER_AUTO=y
  504. +CONFIG_I2C_BCM2708=m
  505. +CONFIG_SPI=y
  506. +CONFIG_SPI_MASTER=y
  507. +CONFIG_SPI_BCM2708=m
  508. +
  509. diff -Nur linux-3.10.37/arch/arm/configs/bcmrpi_defconfig linux-rpi/arch/arm/configs/bcmrpi_defconfig
  510. --- linux-3.10.37/arch/arm/configs/bcmrpi_defconfig 1970-01-01 01:00:00.000000000 +0100
  511. +++ linux-rpi/arch/arm/configs/bcmrpi_defconfig 2014-04-24 15:35:00.717527267 +0200
  512. @@ -0,0 +1,1101 @@
  513. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  514. +# CONFIG_LOCALVERSION_AUTO is not set
  515. +CONFIG_SYSVIPC=y
  516. +CONFIG_POSIX_MQUEUE=y
  517. +CONFIG_FHANDLE=y
  518. +CONFIG_AUDIT=y
  519. +CONFIG_NO_HZ=y
  520. +CONFIG_HIGH_RES_TIMERS=y
  521. +CONFIG_BSD_PROCESS_ACCT=y
  522. +CONFIG_BSD_PROCESS_ACCT_V3=y
  523. +CONFIG_TASKSTATS=y
  524. +CONFIG_TASK_DELAY_ACCT=y
  525. +CONFIG_TASK_XACCT=y
  526. +CONFIG_TASK_IO_ACCOUNTING=y
  527. +CONFIG_IKCONFIG=y
  528. +CONFIG_IKCONFIG_PROC=y
  529. +CONFIG_CGROUP_FREEZER=y
  530. +CONFIG_CGROUP_DEVICE=y
  531. +CONFIG_CGROUP_CPUACCT=y
  532. +CONFIG_RESOURCE_COUNTERS=y
  533. +CONFIG_MEMCG=y
  534. +CONFIG_BLK_CGROUP=y
  535. +CONFIG_NAMESPACES=y
  536. +CONFIG_SCHED_AUTOGROUP=y
  537. +CONFIG_RELAY=y
  538. +CONFIG_BLK_DEV_INITRD=y
  539. +CONFIG_EMBEDDED=y
  540. +# CONFIG_COMPAT_BRK is not set
  541. +CONFIG_PROFILING=y
  542. +CONFIG_OPROFILE=m
  543. +CONFIG_KPROBES=y
  544. +CONFIG_JUMP_LABEL=y
  545. +CONFIG_MODULES=y
  546. +CONFIG_MODULE_UNLOAD=y
  547. +CONFIG_MODVERSIONS=y
  548. +CONFIG_MODULE_SRCVERSION_ALL=y
  549. +CONFIG_BLK_DEV_THROTTLING=y
  550. +CONFIG_PARTITION_ADVANCED=y
  551. +CONFIG_MAC_PARTITION=y
  552. +CONFIG_CFQ_GROUP_IOSCHED=y
  553. +CONFIG_ARCH_BCM2708=y
  554. +CONFIG_PREEMPT=y
  555. +CONFIG_AEABI=y
  556. +CONFIG_CLEANCACHE=y
  557. +CONFIG_FRONTSWAP=y
  558. +CONFIG_UACCESS_WITH_MEMCPY=y
  559. +CONFIG_SECCOMP=y
  560. +CONFIG_CC_STACKPROTECTOR=y
  561. +CONFIG_ZBOOT_ROM_TEXT=0x0
  562. +CONFIG_ZBOOT_ROM_BSS=0x0
  563. +CONFIG_CMDLINE="console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  564. +CONFIG_KEXEC=y
  565. +CONFIG_CPU_FREQ=y
  566. +CONFIG_CPU_FREQ_STAT=m
  567. +CONFIG_CPU_FREQ_STAT_DETAILS=y
  568. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  569. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  570. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  571. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  572. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  573. +CONFIG_CPU_IDLE=y
  574. +CONFIG_VFP=y
  575. +CONFIG_BINFMT_MISC=m
  576. +CONFIG_NET=y
  577. +CONFIG_PACKET=y
  578. +CONFIG_UNIX=y
  579. +CONFIG_XFRM_USER=y
  580. +CONFIG_NET_KEY=m
  581. +CONFIG_INET=y
  582. +CONFIG_IP_MULTICAST=y
  583. +CONFIG_IP_ADVANCED_ROUTER=y
  584. +CONFIG_IP_MULTIPLE_TABLES=y
  585. +CONFIG_IP_ROUTE_MULTIPATH=y
  586. +CONFIG_IP_ROUTE_VERBOSE=y
  587. +CONFIG_IP_PNP=y
  588. +CONFIG_IP_PNP_DHCP=y
  589. +CONFIG_IP_PNP_RARP=y
  590. +CONFIG_NET_IPIP=m
  591. +CONFIG_NET_IPGRE_DEMUX=m
  592. +CONFIG_NET_IPGRE=m
  593. +CONFIG_IP_MROUTE=y
  594. +CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
  595. +CONFIG_IP_PIMSM_V1=y
  596. +CONFIG_IP_PIMSM_V2=y
  597. +CONFIG_SYN_COOKIES=y
  598. +CONFIG_INET_AH=m
  599. +CONFIG_INET_ESP=m
  600. +CONFIG_INET_IPCOMP=m
  601. +CONFIG_INET_XFRM_MODE_TRANSPORT=m
  602. +CONFIG_INET_XFRM_MODE_TUNNEL=m
  603. +CONFIG_INET_XFRM_MODE_BEET=m
  604. +CONFIG_INET_LRO=m
  605. +CONFIG_INET_DIAG=m
  606. +CONFIG_IPV6_PRIVACY=y
  607. +CONFIG_INET6_AH=m
  608. +CONFIG_INET6_ESP=m
  609. +CONFIG_INET6_IPCOMP=m
  610. +CONFIG_IPV6_TUNNEL=m
  611. +CONFIG_IPV6_MULTIPLE_TABLES=y
  612. +CONFIG_IPV6_MROUTE=y
  613. +CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
  614. +CONFIG_IPV6_PIMSM_V2=y
  615. +CONFIG_NETFILTER=y
  616. +CONFIG_NF_CONNTRACK=m
  617. +CONFIG_NF_CONNTRACK_ZONES=y
  618. +CONFIG_NF_CONNTRACK_EVENTS=y
  619. +CONFIG_NF_CONNTRACK_TIMESTAMP=y
  620. +CONFIG_NF_CT_PROTO_DCCP=m
  621. +CONFIG_NF_CT_PROTO_UDPLITE=m
  622. +CONFIG_NF_CONNTRACK_AMANDA=m
  623. +CONFIG_NF_CONNTRACK_FTP=m
  624. +CONFIG_NF_CONNTRACK_H323=m
  625. +CONFIG_NF_CONNTRACK_IRC=m
  626. +CONFIG_NF_CONNTRACK_NETBIOS_NS=m
  627. +CONFIG_NF_CONNTRACK_SNMP=m
  628. +CONFIG_NF_CONNTRACK_PPTP=m
  629. +CONFIG_NF_CONNTRACK_SANE=m
  630. +CONFIG_NF_CONNTRACK_SIP=m
  631. +CONFIG_NF_CONNTRACK_TFTP=m
  632. +CONFIG_NF_CT_NETLINK=m
  633. +CONFIG_NETFILTER_TPROXY=m
  634. +CONFIG_NETFILTER_XT_SET=m
  635. +CONFIG_NETFILTER_XT_TARGET_AUDIT=m
  636. +CONFIG_NETFILTER_XT_TARGET_CHECKSUM=m
  637. +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
  638. +CONFIG_NETFILTER_XT_TARGET_CONNMARK=m
  639. +CONFIG_NETFILTER_XT_TARGET_DSCP=m
  640. +CONFIG_NETFILTER_XT_TARGET_HMARK=m
  641. +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=m
  642. +CONFIG_NETFILTER_XT_TARGET_LED=m
  643. +CONFIG_NETFILTER_XT_TARGET_LOG=m
  644. +CONFIG_NETFILTER_XT_TARGET_MARK=m
  645. +CONFIG_NETFILTER_XT_TARGET_NFLOG=m
  646. +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
  647. +CONFIG_NETFILTER_XT_TARGET_NOTRACK=m
  648. +CONFIG_NETFILTER_XT_TARGET_TEE=m
  649. +CONFIG_NETFILTER_XT_TARGET_TPROXY=m
  650. +CONFIG_NETFILTER_XT_TARGET_TRACE=m
  651. +CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
  652. +CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=m
  653. +CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=m
  654. +CONFIG_NETFILTER_XT_MATCH_BPF=m
  655. +CONFIG_NETFILTER_XT_MATCH_CLUSTER=m
  656. +CONFIG_NETFILTER_XT_MATCH_COMMENT=m
  657. +CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
  658. +CONFIG_NETFILTER_XT_MATCH_CONNLABEL=m
  659. +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=m
  660. +CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
  661. +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
  662. +CONFIG_NETFILTER_XT_MATCH_CPU=m
  663. +CONFIG_NETFILTER_XT_MATCH_DCCP=m
  664. +CONFIG_NETFILTER_XT_MATCH_DEVGROUP=m
  665. +CONFIG_NETFILTER_XT_MATCH_DSCP=m
  666. +CONFIG_NETFILTER_XT_MATCH_ESP=m
  667. +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
  668. +CONFIG_NETFILTER_XT_MATCH_HELPER=m
  669. +CONFIG_NETFILTER_XT_MATCH_IPRANGE=m
  670. +CONFIG_NETFILTER_XT_MATCH_IPVS=m
  671. +CONFIG_NETFILTER_XT_MATCH_LENGTH=m
  672. +CONFIG_NETFILTER_XT_MATCH_LIMIT=m
  673. +CONFIG_NETFILTER_XT_MATCH_MAC=m
  674. +CONFIG_NETFILTER_XT_MATCH_MARK=m
  675. +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
  676. +CONFIG_NETFILTER_XT_MATCH_NFACCT=m
  677. +CONFIG_NETFILTER_XT_MATCH_OSF=m
  678. +CONFIG_NETFILTER_XT_MATCH_OWNER=m
  679. +CONFIG_NETFILTER_XT_MATCH_POLICY=m
  680. +CONFIG_NETFILTER_XT_MATCH_PHYSDEV=m
  681. +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
  682. +CONFIG_NETFILTER_XT_MATCH_QUOTA=m
  683. +CONFIG_NETFILTER_XT_MATCH_RATEEST=m
  684. +CONFIG_NETFILTER_XT_MATCH_REALM=m
  685. +CONFIG_NETFILTER_XT_MATCH_RECENT=m
  686. +CONFIG_NETFILTER_XT_MATCH_SOCKET=m
  687. +CONFIG_NETFILTER_XT_MATCH_STATE=m
  688. +CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
  689. +CONFIG_NETFILTER_XT_MATCH_STRING=m
  690. +CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
  691. +CONFIG_NETFILTER_XT_MATCH_TIME=m
  692. +CONFIG_NETFILTER_XT_MATCH_U32=m
  693. +CONFIG_IP_SET=m
  694. +CONFIG_IP_SET_BITMAP_IP=m
  695. +CONFIG_IP_SET_BITMAP_IPMAC=m
  696. +CONFIG_IP_SET_BITMAP_PORT=m
  697. +CONFIG_IP_SET_HASH_IP=m
  698. +CONFIG_IP_SET_HASH_IPPORT=m
  699. +CONFIG_IP_SET_HASH_IPPORTIP=m
  700. +CONFIG_IP_SET_HASH_IPPORTNET=m
  701. +CONFIG_IP_SET_HASH_NET=m
  702. +CONFIG_IP_SET_HASH_NETPORT=m
  703. +CONFIG_IP_SET_HASH_NETIFACE=m
  704. +CONFIG_IP_SET_LIST_SET=m
  705. +CONFIG_IP_VS=m
  706. +CONFIG_IP_VS_PROTO_TCP=y
  707. +CONFIG_IP_VS_PROTO_UDP=y
  708. +CONFIG_IP_VS_PROTO_ESP=y
  709. +CONFIG_IP_VS_PROTO_AH=y
  710. +CONFIG_IP_VS_PROTO_SCTP=y
  711. +CONFIG_IP_VS_RR=m
  712. +CONFIG_IP_VS_WRR=m
  713. +CONFIG_IP_VS_LC=m
  714. +CONFIG_IP_VS_WLC=m
  715. +CONFIG_IP_VS_LBLC=m
  716. +CONFIG_IP_VS_LBLCR=m
  717. +CONFIG_IP_VS_DH=m
  718. +CONFIG_IP_VS_SH=m
  719. +CONFIG_IP_VS_SED=m
  720. +CONFIG_IP_VS_NQ=m
  721. +CONFIG_IP_VS_FTP=m
  722. +CONFIG_IP_VS_PE_SIP=m
  723. +CONFIG_NF_CONNTRACK_IPV4=m
  724. +CONFIG_IP_NF_IPTABLES=m
  725. +CONFIG_IP_NF_MATCH_AH=m
  726. +CONFIG_IP_NF_MATCH_ECN=m
  727. +CONFIG_IP_NF_MATCH_TTL=m
  728. +CONFIG_IP_NF_FILTER=m
  729. +CONFIG_IP_NF_TARGET_REJECT=m
  730. +CONFIG_IP_NF_TARGET_ULOG=m
  731. +CONFIG_NF_NAT_IPV4=m
  732. +CONFIG_IP_NF_TARGET_MASQUERADE=m
  733. +CONFIG_IP_NF_TARGET_NETMAP=m
  734. +CONFIG_IP_NF_TARGET_REDIRECT=m
  735. +CONFIG_IP_NF_MANGLE=m
  736. +CONFIG_IP_NF_TARGET_ECN=m
  737. +CONFIG_IP_NF_TARGET_TTL=m
  738. +CONFIG_IP_NF_RAW=m
  739. +CONFIG_IP_NF_ARPTABLES=m
  740. +CONFIG_IP_NF_ARPFILTER=m
  741. +CONFIG_IP_NF_ARP_MANGLE=m
  742. +CONFIG_NF_CONNTRACK_IPV6=m
  743. +CONFIG_IP6_NF_IPTABLES=m
  744. +CONFIG_IP6_NF_MATCH_AH=m
  745. +CONFIG_IP6_NF_MATCH_EUI64=m
  746. +CONFIG_IP6_NF_MATCH_FRAG=m
  747. +CONFIG_IP6_NF_MATCH_OPTS=m
  748. +CONFIG_IP6_NF_MATCH_HL=m
  749. +CONFIG_IP6_NF_MATCH_IPV6HEADER=m
  750. +CONFIG_IP6_NF_MATCH_MH=m
  751. +CONFIG_IP6_NF_MATCH_RT=m
  752. +CONFIG_IP6_NF_TARGET_HL=m
  753. +CONFIG_IP6_NF_FILTER=m
  754. +CONFIG_IP6_NF_TARGET_REJECT=m
  755. +CONFIG_IP6_NF_MANGLE=m
  756. +CONFIG_IP6_NF_RAW=m
  757. +CONFIG_NF_NAT_IPV6=m
  758. +CONFIG_IP6_NF_TARGET_MASQUERADE=m
  759. +CONFIG_IP6_NF_TARGET_NPT=m
  760. +CONFIG_BRIDGE_NF_EBTABLES=m
  761. +CONFIG_BRIDGE_EBT_BROUTE=m
  762. +CONFIG_BRIDGE_EBT_T_FILTER=m
  763. +CONFIG_BRIDGE_EBT_T_NAT=m
  764. +CONFIG_BRIDGE_EBT_802_3=m
  765. +CONFIG_BRIDGE_EBT_AMONG=m
  766. +CONFIG_BRIDGE_EBT_ARP=m
  767. +CONFIG_BRIDGE_EBT_IP=m
  768. +CONFIG_BRIDGE_EBT_IP6=m
  769. +CONFIG_BRIDGE_EBT_LIMIT=m
  770. +CONFIG_BRIDGE_EBT_MARK=m
  771. +CONFIG_BRIDGE_EBT_PKTTYPE=m
  772. +CONFIG_BRIDGE_EBT_STP=m
  773. +CONFIG_BRIDGE_EBT_VLAN=m
  774. +CONFIG_BRIDGE_EBT_ARPREPLY=m
  775. +CONFIG_BRIDGE_EBT_DNAT=m
  776. +CONFIG_BRIDGE_EBT_MARK_T=m
  777. +CONFIG_BRIDGE_EBT_REDIRECT=m
  778. +CONFIG_BRIDGE_EBT_SNAT=m
  779. +CONFIG_BRIDGE_EBT_LOG=m
  780. +CONFIG_BRIDGE_EBT_ULOG=m
  781. +CONFIG_BRIDGE_EBT_NFLOG=m
  782. +CONFIG_SCTP_COOKIE_HMAC_SHA1=y
  783. +CONFIG_L2TP=m
  784. +CONFIG_L2TP_V3=y
  785. +CONFIG_L2TP_IP=m
  786. +CONFIG_L2TP_ETH=m
  787. +CONFIG_BRIDGE=m
  788. +CONFIG_VLAN_8021Q=m
  789. +CONFIG_VLAN_8021Q_GVRP=y
  790. +CONFIG_ATALK=m
  791. +CONFIG_NET_SCHED=y
  792. +CONFIG_NET_SCH_CBQ=m
  793. +CONFIG_NET_SCH_HTB=m
  794. +CONFIG_NET_SCH_HFSC=m
  795. +CONFIG_NET_SCH_PRIO=m
  796. +CONFIG_NET_SCH_MULTIQ=m
  797. +CONFIG_NET_SCH_RED=m
  798. +CONFIG_NET_SCH_SFB=m
  799. +CONFIG_NET_SCH_SFQ=m
  800. +CONFIG_NET_SCH_TEQL=m
  801. +CONFIG_NET_SCH_TBF=m
  802. +CONFIG_NET_SCH_GRED=m
  803. +CONFIG_NET_SCH_DSMARK=m
  804. +CONFIG_NET_SCH_NETEM=m
  805. +CONFIG_NET_SCH_DRR=m
  806. +CONFIG_NET_SCH_MQPRIO=m
  807. +CONFIG_NET_SCH_CHOKE=m
  808. +CONFIG_NET_SCH_QFQ=m
  809. +CONFIG_NET_SCH_CODEL=m
  810. +CONFIG_NET_SCH_FQ_CODEL=m
  811. +CONFIG_NET_SCH_INGRESS=m
  812. +CONFIG_NET_SCH_PLUG=m
  813. +CONFIG_NET_CLS_BASIC=m
  814. +CONFIG_NET_CLS_TCINDEX=m
  815. +CONFIG_NET_CLS_ROUTE4=m
  816. +CONFIG_NET_CLS_FW=m
  817. +CONFIG_NET_CLS_U32=m
  818. +CONFIG_CLS_U32_MARK=y
  819. +CONFIG_NET_CLS_RSVP=m
  820. +CONFIG_NET_CLS_RSVP6=m
  821. +CONFIG_NET_CLS_FLOW=m
  822. +CONFIG_NET_CLS_CGROUP=m
  823. +CONFIG_NET_EMATCH=y
  824. +CONFIG_NET_EMATCH_CMP=m
  825. +CONFIG_NET_EMATCH_NBYTE=m
  826. +CONFIG_NET_EMATCH_U32=m
  827. +CONFIG_NET_EMATCH_META=m
  828. +CONFIG_NET_EMATCH_TEXT=m
  829. +CONFIG_NET_EMATCH_IPSET=m
  830. +CONFIG_NET_CLS_ACT=y
  831. +CONFIG_NET_ACT_POLICE=m
  832. +CONFIG_NET_ACT_GACT=m
  833. +CONFIG_GACT_PROB=y
  834. +CONFIG_NET_ACT_MIRRED=m
  835. +CONFIG_NET_ACT_IPT=m
  836. +CONFIG_NET_ACT_NAT=m
  837. +CONFIG_NET_ACT_PEDIT=m
  838. +CONFIG_NET_ACT_SIMP=m
  839. +CONFIG_NET_ACT_SKBEDIT=m
  840. +CONFIG_NET_ACT_CSUM=m
  841. +CONFIG_BATMAN_ADV=m
  842. +CONFIG_OPENVSWITCH=m
  843. +CONFIG_NET_PKTGEN=m
  844. +CONFIG_HAMRADIO=y
  845. +CONFIG_AX25=m
  846. +CONFIG_NETROM=m
  847. +CONFIG_ROSE=m
  848. +CONFIG_MKISS=m
  849. +CONFIG_6PACK=m
  850. +CONFIG_BPQETHER=m
  851. +CONFIG_BAYCOM_SER_FDX=m
  852. +CONFIG_BAYCOM_SER_HDX=m
  853. +CONFIG_YAM=m
  854. +CONFIG_IRDA=m
  855. +CONFIG_IRLAN=m
  856. +CONFIG_IRNET=m
  857. +CONFIG_IRCOMM=m
  858. +CONFIG_IRDA_ULTRA=y
  859. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  860. +CONFIG_IRDA_FAST_RR=y
  861. +CONFIG_IRTTY_SIR=m
  862. +CONFIG_KINGSUN_DONGLE=m
  863. +CONFIG_KSDAZZLE_DONGLE=m
  864. +CONFIG_KS959_DONGLE=m
  865. +CONFIG_USB_IRDA=m
  866. +CONFIG_SIGMATEL_FIR=m
  867. +CONFIG_MCS_FIR=m
  868. +CONFIG_BT=m
  869. +CONFIG_BT_RFCOMM=m
  870. +CONFIG_BT_RFCOMM_TTY=y
  871. +CONFIG_BT_BNEP=m
  872. +CONFIG_BT_BNEP_MC_FILTER=y
  873. +CONFIG_BT_BNEP_PROTO_FILTER=y
  874. +CONFIG_BT_HIDP=m
  875. +CONFIG_BT_HCIBTUSB=m
  876. +CONFIG_BT_HCIBCM203X=m
  877. +CONFIG_BT_HCIBPA10X=m
  878. +CONFIG_BT_HCIBFUSB=m
  879. +CONFIG_BT_HCIVHCI=m
  880. +CONFIG_BT_MRVL=m
  881. +CONFIG_BT_MRVL_SDIO=m
  882. +CONFIG_BT_ATH3K=m
  883. +CONFIG_BT_WILINK=m
  884. +CONFIG_CFG80211=m
  885. +CONFIG_CFG80211_WEXT=y
  886. +CONFIG_MAC80211=m
  887. +CONFIG_MAC80211_RC_PID=y
  888. +CONFIG_MAC80211_MESH=y
  889. +CONFIG_WIMAX=m
  890. +CONFIG_RFKILL=m
  891. +CONFIG_RFKILL_INPUT=y
  892. +CONFIG_NET_9P=m
  893. +CONFIG_NFC=m
  894. +CONFIG_NFC_PN533=m
  895. +CONFIG_DEVTMPFS=y
  896. +CONFIG_DEVTMPFS_MOUNT=y
  897. +CONFIG_CMA=y
  898. +CONFIG_BLK_DEV_LOOP=y
  899. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  900. +CONFIG_BLK_DEV_DRBD=m
  901. +CONFIG_BLK_DEV_NBD=m
  902. +CONFIG_BLK_DEV_RAM=y
  903. +CONFIG_CDROM_PKTCDVD=m
  904. +CONFIG_SCSI=y
  905. +# CONFIG_SCSI_PROC_FS is not set
  906. +CONFIG_BLK_DEV_SD=y
  907. +CONFIG_CHR_DEV_ST=m
  908. +CONFIG_CHR_DEV_OSST=m
  909. +CONFIG_BLK_DEV_SR=m
  910. +CONFIG_CHR_DEV_SG=m
  911. +CONFIG_SCSI_MULTI_LUN=y
  912. +CONFIG_SCSI_ISCSI_ATTRS=y
  913. +CONFIG_ISCSI_TCP=m
  914. +CONFIG_ISCSI_BOOT_SYSFS=m
  915. +CONFIG_MD=y
  916. +CONFIG_MD_LINEAR=m
  917. +CONFIG_MD_RAID0=m
  918. +CONFIG_BCACHE=m
  919. +CONFIG_BLK_DEV_DM=m
  920. +CONFIG_DM_CRYPT=m
  921. +CONFIG_DM_SNAPSHOT=m
  922. +CONFIG_DM_MIRROR=m
  923. +CONFIG_DM_RAID=m
  924. +CONFIG_DM_LOG_USERSPACE=m
  925. +CONFIG_DM_ZERO=m
  926. +CONFIG_DM_DELAY=m
  927. +CONFIG_NETDEVICES=y
  928. +CONFIG_BONDING=m
  929. +CONFIG_DUMMY=m
  930. +CONFIG_IFB=m
  931. +CONFIG_MACVLAN=m
  932. +CONFIG_NETCONSOLE=m
  933. +CONFIG_TUN=m
  934. +CONFIG_VETH=m
  935. +CONFIG_MDIO_BITBANG=m
  936. +CONFIG_PPP=m
  937. +CONFIG_PPP_BSDCOMP=m
  938. +CONFIG_PPP_DEFLATE=m
  939. +CONFIG_PPP_FILTER=y
  940. +CONFIG_PPP_MPPE=m
  941. +CONFIG_PPP_MULTILINK=y
  942. +CONFIG_PPPOE=m
  943. +CONFIG_PPPOL2TP=m
  944. +CONFIG_PPP_ASYNC=m
  945. +CONFIG_PPP_SYNC_TTY=m
  946. +CONFIG_SLIP=m
  947. +CONFIG_SLIP_COMPRESSED=y
  948. +CONFIG_SLIP_SMART=y
  949. +CONFIG_USB_CATC=m
  950. +CONFIG_USB_KAWETH=m
  951. +CONFIG_USB_PEGASUS=m
  952. +CONFIG_USB_RTL8150=m
  953. +CONFIG_USB_RTL8152=m
  954. +CONFIG_USB_USBNET=y
  955. +CONFIG_USB_NET_AX8817X=m
  956. +CONFIG_USB_NET_AX88179_178A=m
  957. +CONFIG_USB_NET_CDCETHER=m
  958. +CONFIG_USB_NET_CDC_EEM=m
  959. +CONFIG_USB_NET_CDC_NCM=m
  960. +CONFIG_USB_NET_CDC_MBIM=m
  961. +CONFIG_USB_NET_DM9601=m
  962. +CONFIG_USB_NET_SMSC75XX=m
  963. +CONFIG_USB_NET_SMSC95XX=y
  964. +CONFIG_USB_NET_GL620A=m
  965. +CONFIG_USB_NET_NET1080=m
  966. +CONFIG_USB_NET_PLUSB=m
  967. +CONFIG_USB_NET_MCS7830=m
  968. +CONFIG_USB_NET_CDC_SUBSET=m
  969. +CONFIG_USB_ALI_M5632=y
  970. +CONFIG_USB_AN2720=y
  971. +CONFIG_USB_EPSON2888=y
  972. +CONFIG_USB_KC2190=y
  973. +CONFIG_USB_NET_ZAURUS=m
  974. +CONFIG_USB_NET_CX82310_ETH=m
  975. +CONFIG_USB_NET_KALMIA=m
  976. +CONFIG_USB_NET_QMI_WWAN=m
  977. +CONFIG_USB_NET_INT51X1=m
  978. +CONFIG_USB_IPHETH=m
  979. +CONFIG_USB_SIERRA_NET=m
  980. +CONFIG_USB_VL600=m
  981. +CONFIG_LIBERTAS_THINFIRM=m
  982. +CONFIG_LIBERTAS_THINFIRM_USB=m
  983. +CONFIG_AT76C50X_USB=m
  984. +CONFIG_USB_ZD1201=m
  985. +CONFIG_USB_NET_RNDIS_WLAN=m
  986. +CONFIG_RTL8187=m
  987. +CONFIG_MAC80211_HWSIM=m
  988. +CONFIG_ATH_CARDS=m
  989. +CONFIG_ATH9K=m
  990. +CONFIG_ATH9K_HTC=m
  991. +CONFIG_CARL9170=m
  992. +CONFIG_ATH6KL=m
  993. +CONFIG_ATH6KL_USB=m
  994. +CONFIG_AR5523=m
  995. +CONFIG_B43=m
  996. +CONFIG_B43LEGACY=m
  997. +CONFIG_BRCMFMAC=m
  998. +# CONFIG_BRCMFMAC_SDIO is not set
  999. +CONFIG_BRCMFMAC_USB=y
  1000. +CONFIG_HOSTAP=m
  1001. +CONFIG_LIBERTAS=m
  1002. +CONFIG_LIBERTAS_USB=m
  1003. +CONFIG_LIBERTAS_SDIO=m
  1004. +CONFIG_P54_COMMON=m
  1005. +CONFIG_P54_USB=m
  1006. +CONFIG_RT2X00=m
  1007. +CONFIG_RT2500USB=m
  1008. +CONFIG_RT73USB=m
  1009. +CONFIG_RT2800USB=m
  1010. +CONFIG_RT2800USB_RT53XX=y
  1011. +CONFIG_RT2800USB_RT55XX=y
  1012. +CONFIG_RT2800USB_UNKNOWN=y
  1013. +CONFIG_ZD1211RW=m
  1014. +CONFIG_MWIFIEX=m
  1015. +CONFIG_MWIFIEX_SDIO=m
  1016. +CONFIG_RTL8192CU=m
  1017. +CONFIG_WIMAX_I2400M_USB=m
  1018. +CONFIG_INPUT_POLLDEV=m
  1019. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1020. +CONFIG_INPUT_JOYDEV=m
  1021. +CONFIG_INPUT_EVDEV=m
  1022. +# CONFIG_INPUT_KEYBOARD is not set
  1023. +# CONFIG_INPUT_MOUSE is not set
  1024. +CONFIG_INPUT_JOYSTICK=y
  1025. +CONFIG_JOYSTICK_IFORCE=m
  1026. +CONFIG_JOYSTICK_IFORCE_USB=y
  1027. +CONFIG_JOYSTICK_XPAD=m
  1028. +CONFIG_JOYSTICK_XPAD_FF=y
  1029. +CONFIG_INPUT_MISC=y
  1030. +CONFIG_INPUT_AD714X=m
  1031. +CONFIG_INPUT_ATI_REMOTE2=m
  1032. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1033. +CONFIG_INPUT_POWERMATE=m
  1034. +CONFIG_INPUT_YEALINK=m
  1035. +CONFIG_INPUT_CM109=m
  1036. +CONFIG_INPUT_UINPUT=m
  1037. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1038. +CONFIG_INPUT_ADXL34X=m
  1039. +CONFIG_INPUT_CMA3000=m
  1040. +CONFIG_SERIO=m
  1041. +CONFIG_SERIO_RAW=m
  1042. +CONFIG_GAMEPORT=m
  1043. +CONFIG_GAMEPORT_NS558=m
  1044. +CONFIG_GAMEPORT_L4=m
  1045. +CONFIG_VT_HW_CONSOLE_BINDING=y
  1046. +# CONFIG_LEGACY_PTYS is not set
  1047. +# CONFIG_DEVKMEM is not set
  1048. +CONFIG_SERIAL_AMBA_PL011=y
  1049. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1050. +CONFIG_TTY_PRINTK=y
  1051. +CONFIG_HW_RANDOM=y
  1052. +CONFIG_HW_RANDOM_BCM2708=m
  1053. +CONFIG_RAW_DRIVER=y
  1054. +CONFIG_BRCM_CHAR_DRIVERS=y
  1055. +CONFIG_BCM_VC_CMA=y
  1056. +CONFIG_I2C=y
  1057. +CONFIG_I2C_CHARDEV=m
  1058. +CONFIG_I2C_BCM2708=m
  1059. +CONFIG_SPI=y
  1060. +CONFIG_SPI_BCM2708=m
  1061. +CONFIG_SPI_SPIDEV=y
  1062. +CONFIG_GPIO_SYSFS=y
  1063. +CONFIG_W1=m
  1064. +CONFIG_W1_MASTER_DS2490=m
  1065. +CONFIG_W1_MASTER_DS2482=m
  1066. +CONFIG_W1_MASTER_DS1WM=m
  1067. +CONFIG_W1_MASTER_GPIO=m
  1068. +CONFIG_W1_SLAVE_THERM=m
  1069. +CONFIG_W1_SLAVE_SMEM=m
  1070. +CONFIG_W1_SLAVE_DS2408=m
  1071. +CONFIG_W1_SLAVE_DS2413=m
  1072. +CONFIG_W1_SLAVE_DS2423=m
  1073. +CONFIG_W1_SLAVE_DS2431=m
  1074. +CONFIG_W1_SLAVE_DS2433=m
  1075. +CONFIG_W1_SLAVE_DS2760=m
  1076. +CONFIG_W1_SLAVE_DS2780=m
  1077. +CONFIG_W1_SLAVE_DS2781=m
  1078. +CONFIG_W1_SLAVE_DS28E04=m
  1079. +CONFIG_W1_SLAVE_BQ27000=m
  1080. +CONFIG_BATTERY_DS2760=m
  1081. +# CONFIG_HWMON is not set
  1082. +CONFIG_THERMAL=y
  1083. +CONFIG_THERMAL_BCM2835=y
  1084. +CONFIG_WATCHDOG=y
  1085. +CONFIG_BCM2708_WDT=m
  1086. +CONFIG_MEDIA_SUPPORT=m
  1087. +CONFIG_MEDIA_CAMERA_SUPPORT=y
  1088. +CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
  1089. +CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
  1090. +CONFIG_MEDIA_RADIO_SUPPORT=y
  1091. +CONFIG_MEDIA_RC_SUPPORT=y
  1092. +CONFIG_MEDIA_CONTROLLER=y
  1093. +CONFIG_LIRC=m
  1094. +CONFIG_RC_DEVICES=y
  1095. +CONFIG_RC_ATI_REMOTE=m
  1096. +CONFIG_IR_IMON=m
  1097. +CONFIG_IR_MCEUSB=m
  1098. +CONFIG_IR_REDRAT3=m
  1099. +CONFIG_IR_STREAMZAP=m
  1100. +CONFIG_IR_IGUANA=m
  1101. +CONFIG_IR_TTUSBIR=m
  1102. +CONFIG_RC_LOOPBACK=m
  1103. +CONFIG_IR_GPIO_CIR=m
  1104. +CONFIG_MEDIA_USB_SUPPORT=y
  1105. +CONFIG_USB_VIDEO_CLASS=m
  1106. +CONFIG_USB_M5602=m
  1107. +CONFIG_USB_STV06XX=m
  1108. +CONFIG_USB_GL860=m
  1109. +CONFIG_USB_GSPCA_BENQ=m
  1110. +CONFIG_USB_GSPCA_CONEX=m
  1111. +CONFIG_USB_GSPCA_CPIA1=m
  1112. +CONFIG_USB_GSPCA_ETOMS=m
  1113. +CONFIG_USB_GSPCA_FINEPIX=m
  1114. +CONFIG_USB_GSPCA_JEILINJ=m
  1115. +CONFIG_USB_GSPCA_JL2005BCD=m
  1116. +CONFIG_USB_GSPCA_KINECT=m
  1117. +CONFIG_USB_GSPCA_KONICA=m
  1118. +CONFIG_USB_GSPCA_MARS=m
  1119. +CONFIG_USB_GSPCA_MR97310A=m
  1120. +CONFIG_USB_GSPCA_NW80X=m
  1121. +CONFIG_USB_GSPCA_OV519=m
  1122. +CONFIG_USB_GSPCA_OV534=m
  1123. +CONFIG_USB_GSPCA_OV534_9=m
  1124. +CONFIG_USB_GSPCA_PAC207=m
  1125. +CONFIG_USB_GSPCA_PAC7302=m
  1126. +CONFIG_USB_GSPCA_PAC7311=m
  1127. +CONFIG_USB_GSPCA_SE401=m
  1128. +CONFIG_USB_GSPCA_SN9C2028=m
  1129. +CONFIG_USB_GSPCA_SN9C20X=m
  1130. +CONFIG_USB_GSPCA_SONIXB=m
  1131. +CONFIG_USB_GSPCA_SONIXJ=m
  1132. +CONFIG_USB_GSPCA_SPCA500=m
  1133. +CONFIG_USB_GSPCA_SPCA501=m
  1134. +CONFIG_USB_GSPCA_SPCA505=m
  1135. +CONFIG_USB_GSPCA_SPCA506=m
  1136. +CONFIG_USB_GSPCA_SPCA508=m
  1137. +CONFIG_USB_GSPCA_SPCA561=m
  1138. +CONFIG_USB_GSPCA_SPCA1528=m
  1139. +CONFIG_USB_GSPCA_SQ905=m
  1140. +CONFIG_USB_GSPCA_SQ905C=m
  1141. +CONFIG_USB_GSPCA_SQ930X=m
  1142. +CONFIG_USB_GSPCA_STK014=m
  1143. +CONFIG_USB_GSPCA_STV0680=m
  1144. +CONFIG_USB_GSPCA_SUNPLUS=m
  1145. +CONFIG_USB_GSPCA_T613=m
  1146. +CONFIG_USB_GSPCA_TOPRO=m
  1147. +CONFIG_USB_GSPCA_TV8532=m
  1148. +CONFIG_USB_GSPCA_VC032X=m
  1149. +CONFIG_USB_GSPCA_VICAM=m
  1150. +CONFIG_USB_GSPCA_XIRLINK_CIT=m
  1151. +CONFIG_USB_GSPCA_ZC3XX=m
  1152. +CONFIG_USB_PWC=m
  1153. +CONFIG_VIDEO_CPIA2=m
  1154. +CONFIG_USB_ZR364XX=m
  1155. +CONFIG_USB_STKWEBCAM=m
  1156. +CONFIG_USB_S2255=m
  1157. +CONFIG_USB_SN9C102=m
  1158. +CONFIG_VIDEO_PVRUSB2=m
  1159. +CONFIG_VIDEO_HDPVR=m
  1160. +CONFIG_VIDEO_TLG2300=m
  1161. +CONFIG_VIDEO_USBVISION=m
  1162. +CONFIG_VIDEO_STK1160=m
  1163. +CONFIG_VIDEO_STK1160_AC97=y
  1164. +CONFIG_VIDEO_AU0828=m
  1165. +CONFIG_VIDEO_CX231XX=m
  1166. +CONFIG_VIDEO_CX231XX_ALSA=m
  1167. +CONFIG_VIDEO_CX231XX_DVB=m
  1168. +CONFIG_VIDEO_TM6000=m
  1169. +CONFIG_VIDEO_TM6000_ALSA=m
  1170. +CONFIG_VIDEO_TM6000_DVB=m
  1171. +CONFIG_DVB_USB=m
  1172. +CONFIG_DVB_USB_A800=m
  1173. +CONFIG_DVB_USB_DIBUSB_MB=m
  1174. +CONFIG_DVB_USB_DIBUSB_MB_FAULTY=y
  1175. +CONFIG_DVB_USB_DIBUSB_MC=m
  1176. +CONFIG_DVB_USB_DIB0700=m
  1177. +CONFIG_DVB_USB_UMT_010=m
  1178. +CONFIG_DVB_USB_CXUSB=m
  1179. +CONFIG_DVB_USB_M920X=m
  1180. +CONFIG_DVB_USB_DIGITV=m
  1181. +CONFIG_DVB_USB_VP7045=m
  1182. +CONFIG_DVB_USB_VP702X=m
  1183. +CONFIG_DVB_USB_GP8PSK=m
  1184. +CONFIG_DVB_USB_NOVA_T_USB2=m
  1185. +CONFIG_DVB_USB_TTUSB2=m
  1186. +CONFIG_DVB_USB_DTT200U=m
  1187. +CONFIG_DVB_USB_OPERA1=m
  1188. +CONFIG_DVB_USB_AF9005=m
  1189. +CONFIG_DVB_USB_AF9005_REMOTE=m
  1190. +CONFIG_DVB_USB_PCTV452E=m
  1191. +CONFIG_DVB_USB_DW2102=m
  1192. +CONFIG_DVB_USB_CINERGY_T2=m
  1193. +CONFIG_DVB_USB_DTV5100=m
  1194. +CONFIG_DVB_USB_FRIIO=m
  1195. +CONFIG_DVB_USB_AZ6027=m
  1196. +CONFIG_DVB_USB_TECHNISAT_USB2=m
  1197. +CONFIG_DVB_USB_V2=m
  1198. +CONFIG_DVB_USB_AF9015=m
  1199. +CONFIG_DVB_USB_AF9035=m
  1200. +CONFIG_DVB_USB_ANYSEE=m
  1201. +CONFIG_DVB_USB_AU6610=m
  1202. +CONFIG_DVB_USB_AZ6007=m
  1203. +CONFIG_DVB_USB_CE6230=m
  1204. +CONFIG_DVB_USB_EC168=m
  1205. +CONFIG_DVB_USB_GL861=m
  1206. +CONFIG_DVB_USB_IT913X=m
  1207. +CONFIG_DVB_USB_LME2510=m
  1208. +CONFIG_DVB_USB_MXL111SF=m
  1209. +CONFIG_DVB_USB_RTL28XXU=m
  1210. +CONFIG_SMS_USB_DRV=m
  1211. +CONFIG_DVB_B2C2_FLEXCOP_USB=m
  1212. +CONFIG_VIDEO_EM28XX=m
  1213. +CONFIG_VIDEO_EM28XX_ALSA=m
  1214. +CONFIG_VIDEO_EM28XX_DVB=m
  1215. +CONFIG_V4L_PLATFORM_DRIVERS=y
  1216. +CONFIG_VIDEO_BCM2835=y
  1217. +CONFIG_VIDEO_BCM2835_MMAL=m
  1218. +CONFIG_RADIO_SI470X=y
  1219. +CONFIG_USB_SI470X=m
  1220. +CONFIG_I2C_SI470X=m
  1221. +CONFIG_USB_MR800=m
  1222. +CONFIG_USB_DSBR=m
  1223. +CONFIG_RADIO_SHARK=m
  1224. +CONFIG_RADIO_SHARK2=m
  1225. +CONFIG_RADIO_SI4713=m
  1226. +CONFIG_USB_KEENE=m
  1227. +CONFIG_USB_MA901=m
  1228. +CONFIG_RADIO_TEA5764=m
  1229. +CONFIG_RADIO_SAA7706H=m
  1230. +CONFIG_RADIO_TEF6862=m
  1231. +CONFIG_RADIO_WL1273=m
  1232. +CONFIG_RADIO_WL128X=m
  1233. +CONFIG_FB=y
  1234. +CONFIG_FB_BCM2708=y
  1235. +# CONFIG_BACKLIGHT_GENERIC is not set
  1236. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1237. +CONFIG_LOGO=y
  1238. +# CONFIG_LOGO_LINUX_MONO is not set
  1239. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1240. +CONFIG_SOUND=y
  1241. +CONFIG_SND=m
  1242. +CONFIG_SND_SEQUENCER=m
  1243. +CONFIG_SND_SEQ_DUMMY=m
  1244. +CONFIG_SND_MIXER_OSS=m
  1245. +CONFIG_SND_PCM_OSS=m
  1246. +CONFIG_SND_SEQUENCER_OSS=y
  1247. +CONFIG_SND_HRTIMER=m
  1248. +CONFIG_SND_DUMMY=m
  1249. +CONFIG_SND_ALOOP=m
  1250. +CONFIG_SND_VIRMIDI=m
  1251. +CONFIG_SND_MTPAV=m
  1252. +CONFIG_SND_SERIAL_U16550=m
  1253. +CONFIG_SND_MPU401=m
  1254. +CONFIG_SND_BCM2835=m
  1255. +CONFIG_SND_USB_AUDIO=m
  1256. +CONFIG_SND_USB_UA101=m
  1257. +CONFIG_SND_USB_CAIAQ=m
  1258. +CONFIG_SND_USB_CAIAQ_INPUT=y
  1259. +CONFIG_SND_USB_6FIRE=m
  1260. +CONFIG_SND_SOC=m
  1261. +CONFIG_SND_BCM2708_SOC_I2S=m
  1262. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
  1263. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
  1264. +CONFIG_SND_BCM2708_SOC_RPI_DAC=m
  1265. +CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
  1266. +CONFIG_SOUND_PRIME=m
  1267. +CONFIG_HIDRAW=y
  1268. +CONFIG_HID_A4TECH=m
  1269. +CONFIG_HID_ACRUX=m
  1270. +CONFIG_HID_APPLE=m
  1271. +CONFIG_HID_BELKIN=m
  1272. +CONFIG_HID_CHERRY=m
  1273. +CONFIG_HID_CHICONY=m
  1274. +CONFIG_HID_CYPRESS=m
  1275. +CONFIG_HID_DRAGONRISE=m
  1276. +CONFIG_HID_EMS_FF=m
  1277. +CONFIG_HID_ELECOM=m
  1278. +CONFIG_HID_EZKEY=m
  1279. +CONFIG_HID_HOLTEK=m
  1280. +CONFIG_HID_KEYTOUCH=m
  1281. +CONFIG_HID_KYE=m
  1282. +CONFIG_HID_UCLOGIC=m
  1283. +CONFIG_HID_WALTOP=m
  1284. +CONFIG_HID_GYRATION=m
  1285. +CONFIG_HID_TWINHAN=m
  1286. +CONFIG_HID_KENSINGTON=m
  1287. +CONFIG_HID_LCPOWER=m
  1288. +CONFIG_HID_LOGITECH=m
  1289. +CONFIG_HID_MAGICMOUSE=m
  1290. +CONFIG_HID_MICROSOFT=m
  1291. +CONFIG_HID_MONTEREY=m
  1292. +CONFIG_HID_MULTITOUCH=m
  1293. +CONFIG_HID_NTRIG=m
  1294. +CONFIG_HID_ORTEK=m
  1295. +CONFIG_HID_PANTHERLORD=m
  1296. +CONFIG_HID_PETALYNX=m
  1297. +CONFIG_HID_PICOLCD=m
  1298. +CONFIG_HID_ROCCAT=m
  1299. +CONFIG_HID_SAMSUNG=m
  1300. +CONFIG_HID_SONY=m
  1301. +CONFIG_HID_SPEEDLINK=m
  1302. +CONFIG_HID_SUNPLUS=m
  1303. +CONFIG_HID_GREENASIA=m
  1304. +CONFIG_HID_SMARTJOYPLUS=m
  1305. +CONFIG_HID_TOPSEED=m
  1306. +CONFIG_HID_THINGM=m
  1307. +CONFIG_HID_THRUSTMASTER=m
  1308. +CONFIG_HID_WACOM=m
  1309. +CONFIG_HID_WIIMOTE=m
  1310. +CONFIG_HID_ZEROPLUS=m
  1311. +CONFIG_HID_ZYDACRON=m
  1312. +CONFIG_HID_PID=y
  1313. +CONFIG_USB_HIDDEV=y
  1314. +CONFIG_USB=y
  1315. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1316. +CONFIG_USB_MON=m
  1317. +CONFIG_USB_DWCOTG=y
  1318. +CONFIG_USB_PRINTER=m
  1319. +CONFIG_USB_STORAGE=y
  1320. +CONFIG_USB_STORAGE_REALTEK=m
  1321. +CONFIG_USB_STORAGE_DATAFAB=m
  1322. +CONFIG_USB_STORAGE_FREECOM=m
  1323. +CONFIG_USB_STORAGE_ISD200=m
  1324. +CONFIG_USB_STORAGE_USBAT=m
  1325. +CONFIG_USB_STORAGE_SDDR09=m
  1326. +CONFIG_USB_STORAGE_SDDR55=m
  1327. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1328. +CONFIG_USB_STORAGE_ALAUDA=m
  1329. +CONFIG_USB_STORAGE_ONETOUCH=m
  1330. +CONFIG_USB_STORAGE_KARMA=m
  1331. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1332. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1333. +CONFIG_USB_MDC800=m
  1334. +CONFIG_USB_MICROTEK=m
  1335. +CONFIG_USB_SERIAL=m
  1336. +CONFIG_USB_SERIAL_GENERIC=y
  1337. +CONFIG_USB_SERIAL_AIRCABLE=m
  1338. +CONFIG_USB_SERIAL_ARK3116=m
  1339. +CONFIG_USB_SERIAL_BELKIN=m
  1340. +CONFIG_USB_SERIAL_CH341=m
  1341. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1342. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1343. +CONFIG_USB_SERIAL_CP210X=m
  1344. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1345. +CONFIG_USB_SERIAL_EMPEG=m
  1346. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1347. +CONFIG_USB_SERIAL_FUNSOFT=m
  1348. +CONFIG_USB_SERIAL_VISOR=m
  1349. +CONFIG_USB_SERIAL_IPAQ=m
  1350. +CONFIG_USB_SERIAL_IR=m
  1351. +CONFIG_USB_SERIAL_EDGEPORT=m
  1352. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1353. +CONFIG_USB_SERIAL_F81232=m
  1354. +CONFIG_USB_SERIAL_GARMIN=m
  1355. +CONFIG_USB_SERIAL_IPW=m
  1356. +CONFIG_USB_SERIAL_IUU=m
  1357. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1358. +CONFIG_USB_SERIAL_KEYSPAN=m
  1359. +CONFIG_USB_SERIAL_KLSI=m
  1360. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1361. +CONFIG_USB_SERIAL_MCT_U232=m
  1362. +CONFIG_USB_SERIAL_METRO=m
  1363. +CONFIG_USB_SERIAL_MOS7720=m
  1364. +CONFIG_USB_SERIAL_MOS7840=m
  1365. +CONFIG_USB_SERIAL_MOTOROLA=m
  1366. +CONFIG_USB_SERIAL_NAVMAN=m
  1367. +CONFIG_USB_SERIAL_PL2303=m
  1368. +CONFIG_USB_SERIAL_OTI6858=m
  1369. +CONFIG_USB_SERIAL_QCAUX=m
  1370. +CONFIG_USB_SERIAL_QUALCOMM=m
  1371. +CONFIG_USB_SERIAL_SPCP8X5=m
  1372. +CONFIG_USB_SERIAL_HP4X=m
  1373. +CONFIG_USB_SERIAL_SAFE=m
  1374. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  1375. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1376. +CONFIG_USB_SERIAL_SYMBOL=m
  1377. +CONFIG_USB_SERIAL_TI=m
  1378. +CONFIG_USB_SERIAL_CYBERJACK=m
  1379. +CONFIG_USB_SERIAL_XIRCOM=m
  1380. +CONFIG_USB_SERIAL_OPTION=m
  1381. +CONFIG_USB_SERIAL_OMNINET=m
  1382. +CONFIG_USB_SERIAL_OPTICON=m
  1383. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  1384. +CONFIG_USB_SERIAL_XSENS_MT=m
  1385. +CONFIG_USB_SERIAL_ZIO=m
  1386. +CONFIG_USB_SERIAL_WISHBONE=m
  1387. +CONFIG_USB_SERIAL_ZTE=m
  1388. +CONFIG_USB_SERIAL_SSU100=m
  1389. +CONFIG_USB_SERIAL_QT2=m
  1390. +CONFIG_USB_SERIAL_DEBUG=m
  1391. +CONFIG_USB_EMI62=m
  1392. +CONFIG_USB_EMI26=m
  1393. +CONFIG_USB_ADUTUX=m
  1394. +CONFIG_USB_SEVSEG=m
  1395. +CONFIG_USB_RIO500=m
  1396. +CONFIG_USB_LEGOTOWER=m
  1397. +CONFIG_USB_LCD=m
  1398. +CONFIG_USB_LED=m
  1399. +CONFIG_USB_CYPRESS_CY7C63=m
  1400. +CONFIG_USB_CYTHERM=m
  1401. +CONFIG_USB_IDMOUSE=m
  1402. +CONFIG_USB_FTDI_ELAN=m
  1403. +CONFIG_USB_APPLEDISPLAY=m
  1404. +CONFIG_USB_LD=m
  1405. +CONFIG_USB_TRANCEVIBRATOR=m
  1406. +CONFIG_USB_IOWARRIOR=m
  1407. +CONFIG_USB_TEST=m
  1408. +CONFIG_USB_ISIGHTFW=m
  1409. +CONFIG_USB_YUREX=m
  1410. +CONFIG_MMC=y
  1411. +CONFIG_MMC_BLOCK_MINORS=32
  1412. +CONFIG_MMC_SDHCI=y
  1413. +CONFIG_MMC_SDHCI_PLTFM=y
  1414. +CONFIG_MMC_SDHCI_BCM2708=y
  1415. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1416. +CONFIG_MMC_SPI=m
  1417. +CONFIG_LEDS_GPIO=m
  1418. +CONFIG_LEDS_TRIGGER_TIMER=y
  1419. +CONFIG_LEDS_TRIGGER_ONESHOT=y
  1420. +CONFIG_LEDS_TRIGGER_HEARTBEAT=y
  1421. +CONFIG_LEDS_TRIGGER_BACKLIGHT=y
  1422. +CONFIG_LEDS_TRIGGER_CPU=y
  1423. +CONFIG_LEDS_TRIGGER_GPIO=y
  1424. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
  1425. +CONFIG_LEDS_TRIGGER_TRANSIENT=m
  1426. +CONFIG_LEDS_TRIGGER_CAMERA=m
  1427. +CONFIG_RTC_CLASS=y
  1428. +# CONFIG_RTC_HCTOSYS is not set
  1429. +CONFIG_RTC_DRV_DS1307=m
  1430. +CONFIG_RTC_DRV_DS1374=m
  1431. +CONFIG_RTC_DRV_DS1672=m
  1432. +CONFIG_RTC_DRV_DS3232=m
  1433. +CONFIG_RTC_DRV_MAX6900=m
  1434. +CONFIG_RTC_DRV_RS5C372=m
  1435. +CONFIG_RTC_DRV_ISL1208=m
  1436. +CONFIG_RTC_DRV_ISL12022=m
  1437. +CONFIG_RTC_DRV_X1205=m
  1438. +CONFIG_RTC_DRV_PCF8523=m
  1439. +CONFIG_RTC_DRV_PCF8563=m
  1440. +CONFIG_RTC_DRV_PCF8583=m
  1441. +CONFIG_RTC_DRV_M41T80=m
  1442. +CONFIG_RTC_DRV_BQ32K=m
  1443. +CONFIG_RTC_DRV_S35390A=m
  1444. +CONFIG_RTC_DRV_FM3130=m
  1445. +CONFIG_RTC_DRV_RX8581=m
  1446. +CONFIG_RTC_DRV_RX8025=m
  1447. +CONFIG_RTC_DRV_EM3027=m
  1448. +CONFIG_RTC_DRV_RV3029C2=m
  1449. +CONFIG_RTC_DRV_M41T93=m
  1450. +CONFIG_RTC_DRV_M41T94=m
  1451. +CONFIG_RTC_DRV_DS1305=m
  1452. +CONFIG_RTC_DRV_DS1390=m
  1453. +CONFIG_RTC_DRV_MAX6902=m
  1454. +CONFIG_RTC_DRV_R9701=m
  1455. +CONFIG_RTC_DRV_RS5C348=m
  1456. +CONFIG_RTC_DRV_DS3234=m
  1457. +CONFIG_RTC_DRV_PCF2123=m
  1458. +CONFIG_RTC_DRV_RX4581=m
  1459. +CONFIG_DMADEVICES=y
  1460. +CONFIG_DMA_BCM2708=m
  1461. +CONFIG_UIO=m
  1462. +CONFIG_UIO_PDRV=m
  1463. +CONFIG_UIO_PDRV_GENIRQ=m
  1464. +CONFIG_STAGING=y
  1465. +CONFIG_W35UND=m
  1466. +CONFIG_PRISM2_USB=m
  1467. +CONFIG_R8712U=m
  1468. +CONFIG_VT6656=m
  1469. +CONFIG_SPEAKUP=m
  1470. +CONFIG_SPEAKUP_SYNTH_SOFT=m
  1471. +CONFIG_STAGING_MEDIA=y
  1472. +CONFIG_DVB_AS102=m
  1473. +CONFIG_LIRC_STAGING=y
  1474. +CONFIG_LIRC_IGORPLUGUSB=m
  1475. +CONFIG_LIRC_IMON=m
  1476. +CONFIG_LIRC_RPI=m
  1477. +CONFIG_LIRC_SASEM=m
  1478. +CONFIG_LIRC_SERIAL=m
  1479. +# CONFIG_IOMMU_SUPPORT is not set
  1480. +CONFIG_EXT4_FS=y
  1481. +CONFIG_EXT4_FS_POSIX_ACL=y
  1482. +CONFIG_EXT4_FS_SECURITY=y
  1483. +CONFIG_REISERFS_FS=m
  1484. +CONFIG_REISERFS_FS_XATTR=y
  1485. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1486. +CONFIG_REISERFS_FS_SECURITY=y
  1487. +CONFIG_JFS_FS=m
  1488. +CONFIG_JFS_POSIX_ACL=y
  1489. +CONFIG_JFS_SECURITY=y
  1490. +CONFIG_JFS_STATISTICS=y
  1491. +CONFIG_XFS_FS=m
  1492. +CONFIG_XFS_QUOTA=y
  1493. +CONFIG_XFS_POSIX_ACL=y
  1494. +CONFIG_XFS_RT=y
  1495. +CONFIG_GFS2_FS=m
  1496. +CONFIG_OCFS2_FS=m
  1497. +CONFIG_BTRFS_FS=m
  1498. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1499. +CONFIG_NILFS2_FS=m
  1500. +CONFIG_FANOTIFY=y
  1501. +CONFIG_QFMT_V1=m
  1502. +CONFIG_QFMT_V2=m
  1503. +CONFIG_AUTOFS4_FS=y
  1504. +CONFIG_FUSE_FS=m
  1505. +CONFIG_CUSE=m
  1506. +CONFIG_FSCACHE=y
  1507. +CONFIG_FSCACHE_STATS=y
  1508. +CONFIG_FSCACHE_HISTOGRAM=y
  1509. +CONFIG_CACHEFILES=y
  1510. +CONFIG_ISO9660_FS=m
  1511. +CONFIG_JOLIET=y
  1512. +CONFIG_ZISOFS=y
  1513. +CONFIG_UDF_FS=m
  1514. +CONFIG_MSDOS_FS=y
  1515. +CONFIG_VFAT_FS=y
  1516. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1517. +CONFIG_NTFS_FS=m
  1518. +CONFIG_NTFS_RW=y
  1519. +CONFIG_TMPFS=y
  1520. +CONFIG_TMPFS_POSIX_ACL=y
  1521. +CONFIG_CONFIGFS_FS=y
  1522. +CONFIG_ECRYPT_FS=m
  1523. +CONFIG_HFS_FS=m
  1524. +CONFIG_HFSPLUS_FS=m
  1525. +CONFIG_SQUASHFS=m
  1526. +CONFIG_SQUASHFS_XATTR=y
  1527. +CONFIG_SQUASHFS_LZO=y
  1528. +CONFIG_SQUASHFS_XZ=y
  1529. +CONFIG_F2FS_FS=y
  1530. +CONFIG_NFS_FS=y
  1531. +CONFIG_NFS_V3_ACL=y
  1532. +CONFIG_NFS_V4=y
  1533. +CONFIG_ROOT_NFS=y
  1534. +CONFIG_NFS_FSCACHE=y
  1535. +CONFIG_NFSD=m
  1536. +CONFIG_NFSD_V3_ACL=y
  1537. +CONFIG_NFSD_V4=y
  1538. +CONFIG_CIFS=m
  1539. +CONFIG_CIFS_WEAK_PW_HASH=y
  1540. +CONFIG_CIFS_XATTR=y
  1541. +CONFIG_CIFS_POSIX=y
  1542. +CONFIG_9P_FS=m
  1543. +CONFIG_9P_FS_POSIX_ACL=y
  1544. +CONFIG_NLS_DEFAULT="utf8"
  1545. +CONFIG_NLS_CODEPAGE_437=y
  1546. +CONFIG_NLS_CODEPAGE_737=m
  1547. +CONFIG_NLS_CODEPAGE_775=m
  1548. +CONFIG_NLS_CODEPAGE_850=m
  1549. +CONFIG_NLS_CODEPAGE_852=m
  1550. +CONFIG_NLS_CODEPAGE_855=m
  1551. +CONFIG_NLS_CODEPAGE_857=m
  1552. +CONFIG_NLS_CODEPAGE_860=m
  1553. +CONFIG_NLS_CODEPAGE_861=m
  1554. +CONFIG_NLS_CODEPAGE_862=m
  1555. +CONFIG_NLS_CODEPAGE_863=m
  1556. +CONFIG_NLS_CODEPAGE_864=m
  1557. +CONFIG_NLS_CODEPAGE_865=m
  1558. +CONFIG_NLS_CODEPAGE_866=m
  1559. +CONFIG_NLS_CODEPAGE_869=m
  1560. +CONFIG_NLS_CODEPAGE_936=m
  1561. +CONFIG_NLS_CODEPAGE_950=m
  1562. +CONFIG_NLS_CODEPAGE_932=m
  1563. +CONFIG_NLS_CODEPAGE_949=m
  1564. +CONFIG_NLS_CODEPAGE_874=m
  1565. +CONFIG_NLS_ISO8859_8=m
  1566. +CONFIG_NLS_CODEPAGE_1250=m
  1567. +CONFIG_NLS_CODEPAGE_1251=m
  1568. +CONFIG_NLS_ASCII=y
  1569. +CONFIG_NLS_ISO8859_1=m
  1570. +CONFIG_NLS_ISO8859_2=m
  1571. +CONFIG_NLS_ISO8859_3=m
  1572. +CONFIG_NLS_ISO8859_4=m
  1573. +CONFIG_NLS_ISO8859_5=m
  1574. +CONFIG_NLS_ISO8859_6=m
  1575. +CONFIG_NLS_ISO8859_7=m
  1576. +CONFIG_NLS_ISO8859_9=m
  1577. +CONFIG_NLS_ISO8859_13=m
  1578. +CONFIG_NLS_ISO8859_14=m
  1579. +CONFIG_NLS_ISO8859_15=m
  1580. +CONFIG_NLS_KOI8_R=m
  1581. +CONFIG_NLS_KOI8_U=m
  1582. +CONFIG_DLM=m
  1583. +CONFIG_PRINTK_TIME=y
  1584. +CONFIG_DEBUG_FS=y
  1585. +CONFIG_DETECT_HUNG_TASK=y
  1586. +CONFIG_TIMER_STATS=y
  1587. +# CONFIG_DEBUG_PREEMPT is not set
  1588. +CONFIG_DEBUG_MEMORY_INIT=y
  1589. +CONFIG_BOOT_PRINTK_DELAY=y
  1590. +CONFIG_LATENCYTOP=y
  1591. +# CONFIG_KPROBE_EVENT is not set
  1592. +CONFIG_KGDB=y
  1593. +CONFIG_KGDB_KDB=y
  1594. +CONFIG_KDB_KEYBOARD=y
  1595. +CONFIG_STRICT_DEVMEM=y
  1596. +CONFIG_CRYPTO_USER=m
  1597. +CONFIG_CRYPTO_NULL=m
  1598. +CONFIG_CRYPTO_CRYPTD=m
  1599. +CONFIG_CRYPTO_SEQIV=m
  1600. +CONFIG_CRYPTO_CBC=y
  1601. +CONFIG_CRYPTO_XTS=m
  1602. +CONFIG_CRYPTO_XCBC=m
  1603. +CONFIG_CRYPTO_SHA1_ARM=m
  1604. +CONFIG_CRYPTO_SHA512=m
  1605. +CONFIG_CRYPTO_TGR192=m
  1606. +CONFIG_CRYPTO_WP512=m
  1607. +CONFIG_CRYPTO_AES_ARM=m
  1608. +CONFIG_CRYPTO_CAST5=m
  1609. +CONFIG_CRYPTO_DES=y
  1610. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1611. +# CONFIG_CRYPTO_HW is not set
  1612. +CONFIG_CRC_ITU_T=y
  1613. +CONFIG_LIBCRC32C=y
  1614. diff -Nur linux-3.10.37/arch/arm/configs/bcmrpi_quick_defconfig linux-rpi/arch/arm/configs/bcmrpi_quick_defconfig
  1615. --- linux-3.10.37/arch/arm/configs/bcmrpi_quick_defconfig 1970-01-01 01:00:00.000000000 +0100
  1616. +++ linux-rpi/arch/arm/configs/bcmrpi_quick_defconfig 2014-04-24 15:35:00.717527267 +0200
  1617. @@ -0,0 +1,197 @@
  1618. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  1619. +CONFIG_LOCALVERSION="-quick"
  1620. +# CONFIG_LOCALVERSION_AUTO is not set
  1621. +# CONFIG_SWAP is not set
  1622. +CONFIG_SYSVIPC=y
  1623. +CONFIG_POSIX_MQUEUE=y
  1624. +CONFIG_NO_HZ=y
  1625. +CONFIG_HIGH_RES_TIMERS=y
  1626. +CONFIG_IKCONFIG=y
  1627. +CONFIG_IKCONFIG_PROC=y
  1628. +CONFIG_KALLSYMS_ALL=y
  1629. +CONFIG_EMBEDDED=y
  1630. +CONFIG_PERF_EVENTS=y
  1631. +# CONFIG_COMPAT_BRK is not set
  1632. +CONFIG_SLAB=y
  1633. +CONFIG_MODULES=y
  1634. +CONFIG_MODULE_UNLOAD=y
  1635. +CONFIG_MODVERSIONS=y
  1636. +CONFIG_MODULE_SRCVERSION_ALL=y
  1637. +# CONFIG_BLK_DEV_BSG is not set
  1638. +CONFIG_ARCH_BCM2708=y
  1639. +CONFIG_PREEMPT=y
  1640. +CONFIG_AEABI=y
  1641. +CONFIG_UACCESS_WITH_MEMCPY=y
  1642. +CONFIG_ZBOOT_ROM_TEXT=0x0
  1643. +CONFIG_ZBOOT_ROM_BSS=0x0
  1644. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  1645. +CONFIG_CPU_FREQ=y
  1646. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  1647. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  1648. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  1649. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  1650. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  1651. +CONFIG_CPU_IDLE=y
  1652. +CONFIG_VFP=y
  1653. +CONFIG_BINFMT_MISC=y
  1654. +CONFIG_NET=y
  1655. +CONFIG_PACKET=y
  1656. +CONFIG_UNIX=y
  1657. +CONFIG_INET=y
  1658. +CONFIG_IP_MULTICAST=y
  1659. +CONFIG_IP_PNP=y
  1660. +CONFIG_IP_PNP_DHCP=y
  1661. +CONFIG_IP_PNP_RARP=y
  1662. +CONFIG_SYN_COOKIES=y
  1663. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  1664. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  1665. +# CONFIG_INET_XFRM_MODE_BEET is not set
  1666. +# CONFIG_INET_LRO is not set
  1667. +# CONFIG_INET_DIAG is not set
  1668. +# CONFIG_IPV6 is not set
  1669. +# CONFIG_WIRELESS is not set
  1670. +CONFIG_DEVTMPFS=y
  1671. +CONFIG_DEVTMPFS_MOUNT=y
  1672. +CONFIG_BLK_DEV_LOOP=y
  1673. +CONFIG_BLK_DEV_RAM=y
  1674. +CONFIG_SCSI=y
  1675. +# CONFIG_SCSI_PROC_FS is not set
  1676. +# CONFIG_SCSI_LOWLEVEL is not set
  1677. +CONFIG_NETDEVICES=y
  1678. +# CONFIG_NET_VENDOR_BROADCOM is not set
  1679. +# CONFIG_NET_VENDOR_CIRRUS is not set
  1680. +# CONFIG_NET_VENDOR_FARADAY is not set
  1681. +# CONFIG_NET_VENDOR_INTEL is not set
  1682. +# CONFIG_NET_VENDOR_MARVELL is not set
  1683. +# CONFIG_NET_VENDOR_MICREL is not set
  1684. +# CONFIG_NET_VENDOR_NATSEMI is not set
  1685. +# CONFIG_NET_VENDOR_SEEQ is not set
  1686. +# CONFIG_NET_VENDOR_STMICRO is not set
  1687. +# CONFIG_NET_VENDOR_WIZNET is not set
  1688. +CONFIG_USB_USBNET=y
  1689. +# CONFIG_USB_NET_AX8817X is not set
  1690. +# CONFIG_USB_NET_CDCETHER is not set
  1691. +# CONFIG_USB_NET_CDC_NCM is not set
  1692. +CONFIG_USB_NET_SMSC95XX=y
  1693. +# CONFIG_USB_NET_NET1080 is not set
  1694. +# CONFIG_USB_NET_CDC_SUBSET is not set
  1695. +# CONFIG_USB_NET_ZAURUS is not set
  1696. +# CONFIG_WLAN is not set
  1697. +# CONFIG_INPUT_MOUSEDEV is not set
  1698. +CONFIG_INPUT_EVDEV=y
  1699. +# CONFIG_INPUT_KEYBOARD is not set
  1700. +# CONFIG_INPUT_MOUSE is not set
  1701. +# CONFIG_SERIO is not set
  1702. +CONFIG_VT_HW_CONSOLE_BINDING=y
  1703. +# CONFIG_LEGACY_PTYS is not set
  1704. +# CONFIG_DEVKMEM is not set
  1705. +CONFIG_SERIAL_AMBA_PL011=y
  1706. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1707. +CONFIG_TTY_PRINTK=y
  1708. +CONFIG_HW_RANDOM=y
  1709. +CONFIG_HW_RANDOM_BCM2708=y
  1710. +CONFIG_RAW_DRIVER=y
  1711. +CONFIG_THERMAL=y
  1712. +CONFIG_THERMAL_BCM2835=y
  1713. +CONFIG_WATCHDOG=y
  1714. +CONFIG_BCM2708_WDT=y
  1715. +CONFIG_REGULATOR=y
  1716. +CONFIG_REGULATOR_DEBUG=y
  1717. +CONFIG_REGULATOR_FIXED_VOLTAGE=y
  1718. +CONFIG_REGULATOR_VIRTUAL_CONSUMER=y
  1719. +CONFIG_REGULATOR_USERSPACE_CONSUMER=y
  1720. +CONFIG_FB=y
  1721. +CONFIG_FB_BCM2708=y
  1722. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1723. +CONFIG_LOGO=y
  1724. +# CONFIG_LOGO_LINUX_MONO is not set
  1725. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1726. +CONFIG_SOUND=y
  1727. +CONFIG_SND=y
  1728. +CONFIG_SND_BCM2835=y
  1729. +# CONFIG_SND_USB is not set
  1730. +CONFIG_USB=y
  1731. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1732. +CONFIG_USB_DWCOTG=y
  1733. +CONFIG_MMC=y
  1734. +CONFIG_MMC_SDHCI=y
  1735. +CONFIG_MMC_SDHCI_PLTFM=y
  1736. +CONFIG_MMC_SDHCI_BCM2708=y
  1737. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1738. +CONFIG_NEW_LEDS=y
  1739. +CONFIG_LEDS_CLASS=y
  1740. +CONFIG_LEDS_TRIGGERS=y
  1741. +# CONFIG_IOMMU_SUPPORT is not set
  1742. +CONFIG_EXT4_FS=y
  1743. +CONFIG_EXT4_FS_POSIX_ACL=y
  1744. +CONFIG_EXT4_FS_SECURITY=y
  1745. +CONFIG_AUTOFS4_FS=y
  1746. +CONFIG_FSCACHE=y
  1747. +CONFIG_CACHEFILES=y
  1748. +CONFIG_MSDOS_FS=y
  1749. +CONFIG_VFAT_FS=y
  1750. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1751. +CONFIG_TMPFS=y
  1752. +CONFIG_TMPFS_POSIX_ACL=y
  1753. +CONFIG_CONFIGFS_FS=y
  1754. +# CONFIG_MISC_FILESYSTEMS is not set
  1755. +CONFIG_NFS_FS=y
  1756. +CONFIG_NFS_V3_ACL=y
  1757. +CONFIG_NFS_V4=y
  1758. +CONFIG_ROOT_NFS=y
  1759. +CONFIG_NFS_FSCACHE=y
  1760. +CONFIG_NLS_DEFAULT="utf8"
  1761. +CONFIG_NLS_CODEPAGE_437=y
  1762. +CONFIG_NLS_CODEPAGE_737=y
  1763. +CONFIG_NLS_CODEPAGE_775=y
  1764. +CONFIG_NLS_CODEPAGE_850=y
  1765. +CONFIG_NLS_CODEPAGE_852=y
  1766. +CONFIG_NLS_CODEPAGE_855=y
  1767. +CONFIG_NLS_CODEPAGE_857=y
  1768. +CONFIG_NLS_CODEPAGE_860=y
  1769. +CONFIG_NLS_CODEPAGE_861=y
  1770. +CONFIG_NLS_CODEPAGE_862=y
  1771. +CONFIG_NLS_CODEPAGE_863=y
  1772. +CONFIG_NLS_CODEPAGE_864=y
  1773. +CONFIG_NLS_CODEPAGE_865=y
  1774. +CONFIG_NLS_CODEPAGE_866=y
  1775. +CONFIG_NLS_CODEPAGE_869=y
  1776. +CONFIG_NLS_CODEPAGE_936=y
  1777. +CONFIG_NLS_CODEPAGE_950=y
  1778. +CONFIG_NLS_CODEPAGE_932=y
  1779. +CONFIG_NLS_CODEPAGE_949=y
  1780. +CONFIG_NLS_CODEPAGE_874=y
  1781. +CONFIG_NLS_ISO8859_8=y
  1782. +CONFIG_NLS_CODEPAGE_1250=y
  1783. +CONFIG_NLS_CODEPAGE_1251=y
  1784. +CONFIG_NLS_ASCII=y
  1785. +CONFIG_NLS_ISO8859_1=y
  1786. +CONFIG_NLS_ISO8859_2=y
  1787. +CONFIG_NLS_ISO8859_3=y
  1788. +CONFIG_NLS_ISO8859_4=y
  1789. +CONFIG_NLS_ISO8859_5=y
  1790. +CONFIG_NLS_ISO8859_6=y
  1791. +CONFIG_NLS_ISO8859_7=y
  1792. +CONFIG_NLS_ISO8859_9=y
  1793. +CONFIG_NLS_ISO8859_13=y
  1794. +CONFIG_NLS_ISO8859_14=y
  1795. +CONFIG_NLS_ISO8859_15=y
  1796. +CONFIG_NLS_UTF8=y
  1797. +CONFIG_PRINTK_TIME=y
  1798. +CONFIG_DEBUG_FS=y
  1799. +CONFIG_DETECT_HUNG_TASK=y
  1800. +# CONFIG_DEBUG_PREEMPT is not set
  1801. +# CONFIG_DEBUG_BUGVERBOSE is not set
  1802. +# CONFIG_FTRACE is not set
  1803. +CONFIG_KGDB=y
  1804. +CONFIG_KGDB_KDB=y
  1805. +# CONFIG_ARM_UNWIND is not set
  1806. +CONFIG_CRYPTO_CBC=y
  1807. +CONFIG_CRYPTO_HMAC=y
  1808. +CONFIG_CRYPTO_MD5=y
  1809. +CONFIG_CRYPTO_SHA1=y
  1810. +CONFIG_CRYPTO_DES=y
  1811. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1812. +# CONFIG_CRYPTO_HW is not set
  1813. +CONFIG_CRC_ITU_T=y
  1814. +CONFIG_LIBCRC32C=y
  1815. diff -Nur linux-3.10.37/arch/arm/include/asm/fiq.h linux-rpi/arch/arm/include/asm/fiq.h
  1816. --- linux-3.10.37/arch/arm/include/asm/fiq.h 2014-04-14 15:42:31.000000000 +0200
  1817. +++ linux-rpi/arch/arm/include/asm/fiq.h 2014-04-24 15:35:00.733527446 +0200
  1818. @@ -42,6 +42,7 @@
  1819. /* helpers defined in fiqasm.S: */
  1820. extern void __set_fiq_regs(unsigned long const *regs);
  1821. extern void __get_fiq_regs(unsigned long *regs);
  1822. +extern void __FIQ_Branch(unsigned long *regs);
  1823. static inline void set_fiq_regs(struct pt_regs const *regs)
  1824. {
  1825. diff -Nur linux-3.10.37/arch/arm/Kconfig linux-rpi/arch/arm/Kconfig
  1826. --- linux-3.10.37/arch/arm/Kconfig 2014-04-14 15:42:31.000000000 +0200
  1827. +++ linux-rpi/arch/arm/Kconfig 2014-04-24 15:35:00.677526821 +0200
  1828. @@ -361,6 +361,24 @@
  1829. This enables support for systems based on Atmel
  1830. AT91RM9200 and AT91SAM9* processors.
  1831. +config ARCH_BCM2708
  1832. + bool "Broadcom BCM2708 family"
  1833. + select CPU_V6
  1834. + select ARM_AMBA
  1835. + select HAVE_CLK
  1836. + select HAVE_SCHED_CLOCK
  1837. + select NEED_MACH_GPIO_H
  1838. + select NEED_MACH_MEMORY_H
  1839. + select CLKDEV_LOOKUP
  1840. + select ARCH_HAS_CPUFREQ
  1841. + select GENERIC_CLOCKEVENTS
  1842. + select ARM_ERRATA_411920
  1843. + select MACH_BCM2708
  1844. + select VC4
  1845. + select FIQ
  1846. + help
  1847. + This enables support for Broadcom BCM2708 boards.
  1848. +
  1849. config ARCH_CLPS711X
  1850. bool "Cirrus Logic CLPS711x/EP721x/EP731x-based"
  1851. select ARCH_REQUIRE_GPIOLIB
  1852. @@ -1025,6 +1043,7 @@
  1853. source "arch/arm/mach-vt8500/Kconfig"
  1854. source "arch/arm/mach-w90x900/Kconfig"
  1855. +source "arch/arm/mach-bcm2708/Kconfig"
  1856. source "arch/arm/mach-zynq/Kconfig"
  1857. diff -Nur linux-3.10.37/arch/arm/Kconfig.debug linux-rpi/arch/arm/Kconfig.debug
  1858. --- linux-3.10.37/arch/arm/Kconfig.debug 2014-04-14 15:42:31.000000000 +0200
  1859. +++ linux-rpi/arch/arm/Kconfig.debug 2014-04-24 15:35:00.677526821 +0200
  1860. @@ -519,6 +519,14 @@
  1861. For more details about semihosting, please see
  1862. chapter 8 of DUI0203I_rvct_developer_guide.pdf from ARM Ltd.
  1863. + config DEBUG_BCM2708_UART0
  1864. + bool "Broadcom BCM2708 UART0 (PL011)"
  1865. + depends on MACH_BCM2708
  1866. + help
  1867. + Say Y here if you want the debug print routines to direct
  1868. + their output to UART 0. The port must have been initialised
  1869. + by the boot-loader before use.
  1870. +
  1871. endchoice
  1872. config DEBUG_EXYNOS_UART
  1873. diff -Nur linux-3.10.37/arch/arm/kernel/armksyms.c linux-rpi/arch/arm/kernel/armksyms.c
  1874. --- linux-3.10.37/arch/arm/kernel/armksyms.c 2014-04-14 15:42:31.000000000 +0200
  1875. +++ linux-rpi/arch/arm/kernel/armksyms.c 2014-04-24 15:35:00.745527579 +0200
  1876. @@ -156,3 +156,7 @@
  1877. #ifdef CONFIG_ARM_PATCH_PHYS_VIRT
  1878. EXPORT_SYMBOL(__pv_phys_offset);
  1879. #endif
  1880. +
  1881. +extern void v6wbi_flush_kern_tlb_range(void);
  1882. +EXPORT_SYMBOL(v6wbi_flush_kern_tlb_range);
  1883. +
  1884. diff -Nur linux-3.10.37/arch/arm/kernel/fiqasm.S linux-rpi/arch/arm/kernel/fiqasm.S
  1885. --- linux-3.10.37/arch/arm/kernel/fiqasm.S 2014-04-14 15:42:31.000000000 +0200
  1886. +++ linux-rpi/arch/arm/kernel/fiqasm.S 2014-04-24 15:35:00.749527624 +0200
  1887. @@ -25,6 +25,9 @@
  1888. ENTRY(__set_fiq_regs)
  1889. mov r2, #PSR_I_BIT | PSR_F_BIT | FIQ_MODE
  1890. mrs r1, cpsr
  1891. +@@@@@@@@@@@@@@@ hack: enable the fiq here to keep usb driver happy
  1892. + and r1, #~PSR_F_BIT
  1893. +@@@@@@@@@@@@@@@ endhack: (need to find better place for this to happen)
  1894. msr cpsr_c, r2 @ select FIQ mode
  1895. mov r0, r0 @ avoid hazard prior to ARMv4
  1896. ldmia r0!, {r8 - r12}
  1897. @@ -47,3 +50,7 @@
  1898. mov r0, r0 @ avoid hazard prior to ARMv4
  1899. mov pc, lr
  1900. ENDPROC(__get_fiq_regs)
  1901. +
  1902. +ENTRY(__FIQ_Branch)
  1903. + mov pc, r8
  1904. +ENDPROC(__FIQ_Branch)
  1905. diff -Nur linux-3.10.37/arch/arm/kernel/fiq.c linux-rpi/arch/arm/kernel/fiq.c
  1906. --- linux-3.10.37/arch/arm/kernel/fiq.c 2014-04-14 15:42:31.000000000 +0200
  1907. +++ linux-rpi/arch/arm/kernel/fiq.c 2014-04-24 15:35:00.749527624 +0200
  1908. @@ -84,17 +84,14 @@
  1909. void set_fiq_handler(void *start, unsigned int length)
  1910. {
  1911. -#if defined(CONFIG_CPU_USE_DOMAINS)
  1912. - void *base = (void *)0xffff0000;
  1913. -#else
  1914. void *base = vectors_page;
  1915. -#endif
  1916. unsigned offset = FIQ_OFFSET;
  1917. memcpy(base + offset, start, length);
  1918. + if (!cache_is_vipt_nonaliasing())
  1919. + flush_icache_range((unsigned long)base + offset, offset +
  1920. + length);
  1921. flush_icache_range(0xffff0000 + offset, 0xffff0000 + offset + length);
  1922. - if (!vectors_high())
  1923. - flush_icache_range(offset, offset + length);
  1924. }
  1925. int claim_fiq(struct fiq_handler *f)
  1926. @@ -145,6 +142,7 @@
  1927. EXPORT_SYMBOL(set_fiq_handler);
  1928. EXPORT_SYMBOL(__set_fiq_regs); /* defined in fiqasm.S */
  1929. EXPORT_SYMBOL(__get_fiq_regs); /* defined in fiqasm.S */
  1930. +EXPORT_SYMBOL(__FIQ_Branch); /* defined in fiqasm.S */
  1931. EXPORT_SYMBOL(claim_fiq);
  1932. EXPORT_SYMBOL(release_fiq);
  1933. EXPORT_SYMBOL(enable_fiq);
  1934. diff -Nur linux-3.10.37/arch/arm/kernel/process.c linux-rpi/arch/arm/kernel/process.c
  1935. --- linux-3.10.37/arch/arm/kernel/process.c 2014-04-14 15:42:31.000000000 +0200
  1936. +++ linux-rpi/arch/arm/kernel/process.c 2014-04-24 15:35:00.753527668 +0200
  1937. @@ -174,7 +174,7 @@
  1938. default_idle();
  1939. }
  1940. -static char reboot_mode = 'h';
  1941. +char reboot_mode = 'h';
  1942. int __init reboot_setup(char *str)
  1943. {
  1944. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/armctrl.c linux-rpi/arch/arm/mach-bcm2708/armctrl.c
  1945. --- linux-3.10.37/arch/arm/mach-bcm2708/armctrl.c 1970-01-01 01:00:00.000000000 +0100
  1946. +++ linux-rpi/arch/arm/mach-bcm2708/armctrl.c 2014-04-24 15:35:00.773527891 +0200
  1947. @@ -0,0 +1,219 @@
  1948. +/*
  1949. + * linux/arch/arm/mach-bcm2708/armctrl.c
  1950. + *
  1951. + * Copyright (C) 2010 Broadcom
  1952. + *
  1953. + * This program is free software; you can redistribute it and/or modify
  1954. + * it under the terms of the GNU General Public License as published by
  1955. + * the Free Software Foundation; either version 2 of the License, or
  1956. + * (at your option) any later version.
  1957. + *
  1958. + * This program is distributed in the hope that it will be useful,
  1959. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1960. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1961. + * GNU General Public License for more details.
  1962. + *
  1963. + * You should have received a copy of the GNU General Public License
  1964. + * along with this program; if not, write to the Free Software
  1965. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  1966. + */
  1967. +#include <linux/init.h>
  1968. +#include <linux/list.h>
  1969. +#include <linux/io.h>
  1970. +#include <linux/version.h>
  1971. +#include <linux/syscore_ops.h>
  1972. +#include <linux/interrupt.h>
  1973. +
  1974. +#include <asm/mach/irq.h>
  1975. +#include <mach/hardware.h>
  1976. +#include "armctrl.h"
  1977. +
  1978. +/* For support of kernels >= 3.0 assume only one VIC for now*/
  1979. +static unsigned int remap_irqs[(INTERRUPT_ARASANSDIO + 1) - INTERRUPT_JPEG] = {
  1980. + INTERRUPT_VC_JPEG,
  1981. + INTERRUPT_VC_USB,
  1982. + INTERRUPT_VC_3D,
  1983. + INTERRUPT_VC_DMA2,
  1984. + INTERRUPT_VC_DMA3,
  1985. + INTERRUPT_VC_I2C,
  1986. + INTERRUPT_VC_SPI,
  1987. + INTERRUPT_VC_I2SPCM,
  1988. + INTERRUPT_VC_SDIO,
  1989. + INTERRUPT_VC_UART,
  1990. + INTERRUPT_VC_ARASANSDIO
  1991. +};
  1992. +
  1993. +static void armctrl_mask_irq(struct irq_data *d)
  1994. +{
  1995. + static const unsigned int disables[4] = {
  1996. + ARM_IRQ_DIBL1,
  1997. + ARM_IRQ_DIBL2,
  1998. + ARM_IRQ_DIBL3,
  1999. + 0
  2000. + };
  2001. +
  2002. + if (d->irq >= FIQ_START) {
  2003. + writel(0, __io_address(ARM_IRQ_FAST));
  2004. + } else {
  2005. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2006. + writel(1 << (data & 0x1f), __io_address(disables[(data >> 5) & 0x3]));
  2007. + }
  2008. +}
  2009. +
  2010. +static void armctrl_unmask_irq(struct irq_data *d)
  2011. +{
  2012. + static const unsigned int enables[4] = {
  2013. + ARM_IRQ_ENBL1,
  2014. + ARM_IRQ_ENBL2,
  2015. + ARM_IRQ_ENBL3,
  2016. + 0
  2017. + };
  2018. +
  2019. + if (d->irq >= FIQ_START) {
  2020. + unsigned int data =
  2021. + (unsigned int)irq_get_chip_data(d->irq) - FIQ_START;
  2022. + writel(0x80 | data, __io_address(ARM_IRQ_FAST));
  2023. + } else {
  2024. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2025. + writel(1 << (data & 0x1f), __io_address(enables[(data >> 5) & 0x3]));
  2026. + }
  2027. +}
  2028. +
  2029. +#if defined(CONFIG_PM)
  2030. +
  2031. +/* for kernels 3.xx use the new syscore_ops apis but for older kernels use the sys dev class */
  2032. +
  2033. +/* Static defines
  2034. + * struct armctrl_device - VIC PM device (< 3.xx)
  2035. + * @sysdev: The system device which is registered. (< 3.xx)
  2036. + * @irq: The IRQ number for the base of the VIC.
  2037. + * @base: The register base for the VIC.
  2038. + * @resume_sources: A bitmask of interrupts for resume.
  2039. + * @resume_irqs: The IRQs enabled for resume.
  2040. + * @int_select: Save for VIC_INT_SELECT.
  2041. + * @int_enable: Save for VIC_INT_ENABLE.
  2042. + * @soft_int: Save for VIC_INT_SOFT.
  2043. + * @protect: Save for VIC_PROTECT.
  2044. + */
  2045. +struct armctrl_info {
  2046. + void __iomem *base;
  2047. + int irq;
  2048. + u32 resume_sources;
  2049. + u32 resume_irqs;
  2050. + u32 int_select;
  2051. + u32 int_enable;
  2052. + u32 soft_int;
  2053. + u32 protect;
  2054. +} armctrl;
  2055. +
  2056. +static int armctrl_suspend(void)
  2057. +{
  2058. + return 0;
  2059. +}
  2060. +
  2061. +static void armctrl_resume(void)
  2062. +{
  2063. + return;
  2064. +}
  2065. +
  2066. +/**
  2067. + * armctrl_pm_register - Register a VIC for later power management control
  2068. + * @base: The base address of the VIC.
  2069. + * @irq: The base IRQ for the VIC.
  2070. + * @resume_sources: bitmask of interrupts allowed for resume sources.
  2071. + *
  2072. + * For older kernels (< 3.xx) do -
  2073. + * Register the VIC with the system device tree so that it can be notified
  2074. + * of suspend and resume requests and ensure that the correct actions are
  2075. + * taken to re-instate the settings on resume.
  2076. + */
  2077. +static void __init armctrl_pm_register(void __iomem * base, unsigned int irq,
  2078. + u32 resume_sources)
  2079. +{
  2080. + armctrl.base = base;
  2081. + armctrl.resume_sources = resume_sources;
  2082. + armctrl.irq = irq;
  2083. +}
  2084. +
  2085. +static int armctrl_set_wake(struct irq_data *d, unsigned int on)
  2086. +{
  2087. + unsigned int off = d->irq & 31;
  2088. + u32 bit = 1 << off;
  2089. +
  2090. + if (!(bit & armctrl.resume_sources))
  2091. + return -EINVAL;
  2092. +
  2093. + if (on)
  2094. + armctrl.resume_irqs |= bit;
  2095. + else
  2096. + armctrl.resume_irqs &= ~bit;
  2097. +
  2098. + return 0;
  2099. +}
  2100. +
  2101. +#else
  2102. +static inline void armctrl_pm_register(void __iomem * base, unsigned int irq,
  2103. + u32 arg1)
  2104. +{
  2105. +}
  2106. +
  2107. +#define armctrl_suspend NULL
  2108. +#define armctrl_resume NULL
  2109. +#define armctrl_set_wake NULL
  2110. +#endif /* CONFIG_PM */
  2111. +
  2112. +static struct syscore_ops armctrl_syscore_ops = {
  2113. + .suspend = armctrl_suspend,
  2114. + .resume = armctrl_resume,
  2115. +};
  2116. +
  2117. +/**
  2118. + * armctrl_syscore_init - initicall to register VIC pm functions
  2119. + *
  2120. + * This is called via late_initcall() to register
  2121. + * the resources for the VICs due to the early
  2122. + * nature of the VIC's registration.
  2123. +*/
  2124. +static int __init armctrl_syscore_init(void)
  2125. +{
  2126. + register_syscore_ops(&armctrl_syscore_ops);
  2127. + return 0;
  2128. +}
  2129. +
  2130. +late_initcall(armctrl_syscore_init);
  2131. +
  2132. +static struct irq_chip armctrl_chip = {
  2133. + .name = "ARMCTRL",
  2134. + .irq_ack = armctrl_mask_irq,
  2135. + .irq_mask = armctrl_mask_irq,
  2136. + .irq_unmask = armctrl_unmask_irq,
  2137. + .irq_set_wake = armctrl_set_wake,
  2138. +};
  2139. +
  2140. +/**
  2141. + * armctrl_init - initialise a vectored interrupt controller
  2142. + * @base: iomem base address
  2143. + * @irq_start: starting interrupt number, must be muliple of 32
  2144. + * @armctrl_sources: bitmask of interrupt sources to allow
  2145. + * @resume_sources: bitmask of interrupt sources to allow for resume
  2146. + */
  2147. +int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2148. + u32 armctrl_sources, u32 resume_sources)
  2149. +{
  2150. + unsigned int irq;
  2151. +
  2152. + for (irq = 0; irq < NR_IRQS; irq++) {
  2153. + unsigned int data = irq;
  2154. + if (irq >= INTERRUPT_JPEG && irq <= INTERRUPT_ARASANSDIO)
  2155. + data = remap_irqs[irq - INTERRUPT_JPEG];
  2156. +
  2157. + irq_set_chip(irq, &armctrl_chip);
  2158. + irq_set_chip_data(irq, (void *)data);
  2159. + irq_set_handler(irq, handle_level_irq);
  2160. + set_irq_flags(irq, IRQF_VALID | IRQF_PROBE | IRQF_DISABLED);
  2161. + }
  2162. +
  2163. + armctrl_pm_register(base, irq_start, resume_sources);
  2164. + init_FIQ(FIQ_START);
  2165. + return 0;
  2166. +}
  2167. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/armctrl.h linux-rpi/arch/arm/mach-bcm2708/armctrl.h
  2168. --- linux-3.10.37/arch/arm/mach-bcm2708/armctrl.h 1970-01-01 01:00:00.000000000 +0100
  2169. +++ linux-rpi/arch/arm/mach-bcm2708/armctrl.h 2014-04-24 15:35:00.773527891 +0200
  2170. @@ -0,0 +1,27 @@
  2171. +/*
  2172. + * linux/arch/arm/mach-bcm2708/armctrl.h
  2173. + *
  2174. + * Copyright (C) 2010 Broadcom
  2175. + *
  2176. + * This program is free software; you can redistribute it and/or modify
  2177. + * it under the terms of the GNU General Public License as published by
  2178. + * the Free Software Foundation; either version 2 of the License, or
  2179. + * (at your option) any later version.
  2180. + *
  2181. + * This program is distributed in the hope that it will be useful,
  2182. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2183. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2184. + * GNU General Public License for more details.
  2185. + *
  2186. + * You should have received a copy of the GNU General Public License
  2187. + * along with this program; if not, write to the Free Software
  2188. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2189. + */
  2190. +
  2191. +#ifndef __BCM2708_ARMCTRL_H
  2192. +#define __BCM2708_ARMCTRL_H
  2193. +
  2194. +extern int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2195. + u32 armctrl_sources, u32 resume_sources);
  2196. +
  2197. +#endif
  2198. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/bcm2708.c linux-rpi/arch/arm/mach-bcm2708/bcm2708.c
  2199. --- linux-3.10.37/arch/arm/mach-bcm2708/bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  2200. +++ linux-rpi/arch/arm/mach-bcm2708/bcm2708.c 2014-04-24 15:35:00.773527891 +0200
  2201. @@ -0,0 +1,1036 @@
  2202. +/*
  2203. + * linux/arch/arm/mach-bcm2708/bcm2708.c
  2204. + *
  2205. + * Copyright (C) 2010 Broadcom
  2206. + *
  2207. + * This program is free software; you can redistribute it and/or modify
  2208. + * it under the terms of the GNU General Public License as published by
  2209. + * the Free Software Foundation; either version 2 of the License, or
  2210. + * (at your option) any later version.
  2211. + *
  2212. + * This program is distributed in the hope that it will be useful,
  2213. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2214. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2215. + * GNU General Public License for more details.
  2216. + *
  2217. + * You should have received a copy of the GNU General Public License
  2218. + * along with this program; if not, write to the Free Software
  2219. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2220. + */
  2221. +
  2222. +#include <linux/init.h>
  2223. +#include <linux/device.h>
  2224. +#include <linux/dma-mapping.h>
  2225. +#include <linux/serial_8250.h>
  2226. +#include <linux/platform_device.h>
  2227. +#include <linux/syscore_ops.h>
  2228. +#include <linux/interrupt.h>
  2229. +#include <linux/amba/bus.h>
  2230. +#include <linux/amba/clcd.h>
  2231. +#include <linux/clockchips.h>
  2232. +#include <linux/cnt32_to_63.h>
  2233. +#include <linux/io.h>
  2234. +#include <linux/module.h>
  2235. +#include <linux/spi/spi.h>
  2236. +#include <linux/w1-gpio.h>
  2237. +
  2238. +#include <linux/version.h>
  2239. +#include <linux/clkdev.h>
  2240. +#include <asm/system.h>
  2241. +#include <mach/hardware.h>
  2242. +#include <asm/irq.h>
  2243. +#include <linux/leds.h>
  2244. +#include <asm/mach-types.h>
  2245. +#include <asm/sched_clock.h>
  2246. +
  2247. +#include <asm/mach/arch.h>
  2248. +#include <asm/mach/flash.h>
  2249. +#include <asm/mach/irq.h>
  2250. +#include <asm/mach/time.h>
  2251. +#include <asm/mach/map.h>
  2252. +
  2253. +#include <mach/timex.h>
  2254. +#include <mach/dma.h>
  2255. +#include <mach/vcio.h>
  2256. +#include <mach/system.h>
  2257. +
  2258. +#include <linux/delay.h>
  2259. +
  2260. +#include "bcm2708.h"
  2261. +#include "armctrl.h"
  2262. +#include "clock.h"
  2263. +
  2264. +#ifdef CONFIG_BCM_VC_CMA
  2265. +#include <linux/broadcom/vc_cma.h>
  2266. +#endif
  2267. +
  2268. +
  2269. +/* Effectively we have an IOMMU (ARM<->VideoCore map) that is set up to
  2270. + * give us IO access only to 64Mbytes of physical memory (26 bits). We could
  2271. + * represent this window by setting our dmamasks to 26 bits but, in fact
  2272. + * we're not going to use addresses outside this range (they're not in real
  2273. + * memory) so we don't bother.
  2274. + *
  2275. + * In the future we might include code to use this IOMMU to remap other
  2276. + * physical addresses onto VideoCore memory then the use of 32-bits would be
  2277. + * more legitimate.
  2278. + */
  2279. +#define DMA_MASK_BITS_COMMON 32
  2280. +
  2281. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  2282. +#define W1_GPIO 4
  2283. +
  2284. +/* command line parameters */
  2285. +static unsigned boardrev, serial;
  2286. +static unsigned uart_clock;
  2287. +static unsigned disk_led_gpio = 16;
  2288. +static unsigned disk_led_active_low = 1;
  2289. +static unsigned reboot_part = 0;
  2290. +static unsigned w1_gpio_pin = W1_GPIO;
  2291. +
  2292. +static void __init bcm2708_init_led(void);
  2293. +
  2294. +void __init bcm2708_init_irq(void)
  2295. +{
  2296. + armctrl_init(__io_address(ARMCTRL_IC_BASE), 0, 0, 0);
  2297. +}
  2298. +
  2299. +static struct map_desc bcm2708_io_desc[] __initdata = {
  2300. + {
  2301. + .virtual = IO_ADDRESS(ARMCTRL_BASE),
  2302. + .pfn = __phys_to_pfn(ARMCTRL_BASE),
  2303. + .length = SZ_4K,
  2304. + .type = MT_DEVICE},
  2305. + {
  2306. + .virtual = IO_ADDRESS(UART0_BASE),
  2307. + .pfn = __phys_to_pfn(UART0_BASE),
  2308. + .length = SZ_4K,
  2309. + .type = MT_DEVICE},
  2310. + {
  2311. + .virtual = IO_ADDRESS(UART1_BASE),
  2312. + .pfn = __phys_to_pfn(UART1_BASE),
  2313. + .length = SZ_4K,
  2314. + .type = MT_DEVICE},
  2315. + {
  2316. + .virtual = IO_ADDRESS(DMA_BASE),
  2317. + .pfn = __phys_to_pfn(DMA_BASE),
  2318. + .length = SZ_4K,
  2319. + .type = MT_DEVICE},
  2320. + {
  2321. + .virtual = IO_ADDRESS(MCORE_BASE),
  2322. + .pfn = __phys_to_pfn(MCORE_BASE),
  2323. + .length = SZ_4K,
  2324. + .type = MT_DEVICE},
  2325. + {
  2326. + .virtual = IO_ADDRESS(ST_BASE),
  2327. + .pfn = __phys_to_pfn(ST_BASE),
  2328. + .length = SZ_4K,
  2329. + .type = MT_DEVICE},
  2330. + {
  2331. + .virtual = IO_ADDRESS(USB_BASE),
  2332. + .pfn = __phys_to_pfn(USB_BASE),
  2333. + .length = SZ_128K,
  2334. + .type = MT_DEVICE},
  2335. + {
  2336. + .virtual = IO_ADDRESS(PM_BASE),
  2337. + .pfn = __phys_to_pfn(PM_BASE),
  2338. + .length = SZ_4K,
  2339. + .type = MT_DEVICE},
  2340. + {
  2341. + .virtual = IO_ADDRESS(GPIO_BASE),
  2342. + .pfn = __phys_to_pfn(GPIO_BASE),
  2343. + .length = SZ_4K,
  2344. + .type = MT_DEVICE}
  2345. +};
  2346. +
  2347. +void __init bcm2708_map_io(void)
  2348. +{
  2349. + iotable_init(bcm2708_io_desc, ARRAY_SIZE(bcm2708_io_desc));
  2350. +}
  2351. +
  2352. +/* The STC is a free running counter that increments at the rate of 1MHz */
  2353. +#define STC_FREQ_HZ 1000000
  2354. +
  2355. +static inline uint32_t timer_read(void)
  2356. +{
  2357. + /* STC: a free running counter that increments at the rate of 1MHz */
  2358. + return readl(__io_address(ST_BASE + 0x04));
  2359. +}
  2360. +
  2361. +static unsigned long bcm2708_read_current_timer(void)
  2362. +{
  2363. + return timer_read();
  2364. +}
  2365. +
  2366. +static u32 notrace bcm2708_read_sched_clock(void)
  2367. +{
  2368. + return timer_read();
  2369. +}
  2370. +
  2371. +static cycle_t clksrc_read(struct clocksource *cs)
  2372. +{
  2373. + return timer_read();
  2374. +}
  2375. +
  2376. +static struct clocksource clocksource_stc = {
  2377. + .name = "stc",
  2378. + .rating = 300,
  2379. + .read = clksrc_read,
  2380. + .mask = CLOCKSOURCE_MASK(32),
  2381. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  2382. +};
  2383. +
  2384. +unsigned long frc_clock_ticks32(void)
  2385. +{
  2386. + return timer_read();
  2387. +}
  2388. +
  2389. +static void __init bcm2708_clocksource_init(void)
  2390. +{
  2391. + if (clocksource_register_hz(&clocksource_stc, STC_FREQ_HZ)) {
  2392. + printk(KERN_ERR "timer: failed to initialize clock "
  2393. + "source %s\n", clocksource_stc.name);
  2394. + }
  2395. +}
  2396. +
  2397. +
  2398. +/*
  2399. + * These are fixed clocks.
  2400. + */
  2401. +static struct clk ref24_clk = {
  2402. + .rate = UART0_CLOCK, /* The UART is clocked at 3MHz via APB_CLK */
  2403. +};
  2404. +
  2405. +static struct clk osc_clk = {
  2406. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  2407. + .rate = 27000000,
  2408. +#else
  2409. + .rate = 500000000, /* ARM clock is set from the VideoCore booter */
  2410. +#endif
  2411. +};
  2412. +
  2413. +/* warning - the USB needs a clock > 34MHz */
  2414. +
  2415. +static struct clk sdhost_clk = {
  2416. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  2417. + .rate = 4000000, /* 4MHz */
  2418. +#else
  2419. + .rate = 250000000, /* 250MHz */
  2420. +#endif
  2421. +};
  2422. +
  2423. +static struct clk_lookup lookups[] = {
  2424. + { /* UART0 */
  2425. + .dev_id = "dev:f1",
  2426. + .clk = &ref24_clk,
  2427. + },
  2428. + { /* USB */
  2429. + .dev_id = "bcm2708_usb",
  2430. + .clk = &osc_clk,
  2431. + }, { /* SPI */
  2432. + .dev_id = "bcm2708_spi.0",
  2433. + .clk = &sdhost_clk,
  2434. + }, { /* BSC0 */
  2435. + .dev_id = "bcm2708_i2c.0",
  2436. + .clk = &sdhost_clk,
  2437. + }, { /* BSC1 */
  2438. + .dev_id = "bcm2708_i2c.1",
  2439. + .clk = &sdhost_clk,
  2440. + }
  2441. +};
  2442. +
  2443. +#define UART0_IRQ { IRQ_UART, 0 /*NO_IRQ*/ }
  2444. +#define UART0_DMA { 15, 14 }
  2445. +
  2446. +AMBA_DEVICE(uart0, "dev:f1", UART0, NULL);
  2447. +
  2448. +static struct amba_device *amba_devs[] __initdata = {
  2449. + &uart0_device,
  2450. +};
  2451. +
  2452. +static struct resource bcm2708_dmaman_resources[] = {
  2453. + {
  2454. + .start = DMA_BASE,
  2455. + .end = DMA_BASE + SZ_4K - 1,
  2456. + .flags = IORESOURCE_MEM,
  2457. + }
  2458. +};
  2459. +
  2460. +static struct platform_device bcm2708_dmaman_device = {
  2461. + .name = BCM_DMAMAN_DRIVER_NAME,
  2462. + .id = 0, /* first bcm2708_dma */
  2463. + .resource = bcm2708_dmaman_resources,
  2464. + .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  2465. +};
  2466. +
  2467. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  2468. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  2469. + .pin = W1_GPIO,
  2470. + .is_open_drain = 0,
  2471. +};
  2472. +
  2473. +static struct platform_device w1_device = {
  2474. + .name = "w1-gpio",
  2475. + .id = -1,
  2476. + .dev.platform_data = &w1_gpio_pdata,
  2477. +};
  2478. +#endif
  2479. +
  2480. +static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2481. +
  2482. +static struct platform_device bcm2708_fb_device = {
  2483. + .name = "bcm2708_fb",
  2484. + .id = -1, /* only one bcm2708_fb */
  2485. + .resource = NULL,
  2486. + .num_resources = 0,
  2487. + .dev = {
  2488. + .dma_mask = &fb_dmamask,
  2489. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2490. + },
  2491. +};
  2492. +
  2493. +static struct plat_serial8250_port bcm2708_uart1_platform_data[] = {
  2494. + {
  2495. + .mapbase = UART1_BASE + 0x40,
  2496. + .irq = IRQ_AUX,
  2497. + .uartclk = 125000000,
  2498. + .regshift = 2,
  2499. + .iotype = UPIO_MEM,
  2500. + .flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_SKIP_TEST,
  2501. + .type = PORT_8250,
  2502. + },
  2503. + {},
  2504. +};
  2505. +
  2506. +static struct platform_device bcm2708_uart1_device = {
  2507. + .name = "serial8250",
  2508. + .id = PLAT8250_DEV_PLATFORM,
  2509. + .dev = {
  2510. + .platform_data = bcm2708_uart1_platform_data,
  2511. + },
  2512. +};
  2513. +
  2514. +static struct resource bcm2708_usb_resources[] = {
  2515. + [0] = {
  2516. + .start = USB_BASE,
  2517. + .end = USB_BASE + SZ_128K - 1,
  2518. + .flags = IORESOURCE_MEM,
  2519. + },
  2520. + [1] = {
  2521. + .start = MPHI_BASE,
  2522. + .end = MPHI_BASE + SZ_4K - 1,
  2523. + .flags = IORESOURCE_MEM,
  2524. + },
  2525. + [2] = {
  2526. + .start = IRQ_HOSTPORT,
  2527. + .end = IRQ_HOSTPORT,
  2528. + .flags = IORESOURCE_IRQ,
  2529. + },
  2530. +};
  2531. +
  2532. +bool fiq_fix_enable = true;
  2533. +
  2534. +static struct resource bcm2708_usb_resources_no_fiq_fix[] = {
  2535. + [0] = {
  2536. + .start = USB_BASE,
  2537. + .end = USB_BASE + SZ_128K - 1,
  2538. + .flags = IORESOURCE_MEM,
  2539. + },
  2540. + [1] = {
  2541. + .start = IRQ_USB,
  2542. + .end = IRQ_USB,
  2543. + .flags = IORESOURCE_IRQ,
  2544. + },
  2545. +};
  2546. +
  2547. +static u64 usb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2548. +
  2549. +static struct platform_device bcm2708_usb_device = {
  2550. + .name = "bcm2708_usb",
  2551. + .id = -1, /* only one bcm2708_usb */
  2552. + .resource = bcm2708_usb_resources,
  2553. + .num_resources = ARRAY_SIZE(bcm2708_usb_resources),
  2554. + .dev = {
  2555. + .dma_mask = &usb_dmamask,
  2556. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2557. + },
  2558. +};
  2559. +
  2560. +static struct resource bcm2708_vcio_resources[] = {
  2561. + [0] = { /* mailbox/semaphore/doorbell access */
  2562. + .start = MCORE_BASE,
  2563. + .end = MCORE_BASE + SZ_4K - 1,
  2564. + .flags = IORESOURCE_MEM,
  2565. + },
  2566. +};
  2567. +
  2568. +static u64 vcio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2569. +
  2570. +static struct platform_device bcm2708_vcio_device = {
  2571. + .name = BCM_VCIO_DRIVER_NAME,
  2572. + .id = -1, /* only one VideoCore I/O area */
  2573. + .resource = bcm2708_vcio_resources,
  2574. + .num_resources = ARRAY_SIZE(bcm2708_vcio_resources),
  2575. + .dev = {
  2576. + .dma_mask = &vcio_dmamask,
  2577. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2578. + },
  2579. +};
  2580. +
  2581. +#ifdef CONFIG_BCM2708_GPIO
  2582. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  2583. +
  2584. +static struct resource bcm2708_gpio_resources[] = {
  2585. + [0] = { /* general purpose I/O */
  2586. + .start = GPIO_BASE,
  2587. + .end = GPIO_BASE + SZ_4K - 1,
  2588. + .flags = IORESOURCE_MEM,
  2589. + },
  2590. +};
  2591. +
  2592. +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2593. +
  2594. +static struct platform_device bcm2708_gpio_device = {
  2595. + .name = BCM_GPIO_DRIVER_NAME,
  2596. + .id = -1, /* only one VideoCore I/O area */
  2597. + .resource = bcm2708_gpio_resources,
  2598. + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
  2599. + .dev = {
  2600. + .dma_mask = &gpio_dmamask,
  2601. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2602. + },
  2603. +};
  2604. +#endif
  2605. +
  2606. +static struct resource bcm2708_systemtimer_resources[] = {
  2607. + [0] = { /* system timer access */
  2608. + .start = ST_BASE,
  2609. + .end = ST_BASE + SZ_4K - 1,
  2610. + .flags = IORESOURCE_MEM,
  2611. + },
  2612. + {
  2613. + .start = IRQ_TIMER3,
  2614. + .end = IRQ_TIMER3,
  2615. + .flags = IORESOURCE_IRQ,
  2616. + }
  2617. +
  2618. +};
  2619. +
  2620. +static u64 systemtimer_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2621. +
  2622. +static struct platform_device bcm2708_systemtimer_device = {
  2623. + .name = "bcm2708_systemtimer",
  2624. + .id = -1, /* only one VideoCore I/O area */
  2625. + .resource = bcm2708_systemtimer_resources,
  2626. + .num_resources = ARRAY_SIZE(bcm2708_systemtimer_resources),
  2627. + .dev = {
  2628. + .dma_mask = &systemtimer_dmamask,
  2629. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2630. + },
  2631. +};
  2632. +
  2633. +#ifdef CONFIG_MMC_SDHCI_BCM2708 /* Arasan emmc SD */
  2634. +static struct resource bcm2708_emmc_resources[] = {
  2635. + [0] = {
  2636. + .start = EMMC_BASE,
  2637. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  2638. + /* the memory map actually makes SZ_4K available */
  2639. + .flags = IORESOURCE_MEM,
  2640. + },
  2641. + [1] = {
  2642. + .start = IRQ_ARASANSDIO,
  2643. + .end = IRQ_ARASANSDIO,
  2644. + .flags = IORESOURCE_IRQ,
  2645. + },
  2646. +};
  2647. +
  2648. +static u64 bcm2708_emmc_dmamask = 0xffffffffUL;
  2649. +
  2650. +struct platform_device bcm2708_emmc_device = {
  2651. + .name = "bcm2708_sdhci",
  2652. + .id = 0,
  2653. + .num_resources = ARRAY_SIZE(bcm2708_emmc_resources),
  2654. + .resource = bcm2708_emmc_resources,
  2655. + .dev = {
  2656. + .dma_mask = &bcm2708_emmc_dmamask,
  2657. + .coherent_dma_mask = 0xffffffffUL},
  2658. +};
  2659. +#endif /* CONFIG_MMC_SDHCI_BCM2708 */
  2660. +
  2661. +static struct resource bcm2708_powerman_resources[] = {
  2662. + [0] = {
  2663. + .start = PM_BASE,
  2664. + .end = PM_BASE + SZ_256 - 1,
  2665. + .flags = IORESOURCE_MEM,
  2666. + },
  2667. +};
  2668. +
  2669. +static u64 powerman_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2670. +
  2671. +struct platform_device bcm2708_powerman_device = {
  2672. + .name = "bcm2708_powerman",
  2673. + .id = 0,
  2674. + .num_resources = ARRAY_SIZE(bcm2708_powerman_resources),
  2675. + .resource = bcm2708_powerman_resources,
  2676. + .dev = {
  2677. + .dma_mask = &powerman_dmamask,
  2678. + .coherent_dma_mask = 0xffffffffUL},
  2679. +};
  2680. +
  2681. +
  2682. +static struct platform_device bcm2708_alsa_devices[] = {
  2683. + [0] = {
  2684. + .name = "bcm2835_AUD0",
  2685. + .id = 0, /* first audio device */
  2686. + .resource = 0,
  2687. + .num_resources = 0,
  2688. + },
  2689. + [1] = {
  2690. + .name = "bcm2835_AUD1",
  2691. + .id = 1, /* second audio device */
  2692. + .resource = 0,
  2693. + .num_resources = 0,
  2694. + },
  2695. + [2] = {
  2696. + .name = "bcm2835_AUD2",
  2697. + .id = 2, /* third audio device */
  2698. + .resource = 0,
  2699. + .num_resources = 0,
  2700. + },
  2701. + [3] = {
  2702. + .name = "bcm2835_AUD3",
  2703. + .id = 3, /* forth audio device */
  2704. + .resource = 0,
  2705. + .num_resources = 0,
  2706. + },
  2707. + [4] = {
  2708. + .name = "bcm2835_AUD4",
  2709. + .id = 4, /* fifth audio device */
  2710. + .resource = 0,
  2711. + .num_resources = 0,
  2712. + },
  2713. + [5] = {
  2714. + .name = "bcm2835_AUD5",
  2715. + .id = 5, /* sixth audio device */
  2716. + .resource = 0,
  2717. + .num_resources = 0,
  2718. + },
  2719. + [6] = {
  2720. + .name = "bcm2835_AUD6",
  2721. + .id = 6, /* seventh audio device */
  2722. + .resource = 0,
  2723. + .num_resources = 0,
  2724. + },
  2725. + [7] = {
  2726. + .name = "bcm2835_AUD7",
  2727. + .id = 7, /* eighth audio device */
  2728. + .resource = 0,
  2729. + .num_resources = 0,
  2730. + },
  2731. +};
  2732. +
  2733. +static struct resource bcm2708_spi_resources[] = {
  2734. + {
  2735. + .start = SPI0_BASE,
  2736. + .end = SPI0_BASE + SZ_256 - 1,
  2737. + .flags = IORESOURCE_MEM,
  2738. + }, {
  2739. + .start = IRQ_SPI,
  2740. + .end = IRQ_SPI,
  2741. + .flags = IORESOURCE_IRQ,
  2742. + }
  2743. +};
  2744. +
  2745. +
  2746. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2747. +static struct platform_device bcm2708_spi_device = {
  2748. + .name = "bcm2708_spi",
  2749. + .id = 0,
  2750. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  2751. + .resource = bcm2708_spi_resources,
  2752. + .dev = {
  2753. + .dma_mask = &bcm2708_spi_dmamask,
  2754. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  2755. +};
  2756. +
  2757. +#ifdef CONFIG_BCM2708_SPIDEV
  2758. +static struct spi_board_info bcm2708_spi_devices[] = {
  2759. +#ifdef CONFIG_SPI_SPIDEV
  2760. + {
  2761. + .modalias = "spidev",
  2762. + .max_speed_hz = 500000,
  2763. + .bus_num = 0,
  2764. + .chip_select = 0,
  2765. + .mode = SPI_MODE_0,
  2766. + }, {
  2767. + .modalias = "spidev",
  2768. + .max_speed_hz = 500000,
  2769. + .bus_num = 0,
  2770. + .chip_select = 1,
  2771. + .mode = SPI_MODE_0,
  2772. + }
  2773. +#endif
  2774. +};
  2775. +#endif
  2776. +
  2777. +static struct resource bcm2708_bsc0_resources[] = {
  2778. + {
  2779. + .start = BSC0_BASE,
  2780. + .end = BSC0_BASE + SZ_256 - 1,
  2781. + .flags = IORESOURCE_MEM,
  2782. + }, {
  2783. + .start = INTERRUPT_I2C,
  2784. + .end = INTERRUPT_I2C,
  2785. + .flags = IORESOURCE_IRQ,
  2786. + }
  2787. +};
  2788. +
  2789. +static struct platform_device bcm2708_bsc0_device = {
  2790. + .name = "bcm2708_i2c",
  2791. + .id = 0,
  2792. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  2793. + .resource = bcm2708_bsc0_resources,
  2794. +};
  2795. +
  2796. +
  2797. +static struct resource bcm2708_bsc1_resources[] = {
  2798. + {
  2799. + .start = BSC1_BASE,
  2800. + .end = BSC1_BASE + SZ_256 - 1,
  2801. + .flags = IORESOURCE_MEM,
  2802. + }, {
  2803. + .start = INTERRUPT_I2C,
  2804. + .end = INTERRUPT_I2C,
  2805. + .flags = IORESOURCE_IRQ,
  2806. + }
  2807. +};
  2808. +
  2809. +static struct platform_device bcm2708_bsc1_device = {
  2810. + .name = "bcm2708_i2c",
  2811. + .id = 1,
  2812. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  2813. + .resource = bcm2708_bsc1_resources,
  2814. +};
  2815. +
  2816. +static struct platform_device bcm2835_hwmon_device = {
  2817. + .name = "bcm2835_hwmon",
  2818. +};
  2819. +
  2820. +static struct platform_device bcm2835_thermal_device = {
  2821. + .name = "bcm2835_thermal",
  2822. +};
  2823. +
  2824. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  2825. +static struct resource bcm2708_i2s_resources[] = {
  2826. + {
  2827. + .start = I2S_BASE,
  2828. + .end = I2S_BASE + 0x20,
  2829. + .flags = IORESOURCE_MEM,
  2830. + },
  2831. + {
  2832. + .start = PCM_CLOCK_BASE,
  2833. + .end = PCM_CLOCK_BASE + 0x02,
  2834. + .flags = IORESOURCE_MEM,
  2835. + }
  2836. +};
  2837. +
  2838. +static struct platform_device bcm2708_i2s_device = {
  2839. + .name = "bcm2708-i2s",
  2840. + .id = 0,
  2841. + .num_resources = ARRAY_SIZE(bcm2708_i2s_resources),
  2842. + .resource = bcm2708_i2s_resources,
  2843. +};
  2844. +#endif
  2845. +
  2846. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  2847. +static struct platform_device snd_hifiberry_dac_device = {
  2848. + .name = "snd-hifiberry-dac",
  2849. + .id = 0,
  2850. + .num_resources = 0,
  2851. +};
  2852. +
  2853. +static struct platform_device snd_pcm5102a_codec_device = {
  2854. + .name = "pcm5102a-codec",
  2855. + .id = -1,
  2856. + .num_resources = 0,
  2857. +};
  2858. +#endif
  2859. +
  2860. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  2861. +static struct platform_device snd_hifiberry_digi_device = {
  2862. + .name = "snd-hifiberry-digi",
  2863. + .id = 0,
  2864. + .num_resources = 0,
  2865. +};
  2866. +
  2867. +static struct i2c_board_info __initdata snd_wm8804_i2c_devices[] = {
  2868. + {
  2869. + I2C_BOARD_INFO("wm8804", 0x3b)
  2870. + },
  2871. +};
  2872. +
  2873. +#endif
  2874. +
  2875. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  2876. +static struct platform_device snd_rpi_dac_device = {
  2877. + .name = "snd-rpi-dac",
  2878. + .id = 0,
  2879. + .num_resources = 0,
  2880. +};
  2881. +
  2882. +static struct platform_device snd_pcm1794a_codec_device = {
  2883. + .name = "pcm1794a-codec",
  2884. + .id = -1,
  2885. + .num_resources = 0,
  2886. +};
  2887. +#endif
  2888. +
  2889. +
  2890. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  2891. +static struct platform_device snd_rpi_iqaudio_dac_device = {
  2892. + .name = "snd-rpi-iqaudio-dac",
  2893. + .id = 0,
  2894. + .num_resources = 0,
  2895. +};
  2896. +
  2897. +// Use the actual device name rather than generic driver name
  2898. +static struct i2c_board_info __initdata snd_pcm512x_i2c_devices[] = {
  2899. + {
  2900. + I2C_BOARD_INFO("pcm5122", 0x4c)
  2901. + },
  2902. +};
  2903. +#endif
  2904. +
  2905. +int __init bcm_register_device(struct platform_device *pdev)
  2906. +{
  2907. + int ret;
  2908. +
  2909. + ret = platform_device_register(pdev);
  2910. + if (ret)
  2911. + pr_debug("Unable to register platform device '%s': %d\n",
  2912. + pdev->name, ret);
  2913. +
  2914. + return ret;
  2915. +}
  2916. +
  2917. +int calc_rsts(int partition)
  2918. +{
  2919. + return PM_PASSWORD |
  2920. + ((partition & (1 << 0)) << 0) |
  2921. + ((partition & (1 << 1)) << 1) |
  2922. + ((partition & (1 << 2)) << 2) |
  2923. + ((partition & (1 << 3)) << 3) |
  2924. + ((partition & (1 << 4)) << 4) |
  2925. + ((partition & (1 << 5)) << 5);
  2926. +}
  2927. +
  2928. +static void bcm2708_restart(char mode, const char *cmd)
  2929. +{
  2930. + uint32_t pm_rstc, pm_wdog;
  2931. + uint32_t timeout = 10;
  2932. + uint32_t pm_rsts = 0;
  2933. +
  2934. + if(mode == 'q')
  2935. + {
  2936. + // NOOBS < 1.3 booting with reboot=q
  2937. + pm_rsts = readl(__io_address(PM_RSTS));
  2938. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRQ_SET;
  2939. + }
  2940. + else if(mode == 'p')
  2941. + {
  2942. + // NOOBS < 1.3 halting
  2943. + pm_rsts = readl(__io_address(PM_RSTS));
  2944. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRH_SET;
  2945. + }
  2946. + else
  2947. + {
  2948. + pm_rsts = calc_rsts(reboot_part);
  2949. + }
  2950. +
  2951. + writel(pm_rsts, __io_address(PM_RSTS));
  2952. +
  2953. + /* Setup watchdog for reset */
  2954. + pm_rstc = readl(__io_address(PM_RSTC));
  2955. +
  2956. + pm_wdog = PM_PASSWORD | (timeout & PM_WDOG_TIME_SET); // watchdog timer = timer clock / 16; need password (31:16) + value (11:0)
  2957. + pm_rstc = PM_PASSWORD | (pm_rstc & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  2958. +
  2959. + writel(pm_wdog, __io_address(PM_WDOG));
  2960. + writel(pm_rstc, __io_address(PM_RSTC));
  2961. +}
  2962. +
  2963. +/* We can't really power off, but if we do the normal reset scheme, and indicate to bootcode.bin not to reboot, then most of the chip will be powered off */
  2964. +static void bcm2708_power_off(void)
  2965. +{
  2966. + extern char reboot_mode;
  2967. +
  2968. + if(reboot_mode == 'q')
  2969. + {
  2970. + // NOOBS < v1.3
  2971. + bcm2708_restart('p', "");
  2972. + }
  2973. + else
  2974. + {
  2975. + /* partition 63 is special code for HALT the bootloader knows not to boot*/
  2976. + reboot_part = 63;
  2977. + /* continue with normal reset mechanism */
  2978. + bcm2708_restart(0, "");
  2979. + }
  2980. +}
  2981. +
  2982. +void __init bcm2708_init(void)
  2983. +{
  2984. + int i;
  2985. +
  2986. +#if defined(CONFIG_BCM_VC_CMA)
  2987. + vc_cma_early_init();
  2988. +#endif
  2989. + printk("bcm2708.uart_clock = %d\n", uart_clock);
  2990. + pm_power_off = bcm2708_power_off;
  2991. +
  2992. + if (uart_clock)
  2993. + lookups[0].clk->rate = uart_clock;
  2994. +
  2995. + for (i = 0; i < ARRAY_SIZE(lookups); i++)
  2996. + clkdev_add(&lookups[i]);
  2997. +
  2998. + bcm_register_device(&bcm2708_dmaman_device);
  2999. + bcm_register_device(&bcm2708_vcio_device);
  3000. +#ifdef CONFIG_BCM2708_GPIO
  3001. + bcm_register_device(&bcm2708_gpio_device);
  3002. +#endif
  3003. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  3004. + w1_gpio_pdata.pin = w1_gpio_pin;
  3005. + platform_device_register(&w1_device);
  3006. +#endif
  3007. + bcm_register_device(&bcm2708_systemtimer_device);
  3008. + bcm_register_device(&bcm2708_fb_device);
  3009. + if (!fiq_fix_enable)
  3010. + {
  3011. + bcm2708_usb_device.resource = bcm2708_usb_resources_no_fiq_fix;
  3012. + bcm2708_usb_device.num_resources = ARRAY_SIZE(bcm2708_usb_resources_no_fiq_fix);
  3013. + }
  3014. + bcm_register_device(&bcm2708_usb_device);
  3015. + bcm_register_device(&bcm2708_uart1_device);
  3016. + bcm_register_device(&bcm2708_powerman_device);
  3017. +
  3018. +#ifdef CONFIG_MMC_SDHCI_BCM2708
  3019. + bcm_register_device(&bcm2708_emmc_device);
  3020. +#endif
  3021. + bcm2708_init_led();
  3022. + for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  3023. + bcm_register_device(&bcm2708_alsa_devices[i]);
  3024. +
  3025. + bcm_register_device(&bcm2708_spi_device);
  3026. + bcm_register_device(&bcm2708_bsc0_device);
  3027. + bcm_register_device(&bcm2708_bsc1_device);
  3028. +
  3029. + bcm_register_device(&bcm2835_hwmon_device);
  3030. + bcm_register_device(&bcm2835_thermal_device);
  3031. +
  3032. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3033. + bcm_register_device(&bcm2708_i2s_device);
  3034. +#endif
  3035. +
  3036. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3037. + bcm_register_device(&snd_hifiberry_dac_device);
  3038. + bcm_register_device(&snd_pcm5102a_codec_device);
  3039. +#endif
  3040. +
  3041. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3042. + bcm_register_device(&snd_hifiberry_digi_device);
  3043. + i2c_register_board_info(1, snd_wm8804_i2c_devices, ARRAY_SIZE(snd_wm8804_i2c_devices));
  3044. +#endif
  3045. +
  3046. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3047. + bcm_register_device(&snd_rpi_dac_device);
  3048. + bcm_register_device(&snd_pcm1794a_codec_device);
  3049. +#endif
  3050. +
  3051. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  3052. + bcm_register_device(&snd_rpi_iqaudio_dac_device);
  3053. + i2c_register_board_info(1, snd_pcm512x_i2c_devices, ARRAY_SIZE(snd_pcm512x_i2c_devices));
  3054. +#endif
  3055. +
  3056. +
  3057. + for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  3058. + struct amba_device *d = amba_devs[i];
  3059. + amba_device_register(d, &iomem_resource);
  3060. + }
  3061. + system_rev = boardrev;
  3062. + system_serial_low = serial;
  3063. +
  3064. +#ifdef CONFIG_BCM2708_SPIDEV
  3065. + spi_register_board_info(bcm2708_spi_devices,
  3066. + ARRAY_SIZE(bcm2708_spi_devices));
  3067. +#endif
  3068. +}
  3069. +
  3070. +static void timer_set_mode(enum clock_event_mode mode,
  3071. + struct clock_event_device *clk)
  3072. +{
  3073. + switch (mode) {
  3074. + case CLOCK_EVT_MODE_ONESHOT: /* Leave the timer disabled, .set_next_event will enable it */
  3075. + case CLOCK_EVT_MODE_SHUTDOWN:
  3076. + break;
  3077. + case CLOCK_EVT_MODE_PERIODIC:
  3078. +
  3079. + case CLOCK_EVT_MODE_UNUSED:
  3080. + case CLOCK_EVT_MODE_RESUME:
  3081. +
  3082. + default:
  3083. + printk(KERN_ERR "timer_set_mode: unhandled mode:%d\n",
  3084. + (int)mode);
  3085. + break;
  3086. + }
  3087. +
  3088. +}
  3089. +
  3090. +static int timer_set_next_event(unsigned long cycles,
  3091. + struct clock_event_device *unused)
  3092. +{
  3093. + unsigned long stc;
  3094. +
  3095. + stc = readl(__io_address(ST_BASE + 0x04));
  3096. + writel(stc + cycles, __io_address(ST_BASE + 0x18)); /* stc3 */
  3097. + return 0;
  3098. +}
  3099. +
  3100. +static struct clock_event_device timer0_clockevent = {
  3101. + .name = "timer0",
  3102. + .shift = 32,
  3103. + .features = CLOCK_EVT_FEAT_ONESHOT,
  3104. + .set_mode = timer_set_mode,
  3105. + .set_next_event = timer_set_next_event,
  3106. +};
  3107. +
  3108. +/*
  3109. + * IRQ handler for the timer
  3110. + */
  3111. +static irqreturn_t bcm2708_timer_interrupt(int irq, void *dev_id)
  3112. +{
  3113. + struct clock_event_device *evt = &timer0_clockevent;
  3114. +
  3115. + writel(1 << 3, __io_address(ST_BASE + 0x00)); /* stcs clear timer int */
  3116. +
  3117. + evt->event_handler(evt);
  3118. +
  3119. + return IRQ_HANDLED;
  3120. +}
  3121. +
  3122. +static struct irqaction bcm2708_timer_irq = {
  3123. + .name = "BCM2708 Timer Tick",
  3124. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  3125. + .handler = bcm2708_timer_interrupt,
  3126. +};
  3127. +
  3128. +/*
  3129. + * Set up timer interrupt, and return the current time in seconds.
  3130. + */
  3131. +
  3132. +static struct delay_timer bcm2708_delay_timer = {
  3133. + .read_current_timer = bcm2708_read_current_timer,
  3134. + .freq = STC_FREQ_HZ,
  3135. +};
  3136. +
  3137. +static void __init bcm2708_timer_init(void)
  3138. +{
  3139. + /* init high res timer */
  3140. + bcm2708_clocksource_init();
  3141. +
  3142. + /*
  3143. + * Initialise to a known state (all timers off)
  3144. + */
  3145. + writel(0, __io_address(ARM_T_CONTROL));
  3146. + /*
  3147. + * Make irqs happen for the system timer
  3148. + */
  3149. + setup_irq(IRQ_TIMER3, &bcm2708_timer_irq);
  3150. +
  3151. + setup_sched_clock(bcm2708_read_sched_clock, 32, STC_FREQ_HZ);
  3152. +
  3153. + timer0_clockevent.mult =
  3154. + div_sc(STC_FREQ_HZ, NSEC_PER_SEC, timer0_clockevent.shift);
  3155. + timer0_clockevent.max_delta_ns =
  3156. + clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  3157. + timer0_clockevent.min_delta_ns =
  3158. + clockevent_delta2ns(0xf, &timer0_clockevent);
  3159. +
  3160. + timer0_clockevent.cpumask = cpumask_of(0);
  3161. + clockevents_register_device(&timer0_clockevent);
  3162. +
  3163. + register_current_timer_delay(&bcm2708_delay_timer);
  3164. +}
  3165. +
  3166. +#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  3167. +#include <linux/leds.h>
  3168. +
  3169. +static struct gpio_led bcm2708_leds[] = {
  3170. + [0] = {
  3171. + .gpio = 16,
  3172. + .name = "led0",
  3173. + .default_trigger = "mmc0",
  3174. + .active_low = 1,
  3175. + },
  3176. +};
  3177. +
  3178. +static struct gpio_led_platform_data bcm2708_led_pdata = {
  3179. + .num_leds = ARRAY_SIZE(bcm2708_leds),
  3180. + .leds = bcm2708_leds,
  3181. +};
  3182. +
  3183. +static struct platform_device bcm2708_led_device = {
  3184. + .name = "leds-gpio",
  3185. + .id = -1,
  3186. + .dev = {
  3187. + .platform_data = &bcm2708_led_pdata,
  3188. + },
  3189. +};
  3190. +
  3191. +static void __init bcm2708_init_led(void)
  3192. +{
  3193. + bcm2708_leds[0].gpio = disk_led_gpio;
  3194. + bcm2708_leds[0].active_low = disk_led_active_low;
  3195. + platform_device_register(&bcm2708_led_device);
  3196. +}
  3197. +#else
  3198. +static inline void bcm2708_init_led(void)
  3199. +{
  3200. +}
  3201. +#endif
  3202. +
  3203. +void __init bcm2708_init_early(void)
  3204. +{
  3205. + /*
  3206. + * Some devices allocate their coherent buffers from atomic
  3207. + * context. Increase size of atomic coherent pool to make sure such
  3208. + * the allocations won't fail.
  3209. + */
  3210. + init_dma_coherent_pool_size(SZ_4M);
  3211. +}
  3212. +
  3213. +static void __init board_reserve(void)
  3214. +{
  3215. +#if defined(CONFIG_BCM_VC_CMA)
  3216. + vc_cma_reserve();
  3217. +#endif
  3218. +}
  3219. +
  3220. +MACHINE_START(BCM2708, "BCM2708")
  3221. + /* Maintainer: Broadcom Europe Ltd. */
  3222. + .map_io = bcm2708_map_io,
  3223. + .init_irq = bcm2708_init_irq,
  3224. + .init_time = bcm2708_timer_init,
  3225. + .init_machine = bcm2708_init,
  3226. + .init_early = bcm2708_init_early,
  3227. + .restart = bcm2708_restart,
  3228. + .reserve = board_reserve,
  3229. +MACHINE_END
  3230. +
  3231. +module_param(boardrev, uint, 0644);
  3232. +module_param(serial, uint, 0644);
  3233. +module_param(uart_clock, uint, 0644);
  3234. +module_param(disk_led_gpio, uint, 0644);
  3235. +module_param(disk_led_active_low, uint, 0644);
  3236. +module_param(reboot_part, uint, 0644);
  3237. +module_param(w1_gpio_pin, uint, 0644);
  3238. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/bcm2708_gpio.c linux-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c
  3239. --- linux-3.10.37/arch/arm/mach-bcm2708/bcm2708_gpio.c 1970-01-01 01:00:00.000000000 +0100
  3240. +++ linux-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c 2014-04-24 15:35:00.773527891 +0200
  3241. @@ -0,0 +1,361 @@
  3242. +/*
  3243. + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
  3244. + *
  3245. + * Copyright (C) 2010 Broadcom
  3246. + *
  3247. + * This program is free software; you can redistribute it and/or modify
  3248. + * it under the terms of the GNU General Public License version 2 as
  3249. + * published by the Free Software Foundation.
  3250. + *
  3251. + */
  3252. +
  3253. +#include <linux/spinlock.h>
  3254. +#include <linux/module.h>
  3255. +#include <linux/list.h>
  3256. +#include <linux/io.h>
  3257. +#include <linux/irq.h>
  3258. +#include <linux/interrupt.h>
  3259. +#include <linux/slab.h>
  3260. +#include <mach/gpio.h>
  3261. +#include <linux/gpio.h>
  3262. +#include <linux/platform_device.h>
  3263. +#include <mach/platform.h>
  3264. +
  3265. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  3266. +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
  3267. +#define BCM_GPIO_USE_IRQ 1
  3268. +
  3269. +#define GPIOFSEL(x) (0x00+(x)*4)
  3270. +#define GPIOSET(x) (0x1c+(x)*4)
  3271. +#define GPIOCLR(x) (0x28+(x)*4)
  3272. +#define GPIOLEV(x) (0x34+(x)*4)
  3273. +#define GPIOEDS(x) (0x40+(x)*4)
  3274. +#define GPIOREN(x) (0x4c+(x)*4)
  3275. +#define GPIOFEN(x) (0x58+(x)*4)
  3276. +#define GPIOHEN(x) (0x64+(x)*4)
  3277. +#define GPIOLEN(x) (0x70+(x)*4)
  3278. +#define GPIOAREN(x) (0x7c+(x)*4)
  3279. +#define GPIOAFEN(x) (0x88+(x)*4)
  3280. +#define GPIOUD(x) (0x94+(x)*4)
  3281. +#define GPIOUDCLK(x) (0x98+(x)*4)
  3282. +
  3283. +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
  3284. + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
  3285. + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
  3286. + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
  3287. +};
  3288. +
  3289. + /* Each of the two spinlocks protects a different set of hardware
  3290. + * regiters and data structurs. This decouples the code of the IRQ from
  3291. + * the GPIO code. This also makes the case of a GPIO routine call from
  3292. + * the IRQ code simpler.
  3293. + */
  3294. +static DEFINE_SPINLOCK(lock); /* GPIO registers */
  3295. +
  3296. +struct bcm2708_gpio {
  3297. + struct list_head list;
  3298. + void __iomem *base;
  3299. + struct gpio_chip gc;
  3300. + unsigned long rising;
  3301. + unsigned long falling;
  3302. + unsigned long high;
  3303. + unsigned long low;
  3304. +};
  3305. +
  3306. +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
  3307. + int function)
  3308. +{
  3309. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3310. + unsigned long flags;
  3311. + unsigned gpiodir;
  3312. + unsigned gpio_bank = offset / 10;
  3313. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  3314. +
  3315. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
  3316. + if (offset >= ARCH_NR_GPIOS)
  3317. + return -EINVAL;
  3318. +
  3319. + spin_lock_irqsave(&lock, flags);
  3320. +
  3321. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  3322. + gpiodir &= ~(7 << gpio_field_offset);
  3323. + gpiodir |= function << gpio_field_offset;
  3324. + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
  3325. + spin_unlock_irqrestore(&lock, flags);
  3326. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  3327. +
  3328. + return 0;
  3329. +}
  3330. +
  3331. +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
  3332. +{
  3333. + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
  3334. +}
  3335. +
  3336. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  3337. +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
  3338. + int value)
  3339. +{
  3340. + int ret;
  3341. + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
  3342. + if (ret >= 0)
  3343. + bcm2708_gpio_set(gc, offset, value);
  3344. + return ret;
  3345. +}
  3346. +
  3347. +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
  3348. +{
  3349. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3350. + unsigned gpio_bank = offset / 32;
  3351. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  3352. + unsigned lev;
  3353. +
  3354. + if (offset >= ARCH_NR_GPIOS)
  3355. + return 0;
  3356. + lev = readl(gpio->base + GPIOLEV(gpio_bank));
  3357. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
  3358. + return 0x1 & (lev >> gpio_field_offset);
  3359. +}
  3360. +
  3361. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  3362. +{
  3363. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3364. + unsigned gpio_bank = offset / 32;
  3365. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  3366. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
  3367. + if (offset >= ARCH_NR_GPIOS)
  3368. + return;
  3369. + if (value)
  3370. + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
  3371. + else
  3372. + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
  3373. +}
  3374. +
  3375. +/*************************************************************************************************************************
  3376. + * bcm2708 GPIO IRQ
  3377. + */
  3378. +
  3379. +#if BCM_GPIO_USE_IRQ
  3380. +
  3381. +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  3382. +{
  3383. + return gpio_to_irq(gpio);
  3384. +}
  3385. +
  3386. +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
  3387. +{
  3388. + unsigned irq = d->irq;
  3389. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3390. +
  3391. + gpio->rising &= ~(1 << irq_to_gpio(irq));
  3392. + gpio->falling &= ~(1 << irq_to_gpio(irq));
  3393. + gpio->high &= ~(1 << irq_to_gpio(irq));
  3394. + gpio->low &= ~(1 << irq_to_gpio(irq));
  3395. +
  3396. + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  3397. + return -EINVAL;
  3398. +
  3399. + if (type & IRQ_TYPE_EDGE_RISING)
  3400. + gpio->rising |= (1 << irq_to_gpio(irq));
  3401. + if (type & IRQ_TYPE_EDGE_FALLING)
  3402. + gpio->falling |= (1 << irq_to_gpio(irq));
  3403. + if (type & IRQ_TYPE_LEVEL_HIGH)
  3404. + gpio->high |= (1 << irq_to_gpio(irq));
  3405. + if (type & IRQ_TYPE_LEVEL_LOW)
  3406. + gpio->low |= (1 << irq_to_gpio(irq));
  3407. + return 0;
  3408. +}
  3409. +
  3410. +static void bcm2708_gpio_irq_mask(struct irq_data *d)
  3411. +{
  3412. + unsigned irq = d->irq;
  3413. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3414. + unsigned gn = irq_to_gpio(irq);
  3415. + unsigned gb = gn / 32;
  3416. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  3417. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  3418. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  3419. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  3420. +
  3421. + gn = gn % 32;
  3422. +
  3423. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  3424. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  3425. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  3426. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  3427. +}
  3428. +
  3429. +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
  3430. +{
  3431. + unsigned irq = d->irq;
  3432. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3433. + unsigned gn = irq_to_gpio(irq);
  3434. + unsigned gb = gn / 32;
  3435. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  3436. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  3437. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  3438. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  3439. +
  3440. + gn = gn % 32;
  3441. +
  3442. + writel(1 << gn, gpio->base + GPIOEDS(gb));
  3443. +
  3444. + if (gpio->rising & (1 << gn)) {
  3445. + writel(rising | (1 << gn), gpio->base + GPIOREN(gb));
  3446. + } else {
  3447. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  3448. + }
  3449. +
  3450. + if (gpio->falling & (1 << gn)) {
  3451. + writel(falling | (1 << gn), gpio->base + GPIOFEN(gb));
  3452. + } else {
  3453. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  3454. + }
  3455. +
  3456. + if (gpio->high & (1 << gn)) {
  3457. + writel(high | (1 << gn), gpio->base + GPIOHEN(gb));
  3458. + } else {
  3459. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  3460. + }
  3461. +
  3462. + if (gpio->low & (1 << gn)) {
  3463. + writel(low | (1 << gn), gpio->base + GPIOLEN(gb));
  3464. + } else {
  3465. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  3466. + }
  3467. +}
  3468. +
  3469. +static struct irq_chip bcm2708_irqchip = {
  3470. + .name = "GPIO",
  3471. + .irq_enable = bcm2708_gpio_irq_unmask,
  3472. + .irq_disable = bcm2708_gpio_irq_mask,
  3473. + .irq_unmask = bcm2708_gpio_irq_unmask,
  3474. + .irq_mask = bcm2708_gpio_irq_mask,
  3475. + .irq_set_type = bcm2708_gpio_irq_set_type,
  3476. +};
  3477. +
  3478. +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
  3479. +{
  3480. + unsigned long edsr;
  3481. + unsigned bank;
  3482. + int i;
  3483. + unsigned gpio;
  3484. + for (bank = 0; bank <= 1; bank++) {
  3485. + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
  3486. + for_each_set_bit(i, &edsr, 32) {
  3487. + gpio = i + bank * 32;
  3488. + generic_handle_irq(gpio_to_irq(gpio));
  3489. + }
  3490. + writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
  3491. + }
  3492. + return IRQ_HANDLED;
  3493. +}
  3494. +
  3495. +static struct irqaction bcm2708_gpio_irq = {
  3496. + .name = "BCM2708 GPIO catchall handler",
  3497. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  3498. + .handler = bcm2708_gpio_interrupt,
  3499. +};
  3500. +
  3501. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  3502. +{
  3503. + unsigned irq;
  3504. +
  3505. + ucb->gc.to_irq = bcm2708_gpio_to_irq;
  3506. +
  3507. + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
  3508. + irq_set_chip_data(irq, ucb);
  3509. + irq_set_chip(irq, &bcm2708_irqchip);
  3510. + set_irq_flags(irq, IRQF_VALID);
  3511. + }
  3512. + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
  3513. +}
  3514. +
  3515. +#else
  3516. +
  3517. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  3518. +{
  3519. +}
  3520. +
  3521. +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
  3522. +
  3523. +static int bcm2708_gpio_probe(struct platform_device *dev)
  3524. +{
  3525. + struct bcm2708_gpio *ucb;
  3526. + struct resource *res;
  3527. + int err = 0;
  3528. +
  3529. + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
  3530. +
  3531. + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
  3532. + if (NULL == ucb) {
  3533. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  3534. + "mailbox memory\n");
  3535. + err = -ENOMEM;
  3536. + goto err;
  3537. + }
  3538. +
  3539. + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  3540. +
  3541. + platform_set_drvdata(dev, ucb);
  3542. + ucb->base = __io_address(GPIO_BASE);
  3543. +
  3544. + ucb->gc.label = "bcm2708_gpio";
  3545. + ucb->gc.base = 0;
  3546. + ucb->gc.ngpio = ARCH_NR_GPIOS;
  3547. + ucb->gc.owner = THIS_MODULE;
  3548. +
  3549. + ucb->gc.direction_input = bcm2708_gpio_dir_in;
  3550. + ucb->gc.direction_output = bcm2708_gpio_dir_out;
  3551. + ucb->gc.get = bcm2708_gpio_get;
  3552. + ucb->gc.set = bcm2708_gpio_set;
  3553. + ucb->gc.can_sleep = 0;
  3554. +
  3555. + bcm2708_gpio_irq_init(ucb);
  3556. +
  3557. + err = gpiochip_add(&ucb->gc);
  3558. + if (err)
  3559. + goto err;
  3560. +
  3561. +err:
  3562. + return err;
  3563. +
  3564. +}
  3565. +
  3566. +static int bcm2708_gpio_remove(struct platform_device *dev)
  3567. +{
  3568. + int err = 0;
  3569. + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
  3570. +
  3571. + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
  3572. +
  3573. + err = gpiochip_remove(&ucb->gc);
  3574. +
  3575. + platform_set_drvdata(dev, NULL);
  3576. + kfree(ucb);
  3577. +
  3578. + return err;
  3579. +}
  3580. +
  3581. +static struct platform_driver bcm2708_gpio_driver = {
  3582. + .probe = bcm2708_gpio_probe,
  3583. + .remove = bcm2708_gpio_remove,
  3584. + .driver = {
  3585. + .name = "bcm2708_gpio"},
  3586. +};
  3587. +
  3588. +static int __init bcm2708_gpio_init(void)
  3589. +{
  3590. + return platform_driver_register(&bcm2708_gpio_driver);
  3591. +}
  3592. +
  3593. +static void __exit bcm2708_gpio_exit(void)
  3594. +{
  3595. + platform_driver_unregister(&bcm2708_gpio_driver);
  3596. +}
  3597. +
  3598. +module_init(bcm2708_gpio_init);
  3599. +module_exit(bcm2708_gpio_exit);
  3600. +
  3601. +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
  3602. +MODULE_LICENSE("GPL");
  3603. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/bcm2708.h linux-rpi/arch/arm/mach-bcm2708/bcm2708.h
  3604. --- linux-3.10.37/arch/arm/mach-bcm2708/bcm2708.h 1970-01-01 01:00:00.000000000 +0100
  3605. +++ linux-rpi/arch/arm/mach-bcm2708/bcm2708.h 2014-04-24 15:35:00.773527891 +0200
  3606. @@ -0,0 +1,51 @@
  3607. +/*
  3608. + * linux/arch/arm/mach-bcm2708/bcm2708.h
  3609. + *
  3610. + * BCM2708 machine support header
  3611. + *
  3612. + * Copyright (C) 2010 Broadcom
  3613. + *
  3614. + * This program is free software; you can redistribute it and/or modify
  3615. + * it under the terms of the GNU General Public License as published by
  3616. + * the Free Software Foundation; either version 2 of the License, or
  3617. + * (at your option) any later version.
  3618. + *
  3619. + * This program is distributed in the hope that it will be useful,
  3620. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3621. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3622. + * GNU General Public License for more details.
  3623. + *
  3624. + * You should have received a copy of the GNU General Public License
  3625. + * along with this program; if not, write to the Free Software
  3626. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3627. + */
  3628. +
  3629. +#ifndef __BCM2708_BCM2708_H
  3630. +#define __BCM2708_BCM2708_H
  3631. +
  3632. +#include <linux/amba/bus.h>
  3633. +
  3634. +extern void __init bcm2708_init(void);
  3635. +extern void __init bcm2708_init_irq(void);
  3636. +extern void __init bcm2708_map_io(void);
  3637. +extern struct sys_timer bcm2708_timer;
  3638. +extern unsigned int mmc_status(struct device *dev);
  3639. +
  3640. +#define AMBA_DEVICE(name, busid, base, plat) \
  3641. +static struct amba_device name##_device = { \
  3642. + .dev = { \
  3643. + .coherent_dma_mask = ~0, \
  3644. + .init_name = busid, \
  3645. + .platform_data = plat, \
  3646. + }, \
  3647. + .res = { \
  3648. + .start = base##_BASE, \
  3649. + .end = (base##_BASE) + SZ_4K - 1,\
  3650. + .flags = IORESOURCE_MEM, \
  3651. + }, \
  3652. + .dma_mask = ~0, \
  3653. + .irq = base##_IRQ, \
  3654. + /* .dma = base##_DMA,*/ \
  3655. +}
  3656. +
  3657. +#endif
  3658. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/clock.c linux-rpi/arch/arm/mach-bcm2708/clock.c
  3659. --- linux-3.10.37/arch/arm/mach-bcm2708/clock.c 1970-01-01 01:00:00.000000000 +0100
  3660. +++ linux-rpi/arch/arm/mach-bcm2708/clock.c 2014-04-24 15:35:00.773527891 +0200
  3661. @@ -0,0 +1,61 @@
  3662. +/*
  3663. + * linux/arch/arm/mach-bcm2708/clock.c
  3664. + *
  3665. + * Copyright (C) 2010 Broadcom
  3666. + *
  3667. + * This program is free software; you can redistribute it and/or modify
  3668. + * it under the terms of the GNU General Public License as published by
  3669. + * the Free Software Foundation; either version 2 of the License, or
  3670. + * (at your option) any later version.
  3671. + *
  3672. + * This program is distributed in the hope that it will be useful,
  3673. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3674. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3675. + * GNU General Public License for more details.
  3676. + *
  3677. + * You should have received a copy of the GNU General Public License
  3678. + * along with this program; if not, write to the Free Software
  3679. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3680. + */
  3681. +#include <linux/module.h>
  3682. +#include <linux/kernel.h>
  3683. +#include <linux/device.h>
  3684. +#include <linux/list.h>
  3685. +#include <linux/errno.h>
  3686. +#include <linux/err.h>
  3687. +#include <linux/string.h>
  3688. +#include <linux/clk.h>
  3689. +#include <linux/mutex.h>
  3690. +
  3691. +#include <asm/clkdev.h>
  3692. +
  3693. +#include "clock.h"
  3694. +
  3695. +int clk_enable(struct clk *clk)
  3696. +{
  3697. + return 0;
  3698. +}
  3699. +EXPORT_SYMBOL(clk_enable);
  3700. +
  3701. +void clk_disable(struct clk *clk)
  3702. +{
  3703. +}
  3704. +EXPORT_SYMBOL(clk_disable);
  3705. +
  3706. +unsigned long clk_get_rate(struct clk *clk)
  3707. +{
  3708. + return clk->rate;
  3709. +}
  3710. +EXPORT_SYMBOL(clk_get_rate);
  3711. +
  3712. +long clk_round_rate(struct clk *clk, unsigned long rate)
  3713. +{
  3714. + return clk->rate;
  3715. +}
  3716. +EXPORT_SYMBOL(clk_round_rate);
  3717. +
  3718. +int clk_set_rate(struct clk *clk, unsigned long rate)
  3719. +{
  3720. + return -EIO;
  3721. +}
  3722. +EXPORT_SYMBOL(clk_set_rate);
  3723. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/clock.h linux-rpi/arch/arm/mach-bcm2708/clock.h
  3724. --- linux-3.10.37/arch/arm/mach-bcm2708/clock.h 1970-01-01 01:00:00.000000000 +0100
  3725. +++ linux-rpi/arch/arm/mach-bcm2708/clock.h 2014-04-24 15:35:00.773527891 +0200
  3726. @@ -0,0 +1,24 @@
  3727. +/*
  3728. + * linux/arch/arm/mach-bcm2708/clock.h
  3729. + *
  3730. + * Copyright (C) 2010 Broadcom
  3731. + *
  3732. + * This program is free software; you can redistribute it and/or modify
  3733. + * it under the terms of the GNU General Public License as published by
  3734. + * the Free Software Foundation; either version 2 of the License, or
  3735. + * (at your option) any later version.
  3736. + *
  3737. + * This program is distributed in the hope that it will be useful,
  3738. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3739. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3740. + * GNU General Public License for more details.
  3741. + *
  3742. + * You should have received a copy of the GNU General Public License
  3743. + * along with this program; if not, write to the Free Software
  3744. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3745. + */
  3746. +struct module;
  3747. +
  3748. +struct clk {
  3749. + unsigned long rate;
  3750. +};
  3751. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/dma.c linux-rpi/arch/arm/mach-bcm2708/dma.c
  3752. --- linux-3.10.37/arch/arm/mach-bcm2708/dma.c 1970-01-01 01:00:00.000000000 +0100
  3753. +++ linux-rpi/arch/arm/mach-bcm2708/dma.c 2014-04-24 15:35:00.773527891 +0200
  3754. @@ -0,0 +1,407 @@
  3755. +/*
  3756. + * linux/arch/arm/mach-bcm2708/dma.c
  3757. + *
  3758. + * Copyright (C) 2010 Broadcom
  3759. + *
  3760. + * This program is free software; you can redistribute it and/or modify
  3761. + * it under the terms of the GNU General Public License version 2 as
  3762. + * published by the Free Software Foundation.
  3763. + */
  3764. +
  3765. +#include <linux/slab.h>
  3766. +#include <linux/device.h>
  3767. +#include <linux/platform_device.h>
  3768. +#include <linux/module.h>
  3769. +#include <linux/scatterlist.h>
  3770. +
  3771. +#include <mach/dma.h>
  3772. +#include <mach/irqs.h>
  3773. +
  3774. +/*****************************************************************************\
  3775. + * *
  3776. + * Configuration *
  3777. + * *
  3778. +\*****************************************************************************/
  3779. +
  3780. +#define CACHE_LINE_MASK 31
  3781. +#define DRIVER_NAME BCM_DMAMAN_DRIVER_NAME
  3782. +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
  3783. +
  3784. +/* valid only for channels 0 - 14, 15 has its own base address */
  3785. +#define BCM2708_DMA_CHAN(n) ((n)<<8) /* base address */
  3786. +#define BCM2708_DMA_CHANIO(dma_base, n) \
  3787. + ((void __iomem *)((char *)(dma_base)+BCM2708_DMA_CHAN(n)))
  3788. +
  3789. +
  3790. +/*****************************************************************************\
  3791. + * *
  3792. + * DMA Auxilliary Functions *
  3793. + * *
  3794. +\*****************************************************************************/
  3795. +
  3796. +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
  3797. + section inside the DMA buffer and another section outside it.
  3798. + Even if we flush DMA buffers from the cache there is always the chance that
  3799. + during a DMA someone will access the part of a cache line that is outside
  3800. + the DMA buffer - which will then bring in unwelcome data.
  3801. + Without being able to dictate our own buffer pools we must insist that
  3802. + DMA buffers consist of a whole number of cache lines.
  3803. +*/
  3804. +
  3805. +extern int
  3806. +bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
  3807. +{
  3808. + int i;
  3809. +
  3810. + for (i = 0; i < sg_len; i++) {
  3811. + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
  3812. + sg_ptr[i].length & CACHE_LINE_MASK)
  3813. + return 0;
  3814. + }
  3815. +
  3816. + return 1;
  3817. +}
  3818. +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
  3819. +
  3820. +extern void
  3821. +bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block)
  3822. +{
  3823. + dsb(); /* ARM data synchronization (push) operation */
  3824. +
  3825. + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
  3826. + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
  3827. +}
  3828. +
  3829. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
  3830. +{
  3831. + dsb();
  3832. +
  3833. + /* ugly busy wait only option for now */
  3834. + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
  3835. + cpu_relax();
  3836. +}
  3837. +
  3838. +EXPORT_SYMBOL_GPL(bcm_dma_start);
  3839. +
  3840. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
  3841. +{
  3842. + dsb();
  3843. +
  3844. + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
  3845. +}
  3846. +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
  3847. +
  3848. +/* Complete an ongoing DMA (assuming its results are to be ignored)
  3849. + Does nothing if there is no DMA in progress.
  3850. + This routine waits for the current AXI transfer to complete before
  3851. + terminating the current DMA. If the current transfer is hung on a DREQ used
  3852. + by an uncooperative peripheral the AXI transfer may never complete. In this
  3853. + case the routine times out and return a non-zero error code.
  3854. + Use of this routine doesn't guarantee that the ongoing or aborted DMA
  3855. + does not produce an interrupt.
  3856. +*/
  3857. +extern int
  3858. +bcm_dma_abort(void __iomem *dma_chan_base)
  3859. +{
  3860. + unsigned long int cs;
  3861. + int rc = 0;
  3862. +
  3863. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  3864. +
  3865. + if (BCM2708_DMA_ACTIVE & cs) {
  3866. + long int timeout = 10000;
  3867. +
  3868. + /* write 0 to the active bit - pause the DMA */
  3869. + writel(0, dma_chan_base + BCM2708_DMA_CS);
  3870. +
  3871. + /* wait for any current AXI transfer to complete */
  3872. + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
  3873. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  3874. +
  3875. + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
  3876. + /* we'll un-pause when we set of our next DMA */
  3877. + rc = -ETIMEDOUT;
  3878. +
  3879. + } else if (BCM2708_DMA_ACTIVE & cs) {
  3880. + /* terminate the control block chain */
  3881. + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
  3882. +
  3883. + /* abort the whole DMA */
  3884. + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
  3885. + dma_chan_base + BCM2708_DMA_CS);
  3886. + }
  3887. + }
  3888. +
  3889. + return rc;
  3890. +}
  3891. +EXPORT_SYMBOL_GPL(bcm_dma_abort);
  3892. +
  3893. +
  3894. +/***************************************************************************** \
  3895. + * *
  3896. + * DMA Manager Device Methods *
  3897. + * *
  3898. +\*****************************************************************************/
  3899. +
  3900. +struct vc_dmaman {
  3901. + void __iomem *dma_base;
  3902. + u32 chan_available; /* bitmap of available channels */
  3903. + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
  3904. +};
  3905. +
  3906. +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
  3907. + u32 chans_available)
  3908. +{
  3909. + dmaman->dma_base = dma_base;
  3910. + dmaman->chan_available = chans_available;
  3911. + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* chans 2 & 3 */
  3912. + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* chan 0 */
  3913. +}
  3914. +
  3915. +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
  3916. + unsigned preferred_feature_set)
  3917. +{
  3918. + u32 chans;
  3919. + int feature;
  3920. +
  3921. + chans = dmaman->chan_available;
  3922. + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
  3923. + /* select the subset of available channels with the desired
  3924. + feature so long as some of the candidate channels have that
  3925. + feature */
  3926. + if ((preferred_feature_set & (1 << feature)) &&
  3927. + (chans & dmaman->has_feature[feature]))
  3928. + chans &= dmaman->has_feature[feature];
  3929. +
  3930. + if (chans) {
  3931. + int chan = 0;
  3932. + /* return the ordinal of the first channel in the bitmap */
  3933. + while (chans != 0 && (chans & 1) == 0) {
  3934. + chans >>= 1;
  3935. + chan++;
  3936. + }
  3937. + /* claim the channel */
  3938. + dmaman->chan_available &= ~(1 << chan);
  3939. + return chan;
  3940. + } else
  3941. + return -ENOMEM;
  3942. +}
  3943. +
  3944. +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
  3945. +{
  3946. + if (chan < 0)
  3947. + return -EINVAL;
  3948. + else if ((1 << chan) & dmaman->chan_available)
  3949. + return -EIDRM;
  3950. + else {
  3951. + dmaman->chan_available |= (1 << chan);
  3952. + return 0;
  3953. + }
  3954. +}
  3955. +
  3956. +/*****************************************************************************\
  3957. + * *
  3958. + * DMA IRQs *
  3959. + * *
  3960. +\*****************************************************************************/
  3961. +
  3962. +static unsigned char bcm_dma_irqs[] = {
  3963. + IRQ_DMA0,
  3964. + IRQ_DMA1,
  3965. + IRQ_DMA2,
  3966. + IRQ_DMA3,
  3967. + IRQ_DMA4,
  3968. + IRQ_DMA5,
  3969. + IRQ_DMA6,
  3970. + IRQ_DMA7,
  3971. + IRQ_DMA8,
  3972. + IRQ_DMA9,
  3973. + IRQ_DMA10,
  3974. + IRQ_DMA11,
  3975. + IRQ_DMA12
  3976. +};
  3977. +
  3978. +
  3979. +/***************************************************************************** \
  3980. + * *
  3981. + * DMA Manager Monitor *
  3982. + * *
  3983. +\*****************************************************************************/
  3984. +
  3985. +static struct device *dmaman_dev; /* we assume there's only one! */
  3986. +
  3987. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  3988. + void __iomem **out_dma_base, int *out_dma_irq)
  3989. +{
  3990. + if (!dmaman_dev)
  3991. + return -ENODEV;
  3992. + else {
  3993. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  3994. + int rc;
  3995. +
  3996. + device_lock(dmaman_dev);
  3997. + rc = vc_dmaman_chan_alloc(dmaman, preferred_feature_set);
  3998. + if (rc >= 0) {
  3999. + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base,
  4000. + rc);
  4001. + *out_dma_irq = bcm_dma_irqs[rc];
  4002. + }
  4003. + device_unlock(dmaman_dev);
  4004. +
  4005. + return rc;
  4006. + }
  4007. +}
  4008. +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
  4009. +
  4010. +extern int bcm_dma_chan_free(int channel)
  4011. +{
  4012. + if (dmaman_dev) {
  4013. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4014. + int rc;
  4015. +
  4016. + device_lock(dmaman_dev);
  4017. + rc = vc_dmaman_chan_free(dmaman, channel);
  4018. + device_unlock(dmaman_dev);
  4019. +
  4020. + return rc;
  4021. + } else
  4022. + return -ENODEV;
  4023. +}
  4024. +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
  4025. +
  4026. +static int dev_dmaman_register(const char *dev_name, struct device *dev)
  4027. +{
  4028. + int rc = dmaman_dev ? -EINVAL : 0;
  4029. + dmaman_dev = dev;
  4030. + return rc;
  4031. +}
  4032. +
  4033. +static void dev_dmaman_deregister(const char *dev_name, struct device *dev)
  4034. +{
  4035. + dmaman_dev = NULL;
  4036. +}
  4037. +
  4038. +/*****************************************************************************\
  4039. + * *
  4040. + * DMA Device *
  4041. + * *
  4042. +\*****************************************************************************/
  4043. +
  4044. +static int dmachans = -1; /* module parameter */
  4045. +
  4046. +static int bcm_dmaman_probe(struct platform_device *pdev)
  4047. +{
  4048. + int ret = 0;
  4049. + struct vc_dmaman *dmaman;
  4050. + struct resource *dma_res = NULL;
  4051. + void __iomem *dma_base = NULL;
  4052. + int have_dma_region = 0;
  4053. +
  4054. + dmaman = kzalloc(sizeof(*dmaman), GFP_KERNEL);
  4055. + if (NULL == dmaman) {
  4056. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  4057. + "DMA management memory\n");
  4058. + ret = -ENOMEM;
  4059. + } else {
  4060. +
  4061. + dma_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4062. + if (dma_res == NULL) {
  4063. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  4064. + "resource\n");
  4065. + ret = -ENODEV;
  4066. + } else if (!request_mem_region(dma_res->start,
  4067. + resource_size(dma_res),
  4068. + DRIVER_NAME)) {
  4069. + dev_err(&pdev->dev, "cannot obtain DMA region\n");
  4070. + ret = -EBUSY;
  4071. + } else {
  4072. + have_dma_region = 1;
  4073. + dma_base = ioremap(dma_res->start,
  4074. + resource_size(dma_res));
  4075. + if (!dma_base) {
  4076. + dev_err(&pdev->dev, "cannot map DMA region\n");
  4077. + ret = -ENOMEM;
  4078. + } else {
  4079. + /* use module parameter if one was provided */
  4080. + if (dmachans > 0)
  4081. + vc_dmaman_init(dmaman, dma_base,
  4082. + dmachans);
  4083. + else
  4084. + vc_dmaman_init(dmaman, dma_base,
  4085. + DEFAULT_DMACHAN_BITMAP);
  4086. +
  4087. + platform_set_drvdata(pdev, dmaman);
  4088. + dev_dmaman_register(DRIVER_NAME, &pdev->dev);
  4089. +
  4090. + printk(KERN_INFO DRIVER_NAME ": DMA manager "
  4091. + "at %p\n", dma_base);
  4092. + }
  4093. + }
  4094. + }
  4095. + if (ret != 0) {
  4096. + if (dma_base)
  4097. + iounmap(dma_base);
  4098. + if (dma_res && have_dma_region)
  4099. + release_mem_region(dma_res->start,
  4100. + resource_size(dma_res));
  4101. + if (dmaman)
  4102. + kfree(dmaman);
  4103. + }
  4104. + return ret;
  4105. +}
  4106. +
  4107. +static int bcm_dmaman_remove(struct platform_device *pdev)
  4108. +{
  4109. + struct vc_dmaman *dmaman = platform_get_drvdata(pdev);
  4110. +
  4111. + platform_set_drvdata(pdev, NULL);
  4112. + dev_dmaman_deregister(DRIVER_NAME, &pdev->dev);
  4113. + kfree(dmaman);
  4114. +
  4115. + return 0;
  4116. +}
  4117. +
  4118. +static struct platform_driver bcm_dmaman_driver = {
  4119. + .probe = bcm_dmaman_probe,
  4120. + .remove = bcm_dmaman_remove,
  4121. +
  4122. + .driver = {
  4123. + .name = DRIVER_NAME,
  4124. + .owner = THIS_MODULE,
  4125. + },
  4126. +};
  4127. +
  4128. +/*****************************************************************************\
  4129. + * *
  4130. + * Driver init/exit *
  4131. + * *
  4132. +\*****************************************************************************/
  4133. +
  4134. +static int __init bcm_dmaman_drv_init(void)
  4135. +{
  4136. + int ret;
  4137. +
  4138. + ret = platform_driver_register(&bcm_dmaman_driver);
  4139. + if (ret != 0) {
  4140. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  4141. + "on platform\n");
  4142. + }
  4143. +
  4144. + return ret;
  4145. +}
  4146. +
  4147. +static void __exit bcm_dmaman_drv_exit(void)
  4148. +{
  4149. + platform_driver_unregister(&bcm_dmaman_driver);
  4150. +}
  4151. +
  4152. +module_init(bcm_dmaman_drv_init);
  4153. +module_exit(bcm_dmaman_drv_exit);
  4154. +
  4155. +module_param(dmachans, int, 0644);
  4156. +
  4157. +MODULE_AUTHOR("Gray Girling <grayg@broadcom.com>");
  4158. +MODULE_DESCRIPTION("DMA channel manager driver");
  4159. +MODULE_LICENSE("GPL");
  4160. +
  4161. +MODULE_PARM_DESC(dmachans, "Bitmap of DMA channels available to the ARM");
  4162. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/dmaer.c linux-rpi/arch/arm/mach-bcm2708/dmaer.c
  4163. --- linux-3.10.37/arch/arm/mach-bcm2708/dmaer.c 1970-01-01 01:00:00.000000000 +0100
  4164. +++ linux-rpi/arch/arm/mach-bcm2708/dmaer.c 2014-04-24 15:35:00.777527936 +0200
  4165. @@ -0,0 +1,887 @@
  4166. +#include <linux/init.h>
  4167. +#include <linux/sched.h>
  4168. +#include <linux/module.h>
  4169. +#include <linux/types.h>
  4170. +#include <linux/kdev_t.h>
  4171. +#include <linux/fs.h>
  4172. +#include <linux/cdev.h>
  4173. +#include <linux/mm.h>
  4174. +#include <linux/slab.h>
  4175. +#include <linux/pagemap.h>
  4176. +#include <linux/device.h>
  4177. +#include <linux/jiffies.h>
  4178. +#include <linux/timex.h>
  4179. +#include <linux/dma-mapping.h>
  4180. +
  4181. +#include <asm/uaccess.h>
  4182. +#include <asm/atomic.h>
  4183. +#include <asm/cacheflush.h>
  4184. +#include <asm/io.h>
  4185. +
  4186. +#include <mach/dma.h>
  4187. +#include <mach/vc_support.h>
  4188. +
  4189. +#ifdef ECLIPSE_IGNORE
  4190. +
  4191. +#define __user
  4192. +#define __init
  4193. +#define __exit
  4194. +#define __iomem
  4195. +#define KERN_DEBUG
  4196. +#define KERN_ERR
  4197. +#define KERN_WARNING
  4198. +#define KERN_INFO
  4199. +#define _IOWR(a, b, c) b
  4200. +#define _IOW(a, b, c) b
  4201. +#define _IO(a, b) b
  4202. +
  4203. +#endif
  4204. +
  4205. +//#define inline
  4206. +
  4207. +#define PRINTK(args...) printk(args)
  4208. +//#define PRINTK_VERBOSE(args...) printk(args)
  4209. +//#define PRINTK(args...)
  4210. +#define PRINTK_VERBOSE(args...)
  4211. +
  4212. +/***** TYPES ****/
  4213. +#define PAGES_PER_LIST 500
  4214. +struct PageList
  4215. +{
  4216. + struct page *m_pPages[PAGES_PER_LIST];
  4217. + unsigned int m_used;
  4218. + struct PageList *m_pNext;
  4219. +};
  4220. +
  4221. +struct VmaPageList
  4222. +{
  4223. + //each vma has a linked list of pages associated with it
  4224. + struct PageList *m_pPageHead;
  4225. + struct PageList *m_pPageTail;
  4226. + unsigned int m_refCount;
  4227. +};
  4228. +
  4229. +struct DmaControlBlock
  4230. +{
  4231. + unsigned int m_transferInfo;
  4232. + void __user *m_pSourceAddr;
  4233. + void __user *m_pDestAddr;
  4234. + unsigned int m_xferLen;
  4235. + unsigned int m_tdStride;
  4236. + struct DmaControlBlock *m_pNext;
  4237. + unsigned int m_blank1, m_blank2;
  4238. +};
  4239. +
  4240. +/***** DEFINES ******/
  4241. +//magic number defining the module
  4242. +#define DMA_MAGIC 0xdd
  4243. +
  4244. +//do user virtual to physical translation of the CB chain
  4245. +#define DMA_PREPARE _IOWR(DMA_MAGIC, 0, struct DmaControlBlock *)
  4246. +
  4247. +//kick the pre-prepared CB chain
  4248. +#define DMA_KICK _IOW(DMA_MAGIC, 1, struct DmaControlBlock *)
  4249. +
  4250. +//prepare it, kick it, wait for it
  4251. +#define DMA_PREPARE_KICK_WAIT _IOWR(DMA_MAGIC, 2, struct DmaControlBlock *)
  4252. +
  4253. +//prepare it, kick it, don't wait for it
  4254. +#define DMA_PREPARE_KICK _IOWR(DMA_MAGIC, 3, struct DmaControlBlock *)
  4255. +
  4256. +//not currently implemented
  4257. +#define DMA_WAIT_ONE _IO(DMA_MAGIC, 4, struct DmaControlBlock *)
  4258. +
  4259. +//wait on all kicked CB chains
  4260. +#define DMA_WAIT_ALL _IO(DMA_MAGIC, 5)
  4261. +
  4262. +//in order to discover the largest AXI burst that should be programmed into the transfer params
  4263. +#define DMA_MAX_BURST _IO(DMA_MAGIC, 6)
  4264. +
  4265. +//set the address range through which the user address is assumed to already by a physical address
  4266. +#define DMA_SET_MIN_PHYS _IOW(DMA_MAGIC, 7, unsigned long)
  4267. +#define DMA_SET_MAX_PHYS _IOW(DMA_MAGIC, 8, unsigned long)
  4268. +#define DMA_SET_PHYS_OFFSET _IOW(DMA_MAGIC, 9, unsigned long)
  4269. +
  4270. +//used to define the size for the CMA-based allocation *in pages*, can only be done once once the file is opened
  4271. +#define DMA_CMA_SET_SIZE _IOW(DMA_MAGIC, 10, unsigned long)
  4272. +
  4273. +//used to get the version of the module, to test for a capability
  4274. +#define DMA_GET_VERSION _IO(DMA_MAGIC, 99)
  4275. +
  4276. +#define VERSION_NUMBER 1
  4277. +
  4278. +#define VIRT_TO_BUS_CACHE_SIZE 8
  4279. +
  4280. +/***** FILE OPS *****/
  4281. +static int Open(struct inode *pInode, struct file *pFile);
  4282. +static int Release(struct inode *pInode, struct file *pFile);
  4283. +static long Ioctl(struct file *pFile, unsigned int cmd, unsigned long arg);
  4284. +static ssize_t Read(struct file *pFile, char __user *pUser, size_t count, loff_t *offp);
  4285. +static int Mmap(struct file *pFile, struct vm_area_struct *pVma);
  4286. +
  4287. +/***** VMA OPS ****/
  4288. +static void VmaOpen4k(struct vm_area_struct *pVma);
  4289. +static void VmaClose4k(struct vm_area_struct *pVma);
  4290. +static int VmaFault4k(struct vm_area_struct *pVma, struct vm_fault *pVmf);
  4291. +
  4292. +/**** DMA PROTOTYPES */
  4293. +static struct DmaControlBlock __user *DmaPrepare(struct DmaControlBlock __user *pUserCB, int *pError);
  4294. +static int DmaKick(struct DmaControlBlock __user *pUserCB);
  4295. +static void DmaWaitAll(void);
  4296. +
  4297. +/**** GENERIC ****/
  4298. +static int __init dmaer_init(void);
  4299. +static void __exit dmaer_exit(void);
  4300. +
  4301. +/*** OPS ***/
  4302. +static struct vm_operations_struct g_vmOps4k = {
  4303. + .open = VmaOpen4k,
  4304. + .close = VmaClose4k,
  4305. + .fault = VmaFault4k,
  4306. +};
  4307. +
  4308. +static struct file_operations g_fOps = {
  4309. + .owner = THIS_MODULE,
  4310. + .llseek = 0,
  4311. + .read = Read,
  4312. + .write = 0,
  4313. + .unlocked_ioctl = Ioctl,
  4314. + .open = Open,
  4315. + .release = Release,
  4316. + .mmap = Mmap,
  4317. +};
  4318. +
  4319. +/***** GLOBALS ******/
  4320. +static dev_t g_majorMinor;
  4321. +
  4322. +//tracking usage of the two files
  4323. +static atomic_t g_oneLock4k = ATOMIC_INIT(1);
  4324. +
  4325. +//device operations
  4326. +static struct cdev g_cDev;
  4327. +static int g_trackedPages = 0;
  4328. +
  4329. +//dma control
  4330. +static unsigned int *g_pDmaChanBase;
  4331. +static int g_dmaIrq;
  4332. +static int g_dmaChan;
  4333. +
  4334. +//cma allocation
  4335. +static int g_cmaHandle;
  4336. +
  4337. +//user virtual to bus address translation acceleration
  4338. +static unsigned long g_virtAddr[VIRT_TO_BUS_CACHE_SIZE];
  4339. +static unsigned long g_busAddr[VIRT_TO_BUS_CACHE_SIZE];
  4340. +static unsigned long g_cbVirtAddr;
  4341. +static unsigned long g_cbBusAddr;
  4342. +static int g_cacheInsertAt;
  4343. +static int g_cacheHit, g_cacheMiss;
  4344. +
  4345. +//off by default
  4346. +static void __user *g_pMinPhys;
  4347. +static void __user *g_pMaxPhys;
  4348. +static unsigned long g_physOffset;
  4349. +
  4350. +/****** CACHE OPERATIONS ********/
  4351. +static inline void FlushAddrCache(void)
  4352. +{
  4353. + int count = 0;
  4354. + for (count = 0; count < VIRT_TO_BUS_CACHE_SIZE; count++)
  4355. + g_virtAddr[count] = 0xffffffff; //never going to match as we always chop the bottom bits anyway
  4356. +
  4357. + g_cbVirtAddr = 0xffffffff;
  4358. +
  4359. + g_cacheInsertAt = 0;
  4360. +}
  4361. +
  4362. +//translate from a user virtual address to a bus address by mapping the page
  4363. +//NB this won't lock a page in memory, so to avoid potential paging issues using kernel logical addresses
  4364. +static inline void __iomem *UserVirtualToBus(void __user *pUser)
  4365. +{
  4366. + int mapped;
  4367. + struct page *pPage;
  4368. + void *phys;
  4369. +
  4370. + //map it (requiring that the pointer points to something that does not hang off the page boundary)
  4371. + mapped = get_user_pages(current, current->mm,
  4372. + (unsigned long)pUser, 1,
  4373. + 1, 0,
  4374. + &pPage,
  4375. + 0);
  4376. +
  4377. + if (mapped <= 0) //error
  4378. + return 0;
  4379. +
  4380. + PRINTK_VERBOSE(KERN_DEBUG "user virtual %p arm phys %p bus %p\n",
  4381. + pUser, page_address(pPage), (void __iomem *)__virt_to_bus(page_address(pPage)));
  4382. +
  4383. + //get the arm physical address
  4384. + phys = page_address(pPage) + offset_in_page(pUser);
  4385. + page_cache_release(pPage);
  4386. +
  4387. + //and now the bus address
  4388. + return (void __iomem *)__virt_to_bus(phys);
  4389. +}
  4390. +
  4391. +static inline void __iomem *UserVirtualToBusViaCbCache(void __user *pUser)
  4392. +{
  4393. + unsigned long virtual_page = (unsigned long)pUser & ~4095;
  4394. + unsigned long page_offset = (unsigned long)pUser & 4095;
  4395. + unsigned long bus_addr;
  4396. +
  4397. + if (g_cbVirtAddr == virtual_page)
  4398. + {
  4399. + bus_addr = g_cbBusAddr + page_offset;
  4400. + g_cacheHit++;
  4401. + return (void __iomem *)bus_addr;
  4402. + }
  4403. + else
  4404. + {
  4405. + bus_addr = (unsigned long)UserVirtualToBus(pUser);
  4406. +
  4407. + if (!bus_addr)
  4408. + return 0;
  4409. +
  4410. + g_cbVirtAddr = virtual_page;
  4411. + g_cbBusAddr = bus_addr & ~4095;
  4412. + g_cacheMiss++;
  4413. +
  4414. + return (void __iomem *)bus_addr;
  4415. + }
  4416. +}
  4417. +
  4418. +//do the same as above, by query our virt->bus cache
  4419. +static inline void __iomem *UserVirtualToBusViaCache(void __user *pUser)
  4420. +{
  4421. + int count;
  4422. + //get the page and its offset
  4423. + unsigned long virtual_page = (unsigned long)pUser & ~4095;
  4424. + unsigned long page_offset = (unsigned long)pUser & 4095;
  4425. + unsigned long bus_addr;
  4426. +
  4427. + if (pUser >= g_pMinPhys && pUser < g_pMaxPhys)
  4428. + {
  4429. + PRINTK_VERBOSE(KERN_DEBUG "user->phys passthrough on %p\n", pUser);
  4430. + return (void __iomem *)((unsigned long)pUser + g_physOffset);
  4431. + }
  4432. +
  4433. + //check the cache for our entry
  4434. + for (count = 0; count < VIRT_TO_BUS_CACHE_SIZE; count++)
  4435. + if (g_virtAddr[count] == virtual_page)
  4436. + {
  4437. + bus_addr = g_busAddr[count] + page_offset;
  4438. + g_cacheHit++;
  4439. + return (void __iomem *)bus_addr;
  4440. + }
  4441. +
  4442. + //not found, look up manually and then insert its page address
  4443. + bus_addr = (unsigned long)UserVirtualToBus(pUser);
  4444. +
  4445. + if (!bus_addr)
  4446. + return 0;
  4447. +
  4448. + g_virtAddr[g_cacheInsertAt] = virtual_page;
  4449. + g_busAddr[g_cacheInsertAt] = bus_addr & ~4095;
  4450. +
  4451. + //round robin
  4452. + g_cacheInsertAt++;
  4453. + if (g_cacheInsertAt == VIRT_TO_BUS_CACHE_SIZE)
  4454. + g_cacheInsertAt = 0;
  4455. +
  4456. + g_cacheMiss++;
  4457. +
  4458. + return (void __iomem *)bus_addr;
  4459. +}
  4460. +
  4461. +/***** FILE OPERATIONS ****/
  4462. +static int Open(struct inode *pInode, struct file *pFile)
  4463. +{
  4464. + PRINTK(KERN_DEBUG "file opening: %d/%d\n", imajor(pInode), iminor(pInode));
  4465. +
  4466. + //check which device we are
  4467. + if (iminor(pInode) == 0) //4k
  4468. + {
  4469. + //only one at a time
  4470. + if (!atomic_dec_and_test(&g_oneLock4k))
  4471. + {
  4472. + atomic_inc(&g_oneLock4k);
  4473. + return -EBUSY;
  4474. + }
  4475. + }
  4476. + else
  4477. + return -EINVAL;
  4478. +
  4479. + //todo there will be trouble if two different processes open the files
  4480. +
  4481. + //reset after any file is opened
  4482. + g_pMinPhys = (void __user *)-1;
  4483. + g_pMaxPhys = (void __user *)0;
  4484. + g_physOffset = 0;
  4485. + g_cmaHandle = 0;
  4486. +
  4487. + return 0;
  4488. +}
  4489. +
  4490. +static int Release(struct inode *pInode, struct file *pFile)
  4491. +{
  4492. + PRINTK(KERN_DEBUG "file closing, %d pages tracked\n", g_trackedPages);
  4493. + if (g_trackedPages)
  4494. + PRINTK(KERN_ERR "we\'re leaking memory!\n");
  4495. +
  4496. + //wait for any dmas to finish
  4497. + DmaWaitAll();
  4498. +
  4499. + //free this memory on the application closing the file or it crashing (implicitly closing the file)
  4500. + if (g_cmaHandle)
  4501. + {
  4502. + PRINTK(KERN_DEBUG "unlocking vc memory\n");
  4503. + if (UnlockVcMemory(g_cmaHandle))
  4504. + PRINTK(KERN_ERR "uh-oh, unable to unlock vc memory!\n");
  4505. + PRINTK(KERN_DEBUG "releasing vc memory\n");
  4506. + if (ReleaseVcMemory(g_cmaHandle))
  4507. + PRINTK(KERN_ERR "uh-oh, unable to release vc memory!\n");
  4508. + }
  4509. +
  4510. + if (iminor(pInode) == 0)
  4511. + atomic_inc(&g_oneLock4k);
  4512. + else
  4513. + return -EINVAL;
  4514. +
  4515. + return 0;
  4516. +}
  4517. +
  4518. +static struct DmaControlBlock __user *DmaPrepare(struct DmaControlBlock __user *pUserCB, int *pError)
  4519. +{
  4520. + struct DmaControlBlock kernCB;
  4521. + struct DmaControlBlock __user *pUNext;
  4522. + void __iomem *pSourceBus, __iomem *pDestBus;
  4523. +
  4524. + //get the control block into kernel memory so we can work on it
  4525. + if (copy_from_user(&kernCB, pUserCB, sizeof(struct DmaControlBlock)) != 0)
  4526. + {
  4527. + PRINTK(KERN_ERR "copy_from_user failed for user cb %p\n", pUserCB);
  4528. + *pError = 1;
  4529. + return 0;
  4530. + }
  4531. +
  4532. + if (kernCB.m_pSourceAddr == 0 || kernCB.m_pDestAddr == 0)
  4533. + {
  4534. + PRINTK(KERN_ERR "faulty source (%p) dest (%p) addresses for user cb %p\n",
  4535. + kernCB.m_pSourceAddr, kernCB.m_pDestAddr, pUserCB);
  4536. + *pError = 1;
  4537. + return 0;
  4538. + }
  4539. +
  4540. + pSourceBus = UserVirtualToBusViaCache(kernCB.m_pSourceAddr);
  4541. + pDestBus = UserVirtualToBusViaCache(kernCB.m_pDestAddr);
  4542. +
  4543. + if (!pSourceBus || !pDestBus)
  4544. + {
  4545. + PRINTK(KERN_ERR "virtual to bus translation failure for source/dest %p/%p->%p/%p\n",
  4546. + kernCB.m_pSourceAddr, kernCB.m_pDestAddr,
  4547. + pSourceBus, pDestBus);
  4548. + *pError = 1;
  4549. + return 0;
  4550. + }
  4551. +
  4552. + //update the user structure with the new bus addresses
  4553. + kernCB.m_pSourceAddr = pSourceBus;
  4554. + kernCB.m_pDestAddr = pDestBus;
  4555. +
  4556. + PRINTK_VERBOSE(KERN_DEBUG "final source %p dest %p\n", kernCB.m_pSourceAddr, kernCB.m_pDestAddr);
  4557. +
  4558. + //sort out the bus address for the next block
  4559. + pUNext = kernCB.m_pNext;
  4560. +
  4561. + if (kernCB.m_pNext)
  4562. + {
  4563. + void __iomem *pNextBus;
  4564. + pNextBus = UserVirtualToBusViaCbCache(kernCB.m_pNext);
  4565. +
  4566. + if (!pNextBus)
  4567. + {
  4568. + PRINTK(KERN_ERR "virtual to bus translation failure for m_pNext\n");
  4569. + *pError = 1;
  4570. + return 0;
  4571. + }
  4572. +
  4573. + //update the pointer with the bus address
  4574. + kernCB.m_pNext = pNextBus;
  4575. + }
  4576. +
  4577. + //write it back to user space
  4578. + if (copy_to_user(pUserCB, &kernCB, sizeof(struct DmaControlBlock)) != 0)
  4579. + {
  4580. + PRINTK(KERN_ERR "copy_to_user failed for cb %p\n", pUserCB);
  4581. + *pError = 1;
  4582. + return 0;
  4583. + }
  4584. +
  4585. + __cpuc_flush_dcache_area(pUserCB, 32);
  4586. +
  4587. + *pError = 0;
  4588. + return pUNext;
  4589. +}
  4590. +
  4591. +static int DmaKick(struct DmaControlBlock __user *pUserCB)
  4592. +{
  4593. + void __iomem *pBusCB;
  4594. +
  4595. + pBusCB = UserVirtualToBusViaCbCache(pUserCB);
  4596. + if (!pBusCB)
  4597. + {
  4598. + PRINTK(KERN_ERR "virtual to bus translation failure for cb\n");
  4599. + return 1;
  4600. + }
  4601. +
  4602. + //flush_cache_all();
  4603. +
  4604. + bcm_dma_start(g_pDmaChanBase, (dma_addr_t)pBusCB);
  4605. +
  4606. + return 0;
  4607. +}
  4608. +
  4609. +static void DmaWaitAll(void)
  4610. +{
  4611. + int counter = 0;
  4612. + volatile int inner_count;
  4613. + volatile unsigned int cs;
  4614. + unsigned long time_before, time_after;
  4615. +
  4616. + time_before = jiffies;
  4617. + //bcm_dma_wait_idle(g_pDmaChanBase);
  4618. + dsb();
  4619. +
  4620. + cs = readl(g_pDmaChanBase);
  4621. +
  4622. + while ((cs & 1) == 1)
  4623. + {
  4624. + cs = readl(g_pDmaChanBase);
  4625. + counter++;
  4626. +
  4627. + for (inner_count = 0; inner_count < 32; inner_count++);
  4628. +
  4629. + asm volatile ("MCR p15,0,r0,c7,c0,4 \n");
  4630. + //cpu_do_idle();
  4631. + if (counter >= 1000000)
  4632. + {
  4633. + PRINTK(KERN_WARNING "DMA failed to finish in a timely fashion\n");
  4634. + break;
  4635. + }
  4636. + }
  4637. + time_after = jiffies;
  4638. + PRINTK_VERBOSE(KERN_DEBUG "done, counter %d, cs %08x", counter, cs);
  4639. + PRINTK_VERBOSE(KERN_DEBUG "took %ld jiffies, %d HZ\n", time_after - time_before, HZ);
  4640. +}
  4641. +
  4642. +static long Ioctl(struct file *pFile, unsigned int cmd, unsigned long arg)
  4643. +{
  4644. + int error = 0;
  4645. + PRINTK_VERBOSE(KERN_DEBUG "ioctl cmd %x arg %lx\n", cmd, arg);
  4646. +
  4647. + switch (cmd)
  4648. + {
  4649. + case DMA_PREPARE:
  4650. + case DMA_PREPARE_KICK:
  4651. + case DMA_PREPARE_KICK_WAIT:
  4652. + {
  4653. + struct DmaControlBlock __user *pUCB = (struct DmaControlBlock *)arg;
  4654. + int steps = 0;
  4655. + unsigned long start_time = jiffies;
  4656. + (void)start_time;
  4657. +
  4658. + //flush our address cache
  4659. + FlushAddrCache();
  4660. +
  4661. + PRINTK_VERBOSE(KERN_DEBUG "dma prepare\n");
  4662. +
  4663. + //do virtual to bus translation for each entry
  4664. + do
  4665. + {
  4666. + pUCB = DmaPrepare(pUCB, &error);
  4667. + } while (error == 0 && ++steps && pUCB);
  4668. + PRINTK_VERBOSE(KERN_DEBUG "prepare done in %d steps, %ld\n", steps, jiffies - start_time);
  4669. +
  4670. + //carry straight on if we want to kick too
  4671. + if (cmd == DMA_PREPARE || error)
  4672. + {
  4673. + PRINTK_VERBOSE(KERN_DEBUG "falling out\n");
  4674. + return error ? -EINVAL : 0;
  4675. + }
  4676. + }
  4677. + case DMA_KICK:
  4678. + PRINTK_VERBOSE(KERN_DEBUG "dma begin\n");
  4679. +
  4680. + if (cmd == DMA_KICK)
  4681. + FlushAddrCache();
  4682. +
  4683. + DmaKick((struct DmaControlBlock __user *)arg);
  4684. +
  4685. + if (cmd != DMA_PREPARE_KICK_WAIT)
  4686. + break;
  4687. +/* case DMA_WAIT_ONE:
  4688. + //PRINTK(KERN_DEBUG "dma wait one\n");
  4689. + break;*/
  4690. + case DMA_WAIT_ALL:
  4691. + //PRINTK(KERN_DEBUG "dma wait all\n");
  4692. + DmaWaitAll();
  4693. + break;
  4694. + case DMA_MAX_BURST:
  4695. + if (g_dmaChan == 0)
  4696. + return 10;
  4697. + else
  4698. + return 5;
  4699. + case DMA_SET_MIN_PHYS:
  4700. + g_pMinPhys = (void __user *)arg;
  4701. + PRINTK(KERN_DEBUG "min/max user/phys bypass set to %p %p\n", g_pMinPhys, g_pMaxPhys);
  4702. + break;
  4703. + case DMA_SET_MAX_PHYS:
  4704. + g_pMaxPhys = (void __user *)arg;
  4705. + PRINTK(KERN_DEBUG "min/max user/phys bypass set to %p %p\n", g_pMinPhys, g_pMaxPhys);
  4706. + break;
  4707. + case DMA_SET_PHYS_OFFSET:
  4708. + g_physOffset = arg;
  4709. + PRINTK(KERN_DEBUG "user/phys bypass offset set to %ld\n", g_physOffset);
  4710. + break;
  4711. + case DMA_CMA_SET_SIZE:
  4712. + {
  4713. + unsigned int pBusAddr;
  4714. +
  4715. + if (g_cmaHandle)
  4716. + {
  4717. + PRINTK(KERN_ERR "memory has already been allocated (handle %d)\n", g_cmaHandle);
  4718. + return -EINVAL;
  4719. + }
  4720. +
  4721. + PRINTK(KERN_INFO "allocating %ld bytes of VC memory\n", arg * 4096);
  4722. +
  4723. + //get the memory
  4724. + if (AllocateVcMemory(&g_cmaHandle, arg * 4096, 4096, MEM_FLAG_L1_NONALLOCATING | MEM_FLAG_NO_INIT | MEM_FLAG_HINT_PERMALOCK))
  4725. + {
  4726. + PRINTK(KERN_ERR "failed to allocate %ld bytes of VC memory\n", arg * 4096);
  4727. + g_cmaHandle = 0;
  4728. + return -EINVAL;
  4729. + }
  4730. +
  4731. + //get an address for it
  4732. + PRINTK(KERN_INFO "trying to map VC memory\n");
  4733. +
  4734. + if (LockVcMemory(&pBusAddr, g_cmaHandle))
  4735. + {
  4736. + PRINTK(KERN_ERR "failed to map CMA handle %d, releasing memory\n", g_cmaHandle);
  4737. + ReleaseVcMemory(g_cmaHandle);
  4738. + g_cmaHandle = 0;
  4739. + }
  4740. +
  4741. + PRINTK(KERN_INFO "bus address for CMA memory is %x\n", pBusAddr);
  4742. + return pBusAddr;
  4743. + }
  4744. + case DMA_GET_VERSION:
  4745. + PRINTK(KERN_DEBUG "returning version number, %d\n", VERSION_NUMBER);
  4746. + return VERSION_NUMBER;
  4747. + default:
  4748. + PRINTK(KERN_DEBUG "unknown ioctl: %d\n", cmd);
  4749. + return -EINVAL;
  4750. + }
  4751. +
  4752. + return 0;
  4753. +}
  4754. +
  4755. +static ssize_t Read(struct file *pFile, char __user *pUser, size_t count, loff_t *offp)
  4756. +{
  4757. + return -EIO;
  4758. +}
  4759. +
  4760. +static int Mmap(struct file *pFile, struct vm_area_struct *pVma)
  4761. +{
  4762. + struct PageList *pPages;
  4763. + struct VmaPageList *pVmaList;
  4764. +
  4765. + PRINTK_VERBOSE(KERN_DEBUG "MMAP vma %p, length %ld (%s %d)\n",
  4766. + pVma, pVma->vm_end - pVma->vm_start,
  4767. + current->comm, current->pid);
  4768. + PRINTK_VERBOSE(KERN_DEBUG "MMAP %p %d (tracked %d)\n", pVma, current->pid, g_trackedPages);
  4769. +
  4770. + //make a new page list
  4771. + pPages = (struct PageList *)kmalloc(sizeof(struct PageList), GFP_KERNEL);
  4772. + if (!pPages)
  4773. + {
  4774. + PRINTK(KERN_ERR "couldn\'t allocate a new page list (%s %d)\n",
  4775. + current->comm, current->pid);
  4776. + return -ENOMEM;
  4777. + }
  4778. +
  4779. + //clear the page list
  4780. + pPages->m_used = 0;
  4781. + pPages->m_pNext = 0;
  4782. +
  4783. + //insert our vma and new page list somewhere
  4784. + if (!pVma->vm_private_data)
  4785. + {
  4786. + struct VmaPageList *pList;
  4787. +
  4788. + PRINTK_VERBOSE(KERN_DEBUG "new vma list, making new one (%s %d)\n",
  4789. + current->comm, current->pid);
  4790. +
  4791. + //make a new vma list
  4792. + pList = (struct VmaPageList *)kmalloc(sizeof(struct VmaPageList), GFP_KERNEL);
  4793. + if (!pList)
  4794. + {
  4795. + PRINTK(KERN_ERR "couldn\'t allocate vma page list (%s %d)\n",
  4796. + current->comm, current->pid);
  4797. + kfree(pPages);
  4798. + return -ENOMEM;
  4799. + }
  4800. +
  4801. + //clear this list
  4802. + pVma->vm_private_data = (void *)pList;
  4803. + pList->m_refCount = 0;
  4804. + }
  4805. +
  4806. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  4807. +
  4808. + //add it to the vma list
  4809. + pVmaList->m_pPageHead = pPages;
  4810. + pVmaList->m_pPageTail = pPages;
  4811. +
  4812. + pVma->vm_ops = &g_vmOps4k;
  4813. + pVma->vm_flags |= VM_IO;
  4814. +
  4815. + VmaOpen4k(pVma);
  4816. +
  4817. + return 0;
  4818. +}
  4819. +
  4820. +/****** VMA OPERATIONS ******/
  4821. +
  4822. +static void VmaOpen4k(struct vm_area_struct *pVma)
  4823. +{
  4824. + struct VmaPageList *pVmaList;
  4825. +
  4826. + PRINTK_VERBOSE(KERN_DEBUG "vma open %p private %p (%s %d), %d live pages\n", pVma, pVma->vm_private_data, current->comm, current->pid, g_trackedPages);
  4827. + PRINTK_VERBOSE(KERN_DEBUG "OPEN %p %d %ld pages (tracked pages %d)\n",
  4828. + pVma, current->pid, (pVma->vm_end - pVma->vm_start) >> 12,
  4829. + g_trackedPages);
  4830. +
  4831. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  4832. +
  4833. + if (pVmaList)
  4834. + {
  4835. + pVmaList->m_refCount++;
  4836. + PRINTK_VERBOSE(KERN_DEBUG "ref count is now %d\n", pVmaList->m_refCount);
  4837. + }
  4838. + else
  4839. + {
  4840. + PRINTK_VERBOSE(KERN_DEBUG "err, open but no vma page list\n");
  4841. + }
  4842. +}
  4843. +
  4844. +static void VmaClose4k(struct vm_area_struct *pVma)
  4845. +{
  4846. + struct VmaPageList *pVmaList;
  4847. + int freed = 0;
  4848. +
  4849. + PRINTK_VERBOSE(KERN_DEBUG "vma close %p private %p (%s %d)\n", pVma, pVma->vm_private_data, current->comm, current->pid);
  4850. +
  4851. + //wait for any dmas to finish
  4852. + DmaWaitAll();
  4853. +
  4854. + //find our vma in the list
  4855. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  4856. +
  4857. + //may be a fork
  4858. + if (pVmaList)
  4859. + {
  4860. + struct PageList *pPages;
  4861. +
  4862. + pVmaList->m_refCount--;
  4863. +
  4864. + if (pVmaList->m_refCount == 0)
  4865. + {
  4866. + PRINTK_VERBOSE(KERN_DEBUG "found vma, freeing pages (%s %d)\n",
  4867. + current->comm, current->pid);
  4868. +
  4869. + pPages = pVmaList->m_pPageHead;
  4870. +
  4871. + if (!pPages)
  4872. + {
  4873. + PRINTK(KERN_ERR "no page list (%s %d)!\n",
  4874. + current->comm, current->pid);
  4875. + return;
  4876. + }
  4877. +
  4878. + while (pPages)
  4879. + {
  4880. + struct PageList *next;
  4881. + int count;
  4882. +
  4883. + PRINTK_VERBOSE(KERN_DEBUG "page list (%s %d)\n",
  4884. + current->comm, current->pid);
  4885. +
  4886. + next = pPages->m_pNext;
  4887. + for (count = 0; count < pPages->m_used; count++)
  4888. + {
  4889. + PRINTK_VERBOSE(KERN_DEBUG "freeing page %p (%s %d)\n",
  4890. + pPages->m_pPages[count],
  4891. + current->comm, current->pid);
  4892. + __free_pages(pPages->m_pPages[count], 0);
  4893. + g_trackedPages--;
  4894. + freed++;
  4895. + }
  4896. +
  4897. + PRINTK_VERBOSE(KERN_DEBUG "freeing page list (%s %d)\n",
  4898. + current->comm, current->pid);
  4899. + kfree(pPages);
  4900. + pPages = next;
  4901. + }
  4902. +
  4903. + //remove our vma from the list
  4904. + kfree(pVmaList);
  4905. + pVma->vm_private_data = 0;
  4906. + }
  4907. + else
  4908. + {
  4909. + PRINTK_VERBOSE(KERN_DEBUG "ref count is %d, not closing\n", pVmaList->m_refCount);
  4910. + }
  4911. + }
  4912. + else
  4913. + {
  4914. + PRINTK_VERBOSE(KERN_ERR "uh-oh, vma %p not found (%s %d)!\n", pVma, current->comm, current->pid);
  4915. + PRINTK_VERBOSE(KERN_ERR "CLOSE ERR\n");
  4916. + }
  4917. +
  4918. + PRINTK_VERBOSE(KERN_DEBUG "CLOSE %p %d %d pages (tracked pages %d)",
  4919. + pVma, current->pid, freed, g_trackedPages);
  4920. +
  4921. + PRINTK_VERBOSE(KERN_DEBUG "%d pages open\n", g_trackedPages);
  4922. +}
  4923. +
  4924. +static int VmaFault4k(struct vm_area_struct *pVma, struct vm_fault *pVmf)
  4925. +{
  4926. + PRINTK_VERBOSE(KERN_DEBUG "vma fault for vma %p private %p at offset %ld (%s %d)\n", pVma, pVma->vm_private_data, pVmf->pgoff,
  4927. + current->comm, current->pid);
  4928. + PRINTK_VERBOSE(KERN_DEBUG "FAULT\n");
  4929. + pVmf->page = alloc_page(GFP_KERNEL);
  4930. +
  4931. + if (pVmf->page)
  4932. + {
  4933. + PRINTK_VERBOSE(KERN_DEBUG "alloc page virtual %p\n", page_address(pVmf->page));
  4934. + }
  4935. +
  4936. + if (!pVmf->page)
  4937. + {
  4938. + PRINTK(KERN_ERR "vma fault oom (%s %d)\n", current->comm, current->pid);
  4939. + return VM_FAULT_OOM;
  4940. + }
  4941. + else
  4942. + {
  4943. + struct VmaPageList *pVmaList;
  4944. +
  4945. + get_page(pVmf->page);
  4946. + g_trackedPages++;
  4947. +
  4948. + //find our vma in the list
  4949. + pVmaList = (struct VmaPageList *)pVma->vm_private_data;
  4950. +
  4951. + if (pVmaList)
  4952. + {
  4953. + PRINTK_VERBOSE(KERN_DEBUG "vma found (%s %d)\n", current->comm, current->pid);
  4954. +
  4955. + if (pVmaList->m_pPageTail->m_used == PAGES_PER_LIST)
  4956. + {
  4957. + PRINTK_VERBOSE(KERN_DEBUG "making new page list (%s %d)\n", current->comm, current->pid);
  4958. + //making a new page list
  4959. + pVmaList->m_pPageTail->m_pNext = (struct PageList *)kmalloc(sizeof(struct PageList), GFP_KERNEL);
  4960. + if (!pVmaList->m_pPageTail->m_pNext)
  4961. + return -ENOMEM;
  4962. +
  4963. + //update the tail pointer
  4964. + pVmaList->m_pPageTail = pVmaList->m_pPageTail->m_pNext;
  4965. + pVmaList->m_pPageTail->m_used = 0;
  4966. + pVmaList->m_pPageTail->m_pNext = 0;
  4967. + }
  4968. +
  4969. + PRINTK_VERBOSE(KERN_DEBUG "adding page to list (%s %d)\n", current->comm, current->pid);
  4970. +
  4971. + pVmaList->m_pPageTail->m_pPages[pVmaList->m_pPageTail->m_used] = pVmf->page;
  4972. + pVmaList->m_pPageTail->m_used++;
  4973. + }
  4974. + else
  4975. + PRINTK(KERN_ERR "returned page for vma we don\'t know %p (%s %d)\n", pVma, current->comm, current->pid);
  4976. +
  4977. + return 0;
  4978. + }
  4979. +}
  4980. +
  4981. +/****** GENERIC FUNCTIONS ******/
  4982. +static int __init dmaer_init(void)
  4983. +{
  4984. + int result = alloc_chrdev_region(&g_majorMinor, 0, 1, "dmaer");
  4985. + if (result < 0)
  4986. + {
  4987. + PRINTK(KERN_ERR "unable to get major device number\n");
  4988. + return result;
  4989. + }
  4990. + else
  4991. + PRINTK(KERN_DEBUG "major device number %d\n", MAJOR(g_majorMinor));
  4992. +
  4993. + PRINTK(KERN_DEBUG "vma list size %d, page list size %d, page size %ld\n",
  4994. + sizeof(struct VmaPageList), sizeof(struct PageList), PAGE_SIZE);
  4995. +
  4996. + //get a dma channel to work with
  4997. + result = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST, (void **)&g_pDmaChanBase, &g_dmaIrq);
  4998. +
  4999. + //uncomment to force to channel 0
  5000. + //result = 0;
  5001. + //g_pDmaChanBase = 0xce808000;
  5002. +
  5003. + if (result < 0)
  5004. + {
  5005. + PRINTK(KERN_ERR "failed to allocate dma channel\n");
  5006. + cdev_del(&g_cDev);
  5007. + unregister_chrdev_region(g_majorMinor, 1);
  5008. + }
  5009. +
  5010. + //reset the channel
  5011. + PRINTK(KERN_DEBUG "allocated dma channel %d (%p), initial state %08x\n", result, g_pDmaChanBase, *g_pDmaChanBase);
  5012. + *g_pDmaChanBase = 1 << 31;
  5013. + PRINTK(KERN_DEBUG "post-reset %08x\n", *g_pDmaChanBase);
  5014. +
  5015. + g_dmaChan = result;
  5016. +
  5017. + //clear the cache stats
  5018. + g_cacheHit = 0;
  5019. + g_cacheMiss = 0;
  5020. +
  5021. + //register our device - after this we are go go go
  5022. + cdev_init(&g_cDev, &g_fOps);
  5023. + g_cDev.owner = THIS_MODULE;
  5024. + g_cDev.ops = &g_fOps;
  5025. +
  5026. + result = cdev_add(&g_cDev, g_majorMinor, 1);
  5027. + if (result < 0)
  5028. + {
  5029. + PRINTK(KERN_ERR "failed to add character device\n");
  5030. + unregister_chrdev_region(g_majorMinor, 1);
  5031. + bcm_dma_chan_free(g_dmaChan);
  5032. + return result;
  5033. + }
  5034. +
  5035. + return 0;
  5036. +}
  5037. +
  5038. +static void __exit dmaer_exit(void)
  5039. +{
  5040. + PRINTK(KERN_INFO "closing dmaer device, cache stats: %d hits %d misses\n", g_cacheHit, g_cacheMiss);
  5041. + //unregister the device
  5042. + cdev_del(&g_cDev);
  5043. + unregister_chrdev_region(g_majorMinor, 1);
  5044. + //free the dma channel
  5045. + bcm_dma_chan_free(g_dmaChan);
  5046. +}
  5047. +
  5048. +MODULE_LICENSE("Dual BSD/GPL");
  5049. +MODULE_AUTHOR("Simon Hall");
  5050. +module_init(dmaer_init);
  5051. +module_exit(dmaer_exit);
  5052. +
  5053. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/arm_control.h linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h
  5054. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/arm_control.h 1970-01-01 01:00:00.000000000 +0100
  5055. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h 2014-04-24 15:35:00.777527936 +0200
  5056. @@ -0,0 +1,419 @@
  5057. +/*
  5058. + * linux/arch/arm/mach-bcm2708/arm_control.h
  5059. + *
  5060. + * Copyright (C) 2010 Broadcom
  5061. + *
  5062. + * This program is free software; you can redistribute it and/or modify
  5063. + * it under the terms of the GNU General Public License as published by
  5064. + * the Free Software Foundation; either version 2 of the License, or
  5065. + * (at your option) any later version.
  5066. + *
  5067. + * This program is distributed in the hope that it will be useful,
  5068. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5069. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5070. + * GNU General Public License for more details.
  5071. + *
  5072. + * You should have received a copy of the GNU General Public License
  5073. + * along with this program; if not, write to the Free Software
  5074. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5075. + */
  5076. +
  5077. +#ifndef __BCM2708_ARM_CONTROL_H
  5078. +#define __BCM2708_ARM_CONTROL_H
  5079. +
  5080. +/*
  5081. + * Definitions and addresses for the ARM CONTROL logic
  5082. + * This file is manually generated.
  5083. + */
  5084. +
  5085. +#define ARM_BASE 0x7E00B000
  5086. +
  5087. +/* Basic configuration */
  5088. +#define ARM_CONTROL0 HW_REGISTER_RW(ARM_BASE+0x000)
  5089. +#define ARM_C0_SIZ128M 0x00000000
  5090. +#define ARM_C0_SIZ256M 0x00000001
  5091. +#define ARM_C0_SIZ512M 0x00000002
  5092. +#define ARM_C0_SIZ1G 0x00000003
  5093. +#define ARM_C0_BRESP0 0x00000000
  5094. +#define ARM_C0_BRESP1 0x00000004
  5095. +#define ARM_C0_BRESP2 0x00000008
  5096. +#define ARM_C0_BOOTHI 0x00000010
  5097. +#define ARM_C0_UNUSED05 0x00000020 /* free */
  5098. +#define ARM_C0_FULLPERI 0x00000040
  5099. +#define ARM_C0_UNUSED78 0x00000180 /* free */
  5100. +#define ARM_C0_JTAGMASK 0x00000E00
  5101. +#define ARM_C0_JTAGOFF 0x00000000
  5102. +#define ARM_C0_JTAGBASH 0x00000800 /* Debug on GPIO off */
  5103. +#define ARM_C0_JTAGGPIO 0x00000C00 /* Debug on GPIO on */
  5104. +#define ARM_C0_APROTMSK 0x0000F000
  5105. +#define ARM_C0_DBG0SYNC 0x00010000 /* VPU0 halt sync */
  5106. +#define ARM_C0_DBG1SYNC 0x00020000 /* VPU1 halt sync */
  5107. +#define ARM_C0_SWDBGREQ 0x00040000 /* HW debug request */
  5108. +#define ARM_C0_PASSHALT 0x00080000 /* ARM halt passed to debugger */
  5109. +#define ARM_C0_PRIO_PER 0x00F00000 /* per priority mask */
  5110. +#define ARM_C0_PRIO_L2 0x0F000000
  5111. +#define ARM_C0_PRIO_UC 0xF0000000
  5112. +
  5113. +#define ARM_C0_APROTPASS 0x0000A000 /* Translate 1:1 */
  5114. +#define ARM_C0_APROTUSER 0x00000000 /* Only user mode */
  5115. +#define ARM_C0_APROTSYST 0x0000F000 /* Only system mode */
  5116. +
  5117. +
  5118. +#define ARM_CONTROL1 HW_REGISTER_RW(ARM_BASE+0x440)
  5119. +#define ARM_C1_TIMER 0x00000001 /* re-route timer IRQ to VC */
  5120. +#define ARM_C1_MAIL 0x00000002 /* re-route Mail IRQ to VC */
  5121. +#define ARM_C1_BELL0 0x00000004 /* re-route Doorbell 0 to VC */
  5122. +#define ARM_C1_BELL1 0x00000008 /* re-route Doorbell 1 to VC */
  5123. +#define ARM_C1_PERSON 0x00000100 /* peripherals on */
  5124. +#define ARM_C1_REQSTOP 0x00000200 /* ASYNC bridge request stop */
  5125. +
  5126. +#define ARM_STATUS HW_REGISTER_RW(ARM_BASE+0x444)
  5127. +#define ARM_S_ACKSTOP 0x80000000 /* Bridge stopped */
  5128. +#define ARM_S_READPEND 0x000003FF /* pending reads counter */
  5129. +#define ARM_S_WRITPEND 0x000FFC00 /* pending writes counter */
  5130. +
  5131. +#define ARM_ERRHALT HW_REGISTER_RW(ARM_BASE+0x448)
  5132. +#define ARM_EH_PERIBURST 0x00000001 /* Burst write seen on peri bus */
  5133. +#define ARM_EH_ILLADDRS1 0x00000002 /* Address bits 25-27 error */
  5134. +#define ARM_EH_ILLADDRS2 0x00000004 /* Address bits 31-28 error */
  5135. +#define ARM_EH_VPU0HALT 0x00000008 /* VPU0 halted & in debug mode */
  5136. +#define ARM_EH_VPU1HALT 0x00000010 /* VPU1 halted & in debug mode */
  5137. +#define ARM_EH_ARMHALT 0x00000020 /* ARM in halted debug mode */
  5138. +
  5139. +#define ARM_ID_SECURE HW_REGISTER_RW(ARM_BASE+0x00C)
  5140. +#define ARM_ID HW_REGISTER_RW(ARM_BASE+0x44C)
  5141. +#define ARM_IDVAL 0x364D5241
  5142. +
  5143. +/* Translation memory */
  5144. +#define ARM_TRANSLATE HW_REGISTER_RW(ARM_BASE+0x100)
  5145. +/* 32 locations: 0x100.. 0x17F */
  5146. +/* 32 spare means we CAN go to 64 pages.... */
  5147. +
  5148. +
  5149. +/* Interrupts */
  5150. +#define ARM_IRQ_PEND0 HW_REGISTER_RW(ARM_BASE+0x200) /* Top IRQ bits */
  5151. +#define ARM_I0_TIMER 0x00000001 /* timer IRQ */
  5152. +#define ARM_I0_MAIL 0x00000002 /* Mail IRQ */
  5153. +#define ARM_I0_BELL0 0x00000004 /* Doorbell 0 */
  5154. +#define ARM_I0_BELL1 0x00000008 /* Doorbell 1 */
  5155. +#define ARM_I0_BANK1 0x00000100 /* Bank1 IRQ */
  5156. +#define ARM_I0_BANK2 0x00000200 /* Bank2 IRQ */
  5157. +
  5158. +#define ARM_IRQ_PEND1 HW_REGISTER_RW(ARM_BASE+0x204) /* All bank1 IRQ bits */
  5159. +/* todo: all I1_interrupt sources */
  5160. +#define ARM_IRQ_PEND2 HW_REGISTER_RW(ARM_BASE+0x208) /* All bank2 IRQ bits */
  5161. +/* todo: all I2_interrupt sources */
  5162. +
  5163. +#define ARM_IRQ_FAST HW_REGISTER_RW(ARM_BASE+0x20C) /* FIQ control */
  5164. +#define ARM_IF_INDEX 0x0000007F /* FIQ select */
  5165. +#define ARM_IF_ENABLE 0x00000080 /* FIQ enable */
  5166. +#define ARM_IF_VCMASK 0x0000003F /* FIQ = (index from VC source) */
  5167. +#define ARM_IF_TIMER 0x00000040 /* FIQ = ARM timer */
  5168. +#define ARM_IF_MAIL 0x00000041 /* FIQ = ARM Mail */
  5169. +#define ARM_IF_BELL0 0x00000042 /* FIQ = ARM Doorbell 0 */
  5170. +#define ARM_IF_BELL1 0x00000043 /* FIQ = ARM Doorbell 1 */
  5171. +#define ARM_IF_VP0HALT 0x00000044 /* FIQ = VPU0 Halt seen */
  5172. +#define ARM_IF_VP1HALT 0x00000045 /* FIQ = VPU1 Halt seen */
  5173. +#define ARM_IF_ILLEGAL 0x00000046 /* FIQ = Illegal access seen */
  5174. +
  5175. +#define ARM_IRQ_ENBL1 HW_REGISTER_RW(ARM_BASE+0x210) /* Bank1 enable bits */
  5176. +#define ARM_IRQ_ENBL2 HW_REGISTER_RW(ARM_BASE+0x214) /* Bank2 enable bits */
  5177. +#define ARM_IRQ_ENBL3 HW_REGISTER_RW(ARM_BASE+0x218) /* ARM irqs enable bits */
  5178. +#define ARM_IRQ_DIBL1 HW_REGISTER_RW(ARM_BASE+0x21C) /* Bank1 disable bits */
  5179. +#define ARM_IRQ_DIBL2 HW_REGISTER_RW(ARM_BASE+0x220) /* Bank2 disable bits */
  5180. +#define ARM_IRQ_DIBL3 HW_REGISTER_RW(ARM_BASE+0x224) /* ARM irqs disable bits */
  5181. +#define ARM_IE_TIMER 0x00000001 /* Timer IRQ */
  5182. +#define ARM_IE_MAIL 0x00000002 /* Mail IRQ */
  5183. +#define ARM_IE_BELL0 0x00000004 /* Doorbell 0 */
  5184. +#define ARM_IE_BELL1 0x00000008 /* Doorbell 1 */
  5185. +#define ARM_IE_VP0HALT 0x00000010 /* VPU0 Halt */
  5186. +#define ARM_IE_VP1HALT 0x00000020 /* VPU1 Halt */
  5187. +#define ARM_IE_ILLEGAL 0x00000040 /* Illegal access seen */
  5188. +
  5189. +/* Timer */
  5190. +/* For reg. fields see sp804 spec. */
  5191. +#define ARM_T_LOAD HW_REGISTER_RW(ARM_BASE+0x400)
  5192. +#define ARM_T_VALUE HW_REGISTER_RW(ARM_BASE+0x404)
  5193. +#define ARM_T_CONTROL HW_REGISTER_RW(ARM_BASE+0x408)
  5194. +#define ARM_T_IRQCNTL HW_REGISTER_RW(ARM_BASE+0x40C)
  5195. +#define ARM_T_RAWIRQ HW_REGISTER_RW(ARM_BASE+0x410)
  5196. +#define ARM_T_MSKIRQ HW_REGISTER_RW(ARM_BASE+0x414)
  5197. +#define ARM_T_RELOAD HW_REGISTER_RW(ARM_BASE+0x418)
  5198. +#define ARM_T_PREDIV HW_REGISTER_RW(ARM_BASE+0x41c)
  5199. +#define ARM_T_FREECNT HW_REGISTER_RW(ARM_BASE+0x420)
  5200. +
  5201. +#define TIMER_CTRL_ONESHOT (1 << 0)
  5202. +#define TIMER_CTRL_32BIT (1 << 1)
  5203. +#define TIMER_CTRL_DIV1 (0 << 2)
  5204. +#define TIMER_CTRL_DIV16 (1 << 2)
  5205. +#define TIMER_CTRL_DIV256 (2 << 2)
  5206. +#define TIMER_CTRL_IE (1 << 5)
  5207. +#define TIMER_CTRL_PERIODIC (1 << 6)
  5208. +#define TIMER_CTRL_ENABLE (1 << 7)
  5209. +#define TIMER_CTRL_DBGHALT (1 << 8)
  5210. +#define TIMER_CTRL_ENAFREE (1 << 9)
  5211. +#define TIMER_CTRL_FREEDIV_SHIFT 16)
  5212. +#define TIMER_CTRL_FREEDIV_MASK 0xff
  5213. +
  5214. +/* Semaphores, Doorbells, Mailboxes */
  5215. +#define ARM_SBM_OWN0 (ARM_BASE+0x800)
  5216. +#define ARM_SBM_OWN1 (ARM_BASE+0x900)
  5217. +#define ARM_SBM_OWN2 (ARM_BASE+0xA00)
  5218. +#define ARM_SBM_OWN3 (ARM_BASE+0xB00)
  5219. +
  5220. +/* MAILBOXES
  5221. + * Register flags are common across all
  5222. + * owner registers. See end of this section
  5223. + *
  5224. + * Semaphores, Doorbells, Mailboxes Owner 0
  5225. + *
  5226. + */
  5227. +
  5228. +#define ARM_0_SEMS HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5229. +#define ARM_0_SEM0 HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5230. +#define ARM_0_SEM1 HW_REGISTER_RW(ARM_SBM_OWN0+0x04)
  5231. +#define ARM_0_SEM2 HW_REGISTER_RW(ARM_SBM_OWN0+0x08)
  5232. +#define ARM_0_SEM3 HW_REGISTER_RW(ARM_SBM_OWN0+0x0C)
  5233. +#define ARM_0_SEM4 HW_REGISTER_RW(ARM_SBM_OWN0+0x10)
  5234. +#define ARM_0_SEM5 HW_REGISTER_RW(ARM_SBM_OWN0+0x14)
  5235. +#define ARM_0_SEM6 HW_REGISTER_RW(ARM_SBM_OWN0+0x18)
  5236. +#define ARM_0_SEM7 HW_REGISTER_RW(ARM_SBM_OWN0+0x1C)
  5237. +#define ARM_0_BELL0 HW_REGISTER_RW(ARM_SBM_OWN0+0x40)
  5238. +#define ARM_0_BELL1 HW_REGISTER_RW(ARM_SBM_OWN0+0x44)
  5239. +#define ARM_0_BELL2 HW_REGISTER_RW(ARM_SBM_OWN0+0x48)
  5240. +#define ARM_0_BELL3 HW_REGISTER_RW(ARM_SBM_OWN0+0x4C)
  5241. +/* MAILBOX 0 access in Owner 0 area */
  5242. +/* Some addresses should ONLY be used by owner 0 */
  5243. +#define ARM_0_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) */
  5244. +#define ARM_0_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) Normal read */
  5245. +#define ARM_0_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN0+0x90) /* none-pop read */
  5246. +#define ARM_0_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN0+0x94) /* Sender read (only LS 2 bits) */
  5247. +#define ARM_0_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN0+0x98) /* Status read */
  5248. +#define ARM_0_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0x9C) /* Config read/write */
  5249. +/* MAILBOX 1 access in Owner 0 area */
  5250. +/* Owner 0 should only WRITE to this mailbox */
  5251. +#define ARM_0_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) /* .. 0xAC (4 locations) */
  5252. +/*#define ARM_0_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) */ /* DO NOT USE THIS !!!!! */
  5253. +/*#define ARM_0_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN0+0xB0) */ /* DO NOT USE THIS !!!!! */
  5254. +/*#define ARM_0_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN0+0xB4) */ /* DO NOT USE THIS !!!!! */
  5255. +#define ARM_0_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN0+0xB8) /* Status read */
  5256. +/*#define ARM_0_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0xBC) */ /* DO NOT USE THIS !!!!! */
  5257. +/* General SEM, BELL, MAIL config/status */
  5258. +#define ARM_0_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE0) /* semaphore clear/debug register */
  5259. +#define ARM_0_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE4) /* Doorbells clear/debug register */
  5260. +#define ARM_0_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xF8) /* ALL interrupts */
  5261. +#define ARM_0_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xFC) /* IRQS pending for owner 0 */
  5262. +
  5263. +/* Semaphores, Doorbells, Mailboxes Owner 1 */
  5264. +#define ARM_1_SEMS HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5265. +#define ARM_1_SEM0 HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5266. +#define ARM_1_SEM1 HW_REGISTER_RW(ARM_SBM_OWN1+0x04)
  5267. +#define ARM_1_SEM2 HW_REGISTER_RW(ARM_SBM_OWN1+0x08)
  5268. +#define ARM_1_SEM3 HW_REGISTER_RW(ARM_SBM_OWN1+0x0C)
  5269. +#define ARM_1_SEM4 HW_REGISTER_RW(ARM_SBM_OWN1+0x10)
  5270. +#define ARM_1_SEM5 HW_REGISTER_RW(ARM_SBM_OWN1+0x14)
  5271. +#define ARM_1_SEM6 HW_REGISTER_RW(ARM_SBM_OWN1+0x18)
  5272. +#define ARM_1_SEM7 HW_REGISTER_RW(ARM_SBM_OWN1+0x1C)
  5273. +#define ARM_1_BELL0 HW_REGISTER_RW(ARM_SBM_OWN1+0x40)
  5274. +#define ARM_1_BELL1 HW_REGISTER_RW(ARM_SBM_OWN1+0x44)
  5275. +#define ARM_1_BELL2 HW_REGISTER_RW(ARM_SBM_OWN1+0x48)
  5276. +#define ARM_1_BELL3 HW_REGISTER_RW(ARM_SBM_OWN1+0x4C)
  5277. +/* MAILBOX 0 access in Owner 0 area */
  5278. +/* Owner 1 should only WRITE to this mailbox */
  5279. +#define ARM_1_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0x80) /* .. 0x8C (4 locations) */
  5280. +/*#define ARM_1_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN1+0x80) */ /* DO NOT USE THIS !!!!! */
  5281. +/*#define ARM_1_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN1+0x90) */ /* DO NOT USE THIS !!!!! */
  5282. +/*#define ARM_1_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN1+0x94) */ /* DO NOT USE THIS !!!!! */
  5283. +#define ARM_1_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN1+0x98) /* Status read */
  5284. +/*#define ARM_1_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0x9C) */ /* DO NOT USE THIS !!!!! */
  5285. +/* MAILBOX 1 access in Owner 0 area */
  5286. +#define ARM_1_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) */
  5287. +#define ARM_1_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) Normal read */
  5288. +#define ARM_1_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN1+0xB0) /* none-pop read */
  5289. +#define ARM_1_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN1+0xB4) /* Sender read (only LS 2 bits) */
  5290. +#define ARM_1_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN1+0xB8) /* Status read */
  5291. +#define ARM_1_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0xBC)
  5292. +/* General SEM, BELL, MAIL config/status */
  5293. +#define ARM_1_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE0) /* semaphore clear/debug register */
  5294. +#define ARM_1_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE4) /* Doorbells clear/debug register */
  5295. +#define ARM_1_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xFC) /* IRQS pending for owner 1 */
  5296. +#define ARM_1_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xF8) /* ALL interrupts */
  5297. +
  5298. +/* Semaphores, Doorbells, Mailboxes Owner 2 */
  5299. +#define ARM_2_SEMS HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5300. +#define ARM_2_SEM0 HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5301. +#define ARM_2_SEM1 HW_REGISTER_RW(ARM_SBM_OWN2+0x04)
  5302. +#define ARM_2_SEM2 HW_REGISTER_RW(ARM_SBM_OWN2+0x08)
  5303. +#define ARM_2_SEM3 HW_REGISTER_RW(ARM_SBM_OWN2+0x0C)
  5304. +#define ARM_2_SEM4 HW_REGISTER_RW(ARM_SBM_OWN2+0x10)
  5305. +#define ARM_2_SEM5 HW_REGISTER_RW(ARM_SBM_OWN2+0x14)
  5306. +#define ARM_2_SEM6 HW_REGISTER_RW(ARM_SBM_OWN2+0x18)
  5307. +#define ARM_2_SEM7 HW_REGISTER_RW(ARM_SBM_OWN2+0x1C)
  5308. +#define ARM_2_BELL0 HW_REGISTER_RW(ARM_SBM_OWN2+0x40)
  5309. +#define ARM_2_BELL1 HW_REGISTER_RW(ARM_SBM_OWN2+0x44)
  5310. +#define ARM_2_BELL2 HW_REGISTER_RW(ARM_SBM_OWN2+0x48)
  5311. +#define ARM_2_BELL3 HW_REGISTER_RW(ARM_SBM_OWN2+0x4C)
  5312. +/* MAILBOX 0 access in Owner 2 area */
  5313. +/* Owner 2 should only WRITE to this mailbox */
  5314. +#define ARM_2_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0x80) /* .. 0x8C (4 locations) */
  5315. +/*#define ARM_2_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN2+0x80) */ /* DO NOT USE THIS !!!!! */
  5316. +/*#define ARM_2_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN2+0x90) */ /* DO NOT USE THIS !!!!! */
  5317. +/*#define ARM_2_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN2+0x94) */ /* DO NOT USE THIS !!!!! */
  5318. +#define ARM_2_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN2+0x98) /* Status read */
  5319. +/*#define ARM_2_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0x9C) */ /* DO NOT USE THIS !!!!! */
  5320. +/* MAILBOX 1 access in Owner 2 area */
  5321. +/* Owner 2 should only WRITE to this mailbox */
  5322. +#define ARM_2_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) /* .. 0xAC (4 locations) */
  5323. +/*#define ARM_2_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) */ /* DO NOT USE THIS !!!!! */
  5324. +/*#define ARM_2_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN2+0xB0) */ /* DO NOT USE THIS !!!!! */
  5325. +/*#define ARM_2_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN2+0xB4) */ /* DO NOT USE THIS !!!!! */
  5326. +#define ARM_2_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN2+0xB8) /* Status read */
  5327. +/*#define ARM_2_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0xBC) */ /* DO NOT USE THIS !!!!! */
  5328. +/* General SEM, BELL, MAIL config/status */
  5329. +#define ARM_2_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE0) /* semaphore clear/debug register */
  5330. +#define ARM_2_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE4) /* Doorbells clear/debug register */
  5331. +#define ARM_2_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xFC) /* IRQS pending for owner 2 */
  5332. +#define ARM_2_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xF8) /* ALL interrupts */
  5333. +
  5334. +/* Semaphores, Doorbells, Mailboxes Owner 3 */
  5335. +#define ARM_3_SEMS HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5336. +#define ARM_3_SEM0 HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5337. +#define ARM_3_SEM1 HW_REGISTER_RW(ARM_SBM_OWN3+0x04)
  5338. +#define ARM_3_SEM2 HW_REGISTER_RW(ARM_SBM_OWN3+0x08)
  5339. +#define ARM_3_SEM3 HW_REGISTER_RW(ARM_SBM_OWN3+0x0C)
  5340. +#define ARM_3_SEM4 HW_REGISTER_RW(ARM_SBM_OWN3+0x10)
  5341. +#define ARM_3_SEM5 HW_REGISTER_RW(ARM_SBM_OWN3+0x14)
  5342. +#define ARM_3_SEM6 HW_REGISTER_RW(ARM_SBM_OWN3+0x18)
  5343. +#define ARM_3_SEM7 HW_REGISTER_RW(ARM_SBM_OWN3+0x1C)
  5344. +#define ARM_3_BELL0 HW_REGISTER_RW(ARM_SBM_OWN3+0x40)
  5345. +#define ARM_3_BELL1 HW_REGISTER_RW(ARM_SBM_OWN3+0x44)
  5346. +#define ARM_3_BELL2 HW_REGISTER_RW(ARM_SBM_OWN3+0x48)
  5347. +#define ARM_3_BELL3 HW_REGISTER_RW(ARM_SBM_OWN3+0x4C)
  5348. +/* MAILBOX 0 access in Owner 3 area */
  5349. +/* Owner 3 should only WRITE to this mailbox */
  5350. +#define ARM_3_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0x80) /* .. 0x8C (4 locations) */
  5351. +/*#define ARM_3_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN3+0x80) */ /* DO NOT USE THIS !!!!! */
  5352. +/*#define ARM_3_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN3+0x90) */ /* DO NOT USE THIS !!!!! */
  5353. +/*#define ARM_3_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN3+0x94) */ /* DO NOT USE THIS !!!!! */
  5354. +#define ARM_3_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN3+0x98) /* Status read */
  5355. +/*#define ARM_3_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0x9C) */ /* DO NOT USE THIS !!!!! */
  5356. +/* MAILBOX 1 access in Owner 3 area */
  5357. +/* Owner 3 should only WRITE to this mailbox */
  5358. +#define ARM_3_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) /* .. 0xAC (4 locations) */
  5359. +/*#define ARM_3_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) */ /* DO NOT USE THIS !!!!! */
  5360. +/*#define ARM_3_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN3+0xB0) */ /* DO NOT USE THIS !!!!! */
  5361. +/*#define ARM_3_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN3+0xB4) */ /* DO NOT USE THIS !!!!! */
  5362. +#define ARM_3_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN3+0xB8) /* Status read */
  5363. +/*#define ARM_3_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0xBC) */ /* DO NOT USE THIS !!!!! */
  5364. +/* General SEM, BELL, MAIL config/status */
  5365. +#define ARM_3_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE0) /* semaphore clear/debug register */
  5366. +#define ARM_3_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE4) /* Doorbells clear/debug register */
  5367. +#define ARM_3_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xFC) /* IRQS pending for owner 3 */
  5368. +#define ARM_3_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xF8) /* ALL interrupts */
  5369. +
  5370. +
  5371. +
  5372. +/* Mailbox flags. Valid for all owners */
  5373. +
  5374. +/* Mailbox status register (...0x98) */
  5375. +#define ARM_MS_FULL 0x80000000
  5376. +#define ARM_MS_EMPTY 0x40000000
  5377. +#define ARM_MS_LEVEL 0x400000FF /* Max. value depdnds on mailbox depth parameter */
  5378. +
  5379. +/* MAILBOX config/status register (...0x9C) */
  5380. +/* ANY write to this register clears the error bits! */
  5381. +#define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mailbox irq enable: has data */
  5382. +#define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mailbox irq enable: has space */
  5383. +#define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mailbox irq enable: Opp. is empty */
  5384. +#define ARM_MC_MAIL_CLEAR 0x00000008 /* mailbox clear write 1, then 0 */
  5385. +#define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mailbox irq pending: has space */
  5386. +#define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mailbox irq pending: Opp. is empty */
  5387. +#define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mailbox irq pending */
  5388. +/* Bit 7 is unused */
  5389. +#define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
  5390. +#define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
  5391. +#define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
  5392. +
  5393. +/* Semaphore clear/debug register (...0xE0) */
  5394. +#define ARM_SD_OWN0 0x00000003 /* Owner of sem 0 */
  5395. +#define ARM_SD_OWN1 0x0000000C /* Owner of sem 1 */
  5396. +#define ARM_SD_OWN2 0x00000030 /* Owner of sem 2 */
  5397. +#define ARM_SD_OWN3 0x000000C0 /* Owner of sem 3 */
  5398. +#define ARM_SD_OWN4 0x00000300 /* Owner of sem 4 */
  5399. +#define ARM_SD_OWN5 0x00000C00 /* Owner of sem 5 */
  5400. +#define ARM_SD_OWN6 0x00003000 /* Owner of sem 6 */
  5401. +#define ARM_SD_OWN7 0x0000C000 /* Owner of sem 7 */
  5402. +#define ARM_SD_SEM0 0x00010000 /* Status of sem 0 */
  5403. +#define ARM_SD_SEM1 0x00020000 /* Status of sem 1 */
  5404. +#define ARM_SD_SEM2 0x00040000 /* Status of sem 2 */
  5405. +#define ARM_SD_SEM3 0x00080000 /* Status of sem 3 */
  5406. +#define ARM_SD_SEM4 0x00100000 /* Status of sem 4 */
  5407. +#define ARM_SD_SEM5 0x00200000 /* Status of sem 5 */
  5408. +#define ARM_SD_SEM6 0x00400000 /* Status of sem 6 */
  5409. +#define ARM_SD_SEM7 0x00800000 /* Status of sem 7 */
  5410. +
  5411. +/* Doorbells clear/debug register (...0xE4) */
  5412. +#define ARM_BD_OWN0 0x00000003 /* Owner of doorbell 0 */
  5413. +#define ARM_BD_OWN1 0x0000000C /* Owner of doorbell 1 */
  5414. +#define ARM_BD_OWN2 0x00000030 /* Owner of doorbell 2 */
  5415. +#define ARM_BD_OWN3 0x000000C0 /* Owner of doorbell 3 */
  5416. +#define ARM_BD_BELL0 0x00000100 /* Status of doorbell 0 */
  5417. +#define ARM_BD_BELL1 0x00000200 /* Status of doorbell 1 */
  5418. +#define ARM_BD_BELL2 0x00000400 /* Status of doorbell 2 */
  5419. +#define ARM_BD_BELL3 0x00000800 /* Status of doorbell 3 */
  5420. +
  5421. +/* MY IRQS register (...0xF8) */
  5422. +#define ARM_MYIRQ_BELL 0x00000001 /* This owner has a doorbell IRQ */
  5423. +#define ARM_MYIRQ_MAIL 0x00000002 /* This owner has a mailbox IRQ */
  5424. +
  5425. +/* ALL IRQS register (...0xF8) */
  5426. +#define ARM_AIS_BELL0 0x00000001 /* Doorbell 0 IRQ pending */
  5427. +#define ARM_AIS_BELL1 0x00000002 /* Doorbell 1 IRQ pending */
  5428. +#define ARM_AIS_BELL2 0x00000004 /* Doorbell 2 IRQ pending */
  5429. +#define ARM_AIS_BELL3 0x00000008 /* Doorbell 3 IRQ pending */
  5430. +#define ARM_AIS0_HAVEDATA 0x00000010 /* MAIL 0 has data IRQ pending */
  5431. +#define ARM_AIS0_HAVESPAC 0x00000020 /* MAIL 0 has space IRQ pending */
  5432. +#define ARM_AIS0_OPPEMPTY 0x00000040 /* MAIL 0 opposite is empty IRQ */
  5433. +#define ARM_AIS1_HAVEDATA 0x00000080 /* MAIL 1 has data IRQ pending */
  5434. +#define ARM_AIS1_HAVESPAC 0x00000100 /* MAIL 1 has space IRQ pending */
  5435. +#define ARM_AIS1_OPPEMPTY 0x00000200 /* MAIL 1 opposite is empty IRQ */
  5436. +/* Note that bell-0, bell-1 and MAIL0 IRQ go only to the ARM */
  5437. +/* Whilst that bell-2, bell-3 and MAIL1 IRQ go only to the VC */
  5438. +/* */
  5439. +/* ARM JTAG BASH */
  5440. +/* */
  5441. +#define AJB_BASE 0x7e2000c0
  5442. +
  5443. +#define AJBCONF HW_REGISTER_RW(AJB_BASE+0x00)
  5444. +#define AJB_BITS0 0x000000
  5445. +#define AJB_BITS4 0x000004
  5446. +#define AJB_BITS8 0x000008
  5447. +#define AJB_BITS12 0x00000C
  5448. +#define AJB_BITS16 0x000010
  5449. +#define AJB_BITS20 0x000014
  5450. +#define AJB_BITS24 0x000018
  5451. +#define AJB_BITS28 0x00001C
  5452. +#define AJB_BITS32 0x000020
  5453. +#define AJB_BITS34 0x000022
  5454. +#define AJB_OUT_MS 0x000040
  5455. +#define AJB_OUT_LS 0x000000
  5456. +#define AJB_INV_CLK 0x000080
  5457. +#define AJB_D0_RISE 0x000100
  5458. +#define AJB_D0_FALL 0x000000
  5459. +#define AJB_D1_RISE 0x000200
  5460. +#define AJB_D1_FALL 0x000000
  5461. +#define AJB_IN_RISE 0x000400
  5462. +#define AJB_IN_FALL 0x000000
  5463. +#define AJB_ENABLE 0x000800
  5464. +#define AJB_HOLD0 0x000000
  5465. +#define AJB_HOLD1 0x001000
  5466. +#define AJB_HOLD2 0x002000
  5467. +#define AJB_HOLD3 0x003000
  5468. +#define AJB_RESETN 0x004000
  5469. +#define AJB_CLKSHFT 16
  5470. +#define AJB_BUSY 0x80000000
  5471. +#define AJBTMS HW_REGISTER_RW(AJB_BASE+0x04)
  5472. +#define AJBTDI HW_REGISTER_RW(AJB_BASE+0x08)
  5473. +#define AJBTDO HW_REGISTER_RW(AJB_BASE+0x0c)
  5474. +
  5475. +#endif
  5476. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/arm_power.h linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5477. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/arm_power.h 1970-01-01 01:00:00.000000000 +0100
  5478. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h 2014-04-24 15:35:00.777527936 +0200
  5479. @@ -0,0 +1,60 @@
  5480. +/*
  5481. + * linux/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5482. + *
  5483. + * Copyright (C) 2010 Broadcom
  5484. + *
  5485. + * This program is free software; you can redistribute it and/or modify
  5486. + * it under the terms of the GNU General Public License as published by
  5487. + * the Free Software Foundation; either version 2 of the License, or
  5488. + * (at your option) any later version.
  5489. + *
  5490. + * This program is distributed in the hope that it will be useful,
  5491. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5492. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5493. + * GNU General Public License for more details.
  5494. + *
  5495. + * You should have received a copy of the GNU General Public License
  5496. + * along with this program; if not, write to the Free Software
  5497. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5498. + */
  5499. +
  5500. +#ifndef _ARM_POWER_H
  5501. +#define _ARM_POWER_H
  5502. +
  5503. +/* Use meaningful names on each side */
  5504. +#ifdef __VIDEOCORE__
  5505. +#define PREFIX(x) ARM_##x
  5506. +#else
  5507. +#define PREFIX(x) BCM_##x
  5508. +#endif
  5509. +
  5510. +enum {
  5511. + PREFIX(POWER_SDCARD_BIT),
  5512. + PREFIX(POWER_UART_BIT),
  5513. + PREFIX(POWER_MINIUART_BIT),
  5514. + PREFIX(POWER_USB_BIT),
  5515. + PREFIX(POWER_I2C0_BIT),
  5516. + PREFIX(POWER_I2C1_BIT),
  5517. + PREFIX(POWER_I2C2_BIT),
  5518. + PREFIX(POWER_SPI_BIT),
  5519. + PREFIX(POWER_CCP2TX_BIT),
  5520. +
  5521. + PREFIX(POWER_MAX)
  5522. +};
  5523. +
  5524. +enum {
  5525. + PREFIX(POWER_SDCARD) = (1 << PREFIX(POWER_SDCARD_BIT)),
  5526. + PREFIX(POWER_UART) = (1 << PREFIX(POWER_UART_BIT)),
  5527. + PREFIX(POWER_MINIUART) = (1 << PREFIX(POWER_MINIUART_BIT)),
  5528. + PREFIX(POWER_USB) = (1 << PREFIX(POWER_USB_BIT)),
  5529. + PREFIX(POWER_I2C0) = (1 << PREFIX(POWER_I2C0_BIT)),
  5530. + PREFIX(POWER_I2C1_MASK) = (1 << PREFIX(POWER_I2C1_BIT)),
  5531. + PREFIX(POWER_I2C2_MASK) = (1 << PREFIX(POWER_I2C2_BIT)),
  5532. + PREFIX(POWER_SPI_MASK) = (1 << PREFIX(POWER_SPI_BIT)),
  5533. + PREFIX(POWER_CCP2TX_MASK) = (1 << PREFIX(POWER_CCP2TX_BIT)),
  5534. +
  5535. + PREFIX(POWER_MASK) = (1 << PREFIX(POWER_MAX)) - 1,
  5536. + PREFIX(POWER_NONE) = 0
  5537. +};
  5538. +
  5539. +#endif
  5540. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/clkdev.h linux-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h
  5541. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/clkdev.h 1970-01-01 01:00:00.000000000 +0100
  5542. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h 2014-04-24 15:35:00.777527936 +0200
  5543. @@ -0,0 +1,7 @@
  5544. +#ifndef __ASM_MACH_CLKDEV_H
  5545. +#define __ASM_MACH_CLKDEV_H
  5546. +
  5547. +#define __clk_get(clk) ({ 1; })
  5548. +#define __clk_put(clk) do { } while (0)
  5549. +
  5550. +#endif
  5551. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/debug-macro.S linux-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S
  5552. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/debug-macro.S 1970-01-01 01:00:00.000000000 +0100
  5553. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S 2014-04-24 15:35:00.777527936 +0200
  5554. @@ -0,0 +1,22 @@
  5555. +/* arch/arm/mach-bcm2708/include/mach/debug-macro.S
  5556. + *
  5557. + * Debugging macro include header
  5558. + *
  5559. + * Copyright (C) 2010 Broadcom
  5560. + * Copyright (C) 1994-1999 Russell King
  5561. + * Moved from linux/arch/arm/kernel/debug.S by Ben Dooks
  5562. + *
  5563. + * This program is free software; you can redistribute it and/or modify
  5564. + * it under the terms of the GNU General Public License version 2 as
  5565. + * published by the Free Software Foundation.
  5566. + *
  5567. +*/
  5568. +
  5569. +#include <mach/platform.h>
  5570. +
  5571. + .macro addruart, rp, rv, tmp
  5572. + ldr \rp, =UART0_BASE
  5573. + ldr \rv, =IO_ADDRESS(UART0_BASE)
  5574. + .endm
  5575. +
  5576. +#include <asm/hardware/debug-pl01x.S>
  5577. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/dma.h linux-rpi/arch/arm/mach-bcm2708/include/mach/dma.h
  5578. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/dma.h 1970-01-01 01:00:00.000000000 +0100
  5579. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/dma.h 2014-04-24 15:35:00.777527936 +0200
  5580. @@ -0,0 +1,90 @@
  5581. +/*
  5582. + * linux/arch/arm/mach-bcm2708/include/mach/dma.h
  5583. + *
  5584. + * Copyright (C) 2010 Broadcom
  5585. + *
  5586. + * This program is free software; you can redistribute it and/or modify
  5587. + * it under the terms of the GNU General Public License version 2 as
  5588. + * published by the Free Software Foundation.
  5589. + */
  5590. +
  5591. +
  5592. +#ifndef _MACH_BCM2708_DMA_H
  5593. +#define _MACH_BCM2708_DMA_H
  5594. +
  5595. +#define BCM_DMAMAN_DRIVER_NAME "bcm2708_dma"
  5596. +
  5597. +/* DMA CS Control and Status bits */
  5598. +#define BCM2708_DMA_ACTIVE (1 << 0)
  5599. +#define BCM2708_DMA_INT (1 << 2)
  5600. +#define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
  5601. +#define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
  5602. +#define BCM2708_DMA_ERR (1 << 8)
  5603. +#define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
  5604. +#define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
  5605. +
  5606. +/* DMA control block "info" field bits */
  5607. +#define BCM2708_DMA_INT_EN (1 << 0)
  5608. +#define BCM2708_DMA_TDMODE (1 << 1)
  5609. +#define BCM2708_DMA_WAIT_RESP (1 << 3)
  5610. +#define BCM2708_DMA_D_INC (1 << 4)
  5611. +#define BCM2708_DMA_D_WIDTH (1 << 5)
  5612. +#define BCM2708_DMA_D_DREQ (1 << 6)
  5613. +#define BCM2708_DMA_S_INC (1 << 8)
  5614. +#define BCM2708_DMA_S_WIDTH (1 << 9)
  5615. +#define BCM2708_DMA_S_DREQ (1 << 10)
  5616. +
  5617. +#define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
  5618. +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
  5619. +#define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
  5620. +
  5621. +#define BCM2708_DMA_DREQ_EMMC 11
  5622. +#define BCM2708_DMA_DREQ_SDHOST 13
  5623. +
  5624. +#define BCM2708_DMA_CS 0x00 /* Control and Status */
  5625. +#define BCM2708_DMA_ADDR 0x04
  5626. +/* the current control block appears in the following registers - read only */
  5627. +#define BCM2708_DMA_INFO 0x08
  5628. +#define BCM2708_DMA_SOURCE_AD 0x0c
  5629. +#define BCM2708_DMA_DEST_AD 0x10
  5630. +#define BCM2708_DMA_NEXTCB 0x1C
  5631. +#define BCM2708_DMA_DEBUG 0x20
  5632. +
  5633. +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
  5634. +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
  5635. +
  5636. +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
  5637. +
  5638. +struct bcm2708_dma_cb {
  5639. + unsigned long info;
  5640. + unsigned long src;
  5641. + unsigned long dst;
  5642. + unsigned long length;
  5643. + unsigned long stride;
  5644. + unsigned long next;
  5645. + unsigned long pad[2];
  5646. +};
  5647. +struct scatterlist;
  5648. +
  5649. +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
  5650. +extern void bcm_dma_start(void __iomem *dma_chan_base,
  5651. + dma_addr_t control_block);
  5652. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base);
  5653. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base);
  5654. +extern int /*rc*/ bcm_dma_abort(void __iomem *dma_chan_base);
  5655. +
  5656. +/* When listing features we can ask for when allocating DMA channels give
  5657. + those with higher priority smaller ordinal numbers */
  5658. +#define BCM_DMA_FEATURE_FAST_ORD 0
  5659. +#define BCM_DMA_FEATURE_BULK_ORD 1
  5660. +#define BCM_DMA_FEATURE_FAST (1<<BCM_DMA_FEATURE_FAST_ORD)
  5661. +#define BCM_DMA_FEATURE_BULK (1<<BCM_DMA_FEATURE_BULK_ORD)
  5662. +#define BCM_DMA_FEATURE_COUNT 2
  5663. +
  5664. +/* return channel no or -ve error */
  5665. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  5666. + void __iomem **out_dma_base, int *out_dma_irq);
  5667. +extern int bcm_dma_chan_free(int channel);
  5668. +
  5669. +
  5670. +#endif /* _MACH_BCM2708_DMA_H */
  5671. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/entry-macro.S linux-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S
  5672. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/entry-macro.S 1970-01-01 01:00:00.000000000 +0100
  5673. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S 2014-04-24 15:35:00.777527936 +0200
  5674. @@ -0,0 +1,69 @@
  5675. +/*
  5676. + * arch/arm/mach-bcm2708/include/mach/entry-macro.S
  5677. + *
  5678. + * Low-level IRQ helper macros for BCM2708 platforms
  5679. + *
  5680. + * Copyright (C) 2010 Broadcom
  5681. + *
  5682. + * This program is free software; you can redistribute it and/or modify
  5683. + * it under the terms of the GNU General Public License as published by
  5684. + * the Free Software Foundation; either version 2 of the License, or
  5685. + * (at your option) any later version.
  5686. + *
  5687. + * This program is distributed in the hope that it will be useful,
  5688. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5689. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5690. + * GNU General Public License for more details.
  5691. + *
  5692. + * You should have received a copy of the GNU General Public License
  5693. + * along with this program; if not, write to the Free Software
  5694. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5695. + */
  5696. +#include <mach/hardware.h>
  5697. +
  5698. + .macro disable_fiq
  5699. + .endm
  5700. +
  5701. + .macro get_irqnr_preamble, base, tmp
  5702. + ldr \base, =IO_ADDRESS(ARMCTRL_IC_BASE)
  5703. + .endm
  5704. +
  5705. + .macro arch_ret_to_user, tmp1, tmp2
  5706. + .endm
  5707. +
  5708. + .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
  5709. + /* get masked status */
  5710. + ldr \irqstat, [\base, #(ARM_IRQ_PEND0 - ARMCTRL_IC_BASE)]
  5711. + mov \irqnr, #(ARM_IRQ0_BASE + 31)
  5712. + and \tmp, \irqstat, #0x300 @ save bits 8 and 9
  5713. + /* clear bits 8 and 9, and test */
  5714. + bics \irqstat, \irqstat, #0x300
  5715. + bne 1010f
  5716. +
  5717. + tst \tmp, #0x100
  5718. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND1 - ARMCTRL_IC_BASE)]
  5719. + movne \irqnr, #(ARM_IRQ1_BASE + 31)
  5720. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  5721. + bicne \irqstat, #((1<<7) | (1<<9) | (1<<10))
  5722. + bicne \irqstat, #((1<<18) | (1<<19))
  5723. + bne 1010f
  5724. +
  5725. + tst \tmp, #0x200
  5726. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND2 - ARMCTRL_IC_BASE)]
  5727. + movne \irqnr, #(ARM_IRQ2_BASE + 31)
  5728. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  5729. + bicne \irqstat, #((1<<21) | (1<<22) | (1<<23) | (1<<24) | (1<<25))
  5730. + bicne \irqstat, #((1<<30))
  5731. + beq 1020f
  5732. +
  5733. +1010:
  5734. + @ For non-zero x, LSB(x) = 31 - CLZ(x^(x-1))
  5735. + @ N.B. CLZ is an ARM5 instruction.
  5736. + sub \tmp, \irqstat, #1
  5737. + eor \irqstat, \irqstat, \tmp
  5738. + clz \tmp, \irqstat
  5739. + sub \irqnr, \tmp
  5740. +
  5741. +1020: @ EQ will be set if no irqs pending
  5742. +
  5743. + .endm
  5744. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/frc.h linux-rpi/arch/arm/mach-bcm2708/include/mach/frc.h
  5745. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/frc.h 1970-01-01 01:00:00.000000000 +0100
  5746. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/frc.h 2014-04-24 15:35:00.777527936 +0200
  5747. @@ -0,0 +1,38 @@
  5748. +/*
  5749. + * arch/arm/mach-bcm2708/include/mach/timex.h
  5750. + *
  5751. + * BCM2708 free running counter (timer)
  5752. + *
  5753. + * Copyright (C) 2010 Broadcom
  5754. + *
  5755. + * This program is free software; you can redistribute it and/or modify
  5756. + * it under the terms of the GNU General Public License as published by
  5757. + * the Free Software Foundation; either version 2 of the License, or
  5758. + * (at your option) any later version.
  5759. + *
  5760. + * This program is distributed in the hope that it will be useful,
  5761. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5762. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5763. + * GNU General Public License for more details.
  5764. + *
  5765. + * You should have received a copy of the GNU General Public License
  5766. + * along with this program; if not, write to the Free Software
  5767. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5768. + */
  5769. +
  5770. +#ifndef _MACH_FRC_H
  5771. +#define _MACH_FRC_H
  5772. +
  5773. +#define FRC_TICK_RATE (1000000)
  5774. +
  5775. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  5776. + (slightly faster than frc_clock_ticks63()
  5777. + */
  5778. +extern unsigned long frc_clock_ticks32(void);
  5779. +
  5780. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  5781. + * Note - top bit should be ignored (see cnt32_to_63)
  5782. + */
  5783. +extern unsigned long long frc_clock_ticks63(void);
  5784. +
  5785. +#endif
  5786. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/gpio.h linux-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h
  5787. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/gpio.h 1970-01-01 01:00:00.000000000 +0100
  5788. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h 2014-04-24 15:35:00.777527936 +0200
  5789. @@ -0,0 +1,18 @@
  5790. +/*
  5791. + * arch/arm/mach-bcm2708/include/mach/gpio.h
  5792. + *
  5793. + * This file is licensed under the terms of the GNU General Public
  5794. + * License version 2. This program is licensed "as is" without any
  5795. + * warranty of any kind, whether express or implied.
  5796. + */
  5797. +
  5798. +#ifndef __ASM_ARCH_GPIO_H
  5799. +#define __ASM_ARCH_GPIO_H
  5800. +
  5801. +#define ARCH_NR_GPIOS 54 // number of gpio lines
  5802. +
  5803. +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
  5804. +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
  5805. +
  5806. +#endif
  5807. +
  5808. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/hardware.h linux-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h
  5809. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/hardware.h 1970-01-01 01:00:00.000000000 +0100
  5810. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h 2014-04-24 15:35:00.777527936 +0200
  5811. @@ -0,0 +1,28 @@
  5812. +/*
  5813. + * arch/arm/mach-bcm2708/include/mach/hardware.h
  5814. + *
  5815. + * This file contains the hardware definitions of the BCM2708 devices.
  5816. + *
  5817. + * Copyright (C) 2010 Broadcom
  5818. + *
  5819. + * This program is free software; you can redistribute it and/or modify
  5820. + * it under the terms of the GNU General Public License as published by
  5821. + * the Free Software Foundation; either version 2 of the License, or
  5822. + * (at your option) any later version.
  5823. + *
  5824. + * This program is distributed in the hope that it will be useful,
  5825. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5826. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5827. + * GNU General Public License for more details.
  5828. + *
  5829. + * You should have received a copy of the GNU General Public License
  5830. + * along with this program; if not, write to the Free Software
  5831. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5832. + */
  5833. +#ifndef __ASM_ARCH_HARDWARE_H
  5834. +#define __ASM_ARCH_HARDWARE_H
  5835. +
  5836. +#include <asm/sizes.h>
  5837. +#include <mach/platform.h>
  5838. +
  5839. +#endif
  5840. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/io.h linux-rpi/arch/arm/mach-bcm2708/include/mach/io.h
  5841. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/io.h 1970-01-01 01:00:00.000000000 +0100
  5842. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/io.h 2014-04-24 15:35:00.777527936 +0200
  5843. @@ -0,0 +1,27 @@
  5844. +/*
  5845. + * arch/arm/mach-bcm2708/include/mach/io.h
  5846. + *
  5847. + * Copyright (C) 2003 ARM Limited
  5848. + *
  5849. + * This program is free software; you can redistribute it and/or modify
  5850. + * it under the terms of the GNU General Public License as published by
  5851. + * the Free Software Foundation; either version 2 of the License, or
  5852. + * (at your option) any later version.
  5853. + *
  5854. + * This program is distributed in the hope that it will be useful,
  5855. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5856. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5857. + * GNU General Public License for more details.
  5858. + *
  5859. + * You should have received a copy of the GNU General Public License
  5860. + * along with this program; if not, write to the Free Software
  5861. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5862. + */
  5863. +#ifndef __ASM_ARM_ARCH_IO_H
  5864. +#define __ASM_ARM_ARCH_IO_H
  5865. +
  5866. +#define IO_SPACE_LIMIT 0xffffffff
  5867. +
  5868. +#define __io(a) __typesafe_io(a)
  5869. +
  5870. +#endif
  5871. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/irqs.h linux-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h
  5872. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/irqs.h 1970-01-01 01:00:00.000000000 +0100
  5873. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h 2014-04-24 15:35:00.777527936 +0200
  5874. @@ -0,0 +1,199 @@
  5875. +/*
  5876. + * arch/arm/mach-bcm2708/include/mach/irqs.h
  5877. + *
  5878. + * Copyright (C) 2010 Broadcom
  5879. + * Copyright (C) 2003 ARM Limited
  5880. + * Copyright (C) 2000 Deep Blue Solutions Ltd.
  5881. + *
  5882. + * This program is free software; you can redistribute it and/or modify
  5883. + * it under the terms of the GNU General Public License as published by
  5884. + * the Free Software Foundation; either version 2 of the License, or
  5885. + * (at your option) any later version.
  5886. + *
  5887. + * This program is distributed in the hope that it will be useful,
  5888. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5889. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5890. + * GNU General Public License for more details.
  5891. + *
  5892. + * You should have received a copy of the GNU General Public License
  5893. + * along with this program; if not, write to the Free Software
  5894. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5895. + */
  5896. +
  5897. +#ifndef _BCM2708_IRQS_H_
  5898. +#define _BCM2708_IRQS_H_
  5899. +
  5900. +#include <mach/platform.h>
  5901. +
  5902. +/*
  5903. + * IRQ interrupts definitions are the same as the INT definitions
  5904. + * held within platform.h
  5905. + */
  5906. +#define IRQ_ARMCTRL_START 0
  5907. +#define IRQ_TIMER0 (IRQ_ARMCTRL_START + INTERRUPT_TIMER0)
  5908. +#define IRQ_TIMER1 (IRQ_ARMCTRL_START + INTERRUPT_TIMER1)
  5909. +#define IRQ_TIMER2 (IRQ_ARMCTRL_START + INTERRUPT_TIMER2)
  5910. +#define IRQ_TIMER3 (IRQ_ARMCTRL_START + INTERRUPT_TIMER3)
  5911. +#define IRQ_CODEC0 (IRQ_ARMCTRL_START + INTERRUPT_CODEC0)
  5912. +#define IRQ_CODEC1 (IRQ_ARMCTRL_START + INTERRUPT_CODEC1)
  5913. +#define IRQ_CODEC2 (IRQ_ARMCTRL_START + INTERRUPT_CODEC2)
  5914. +#define IRQ_JPEG (IRQ_ARMCTRL_START + INTERRUPT_JPEG)
  5915. +#define IRQ_ISP (IRQ_ARMCTRL_START + INTERRUPT_ISP)
  5916. +#define IRQ_USB (IRQ_ARMCTRL_START + INTERRUPT_USB)
  5917. +#define IRQ_3D (IRQ_ARMCTRL_START + INTERRUPT_3D)
  5918. +#define IRQ_TRANSPOSER (IRQ_ARMCTRL_START + INTERRUPT_TRANSPOSER)
  5919. +#define IRQ_MULTICORESYNC0 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC0)
  5920. +#define IRQ_MULTICORESYNC1 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC1)
  5921. +#define IRQ_MULTICORESYNC2 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC2)
  5922. +#define IRQ_MULTICORESYNC3 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC3)
  5923. +#define IRQ_DMA0 (IRQ_ARMCTRL_START + INTERRUPT_DMA0)
  5924. +#define IRQ_DMA1 (IRQ_ARMCTRL_START + INTERRUPT_DMA1)
  5925. +#define IRQ_DMA2 (IRQ_ARMCTRL_START + INTERRUPT_DMA2)
  5926. +#define IRQ_DMA3 (IRQ_ARMCTRL_START + INTERRUPT_DMA3)
  5927. +#define IRQ_DMA4 (IRQ_ARMCTRL_START + INTERRUPT_DMA4)
  5928. +#define IRQ_DMA5 (IRQ_ARMCTRL_START + INTERRUPT_DMA5)
  5929. +#define IRQ_DMA6 (IRQ_ARMCTRL_START + INTERRUPT_DMA6)
  5930. +#define IRQ_DMA7 (IRQ_ARMCTRL_START + INTERRUPT_DMA7)
  5931. +#define IRQ_DMA8 (IRQ_ARMCTRL_START + INTERRUPT_DMA8)
  5932. +#define IRQ_DMA9 (IRQ_ARMCTRL_START + INTERRUPT_DMA9)
  5933. +#define IRQ_DMA10 (IRQ_ARMCTRL_START + INTERRUPT_DMA10)
  5934. +#define IRQ_DMA11 (IRQ_ARMCTRL_START + INTERRUPT_DMA11)
  5935. +#define IRQ_DMA12 (IRQ_ARMCTRL_START + INTERRUPT_DMA12)
  5936. +#define IRQ_AUX (IRQ_ARMCTRL_START + INTERRUPT_AUX)
  5937. +#define IRQ_ARM (IRQ_ARMCTRL_START + INTERRUPT_ARM)
  5938. +#define IRQ_VPUDMA (IRQ_ARMCTRL_START + INTERRUPT_VPUDMA)
  5939. +#define IRQ_HOSTPORT (IRQ_ARMCTRL_START + INTERRUPT_HOSTPORT)
  5940. +#define IRQ_VIDEOSCALER (IRQ_ARMCTRL_START + INTERRUPT_VIDEOSCALER)
  5941. +#define IRQ_CCP2TX (IRQ_ARMCTRL_START + INTERRUPT_CCP2TX)
  5942. +#define IRQ_SDC (IRQ_ARMCTRL_START + INTERRUPT_SDC)
  5943. +#define IRQ_DSI0 (IRQ_ARMCTRL_START + INTERRUPT_DSI0)
  5944. +#define IRQ_AVE (IRQ_ARMCTRL_START + INTERRUPT_AVE)
  5945. +#define IRQ_CAM0 (IRQ_ARMCTRL_START + INTERRUPT_CAM0)
  5946. +#define IRQ_CAM1 (IRQ_ARMCTRL_START + INTERRUPT_CAM1)
  5947. +#define IRQ_HDMI0 (IRQ_ARMCTRL_START + INTERRUPT_HDMI0)
  5948. +#define IRQ_HDMI1 (IRQ_ARMCTRL_START + INTERRUPT_HDMI1)
  5949. +#define IRQ_PIXELVALVE1 (IRQ_ARMCTRL_START + INTERRUPT_PIXELVALVE1)
  5950. +#define IRQ_I2CSPISLV (IRQ_ARMCTRL_START + INTERRUPT_I2CSPISLV)
  5951. +#define IRQ_DSI1 (IRQ_ARMCTRL_START + INTERRUPT_DSI1)
  5952. +#define IRQ_PWA0 (IRQ_ARMCTRL_START + INTERRUPT_PWA0)
  5953. +#define IRQ_PWA1 (IRQ_ARMCTRL_START + INTERRUPT_PWA1)
  5954. +#define IRQ_CPR (IRQ_ARMCTRL_START + INTERRUPT_CPR)
  5955. +#define IRQ_SMI (IRQ_ARMCTRL_START + INTERRUPT_SMI)
  5956. +#define IRQ_GPIO0 (IRQ_ARMCTRL_START + INTERRUPT_GPIO0)
  5957. +#define IRQ_GPIO1 (IRQ_ARMCTRL_START + INTERRUPT_GPIO1)
  5958. +#define IRQ_GPIO2 (IRQ_ARMCTRL_START + INTERRUPT_GPIO2)
  5959. +#define IRQ_GPIO3 (IRQ_ARMCTRL_START + INTERRUPT_GPIO3)
  5960. +#define IRQ_I2C (IRQ_ARMCTRL_START + INTERRUPT_I2C)
  5961. +#define IRQ_SPI (IRQ_ARMCTRL_START + INTERRUPT_SPI)
  5962. +#define IRQ_I2SPCM (IRQ_ARMCTRL_START + INTERRUPT_I2SPCM)
  5963. +#define IRQ_SDIO (IRQ_ARMCTRL_START + INTERRUPT_SDIO)
  5964. +#define IRQ_UART (IRQ_ARMCTRL_START + INTERRUPT_UART)
  5965. +#define IRQ_SLIMBUS (IRQ_ARMCTRL_START + INTERRUPT_SLIMBUS)
  5966. +#define IRQ_VEC (IRQ_ARMCTRL_START + INTERRUPT_VEC)
  5967. +#define IRQ_CPG (IRQ_ARMCTRL_START + INTERRUPT_CPG)
  5968. +#define IRQ_RNG (IRQ_ARMCTRL_START + INTERRUPT_RNG)
  5969. +#define IRQ_ARASANSDIO (IRQ_ARMCTRL_START + INTERRUPT_ARASANSDIO)
  5970. +#define IRQ_AVSPMON (IRQ_ARMCTRL_START + INTERRUPT_AVSPMON)
  5971. +
  5972. +#define IRQ_ARM_TIMER (IRQ_ARMCTRL_START + INTERRUPT_ARM_TIMER)
  5973. +#define IRQ_ARM_MAILBOX (IRQ_ARMCTRL_START + INTERRUPT_ARM_MAILBOX)
  5974. +#define IRQ_ARM_DOORBELL_0 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_0)
  5975. +#define IRQ_ARM_DOORBELL_1 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_1)
  5976. +#define IRQ_VPU0_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU0_HALTED)
  5977. +#define IRQ_VPU1_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU1_HALTED)
  5978. +#define IRQ_ILLEGAL_TYPE0 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE0)
  5979. +#define IRQ_ILLEGAL_TYPE1 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE1)
  5980. +#define IRQ_PENDING1 (IRQ_ARMCTRL_START + INTERRUPT_PENDING1)
  5981. +#define IRQ_PENDING2 (IRQ_ARMCTRL_START + INTERRUPT_PENDING2)
  5982. +
  5983. +#define FIQ_START HARD_IRQS
  5984. +
  5985. +/*
  5986. + * FIQ interrupts definitions are the same as the INT definitions.
  5987. + */
  5988. +#define FIQ_TIMER0 (FIQ_START+INTERRUPT_TIMER0)
  5989. +#define FIQ_TIMER1 (FIQ_START+INTERRUPT_TIMER1)
  5990. +#define FIQ_TIMER2 (FIQ_START+INTERRUPT_TIMER2)
  5991. +#define FIQ_TIMER3 (FIQ_START+INTERRUPT_TIMER3)
  5992. +#define FIQ_CODEC0 (FIQ_START+INTERRUPT_CODEC0)
  5993. +#define FIQ_CODEC1 (FIQ_START+INTERRUPT_CODEC1)
  5994. +#define FIQ_CODEC2 (FIQ_START+INTERRUPT_CODEC2)
  5995. +#define FIQ_JPEG (FIQ_START+INTERRUPT_JPEG)
  5996. +#define FIQ_ISP (FIQ_START+INTERRUPT_ISP)
  5997. +#define FIQ_USB (FIQ_START+INTERRUPT_USB)
  5998. +#define FIQ_3D (FIQ_START+INTERRUPT_3D)
  5999. +#define FIQ_TRANSPOSER (FIQ_START+INTERRUPT_TRANSPOSER)
  6000. +#define FIQ_MULTICORESYNC0 (FIQ_START+INTERRUPT_MULTICORESYNC0)
  6001. +#define FIQ_MULTICORESYNC1 (FIQ_START+INTERRUPT_MULTICORESYNC1)
  6002. +#define FIQ_MULTICORESYNC2 (FIQ_START+INTERRUPT_MULTICORESYNC2)
  6003. +#define FIQ_MULTICORESYNC3 (FIQ_START+INTERRUPT_MULTICORESYNC3)
  6004. +#define FIQ_DMA0 (FIQ_START+INTERRUPT_DMA0)
  6005. +#define FIQ_DMA1 (FIQ_START+INTERRUPT_DMA1)
  6006. +#define FIQ_DMA2 (FIQ_START+INTERRUPT_DMA2)
  6007. +#define FIQ_DMA3 (FIQ_START+INTERRUPT_DMA3)
  6008. +#define FIQ_DMA4 (FIQ_START+INTERRUPT_DMA4)
  6009. +#define FIQ_DMA5 (FIQ_START+INTERRUPT_DMA5)
  6010. +#define FIQ_DMA6 (FIQ_START+INTERRUPT_DMA6)
  6011. +#define FIQ_DMA7 (FIQ_START+INTERRUPT_DMA7)
  6012. +#define FIQ_DMA8 (FIQ_START+INTERRUPT_DMA8)
  6013. +#define FIQ_DMA9 (FIQ_START+INTERRUPT_DMA9)
  6014. +#define FIQ_DMA10 (FIQ_START+INTERRUPT_DMA10)
  6015. +#define FIQ_DMA11 (FIQ_START+INTERRUPT_DMA11)
  6016. +#define FIQ_DMA12 (FIQ_START+INTERRUPT_DMA12)
  6017. +#define FIQ_AUX (FIQ_START+INTERRUPT_AUX)
  6018. +#define FIQ_ARM (FIQ_START+INTERRUPT_ARM)
  6019. +#define FIQ_VPUDMA (FIQ_START+INTERRUPT_VPUDMA)
  6020. +#define FIQ_HOSTPORT (FIQ_START+INTERRUPT_HOSTPORT)
  6021. +#define FIQ_VIDEOSCALER (FIQ_START+INTERRUPT_VIDEOSCALER)
  6022. +#define FIQ_CCP2TX (FIQ_START+INTERRUPT_CCP2TX)
  6023. +#define FIQ_SDC (FIQ_START+INTERRUPT_SDC)
  6024. +#define FIQ_DSI0 (FIQ_START+INTERRUPT_DSI0)
  6025. +#define FIQ_AVE (FIQ_START+INTERRUPT_AVE)
  6026. +#define FIQ_CAM0 (FIQ_START+INTERRUPT_CAM0)
  6027. +#define FIQ_CAM1 (FIQ_START+INTERRUPT_CAM1)
  6028. +#define FIQ_HDMI0 (FIQ_START+INTERRUPT_HDMI0)
  6029. +#define FIQ_HDMI1 (FIQ_START+INTERRUPT_HDMI1)
  6030. +#define FIQ_PIXELVALVE1 (FIQ_START+INTERRUPT_PIXELVALVE1)
  6031. +#define FIQ_I2CSPISLV (FIQ_START+INTERRUPT_I2CSPISLV)
  6032. +#define FIQ_DSI1 (FIQ_START+INTERRUPT_DSI1)
  6033. +#define FIQ_PWA0 (FIQ_START+INTERRUPT_PWA0)
  6034. +#define FIQ_PWA1 (FIQ_START+INTERRUPT_PWA1)
  6035. +#define FIQ_CPR (FIQ_START+INTERRUPT_CPR)
  6036. +#define FIQ_SMI (FIQ_START+INTERRUPT_SMI)
  6037. +#define FIQ_GPIO0 (FIQ_START+INTERRUPT_GPIO0)
  6038. +#define FIQ_GPIO1 (FIQ_START+INTERRUPT_GPIO1)
  6039. +#define FIQ_GPIO2 (FIQ_START+INTERRUPT_GPIO2)
  6040. +#define FIQ_GPIO3 (FIQ_START+INTERRUPT_GPIO3)
  6041. +#define FIQ_I2C (FIQ_START+INTERRUPT_I2C)
  6042. +#define FIQ_SPI (FIQ_START+INTERRUPT_SPI)
  6043. +#define FIQ_I2SPCM (FIQ_START+INTERRUPT_I2SPCM)
  6044. +#define FIQ_SDIO (FIQ_START+INTERRUPT_SDIO)
  6045. +#define FIQ_UART (FIQ_START+INTERRUPT_UART)
  6046. +#define FIQ_SLIMBUS (FIQ_START+INTERRUPT_SLIMBUS)
  6047. +#define FIQ_VEC (FIQ_START+INTERRUPT_VEC)
  6048. +#define FIQ_CPG (FIQ_START+INTERRUPT_CPG)
  6049. +#define FIQ_RNG (FIQ_START+INTERRUPT_RNG)
  6050. +#define FIQ_ARASANSDIO (FIQ_START+INTERRUPT_ARASANSDIO)
  6051. +#define FIQ_AVSPMON (FIQ_START+INTERRUPT_AVSPMON)
  6052. +
  6053. +#define FIQ_ARM_TIMER (FIQ_START+INTERRUPT_ARM_TIMER)
  6054. +#define FIQ_ARM_MAILBOX (FIQ_START+INTERRUPT_ARM_MAILBOX)
  6055. +#define FIQ_ARM_DOORBELL_0 (FIQ_START+INTERRUPT_ARM_DOORBELL_0)
  6056. +#define FIQ_ARM_DOORBELL_1 (FIQ_START+INTERRUPT_ARM_DOORBELL_1)
  6057. +#define FIQ_VPU0_HALTED (FIQ_START+INTERRUPT_VPU0_HALTED)
  6058. +#define FIQ_VPU1_HALTED (FIQ_START+INTERRUPT_VPU1_HALTED)
  6059. +#define FIQ_ILLEGAL_TYPE0 (FIQ_START+INTERRUPT_ILLEGAL_TYPE0)
  6060. +#define FIQ_ILLEGAL_TYPE1 (FIQ_START+INTERRUPT_ILLEGAL_TYPE1)
  6061. +#define FIQ_PENDING1 (FIQ_START+INTERRUPT_PENDING1)
  6062. +#define FIQ_PENDING2 (FIQ_START+INTERRUPT_PENDING2)
  6063. +
  6064. +#define GPIO_IRQ_START (HARD_IRQS + FIQ_IRQS)
  6065. +
  6066. +#define HARD_IRQS (64 + 21)
  6067. +#define FIQ_IRQS (64 + 21)
  6068. +#define GPIO_IRQS (32*5)
  6069. +
  6070. +#define NR_IRQS HARD_IRQS+FIQ_IRQS+GPIO_IRQS
  6071. +
  6072. +
  6073. +#endif /* _BCM2708_IRQS_H_ */
  6074. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/memory.h linux-rpi/arch/arm/mach-bcm2708/include/mach/memory.h
  6075. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/memory.h 1970-01-01 01:00:00.000000000 +0100
  6076. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/memory.h 2014-04-24 15:35:00.777527936 +0200
  6077. @@ -0,0 +1,57 @@
  6078. +/*
  6079. + * arch/arm/mach-bcm2708/include/mach/memory.h
  6080. + *
  6081. + * Copyright (C) 2010 Broadcom
  6082. + *
  6083. + * This program is free software; you can redistribute it and/or modify
  6084. + * it under the terms of the GNU General Public License as published by
  6085. + * the Free Software Foundation; either version 2 of the License, or
  6086. + * (at your option) any later version.
  6087. + *
  6088. + * This program is distributed in the hope that it will be useful,
  6089. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6090. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6091. + * GNU General Public License for more details.
  6092. + *
  6093. + * You should have received a copy of the GNU General Public License
  6094. + * along with this program; if not, write to the Free Software
  6095. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6096. + */
  6097. +#ifndef __ASM_ARCH_MEMORY_H
  6098. +#define __ASM_ARCH_MEMORY_H
  6099. +
  6100. +/* Memory overview:
  6101. +
  6102. + [ARMcore] <--virtual addr-->
  6103. + [ARMmmu] <--physical addr-->
  6104. + [GERTmap] <--bus add-->
  6105. + [VCperiph]
  6106. +
  6107. +*/
  6108. +
  6109. +/*
  6110. + * Physical DRAM offset.
  6111. + */
  6112. +#define PLAT_PHYS_OFFSET UL(0x00000000)
  6113. +#define VC_ARMMEM_OFFSET UL(0x00000000) /* offset in VC of ARM memory */
  6114. +
  6115. +#ifdef CONFIG_BCM2708_NOL2CACHE
  6116. + #define _REAL_BUS_OFFSET UL(0xC0000000) /* don't use L1 or L2 caches */
  6117. +#else
  6118. + #define _REAL_BUS_OFFSET UL(0x40000000) /* use L2 cache */
  6119. +#endif
  6120. +
  6121. +/* We're using the memory at 64M in the VideoCore for Linux - this adjustment
  6122. + * will provide the offset into this area as well as setting the bits that
  6123. + * stop the L1 and L2 cache from being used
  6124. + *
  6125. + * WARNING: this only works because the ARM is given memory at a fixed location
  6126. + * (ARMMEM_OFFSET)
  6127. + */
  6128. +#define BUS_OFFSET (VC_ARMMEM_OFFSET + _REAL_BUS_OFFSET)
  6129. +#define __virt_to_bus(x) ((x) + (BUS_OFFSET - PAGE_OFFSET))
  6130. +#define __bus_to_virt(x) ((x) - (BUS_OFFSET - PAGE_OFFSET))
  6131. +#define __pfn_to_bus(x) (__pfn_to_phys(x) + (BUS_OFFSET - PLAT_PHYS_OFFSET))
  6132. +#define __bus_to_pfn(x) __phys_to_pfn((x) - (BUS_OFFSET - PLAT_PHYS_OFFSET))
  6133. +
  6134. +#endif
  6135. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/platform.h linux-rpi/arch/arm/mach-bcm2708/include/mach/platform.h
  6136. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/platform.h 1970-01-01 01:00:00.000000000 +0100
  6137. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/platform.h 2014-04-24 15:35:00.777527936 +0200
  6138. @@ -0,0 +1,228 @@
  6139. +/*
  6140. + * arch/arm/mach-bcm2708/include/mach/platform.h
  6141. + *
  6142. + * Copyright (C) 2010 Broadcom
  6143. + *
  6144. + * This program is free software; you can redistribute it and/or modify
  6145. + * it under the terms of the GNU General Public License as published by
  6146. + * the Free Software Foundation; either version 2 of the License, or
  6147. + * (at your option) any later version.
  6148. + *
  6149. + * This program is distributed in the hope that it will be useful,
  6150. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6151. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6152. + * GNU General Public License for more details.
  6153. + *
  6154. + * You should have received a copy of the GNU General Public License
  6155. + * along with this program; if not, write to the Free Software
  6156. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6157. + */
  6158. +
  6159. +#ifndef _BCM2708_PLATFORM_H
  6160. +#define _BCM2708_PLATFORM_H
  6161. +
  6162. +
  6163. +/* macros to get at IO space when running virtually */
  6164. +#define IO_ADDRESS(x) (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  6165. +
  6166. +#define __io_address(n) IOMEM(IO_ADDRESS(n))
  6167. +
  6168. +
  6169. +/*
  6170. + * SDRAM
  6171. + */
  6172. +#define BCM2708_SDRAM_BASE 0x00000000
  6173. +
  6174. +/*
  6175. + * Logic expansion modules
  6176. + *
  6177. + */
  6178. +
  6179. +
  6180. +/* ------------------------------------------------------------------------
  6181. + * BCM2708 ARMCTRL Registers
  6182. + * ------------------------------------------------------------------------
  6183. + */
  6184. +
  6185. +#define HW_REGISTER_RW(addr) (addr)
  6186. +#define HW_REGISTER_RO(addr) (addr)
  6187. +
  6188. +#include "arm_control.h"
  6189. +#undef ARM_BASE
  6190. +
  6191. +/*
  6192. + * Definitions and addresses for the ARM CONTROL logic
  6193. + * This file is manually generated.
  6194. + */
  6195. +
  6196. +#define BCM2708_PERI_BASE 0x20000000
  6197. +#define IC0_BASE (BCM2708_PERI_BASE + 0x2000)
  6198. +#define ST_BASE (BCM2708_PERI_BASE + 0x3000) /* System Timer */
  6199. +#define MPHI_BASE (BCM2708_PERI_BASE + 0x6000) /* Message -based Parallel Host Interface */
  6200. +#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
  6201. +#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
  6202. +#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
  6203. +#define PCM_CLOCK_BASE (BCM2708_PERI_BASE + 0x101098) /* PCM Clock */
  6204. +#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
  6205. +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
  6206. +#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
  6207. +#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
  6208. +#define I2S_BASE (BCM2708_PERI_BASE + 0x203000) /* I2S */
  6209. +#define SPI0_BASE (BCM2708_PERI_BASE + 0x204000) /* SPI0 */
  6210. +#define BSC0_BASE (BCM2708_PERI_BASE + 0x205000) /* BSC0 I2C/TWI */
  6211. +#define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */
  6212. +#define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */
  6213. +#define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */
  6214. +#define BSC1_BASE (BCM2708_PERI_BASE + 0x804000) /* BSC1 I2C/TWI */
  6215. +#define USB_BASE (BCM2708_PERI_BASE + 0x980000) /* DTC_OTG USB controller */
  6216. +#define MCORE_BASE (BCM2708_PERI_BASE + 0x0000) /* Fake frame buffer device (actually the multicore sync block*/
  6217. +
  6218. +#define ARMCTRL_BASE (ARM_BASE + 0x000)
  6219. +#define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  6220. +#define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  6221. +#define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  6222. +
  6223. +
  6224. +/*
  6225. + * Interrupt assignments
  6226. + */
  6227. +
  6228. +#define ARM_IRQ1_BASE 0
  6229. +#define INTERRUPT_TIMER0 (ARM_IRQ1_BASE + 0)
  6230. +#define INTERRUPT_TIMER1 (ARM_IRQ1_BASE + 1)
  6231. +#define INTERRUPT_TIMER2 (ARM_IRQ1_BASE + 2)
  6232. +#define INTERRUPT_TIMER3 (ARM_IRQ1_BASE + 3)
  6233. +#define INTERRUPT_CODEC0 (ARM_IRQ1_BASE + 4)
  6234. +#define INTERRUPT_CODEC1 (ARM_IRQ1_BASE + 5)
  6235. +#define INTERRUPT_CODEC2 (ARM_IRQ1_BASE + 6)
  6236. +#define INTERRUPT_VC_JPEG (ARM_IRQ1_BASE + 7)
  6237. +#define INTERRUPT_ISP (ARM_IRQ1_BASE + 8)
  6238. +#define INTERRUPT_VC_USB (ARM_IRQ1_BASE + 9)
  6239. +#define INTERRUPT_VC_3D (ARM_IRQ1_BASE + 10)
  6240. +#define INTERRUPT_TRANSPOSER (ARM_IRQ1_BASE + 11)
  6241. +#define INTERRUPT_MULTICORESYNC0 (ARM_IRQ1_BASE + 12)
  6242. +#define INTERRUPT_MULTICORESYNC1 (ARM_IRQ1_BASE + 13)
  6243. +#define INTERRUPT_MULTICORESYNC2 (ARM_IRQ1_BASE + 14)
  6244. +#define INTERRUPT_MULTICORESYNC3 (ARM_IRQ1_BASE + 15)
  6245. +#define INTERRUPT_DMA0 (ARM_IRQ1_BASE + 16)
  6246. +#define INTERRUPT_DMA1 (ARM_IRQ1_BASE + 17)
  6247. +#define INTERRUPT_VC_DMA2 (ARM_IRQ1_BASE + 18)
  6248. +#define INTERRUPT_VC_DMA3 (ARM_IRQ1_BASE + 19)
  6249. +#define INTERRUPT_DMA4 (ARM_IRQ1_BASE + 20)
  6250. +#define INTERRUPT_DMA5 (ARM_IRQ1_BASE + 21)
  6251. +#define INTERRUPT_DMA6 (ARM_IRQ1_BASE + 22)
  6252. +#define INTERRUPT_DMA7 (ARM_IRQ1_BASE + 23)
  6253. +#define INTERRUPT_DMA8 (ARM_IRQ1_BASE + 24)
  6254. +#define INTERRUPT_DMA9 (ARM_IRQ1_BASE + 25)
  6255. +#define INTERRUPT_DMA10 (ARM_IRQ1_BASE + 26)
  6256. +#define INTERRUPT_DMA11 (ARM_IRQ1_BASE + 27)
  6257. +#define INTERRUPT_DMA12 (ARM_IRQ1_BASE + 28)
  6258. +#define INTERRUPT_AUX (ARM_IRQ1_BASE + 29)
  6259. +#define INTERRUPT_ARM (ARM_IRQ1_BASE + 30)
  6260. +#define INTERRUPT_VPUDMA (ARM_IRQ1_BASE + 31)
  6261. +
  6262. +#define ARM_IRQ2_BASE 32
  6263. +#define INTERRUPT_HOSTPORT (ARM_IRQ2_BASE + 0)
  6264. +#define INTERRUPT_VIDEOSCALER (ARM_IRQ2_BASE + 1)
  6265. +#define INTERRUPT_CCP2TX (ARM_IRQ2_BASE + 2)
  6266. +#define INTERRUPT_SDC (ARM_IRQ2_BASE + 3)
  6267. +#define INTERRUPT_DSI0 (ARM_IRQ2_BASE + 4)
  6268. +#define INTERRUPT_AVE (ARM_IRQ2_BASE + 5)
  6269. +#define INTERRUPT_CAM0 (ARM_IRQ2_BASE + 6)
  6270. +#define INTERRUPT_CAM1 (ARM_IRQ2_BASE + 7)
  6271. +#define INTERRUPT_HDMI0 (ARM_IRQ2_BASE + 8)
  6272. +#define INTERRUPT_HDMI1 (ARM_IRQ2_BASE + 9)
  6273. +#define INTERRUPT_PIXELVALVE1 (ARM_IRQ2_BASE + 10)
  6274. +#define INTERRUPT_I2CSPISLV (ARM_IRQ2_BASE + 11)
  6275. +#define INTERRUPT_DSI1 (ARM_IRQ2_BASE + 12)
  6276. +#define INTERRUPT_PWA0 (ARM_IRQ2_BASE + 13)
  6277. +#define INTERRUPT_PWA1 (ARM_IRQ2_BASE + 14)
  6278. +#define INTERRUPT_CPR (ARM_IRQ2_BASE + 15)
  6279. +#define INTERRUPT_SMI (ARM_IRQ2_BASE + 16)
  6280. +#define INTERRUPT_GPIO0 (ARM_IRQ2_BASE + 17)
  6281. +#define INTERRUPT_GPIO1 (ARM_IRQ2_BASE + 18)
  6282. +#define INTERRUPT_GPIO2 (ARM_IRQ2_BASE + 19)
  6283. +#define INTERRUPT_GPIO3 (ARM_IRQ2_BASE + 20)
  6284. +#define INTERRUPT_VC_I2C (ARM_IRQ2_BASE + 21)
  6285. +#define INTERRUPT_VC_SPI (ARM_IRQ2_BASE + 22)
  6286. +#define INTERRUPT_VC_I2SPCM (ARM_IRQ2_BASE + 23)
  6287. +#define INTERRUPT_VC_SDIO (ARM_IRQ2_BASE + 24)
  6288. +#define INTERRUPT_VC_UART (ARM_IRQ2_BASE + 25)
  6289. +#define INTERRUPT_SLIMBUS (ARM_IRQ2_BASE + 26)
  6290. +#define INTERRUPT_VEC (ARM_IRQ2_BASE + 27)
  6291. +#define INTERRUPT_CPG (ARM_IRQ2_BASE + 28)
  6292. +#define INTERRUPT_RNG (ARM_IRQ2_BASE + 29)
  6293. +#define INTERRUPT_VC_ARASANSDIO (ARM_IRQ2_BASE + 30)
  6294. +#define INTERRUPT_AVSPMON (ARM_IRQ2_BASE + 31)
  6295. +
  6296. +#define ARM_IRQ0_BASE 64
  6297. +#define INTERRUPT_ARM_TIMER (ARM_IRQ0_BASE + 0)
  6298. +#define INTERRUPT_ARM_MAILBOX (ARM_IRQ0_BASE + 1)
  6299. +#define INTERRUPT_ARM_DOORBELL_0 (ARM_IRQ0_BASE + 2)
  6300. +#define INTERRUPT_ARM_DOORBELL_1 (ARM_IRQ0_BASE + 3)
  6301. +#define INTERRUPT_VPU0_HALTED (ARM_IRQ0_BASE + 4)
  6302. +#define INTERRUPT_VPU1_HALTED (ARM_IRQ0_BASE + 5)
  6303. +#define INTERRUPT_ILLEGAL_TYPE0 (ARM_IRQ0_BASE + 6)
  6304. +#define INTERRUPT_ILLEGAL_TYPE1 (ARM_IRQ0_BASE + 7)
  6305. +#define INTERRUPT_PENDING1 (ARM_IRQ0_BASE + 8)
  6306. +#define INTERRUPT_PENDING2 (ARM_IRQ0_BASE + 9)
  6307. +#define INTERRUPT_JPEG (ARM_IRQ0_BASE + 10)
  6308. +#define INTERRUPT_USB (ARM_IRQ0_BASE + 11)
  6309. +#define INTERRUPT_3D (ARM_IRQ0_BASE + 12)
  6310. +#define INTERRUPT_DMA2 (ARM_IRQ0_BASE + 13)
  6311. +#define INTERRUPT_DMA3 (ARM_IRQ0_BASE + 14)
  6312. +#define INTERRUPT_I2C (ARM_IRQ0_BASE + 15)
  6313. +#define INTERRUPT_SPI (ARM_IRQ0_BASE + 16)
  6314. +#define INTERRUPT_I2SPCM (ARM_IRQ0_BASE + 17)
  6315. +#define INTERRUPT_SDIO (ARM_IRQ0_BASE + 18)
  6316. +#define INTERRUPT_UART (ARM_IRQ0_BASE + 19)
  6317. +#define INTERRUPT_ARASANSDIO (ARM_IRQ0_BASE + 20)
  6318. +
  6319. +#define MAXIRQNUM (32 + 32 + 20)
  6320. +#define MAXFIQNUM (32 + 32 + 20)
  6321. +
  6322. +#define MAX_TIMER 2
  6323. +#define MAX_PERIOD 699050
  6324. +#define TICKS_PER_uSEC 1
  6325. +
  6326. +/*
  6327. + * These are useconds NOT ticks.
  6328. + *
  6329. + */
  6330. +#define mSEC_1 1000
  6331. +#define mSEC_5 (mSEC_1 * 5)
  6332. +#define mSEC_10 (mSEC_1 * 10)
  6333. +#define mSEC_25 (mSEC_1 * 25)
  6334. +#define SEC_1 (mSEC_1 * 1000)
  6335. +
  6336. +/*
  6337. + * Watchdog
  6338. + */
  6339. +#define PM_RSTC (PM_BASE+0x1c)
  6340. +#define PM_RSTS (PM_BASE+0x20)
  6341. +#define PM_WDOG (PM_BASE+0x24)
  6342. +
  6343. +#define PM_WDOG_RESET 0000000000
  6344. +#define PM_PASSWORD 0x5a000000
  6345. +#define PM_WDOG_TIME_SET 0x000fffff
  6346. +#define PM_RSTC_WRCFG_CLR 0xffffffcf
  6347. +#define PM_RSTC_WRCFG_SET 0x00000030
  6348. +#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  6349. +#define PM_RSTC_RESET 0x00000102
  6350. +
  6351. +#define PM_RSTS_HADPOR_SET 0x00001000
  6352. +#define PM_RSTS_HADSRH_SET 0x00000400
  6353. +#define PM_RSTS_HADSRF_SET 0x00000200
  6354. +#define PM_RSTS_HADSRQ_SET 0x00000100
  6355. +#define PM_RSTS_HADWRH_SET 0x00000040
  6356. +#define PM_RSTS_HADWRF_SET 0x00000020
  6357. +#define PM_RSTS_HADWRQ_SET 0x00000010
  6358. +#define PM_RSTS_HADDRH_SET 0x00000004
  6359. +#define PM_RSTS_HADDRF_SET 0x00000002
  6360. +#define PM_RSTS_HADDRQ_SET 0x00000001
  6361. +
  6362. +#define UART0_CLOCK 3000000
  6363. +
  6364. +#endif
  6365. +
  6366. +/* END */
  6367. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/power.h linux-rpi/arch/arm/mach-bcm2708/include/mach/power.h
  6368. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/power.h 1970-01-01 01:00:00.000000000 +0100
  6369. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/power.h 2014-04-24 15:35:00.777527936 +0200
  6370. @@ -0,0 +1,26 @@
  6371. +/*
  6372. + * linux/arch/arm/mach-bcm2708/power.h
  6373. + *
  6374. + * Copyright (C) 2010 Broadcom
  6375. + *
  6376. + * This program is free software; you can redistribute it and/or modify
  6377. + * it under the terms of the GNU General Public License version 2 as
  6378. + * published by the Free Software Foundation.
  6379. + *
  6380. + * This device provides a shared mechanism for controlling the power to
  6381. + * VideoCore subsystems.
  6382. + */
  6383. +
  6384. +#ifndef _MACH_BCM2708_POWER_H
  6385. +#define _MACH_BCM2708_POWER_H
  6386. +
  6387. +#include <linux/types.h>
  6388. +#include <mach/arm_power.h>
  6389. +
  6390. +typedef unsigned int BCM_POWER_HANDLE_T;
  6391. +
  6392. +extern int bcm_power_open(BCM_POWER_HANDLE_T *handle);
  6393. +extern int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request);
  6394. +extern int bcm_power_close(BCM_POWER_HANDLE_T handle);
  6395. +
  6396. +#endif
  6397. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/system.h linux-rpi/arch/arm/mach-bcm2708/include/mach/system.h
  6398. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/system.h 1970-01-01 01:00:00.000000000 +0100
  6399. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/system.h 2014-04-24 15:35:00.777527936 +0200
  6400. @@ -0,0 +1,38 @@
  6401. +/*
  6402. + * arch/arm/mach-bcm2708/include/mach/system.h
  6403. + *
  6404. + * Copyright (C) 2010 Broadcom
  6405. + * Copyright (C) 2003 ARM Limited
  6406. + * Copyright (C) 2000 Deep Blue Solutions Ltd
  6407. + *
  6408. + * This program is free software; you can redistribute it and/or modify
  6409. + * it under the terms of the GNU General Public License as published by
  6410. + * the Free Software Foundation; either version 2 of the License, or
  6411. + * (at your option) any later version.
  6412. + *
  6413. + * This program is distributed in the hope that it will be useful,
  6414. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6415. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6416. + * GNU General Public License for more details.
  6417. + *
  6418. + * You should have received a copy of the GNU General Public License
  6419. + * along with this program; if not, write to the Free Software
  6420. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6421. + */
  6422. +#ifndef __ASM_ARCH_SYSTEM_H
  6423. +#define __ASM_ARCH_SYSTEM_H
  6424. +
  6425. +#include <linux/io.h>
  6426. +#include <mach/hardware.h>
  6427. +#include <mach/platform.h>
  6428. +
  6429. +static inline void arch_idle(void)
  6430. +{
  6431. + /*
  6432. + * This should do all the clock switching
  6433. + * and wait for interrupt tricks
  6434. + */
  6435. + cpu_do_idle();
  6436. +}
  6437. +
  6438. +#endif
  6439. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/timex.h linux-rpi/arch/arm/mach-bcm2708/include/mach/timex.h
  6440. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/timex.h 1970-01-01 01:00:00.000000000 +0100
  6441. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/timex.h 2014-04-24 15:35:00.777527936 +0200
  6442. @@ -0,0 +1,23 @@
  6443. +/*
  6444. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6445. + *
  6446. + * BCM2708 sysem clock frequency
  6447. + *
  6448. + * Copyright (C) 2010 Broadcom
  6449. + *
  6450. + * This program is free software; you can redistribute it and/or modify
  6451. + * it under the terms of the GNU General Public License as published by
  6452. + * the Free Software Foundation; either version 2 of the License, or
  6453. + * (at your option) any later version.
  6454. + *
  6455. + * This program is distributed in the hope that it will be useful,
  6456. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6457. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6458. + * GNU General Public License for more details.
  6459. + *
  6460. + * You should have received a copy of the GNU General Public License
  6461. + * along with this program; if not, write to the Free Software
  6462. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6463. + */
  6464. +
  6465. +#define CLOCK_TICK_RATE (1000000)
  6466. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/uncompress.h linux-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h
  6467. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/uncompress.h 1970-01-01 01:00:00.000000000 +0100
  6468. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h 2014-04-24 15:35:00.777527936 +0200
  6469. @@ -0,0 +1,85 @@
  6470. +/*
  6471. + * arch/arm/mach-bcn2708/include/mach/uncompress.h
  6472. + *
  6473. + * Copyright (C) 2010 Broadcom
  6474. + * Copyright (C) 2003 ARM Limited
  6475. + *
  6476. + * This program is free software; you can redistribute it and/or modify
  6477. + * it under the terms of the GNU General Public License as published by
  6478. + * the Free Software Foundation; either version 2 of the License, or
  6479. + * (at your option) any later version.
  6480. + *
  6481. + * This program is distributed in the hope that it will be useful,
  6482. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6483. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6484. + * GNU General Public License for more details.
  6485. + *
  6486. + * You should have received a copy of the GNU General Public License
  6487. + * along with this program; if not, write to the Free Software
  6488. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6489. + */
  6490. +
  6491. +#include <linux/io.h>
  6492. +#include <linux/amba/serial.h>
  6493. +#include <mach/hardware.h>
  6494. +
  6495. +#define UART_BAUD 115200
  6496. +
  6497. +#define BCM2708_UART_DR __io(UART0_BASE + UART01x_DR)
  6498. +#define BCM2708_UART_FR __io(UART0_BASE + UART01x_FR)
  6499. +#define BCM2708_UART_IBRD __io(UART0_BASE + UART011_IBRD)
  6500. +#define BCM2708_UART_FBRD __io(UART0_BASE + UART011_FBRD)
  6501. +#define BCM2708_UART_LCRH __io(UART0_BASE + UART011_LCRH)
  6502. +#define BCM2708_UART_CR __io(UART0_BASE + UART011_CR)
  6503. +
  6504. +/*
  6505. + * This does not append a newline
  6506. + */
  6507. +static inline void putc(int c)
  6508. +{
  6509. + while (__raw_readl(BCM2708_UART_FR) & UART01x_FR_TXFF)
  6510. + barrier();
  6511. +
  6512. + __raw_writel(c, BCM2708_UART_DR);
  6513. +}
  6514. +
  6515. +static inline void flush(void)
  6516. +{
  6517. + int fr;
  6518. +
  6519. + do {
  6520. + fr = __raw_readl(BCM2708_UART_FR);
  6521. + barrier();
  6522. + } while ((fr & (UART011_FR_TXFE | UART01x_FR_BUSY)) != UART011_FR_TXFE);
  6523. +}
  6524. +
  6525. +static inline void arch_decomp_setup(void)
  6526. +{
  6527. + int temp, div, rem, frac;
  6528. +
  6529. + temp = 16 * UART_BAUD;
  6530. + div = UART0_CLOCK / temp;
  6531. + rem = UART0_CLOCK % temp;
  6532. + temp = (8 * rem) / UART_BAUD;
  6533. + frac = (temp >> 1) + (temp & 1);
  6534. +
  6535. + /* Make sure the UART is disabled before we start */
  6536. + __raw_writel(0, BCM2708_UART_CR);
  6537. +
  6538. + /* Set the baud rate */
  6539. + __raw_writel(div, BCM2708_UART_IBRD);
  6540. + __raw_writel(frac, BCM2708_UART_FBRD);
  6541. +
  6542. + /* Set the UART to 8n1, FIFO enabled */
  6543. + __raw_writel(UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN, BCM2708_UART_LCRH);
  6544. +
  6545. + /* Enable the UART */
  6546. + __raw_writel(UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE,
  6547. + BCM2708_UART_CR);
  6548. +}
  6549. +
  6550. +/*
  6551. + * nothing to do
  6552. + */
  6553. +#define arch_decomp_wdog()
  6554. +
  6555. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vcio.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h
  6556. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vcio.h 1970-01-01 01:00:00.000000000 +0100
  6557. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h 2014-04-24 15:35:00.777527936 +0200
  6558. @@ -0,0 +1,141 @@
  6559. +/*
  6560. + * arch/arm/mach-bcm2708/include/mach/vcio.h
  6561. + *
  6562. + * Copyright (C) 2010 Broadcom
  6563. + *
  6564. + * This program is free software; you can redistribute it and/or modify
  6565. + * it under the terms of the GNU General Public License as published by
  6566. + * the Free Software Foundation; either version 2 of the License, or
  6567. + * (at your option) any later version.
  6568. + *
  6569. + * This program is distributed in the hope that it will be useful,
  6570. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6571. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6572. + * GNU General Public License for more details.
  6573. + *
  6574. + * You should have received a copy of the GNU General Public License
  6575. + * along with this program; if not, write to the Free Software
  6576. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6577. + */
  6578. +#ifndef _MACH_BCM2708_VCIO_H
  6579. +#define _MACH_BCM2708_VCIO_H
  6580. +
  6581. +/* Routines to handle I/O via the VideoCore "ARM control" registers
  6582. + * (semaphores, doorbells, mailboxes)
  6583. + */
  6584. +
  6585. +#define BCM_VCIO_DRIVER_NAME "bcm2708_vcio"
  6586. +
  6587. +/* Constants shared with the ARM identifying separate mailbox channels */
  6588. +#define MBOX_CHAN_POWER 0 /* for use by the power management interface */
  6589. +#define MBOX_CHAN_FB 1 /* for use by the frame buffer */
  6590. +#define MBOX_CHAN_VCHIQ 3 /* for use by the VCHIQ interface */
  6591. +#define MBOX_CHAN_PROPERTY 8 /* for use by the property channel */
  6592. +#define MBOX_CHAN_COUNT 9
  6593. +
  6594. +/* Mailbox property tags */
  6595. +enum {
  6596. + VCMSG_PROPERTY_END = 0x00000000,
  6597. + VCMSG_GET_FIRMWARE_REVISION = 0x00000001,
  6598. + VCMSG_GET_BOARD_MODEL = 0x00010001,
  6599. + VCMSG_GET_BOARD_REVISION = 0x00020002,
  6600. + VCMSG_GET_BOARD_MAC_ADDRESS = 0x00020003,
  6601. + VCMSG_GET_BOARD_SERIAL = 0x00020004,
  6602. + VCMSG_GET_ARM_MEMORY = 0x00020005,
  6603. + VCMSG_GET_VC_MEMORY = 0x00020006,
  6604. + VCMSG_GET_CLOCKS = 0x00020007,
  6605. + VCMSG_GET_COMMAND_LINE = 0x00050001,
  6606. + VCMSG_GET_DMA_CHANNELS = 0x00060001,
  6607. + VCMSG_GET_POWER_STATE = 0x00020001,
  6608. + VCMSG_GET_TIMING = 0x00020002,
  6609. + VCMSG_SET_POWER_STATE = 0x00028001,
  6610. + VCMSG_GET_CLOCK_STATE = 0x00030001,
  6611. + VCMSG_SET_CLOCK_STATE = 0x00038001,
  6612. + VCMSG_GET_CLOCK_RATE = 0x00030002,
  6613. + VCMSG_SET_CLOCK_RATE = 0x00038002,
  6614. + VCMSG_GET_VOLTAGE = 0x00030003,
  6615. + VCMSG_SET_VOLTAGE = 0x00038003,
  6616. + VCMSG_GET_MAX_CLOCK = 0x00030004,
  6617. + VCMSG_GET_MAX_VOLTAGE = 0x00030005,
  6618. + VCMSG_GET_TEMPERATURE = 0x00030006,
  6619. + VCMSG_GET_MIN_CLOCK = 0x00030007,
  6620. + VCMSG_GET_MIN_VOLTAGE = 0x00030008,
  6621. + VCMSG_GET_TURBO = 0x00030009,
  6622. + VCMSG_SET_TURBO = 0x00038009,
  6623. + VCMSG_SET_ALLOCATE_BUFFER = 0x00040001,
  6624. + VCMSG_SET_RELEASE_BUFFER = 0x00048001,
  6625. + VCMSG_SET_BLANK_SCREEN = 0x00040002,
  6626. + VCMSG_TST_BLANK_SCREEN = 0x00044002,
  6627. + VCMSG_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
  6628. + VCMSG_TST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  6629. + VCMSG_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
  6630. + VCMSG_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
  6631. + VCMSG_TST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
  6632. + VCMSG_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
  6633. + VCMSG_GET_DEPTH = 0x00040005,
  6634. + VCMSG_TST_DEPTH = 0x00044005,
  6635. + VCMSG_SET_DEPTH = 0x00048005,
  6636. + VCMSG_GET_PIXEL_ORDER = 0x00040006,
  6637. + VCMSG_TST_PIXEL_ORDER = 0x00044006,
  6638. + VCMSG_SET_PIXEL_ORDER = 0x00048006,
  6639. + VCMSG_GET_ALPHA_MODE = 0x00040007,
  6640. + VCMSG_TST_ALPHA_MODE = 0x00044007,
  6641. + VCMSG_SET_ALPHA_MODE = 0x00048007,
  6642. + VCMSG_GET_PITCH = 0x00040008,
  6643. + VCMSG_TST_PITCH = 0x00044008,
  6644. + VCMSG_SET_PITCH = 0x00048008,
  6645. + VCMSG_GET_VIRTUAL_OFFSET = 0x00040009,
  6646. + VCMSG_TST_VIRTUAL_OFFSET = 0x00044009,
  6647. + VCMSG_SET_VIRTUAL_OFFSET = 0x00048009,
  6648. + VCMSG_GET_OVERSCAN = 0x0004000a,
  6649. + VCMSG_TST_OVERSCAN = 0x0004400a,
  6650. + VCMSG_SET_OVERSCAN = 0x0004800a,
  6651. + VCMSG_GET_PALETTE = 0x0004000b,
  6652. + VCMSG_TST_PALETTE = 0x0004400b,
  6653. + VCMSG_SET_PALETTE = 0x0004800b,
  6654. + VCMSG_GET_LAYER = 0x0004000c,
  6655. + VCMSG_TST_LAYER = 0x0004400c,
  6656. + VCMSG_SET_LAYER = 0x0004800c,
  6657. + VCMSG_GET_TRANSFORM = 0x0004000d,
  6658. + VCMSG_TST_TRANSFORM = 0x0004400d,
  6659. + VCMSG_SET_TRANSFORM = 0x0004800d,
  6660. +};
  6661. +
  6662. +extern int /*rc*/ bcm_mailbox_read(unsigned chan, uint32_t *data28);
  6663. +extern int /*rc*/ bcm_mailbox_write(unsigned chan, uint32_t data28);
  6664. +extern int /*rc*/ bcm_mailbox_property(void *data, int size);
  6665. +
  6666. +#include <linux/ioctl.h>
  6667. +
  6668. +/*
  6669. + * The major device number. We can't rely on dynamic
  6670. + * registration any more, because ioctls need to know
  6671. + * it.
  6672. + */
  6673. +#define MAJOR_NUM 100
  6674. +
  6675. +/*
  6676. + * Set the message of the device driver
  6677. + */
  6678. +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
  6679. +/*
  6680. + * _IOWR means that we're creating an ioctl command
  6681. + * number for passing information from a user process
  6682. + * to the kernel module and from the kernel module to user process
  6683. + *
  6684. + * The first arguments, MAJOR_NUM, is the major device
  6685. + * number we're using.
  6686. + *
  6687. + * The second argument is the number of the command
  6688. + * (there could be several with different meanings).
  6689. + *
  6690. + * The third argument is the type we want to get from
  6691. + * the process to the kernel.
  6692. + */
  6693. +
  6694. +/*
  6695. + * The name of the device file
  6696. + */
  6697. +#define DEVICE_FILE_NAME "char_dev"
  6698. +
  6699. +#endif
  6700. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vc_mem.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h
  6701. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vc_mem.h 1970-01-01 01:00:00.000000000 +0100
  6702. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h 2014-04-24 15:35:00.777527936 +0200
  6703. @@ -0,0 +1,36 @@
  6704. +/*****************************************************************************
  6705. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  6706. +*
  6707. +* Unless you and Broadcom execute a separate written software license
  6708. +* agreement governing use of this software, this software is licensed to you
  6709. +* under the terms of the GNU General Public License version 2, available at
  6710. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  6711. +*
  6712. +* Notwithstanding the above, under no circumstances may you combine this
  6713. +* software in any way with any other Broadcom software provided under a
  6714. +* license other than the GPL, without Broadcom's express prior written
  6715. +* consent.
  6716. +*****************************************************************************/
  6717. +
  6718. +#if !defined( VC_MEM_H )
  6719. +#define VC_MEM_H
  6720. +
  6721. +#include <linux/ioctl.h>
  6722. +
  6723. +#define VC_MEM_IOC_MAGIC 'v'
  6724. +
  6725. +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
  6726. +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
  6727. +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
  6728. +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
  6729. +
  6730. +#if defined( __KERNEL__ )
  6731. +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
  6732. +
  6733. +extern unsigned long mm_vc_mem_phys_addr;
  6734. +extern unsigned int mm_vc_mem_size;
  6735. +extern int vc_mem_get_current_size( void );
  6736. +#endif
  6737. +
  6738. +#endif /* VC_MEM_H */
  6739. +
  6740. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vc_support.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_support.h
  6741. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vc_support.h 1970-01-01 01:00:00.000000000 +0100
  6742. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_support.h 2014-04-24 15:35:00.777527936 +0200
  6743. @@ -0,0 +1,69 @@
  6744. +#ifndef _VC_SUPPORT_H_
  6745. +#define _VC_SUPPORT_H_
  6746. +
  6747. +/*
  6748. + * vc_support.h
  6749. + *
  6750. + * Created on: 25 Nov 2012
  6751. + * Author: Simon
  6752. + */
  6753. +
  6754. +enum {
  6755. +/*
  6756. + If a MEM_HANDLE_T is discardable, the memory manager may resize it to size
  6757. + 0 at any time when it is not locked or retained.
  6758. + */
  6759. + MEM_FLAG_DISCARDABLE = 1 << 0,
  6760. +
  6761. + /*
  6762. + If a MEM_HANDLE_T is allocating (or normal), its block of memory will be
  6763. + accessed in an allocating fashion through the cache.
  6764. + */
  6765. + MEM_FLAG_NORMAL = 0 << 2,
  6766. + MEM_FLAG_ALLOCATING = MEM_FLAG_NORMAL,
  6767. +
  6768. + /*
  6769. + If a MEM_HANDLE_T is direct, its block of memory will be accessed
  6770. + directly, bypassing the cache.
  6771. + */
  6772. + MEM_FLAG_DIRECT = 1 << 2,
  6773. +
  6774. + /*
  6775. + If a MEM_HANDLE_T is coherent, its block of memory will be accessed in a
  6776. + non-allocating fashion through the cache.
  6777. + */
  6778. + MEM_FLAG_COHERENT = 2 << 2,
  6779. +
  6780. + /*
  6781. + If a MEM_HANDLE_T is L1-nonallocating, its block of memory will be accessed by
  6782. + the VPU in a fashion which is allocating in L2, but only coherent in L1.
  6783. + */
  6784. + MEM_FLAG_L1_NONALLOCATING = (MEM_FLAG_DIRECT | MEM_FLAG_COHERENT),
  6785. +
  6786. + /*
  6787. + If a MEM_HANDLE_T is zero'd, its contents are set to 0 rather than
  6788. + MEM_HANDLE_INVALID on allocation and resize up.
  6789. + */
  6790. + MEM_FLAG_ZERO = 1 << 4,
  6791. +
  6792. + /*
  6793. + If a MEM_HANDLE_T is uninitialised, it will not be reset to a defined value
  6794. + (either zero, or all 1's) on allocation.
  6795. + */
  6796. + MEM_FLAG_NO_INIT = 1 << 5,
  6797. +
  6798. + /*
  6799. + Hints.
  6800. + */
  6801. + MEM_FLAG_HINT_PERMALOCK = 1 << 6, /* Likely to be locked for long periods of time. */
  6802. +};
  6803. +
  6804. +unsigned int AllocateVcMemory(unsigned int *pHandle, unsigned int size, unsigned int alignment, unsigned int flags);
  6805. +unsigned int ReleaseVcMemory(unsigned int handle);
  6806. +unsigned int LockVcMemory(unsigned int *pBusAddress, unsigned int handle);
  6807. +unsigned int UnlockVcMemory(unsigned int handle);
  6808. +
  6809. +unsigned int ExecuteVcCode(unsigned int code,
  6810. + unsigned int r0, unsigned int r1, unsigned int r2, unsigned int r3, unsigned int r4, unsigned int r5);
  6811. +
  6812. +#endif
  6813. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vmalloc.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h
  6814. --- linux-3.10.37/arch/arm/mach-bcm2708/include/mach/vmalloc.h 1970-01-01 01:00:00.000000000 +0100
  6815. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h 2014-04-24 15:35:00.777527936 +0200
  6816. @@ -0,0 +1,20 @@
  6817. +/*
  6818. + * arch/arm/mach-bcm2708/include/mach/vmalloc.h
  6819. + *
  6820. + * Copyright (C) 2010 Broadcom
  6821. + *
  6822. + * This program is free software; you can redistribute it and/or modify
  6823. + * it under the terms of the GNU General Public License as published by
  6824. + * the Free Software Foundation; either version 2 of the License, or
  6825. + * (at your option) any later version.
  6826. + *
  6827. + * This program is distributed in the hope that it will be useful,
  6828. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6829. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6830. + * GNU General Public License for more details.
  6831. + *
  6832. + * You should have received a copy of the GNU General Public License
  6833. + * along with this program; if not, write to the Free Software
  6834. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6835. + */
  6836. +#define VMALLOC_END (0xe8000000)
  6837. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/Kconfig linux-rpi/arch/arm/mach-bcm2708/Kconfig
  6838. --- linux-3.10.37/arch/arm/mach-bcm2708/Kconfig 1970-01-01 01:00:00.000000000 +0100
  6839. +++ linux-rpi/arch/arm/mach-bcm2708/Kconfig 2014-04-24 15:35:00.773527891 +0200
  6840. @@ -0,0 +1,49 @@
  6841. +menu "Broadcom BCM2708 Implementations"
  6842. + depends on ARCH_BCM2708
  6843. +
  6844. +config MACH_BCM2708
  6845. + bool "Broadcom BCM2708 Development Platform"
  6846. + select NEED_MACH_MEMORY_H
  6847. + select NEED_MACH_IO_H
  6848. + select CPU_V6
  6849. + help
  6850. + Include support for the Broadcom(R) BCM2708 platform.
  6851. +
  6852. +config BCM2708_GPIO
  6853. + bool "BCM2708 gpio support"
  6854. + depends on MACH_BCM2708
  6855. + select ARCH_REQUIRE_GPIOLIB
  6856. + default y
  6857. + help
  6858. + Include support for the Broadcom(R) BCM2708 gpio.
  6859. +
  6860. +config BCM2708_VCMEM
  6861. + bool "Videocore Memory"
  6862. + depends on MACH_BCM2708
  6863. + default y
  6864. + help
  6865. + Helper for videocore memory access and total size allocation.
  6866. +
  6867. +config BCM2708_NOL2CACHE
  6868. + bool "Videocore L2 cache disable"
  6869. + depends on MACH_BCM2708
  6870. + default n
  6871. + help
  6872. + Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  6873. +
  6874. +config BCM2708_SPIDEV
  6875. + bool "Bind spidev to SPI0 master"
  6876. + depends on MACH_BCM2708
  6877. + depends on SPI
  6878. + default y
  6879. + help
  6880. + Binds spidev driver to the SPI0 master
  6881. +
  6882. +config BCM2708_DMAER
  6883. + tristate "BCM2708 DMA helper"
  6884. + depends on MACH_BCM2708
  6885. + default n
  6886. + help
  6887. + Enable DMA helper for accelerating X composition
  6888. +
  6889. +endmenu
  6890. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/Makefile linux-rpi/arch/arm/mach-bcm2708/Makefile
  6891. --- linux-3.10.37/arch/arm/mach-bcm2708/Makefile 1970-01-01 01:00:00.000000000 +0100
  6892. +++ linux-rpi/arch/arm/mach-bcm2708/Makefile 2014-04-24 15:35:00.773527891 +0200
  6893. @@ -0,0 +1,11 @@
  6894. +#
  6895. +# Makefile for the linux kernel.
  6896. +#
  6897. +
  6898. +obj-$(CONFIG_MACH_BCM2708) += clock.o bcm2708.o armctrl.o vcio.o power.o dma.o
  6899. +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
  6900. +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
  6901. +
  6902. +obj-$(CONFIG_BCM2708_DMAER) += dmaer_master.o
  6903. +dmaer_master-objs := dmaer.o vc_support.o
  6904. +
  6905. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/Makefile.boot linux-rpi/arch/arm/mach-bcm2708/Makefile.boot
  6906. --- linux-3.10.37/arch/arm/mach-bcm2708/Makefile.boot 1970-01-01 01:00:00.000000000 +0100
  6907. +++ linux-rpi/arch/arm/mach-bcm2708/Makefile.boot 2014-04-24 15:35:00.773527891 +0200
  6908. @@ -0,0 +1,3 @@
  6909. + zreladdr-y := 0x00008000
  6910. +params_phys-y := 0x00000100
  6911. +initrd_phys-y := 0x00800000
  6912. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/power.c linux-rpi/arch/arm/mach-bcm2708/power.c
  6913. --- linux-3.10.37/arch/arm/mach-bcm2708/power.c 1970-01-01 01:00:00.000000000 +0100
  6914. +++ linux-rpi/arch/arm/mach-bcm2708/power.c 2014-04-24 15:35:00.777527936 +0200
  6915. @@ -0,0 +1,194 @@
  6916. +/*
  6917. + * linux/arch/arm/mach-bcm2708/power.c
  6918. + *
  6919. + * Copyright (C) 2010 Broadcom
  6920. + *
  6921. + * This program is free software; you can redistribute it and/or modify
  6922. + * it under the terms of the GNU General Public License version 2 as
  6923. + * published by the Free Software Foundation.
  6924. + *
  6925. + * This device provides a shared mechanism for controlling the power to
  6926. + * VideoCore subsystems.
  6927. + */
  6928. +
  6929. +#include <linux/module.h>
  6930. +#include <linux/semaphore.h>
  6931. +#include <linux/bug.h>
  6932. +#include <mach/power.h>
  6933. +#include <mach/vcio.h>
  6934. +#include <mach/arm_power.h>
  6935. +
  6936. +#define DRIVER_NAME "bcm2708_power"
  6937. +
  6938. +#define BCM_POWER_MAXCLIENTS 4
  6939. +#define BCM_POWER_NOCLIENT (1<<31)
  6940. +
  6941. +/* Some drivers expect there devices to be permanently powered */
  6942. +#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
  6943. +
  6944. +#if 1
  6945. +#define DPRINTK printk
  6946. +#else
  6947. +#define DPRINTK if (0) printk
  6948. +#endif
  6949. +
  6950. +struct state_struct {
  6951. + uint32_t global_request;
  6952. + uint32_t client_request[BCM_POWER_MAXCLIENTS];
  6953. + struct semaphore client_mutex;
  6954. + struct semaphore mutex;
  6955. +} g_state;
  6956. +
  6957. +int bcm_power_open(BCM_POWER_HANDLE_T *handle)
  6958. +{
  6959. + BCM_POWER_HANDLE_T i;
  6960. + int ret = -EBUSY;
  6961. +
  6962. + down(&g_state.client_mutex);
  6963. +
  6964. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  6965. + if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
  6966. + g_state.client_request[i] = BCM_POWER_NONE;
  6967. + *handle = i;
  6968. + ret = 0;
  6969. + break;
  6970. + }
  6971. + }
  6972. +
  6973. + up(&g_state.client_mutex);
  6974. +
  6975. + DPRINTK("bcm_power_open() -> %d\n", *handle);
  6976. +
  6977. + return ret;
  6978. +}
  6979. +EXPORT_SYMBOL_GPL(bcm_power_open);
  6980. +
  6981. +int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
  6982. +{
  6983. + int rc = 0;
  6984. +
  6985. + DPRINTK("bcm_power_request(%d, %x)\n", handle, request);
  6986. +
  6987. + if ((handle < BCM_POWER_MAXCLIENTS) &&
  6988. + (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
  6989. + if (down_interruptible(&g_state.mutex) != 0) {
  6990. + DPRINTK("bcm_power_request -> interrupted\n");
  6991. + return -EINTR;
  6992. + }
  6993. +
  6994. + if (request != g_state.client_request[handle]) {
  6995. + uint32_t others_request = 0;
  6996. + uint32_t global_request;
  6997. + BCM_POWER_HANDLE_T i;
  6998. +
  6999. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  7000. + if (i != handle)
  7001. + others_request |=
  7002. + g_state.client_request[i];
  7003. + }
  7004. + others_request &= ~BCM_POWER_NOCLIENT;
  7005. +
  7006. + global_request = request | others_request;
  7007. + if (global_request != g_state.global_request) {
  7008. + uint32_t actual;
  7009. +
  7010. + /* Send a request to VideoCore */
  7011. + bcm_mailbox_write(MBOX_CHAN_POWER,
  7012. + global_request << 4);
  7013. +
  7014. + /* Wait for a response during power-up */
  7015. + if (global_request & ~g_state.global_request) {
  7016. + rc = bcm_mailbox_read(MBOX_CHAN_POWER,
  7017. + &actual);
  7018. + DPRINTK
  7019. + ("bcm_mailbox_read -> %08x, %d\n",
  7020. + actual, rc);
  7021. + actual >>= 4;
  7022. + } else {
  7023. + rc = 0;
  7024. + actual = global_request;
  7025. + }
  7026. +
  7027. + if (rc == 0) {
  7028. + if (actual != global_request) {
  7029. + printk(KERN_ERR
  7030. + "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
  7031. + __func__,
  7032. + g_state.global_request,
  7033. + global_request, actual, request, others_request);
  7034. + /* A failure */
  7035. + BUG_ON((others_request & actual)
  7036. + != others_request);
  7037. + request &= actual;
  7038. + rc = -EIO;
  7039. + }
  7040. +
  7041. + g_state.global_request = actual;
  7042. + g_state.client_request[handle] =
  7043. + request;
  7044. + }
  7045. + }
  7046. + }
  7047. + up(&g_state.mutex);
  7048. + } else {
  7049. + rc = -EINVAL;
  7050. + }
  7051. + DPRINTK("bcm_power_request -> %d\n", rc);
  7052. + return rc;
  7053. +}
  7054. +EXPORT_SYMBOL_GPL(bcm_power_request);
  7055. +
  7056. +int bcm_power_close(BCM_POWER_HANDLE_T handle)
  7057. +{
  7058. + int rc;
  7059. +
  7060. + DPRINTK("bcm_power_close(%d)\n", handle);
  7061. +
  7062. + rc = bcm_power_request(handle, BCM_POWER_NONE);
  7063. + if (rc == 0)
  7064. + g_state.client_request[handle] = BCM_POWER_NOCLIENT;
  7065. +
  7066. + return rc;
  7067. +}
  7068. +EXPORT_SYMBOL_GPL(bcm_power_close);
  7069. +
  7070. +static int __init bcm_power_init(void)
  7071. +{
  7072. +#if defined(BCM_POWER_ALWAYS_ON)
  7073. + BCM_POWER_HANDLE_T always_on_handle;
  7074. +#endif
  7075. + int rc = 0;
  7076. + int i;
  7077. +
  7078. + printk(KERN_INFO "bcm_power: Broadcom power driver\n");
  7079. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  7080. +
  7081. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
  7082. + g_state.client_request[i] = BCM_POWER_NOCLIENT;
  7083. +
  7084. + sema_init(&g_state.client_mutex, 1);
  7085. + sema_init(&g_state.mutex, 1);
  7086. +
  7087. + g_state.global_request = 0;
  7088. +
  7089. +#if defined(BCM_POWER_ALWAYS_ON)
  7090. + if (BCM_POWER_ALWAYS_ON) {
  7091. + bcm_power_open(&always_on_handle);
  7092. + bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
  7093. + }
  7094. +#endif
  7095. +
  7096. + return rc;
  7097. +}
  7098. +
  7099. +static void __exit bcm_power_exit(void)
  7100. +{
  7101. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  7102. +}
  7103. +
  7104. +arch_initcall(bcm_power_init); /* Initialize early */
  7105. +module_exit(bcm_power_exit);
  7106. +
  7107. +MODULE_AUTHOR("Phil Elwell");
  7108. +MODULE_DESCRIPTION("Interface to BCM2708 power management");
  7109. +MODULE_LICENSE("GPL");
  7110. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/vcio.c linux-rpi/arch/arm/mach-bcm2708/vcio.c
  7111. --- linux-3.10.37/arch/arm/mach-bcm2708/vcio.c 1970-01-01 01:00:00.000000000 +0100
  7112. +++ linux-rpi/arch/arm/mach-bcm2708/vcio.c 2014-04-24 15:35:00.777527936 +0200
  7113. @@ -0,0 +1,474 @@
  7114. +/*
  7115. + * linux/arch/arm/mach-bcm2708/vcio.c
  7116. + *
  7117. + * Copyright (C) 2010 Broadcom
  7118. + *
  7119. + * This program is free software; you can redistribute it and/or modify
  7120. + * it under the terms of the GNU General Public License version 2 as
  7121. + * published by the Free Software Foundation.
  7122. + *
  7123. + * This device provides a shared mechanism for writing to the mailboxes,
  7124. + * semaphores, doorbells etc. that are shared between the ARM and the
  7125. + * VideoCore processor
  7126. + */
  7127. +
  7128. +#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  7129. +#define SUPPORT_SYSRQ
  7130. +#endif
  7131. +
  7132. +#include <linux/module.h>
  7133. +#include <linux/console.h>
  7134. +#include <linux/serial_core.h>
  7135. +#include <linux/serial.h>
  7136. +#include <linux/errno.h>
  7137. +#include <linux/device.h>
  7138. +#include <linux/init.h>
  7139. +#include <linux/mm.h>
  7140. +#include <linux/dma-mapping.h>
  7141. +#include <linux/platform_device.h>
  7142. +#include <linux/sysrq.h>
  7143. +#include <linux/delay.h>
  7144. +#include <linux/slab.h>
  7145. +#include <linux/interrupt.h>
  7146. +#include <linux/irq.h>
  7147. +
  7148. +#include <linux/io.h>
  7149. +
  7150. +#include <mach/vcio.h>
  7151. +#include <mach/platform.h>
  7152. +
  7153. +#include <asm/uaccess.h>
  7154. +
  7155. +
  7156. +#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
  7157. +
  7158. +/* ----------------------------------------------------------------------
  7159. + * Mailbox
  7160. + * -------------------------------------------------------------------- */
  7161. +
  7162. +/* offsets from a mail box base address */
  7163. +#define MAIL_WRT 0x00 /* write - and next 4 words */
  7164. +#define MAIL_RD 0x00 /* read - and next 4 words */
  7165. +#define MAIL_POL 0x10 /* read without popping the fifo */
  7166. +#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
  7167. +#define MAIL_STA 0x18 /* status */
  7168. +#define MAIL_CNF 0x1C /* configuration */
  7169. +
  7170. +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  7171. +#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
  7172. +#define MBOX_CHAN(msg) ((msg) & 0xf)
  7173. +#define MBOX_DATA28(msg) ((msg) & ~0xf)
  7174. +#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
  7175. +
  7176. +#define MBOX_MAGIC 0xd0d0c0de
  7177. +
  7178. +struct vc_mailbox {
  7179. + struct device *dev; /* parent device */
  7180. + void __iomem *status;
  7181. + void __iomem *config;
  7182. + void __iomem *read;
  7183. + void __iomem *write;
  7184. + uint32_t msg[MBOX_CHAN_COUNT];
  7185. + struct semaphore sema[MBOX_CHAN_COUNT];
  7186. + uint32_t magic;
  7187. +};
  7188. +
  7189. +static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
  7190. + uint32_t addr_mbox)
  7191. +{
  7192. + int i;
  7193. +
  7194. + mbox_out->dev = dev;
  7195. + mbox_out->status = __io_address(addr_mbox + MAIL_STA);
  7196. + mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
  7197. + mbox_out->read = __io_address(addr_mbox + MAIL_RD);
  7198. + /* Write to the other mailbox */
  7199. + mbox_out->write =
  7200. + __io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
  7201. + MAIL_WRT);
  7202. +
  7203. + for (i = 0; i < MBOX_CHAN_COUNT; i++) {
  7204. + mbox_out->msg[i] = 0;
  7205. + sema_init(&mbox_out->sema[i], 0);
  7206. + }
  7207. +
  7208. + /* Enable the interrupt on data reception */
  7209. + writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
  7210. +
  7211. + mbox_out->magic = MBOX_MAGIC;
  7212. +}
  7213. +
  7214. +static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
  7215. +{
  7216. + int rc;
  7217. +
  7218. + if (mbox->magic != MBOX_MAGIC)
  7219. + rc = -EINVAL;
  7220. + else {
  7221. + /* wait for the mailbox FIFO to have some space in it */
  7222. + while (0 != (readl(mbox->status) & ARM_MS_FULL))
  7223. + cpu_relax();
  7224. +
  7225. + writel(MBOX_MSG(chan, data28), mbox->write);
  7226. + rc = 0;
  7227. + }
  7228. + return rc;
  7229. +}
  7230. +
  7231. +static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
  7232. +{
  7233. + int rc;
  7234. +
  7235. + if (mbox->magic != MBOX_MAGIC)
  7236. + rc = -EINVAL;
  7237. + else {
  7238. + down(&mbox->sema[chan]);
  7239. + *data28 = MBOX_DATA28(mbox->msg[chan]);
  7240. + mbox->msg[chan] = 0;
  7241. + rc = 0;
  7242. + }
  7243. + return rc;
  7244. +}
  7245. +
  7246. +static irqreturn_t mbox_irq(int irq, void *dev_id)
  7247. +{
  7248. + /* wait for the mailbox FIFO to have some data in it */
  7249. + struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
  7250. + int status = readl(mbox->status);
  7251. + int ret = IRQ_NONE;
  7252. +
  7253. + while (!(status & ARM_MS_EMPTY)) {
  7254. + uint32_t msg = readl(mbox->read);
  7255. + int chan = MBOX_CHAN(msg);
  7256. + if (chan < MBOX_CHAN_COUNT) {
  7257. + if (mbox->msg[chan]) {
  7258. + /* Overflow */
  7259. + printk(KERN_ERR DRIVER_NAME
  7260. + ": mbox chan %d overflow - drop %08x\n",
  7261. + chan, msg);
  7262. + } else {
  7263. + mbox->msg[chan] = (msg | 0xf);
  7264. + up(&mbox->sema[chan]);
  7265. + }
  7266. + } else {
  7267. + printk(KERN_ERR DRIVER_NAME
  7268. + ": invalid channel selector (msg %08x)\n", msg);
  7269. + }
  7270. + ret = IRQ_HANDLED;
  7271. + status = readl(mbox->status);
  7272. + }
  7273. + return ret;
  7274. +}
  7275. +
  7276. +static struct irqaction mbox_irqaction = {
  7277. + .name = "ARM Mailbox IRQ",
  7278. + .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  7279. + .handler = mbox_irq,
  7280. +};
  7281. +
  7282. +/* ----------------------------------------------------------------------
  7283. + * Mailbox Methods
  7284. + * -------------------------------------------------------------------- */
  7285. +
  7286. +static struct device *mbox_dev; /* we assume there's only one! */
  7287. +
  7288. +static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
  7289. +{
  7290. + int rc;
  7291. +
  7292. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  7293. + device_lock(dev);
  7294. + rc = mbox_write(mailbox, chan, data28);
  7295. + device_unlock(dev);
  7296. +
  7297. + return rc;
  7298. +}
  7299. +
  7300. +static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
  7301. +{
  7302. + int rc;
  7303. +
  7304. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  7305. + device_lock(dev);
  7306. + rc = mbox_read(mailbox, chan, data28);
  7307. + device_unlock(dev);
  7308. +
  7309. + return rc;
  7310. +}
  7311. +
  7312. +extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
  7313. +{
  7314. + if (mbox_dev)
  7315. + return dev_mbox_write(mbox_dev, chan, data28);
  7316. + else
  7317. + return -ENODEV;
  7318. +}
  7319. +EXPORT_SYMBOL_GPL(bcm_mailbox_write);
  7320. +
  7321. +extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
  7322. +{
  7323. + if (mbox_dev)
  7324. + return dev_mbox_read(mbox_dev, chan, data28);
  7325. + else
  7326. + return -ENODEV;
  7327. +}
  7328. +EXPORT_SYMBOL_GPL(bcm_mailbox_read);
  7329. +
  7330. +static void dev_mbox_register(const char *dev_name, struct device *dev)
  7331. +{
  7332. + mbox_dev = dev;
  7333. +}
  7334. +
  7335. +static int mbox_copy_from_user(void *dst, const void *src, int size)
  7336. +{
  7337. + if ( (uint32_t)src < TASK_SIZE)
  7338. + {
  7339. + return copy_from_user(dst, src, size);
  7340. + }
  7341. + else
  7342. + {
  7343. + memcpy( dst, src, size );
  7344. + return 0;
  7345. + }
  7346. +}
  7347. +
  7348. +static int mbox_copy_to_user(void *dst, const void *src, int size)
  7349. +{
  7350. + if ( (uint32_t)dst < TASK_SIZE)
  7351. + {
  7352. + return copy_to_user(dst, src, size);
  7353. + }
  7354. + else
  7355. + {
  7356. + memcpy( dst, src, size );
  7357. + return 0;
  7358. + }
  7359. +}
  7360. +
  7361. +static DEFINE_MUTEX(mailbox_lock);
  7362. +extern int bcm_mailbox_property(void *data, int size)
  7363. +{
  7364. + uint32_t success;
  7365. + dma_addr_t mem_bus; /* the memory address accessed from videocore */
  7366. + void *mem_kern; /* the memory address accessed from driver */
  7367. + int s = 0;
  7368. +
  7369. + mutex_lock(&mailbox_lock);
  7370. + /* allocate some memory for the messages communicating with GPU */
  7371. + mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
  7372. + if (mem_kern) {
  7373. + /* create the message */
  7374. + mbox_copy_from_user(mem_kern, data, size);
  7375. +
  7376. + /* send the message */
  7377. + wmb();
  7378. + s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
  7379. + if (s == 0) {
  7380. + s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
  7381. + }
  7382. + if (s == 0) {
  7383. + /* copy the response */
  7384. + rmb();
  7385. + mbox_copy_to_user(data, mem_kern, size);
  7386. + }
  7387. + dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
  7388. + } else {
  7389. + s = -ENOMEM;
  7390. + }
  7391. + if (s != 0)
  7392. + printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
  7393. +
  7394. + mutex_unlock(&mailbox_lock);
  7395. + return s;
  7396. +}
  7397. +EXPORT_SYMBOL_GPL(bcm_mailbox_property);
  7398. +
  7399. +/* ----------------------------------------------------------------------
  7400. + * Platform Device for Mailbox
  7401. + * -------------------------------------------------------------------- */
  7402. +
  7403. +/*
  7404. + * Is the device open right now? Used to prevent
  7405. + * concurent access into the same device
  7406. + */
  7407. +static int Device_Open = 0;
  7408. +
  7409. +/*
  7410. + * This is called whenever a process attempts to open the device file
  7411. + */
  7412. +static int device_open(struct inode *inode, struct file *file)
  7413. +{
  7414. + /*
  7415. + * We don't want to talk to two processes at the same time
  7416. + */
  7417. + if (Device_Open)
  7418. + return -EBUSY;
  7419. +
  7420. + Device_Open++;
  7421. + /*
  7422. + * Initialize the message
  7423. + */
  7424. + try_module_get(THIS_MODULE);
  7425. + return 0;
  7426. +}
  7427. +
  7428. +static int device_release(struct inode *inode, struct file *file)
  7429. +{
  7430. + /*
  7431. + * We're now ready for our next caller
  7432. + */
  7433. + Device_Open--;
  7434. +
  7435. + module_put(THIS_MODULE);
  7436. + return 0;
  7437. +}
  7438. +
  7439. +/*
  7440. + * This function is called whenever a process tries to do an ioctl on our
  7441. + * device file. We get two extra parameters (additional to the inode and file
  7442. + * structures, which all device functions get): the number of the ioctl called
  7443. + * and the parameter given to the ioctl function.
  7444. + *
  7445. + * If the ioctl is write or read/write (meaning output is returned to the
  7446. + * calling process), the ioctl call returns the output of this function.
  7447. + *
  7448. + */
  7449. +static long device_ioctl(struct file *file, /* see include/linux/fs.h */
  7450. + unsigned int ioctl_num, /* number and param for ioctl */
  7451. + unsigned long ioctl_param)
  7452. +{
  7453. + unsigned size;
  7454. + /*
  7455. + * Switch according to the ioctl called
  7456. + */
  7457. + switch (ioctl_num) {
  7458. + case IOCTL_MBOX_PROPERTY:
  7459. + /*
  7460. + * Receive a pointer to a message (in user space) and set that
  7461. + * to be the device's message. Get the parameter given to
  7462. + * ioctl by the process.
  7463. + */
  7464. + mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
  7465. + return bcm_mailbox_property((void *)ioctl_param, size);
  7466. + break;
  7467. + default:
  7468. + printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
  7469. + return -EINVAL;
  7470. + }
  7471. +
  7472. + return 0;
  7473. +}
  7474. +
  7475. +/* Module Declarations */
  7476. +
  7477. +/*
  7478. + * This structure will hold the functions to be called
  7479. + * when a process does something to the device we
  7480. + * created. Since a pointer to this structure is kept in
  7481. + * the devices table, it can't be local to
  7482. + * init_module. NULL is for unimplemented functios.
  7483. + */
  7484. +struct file_operations fops = {
  7485. + .unlocked_ioctl = device_ioctl,
  7486. + .open = device_open,
  7487. + .release = device_release, /* a.k.a. close */
  7488. +};
  7489. +
  7490. +static int bcm_vcio_probe(struct platform_device *pdev)
  7491. +{
  7492. + int ret = 0;
  7493. + struct vc_mailbox *mailbox;
  7494. +
  7495. + mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
  7496. + if (NULL == mailbox) {
  7497. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  7498. + "mailbox memory\n");
  7499. + ret = -ENOMEM;
  7500. + } else {
  7501. + struct resource *res;
  7502. +
  7503. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  7504. + if (res == NULL) {
  7505. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  7506. + "resource\n");
  7507. + ret = -ENODEV;
  7508. + kfree(mailbox);
  7509. + } else {
  7510. + /* should be based on the registers from res really */
  7511. + mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
  7512. +
  7513. + platform_set_drvdata(pdev, mailbox);
  7514. + dev_mbox_register(DRIVER_NAME, &pdev->dev);
  7515. +
  7516. + mbox_irqaction.dev_id = mailbox;
  7517. + setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
  7518. + printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
  7519. + __io_address(ARM_0_MAIL0_RD));
  7520. + }
  7521. + }
  7522. +
  7523. + if (ret == 0) {
  7524. + /*
  7525. + * Register the character device
  7526. + */
  7527. + ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
  7528. +
  7529. + /*
  7530. + * Negative values signify an error
  7531. + */
  7532. + if (ret < 0) {
  7533. + printk(KERN_ERR DRIVER_NAME
  7534. + "Failed registering the character device %d\n", ret);
  7535. + return ret;
  7536. + }
  7537. + }
  7538. + return ret;
  7539. +}
  7540. +
  7541. +static int bcm_vcio_remove(struct platform_device *pdev)
  7542. +{
  7543. + struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
  7544. +
  7545. + platform_set_drvdata(pdev, NULL);
  7546. + kfree(mailbox);
  7547. +
  7548. + return 0;
  7549. +}
  7550. +
  7551. +static struct platform_driver bcm_mbox_driver = {
  7552. + .probe = bcm_vcio_probe,
  7553. + .remove = bcm_vcio_remove,
  7554. +
  7555. + .driver = {
  7556. + .name = DRIVER_NAME,
  7557. + .owner = THIS_MODULE,
  7558. + },
  7559. +};
  7560. +
  7561. +static int __init bcm_mbox_init(void)
  7562. +{
  7563. + int ret;
  7564. +
  7565. + printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
  7566. +
  7567. + ret = platform_driver_register(&bcm_mbox_driver);
  7568. + if (ret != 0) {
  7569. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  7570. + "on platform\n");
  7571. + }
  7572. +
  7573. + return ret;
  7574. +}
  7575. +
  7576. +static void __exit bcm_mbox_exit(void)
  7577. +{
  7578. + platform_driver_unregister(&bcm_mbox_driver);
  7579. +}
  7580. +
  7581. +arch_initcall(bcm_mbox_init); /* Initialize early */
  7582. +module_exit(bcm_mbox_exit);
  7583. +
  7584. +MODULE_AUTHOR("Gray Girling");
  7585. +MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
  7586. +MODULE_LICENSE("GPL");
  7587. +MODULE_ALIAS("platform:bcm-mbox");
  7588. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/vc_mem.c linux-rpi/arch/arm/mach-bcm2708/vc_mem.c
  7589. --- linux-3.10.37/arch/arm/mach-bcm2708/vc_mem.c 1970-01-01 01:00:00.000000000 +0100
  7590. +++ linux-rpi/arch/arm/mach-bcm2708/vc_mem.c 2014-04-24 15:35:00.777527936 +0200
  7591. @@ -0,0 +1,432 @@
  7592. +/*****************************************************************************
  7593. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  7594. +*
  7595. +* Unless you and Broadcom execute a separate written software license
  7596. +* agreement governing use of this software, this software is licensed to you
  7597. +* under the terms of the GNU General Public License version 2, available at
  7598. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7599. +*
  7600. +* Notwithstanding the above, under no circumstances may you combine this
  7601. +* software in any way with any other Broadcom software provided under a
  7602. +* license other than the GPL, without Broadcom's express prior written
  7603. +* consent.
  7604. +*****************************************************************************/
  7605. +
  7606. +#include <linux/kernel.h>
  7607. +#include <linux/module.h>
  7608. +#include <linux/fs.h>
  7609. +#include <linux/device.h>
  7610. +#include <linux/cdev.h>
  7611. +#include <linux/mm.h>
  7612. +#include <linux/slab.h>
  7613. +#include <linux/debugfs.h>
  7614. +#include <asm/uaccess.h>
  7615. +#include <linux/dma-mapping.h>
  7616. +
  7617. +#ifdef CONFIG_ARCH_KONA
  7618. +#include <chal/chal_ipc.h>
  7619. +#elif CONFIG_ARCH_BCM2708
  7620. +#else
  7621. +#include <csp/chal_ipc.h>
  7622. +#endif
  7623. +
  7624. +#include "mach/vc_mem.h"
  7625. +#include <mach/vcio.h>
  7626. +
  7627. +#define DRIVER_NAME "vc-mem"
  7628. +
  7629. +// Device (/dev) related variables
  7630. +static dev_t vc_mem_devnum = 0;
  7631. +static struct class *vc_mem_class = NULL;
  7632. +static struct cdev vc_mem_cdev;
  7633. +static int vc_mem_inited = 0;
  7634. +
  7635. +#ifdef CONFIG_DEBUG_FS
  7636. +static struct dentry *vc_mem_debugfs_entry;
  7637. +#endif
  7638. +
  7639. +/*
  7640. + * Videocore memory addresses and size
  7641. + *
  7642. + * Drivers that wish to know the videocore memory addresses and sizes should
  7643. + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
  7644. + * headers. This allows the other drivers to not be tied down to a a certain
  7645. + * address/size at compile time.
  7646. + *
  7647. + * In the future, the goal is to have the videocore memory virtual address and
  7648. + * size be calculated at boot time rather than at compile time. The decision of
  7649. + * where the videocore memory resides and its size would be in the hands of the
  7650. + * bootloader (and/or kernel). When that happens, the values of these variables
  7651. + * would be calculated and assigned in the init function.
  7652. + */
  7653. +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
  7654. +unsigned long mm_vc_mem_phys_addr = 0x00000000;
  7655. +unsigned int mm_vc_mem_size = 0;
  7656. +unsigned int mm_vc_mem_base = 0;
  7657. +
  7658. +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
  7659. +EXPORT_SYMBOL(mm_vc_mem_size);
  7660. +EXPORT_SYMBOL(mm_vc_mem_base);
  7661. +
  7662. +static uint phys_addr = 0;
  7663. +static uint mem_size = 0;
  7664. +static uint mem_base = 0;
  7665. +
  7666. +
  7667. +/****************************************************************************
  7668. +*
  7669. +* vc_mem_open
  7670. +*
  7671. +***************************************************************************/
  7672. +
  7673. +static int
  7674. +vc_mem_open(struct inode *inode, struct file *file)
  7675. +{
  7676. + (void) inode;
  7677. + (void) file;
  7678. +
  7679. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  7680. +
  7681. + return 0;
  7682. +}
  7683. +
  7684. +/****************************************************************************
  7685. +*
  7686. +* vc_mem_release
  7687. +*
  7688. +***************************************************************************/
  7689. +
  7690. +static int
  7691. +vc_mem_release(struct inode *inode, struct file *file)
  7692. +{
  7693. + (void) inode;
  7694. + (void) file;
  7695. +
  7696. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  7697. +
  7698. + return 0;
  7699. +}
  7700. +
  7701. +/****************************************************************************
  7702. +*
  7703. +* vc_mem_get_size
  7704. +*
  7705. +***************************************************************************/
  7706. +
  7707. +static void
  7708. +vc_mem_get_size(void)
  7709. +{
  7710. +}
  7711. +
  7712. +/****************************************************************************
  7713. +*
  7714. +* vc_mem_get_base
  7715. +*
  7716. +***************************************************************************/
  7717. +
  7718. +static void
  7719. +vc_mem_get_base(void)
  7720. +{
  7721. +}
  7722. +
  7723. +/****************************************************************************
  7724. +*
  7725. +* vc_mem_get_current_size
  7726. +*
  7727. +***************************************************************************/
  7728. +
  7729. +int
  7730. +vc_mem_get_current_size(void)
  7731. +{
  7732. + return mm_vc_mem_size;
  7733. +}
  7734. +
  7735. +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
  7736. +
  7737. +/****************************************************************************
  7738. +*
  7739. +* vc_mem_ioctl
  7740. +*
  7741. +***************************************************************************/
  7742. +
  7743. +static long
  7744. +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  7745. +{
  7746. + int rc = 0;
  7747. +
  7748. + (void) cmd;
  7749. + (void) arg;
  7750. +
  7751. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  7752. +
  7753. + switch (cmd) {
  7754. + case VC_MEM_IOC_MEM_PHYS_ADDR:
  7755. + {
  7756. + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
  7757. + __func__, (void *) mm_vc_mem_phys_addr);
  7758. +
  7759. + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
  7760. + sizeof (mm_vc_mem_phys_addr)) != 0) {
  7761. + rc = -EFAULT;
  7762. + }
  7763. + break;
  7764. + }
  7765. + case VC_MEM_IOC_MEM_SIZE:
  7766. + {
  7767. + // Get the videocore memory size first
  7768. + vc_mem_get_size();
  7769. +
  7770. + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
  7771. + mm_vc_mem_size);
  7772. +
  7773. + if (copy_to_user((void *) arg, &mm_vc_mem_size,
  7774. + sizeof (mm_vc_mem_size)) != 0) {
  7775. + rc = -EFAULT;
  7776. + }
  7777. + break;
  7778. + }
  7779. + case VC_MEM_IOC_MEM_BASE:
  7780. + {
  7781. + // Get the videocore memory base
  7782. + vc_mem_get_base();
  7783. +
  7784. + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
  7785. + mm_vc_mem_base);
  7786. +
  7787. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  7788. + sizeof (mm_vc_mem_base)) != 0) {
  7789. + rc = -EFAULT;
  7790. + }
  7791. + break;
  7792. + }
  7793. + case VC_MEM_IOC_MEM_LOAD:
  7794. + {
  7795. + // Get the videocore memory base
  7796. + vc_mem_get_base();
  7797. +
  7798. + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
  7799. + mm_vc_mem_base);
  7800. +
  7801. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  7802. + sizeof (mm_vc_mem_base)) != 0) {
  7803. + rc = -EFAULT;
  7804. + }
  7805. + break;
  7806. + }
  7807. + default:
  7808. + {
  7809. + return -ENOTTY;
  7810. + }
  7811. + }
  7812. + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
  7813. +
  7814. + return rc;
  7815. +}
  7816. +
  7817. +/****************************************************************************
  7818. +*
  7819. +* vc_mem_mmap
  7820. +*
  7821. +***************************************************************************/
  7822. +
  7823. +static int
  7824. +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
  7825. +{
  7826. + int rc = 0;
  7827. + unsigned long length = vma->vm_end - vma->vm_start;
  7828. + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  7829. +
  7830. + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
  7831. + __func__, (long) vma->vm_start, (long) vma->vm_end,
  7832. + (long) vma->vm_pgoff);
  7833. +
  7834. + if (offset + length > mm_vc_mem_size) {
  7835. + pr_err("%s: length %ld is too big\n", __func__, length);
  7836. + return -EINVAL;
  7837. + }
  7838. + // Do not cache the memory map
  7839. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  7840. +
  7841. + rc = remap_pfn_range(vma, vma->vm_start,
  7842. + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
  7843. + vma->vm_pgoff, length, vma->vm_page_prot);
  7844. + if (rc != 0) {
  7845. + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
  7846. + }
  7847. +
  7848. + return rc;
  7849. +}
  7850. +
  7851. +/****************************************************************************
  7852. +*
  7853. +* File Operations for the driver.
  7854. +*
  7855. +***************************************************************************/
  7856. +
  7857. +static const struct file_operations vc_mem_fops = {
  7858. + .owner = THIS_MODULE,
  7859. + .open = vc_mem_open,
  7860. + .release = vc_mem_release,
  7861. + .unlocked_ioctl = vc_mem_ioctl,
  7862. + .mmap = vc_mem_mmap,
  7863. +};
  7864. +
  7865. +#ifdef CONFIG_DEBUG_FS
  7866. +static void vc_mem_debugfs_deinit(void)
  7867. +{
  7868. + debugfs_remove_recursive(vc_mem_debugfs_entry);
  7869. + vc_mem_debugfs_entry = NULL;
  7870. +}
  7871. +
  7872. +
  7873. +static int vc_mem_debugfs_init(
  7874. + struct device *dev)
  7875. +{
  7876. + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
  7877. + if (!vc_mem_debugfs_entry) {
  7878. + dev_warn(dev, "could not create debugfs entry\n");
  7879. + return -EFAULT;
  7880. + }
  7881. +
  7882. + if (!debugfs_create_x32("vc_mem_phys_addr",
  7883. + 0444,
  7884. + vc_mem_debugfs_entry,
  7885. + (u32 *)&mm_vc_mem_phys_addr)) {
  7886. + dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
  7887. + __func__);
  7888. + goto fail;
  7889. + }
  7890. +
  7891. + if (!debugfs_create_x32("vc_mem_size",
  7892. + 0444,
  7893. + vc_mem_debugfs_entry,
  7894. + (u32 *)&mm_vc_mem_size)) {
  7895. + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
  7896. + __func__);
  7897. + goto fail;
  7898. + }
  7899. +
  7900. + if (!debugfs_create_x32("vc_mem_base",
  7901. + 0444,
  7902. + vc_mem_debugfs_entry,
  7903. + (u32 *)&mm_vc_mem_base)) {
  7904. + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
  7905. + __func__);
  7906. + goto fail;
  7907. + }
  7908. +
  7909. + return 0;
  7910. +
  7911. +fail:
  7912. + vc_mem_debugfs_deinit();
  7913. + return -EFAULT;
  7914. +}
  7915. +
  7916. +#endif /* CONFIG_DEBUG_FS */
  7917. +
  7918. +
  7919. +/****************************************************************************
  7920. +*
  7921. +* vc_mem_init
  7922. +*
  7923. +***************************************************************************/
  7924. +
  7925. +static int __init
  7926. +vc_mem_init(void)
  7927. +{
  7928. + int rc = -EFAULT;
  7929. + struct device *dev;
  7930. +
  7931. + pr_debug("%s: called\n", __func__);
  7932. +
  7933. + mm_vc_mem_phys_addr = phys_addr;
  7934. + mm_vc_mem_size = mem_size;
  7935. + mm_vc_mem_base = mem_base;
  7936. +
  7937. + vc_mem_get_size();
  7938. +
  7939. + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
  7940. + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
  7941. +
  7942. + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
  7943. + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
  7944. + __func__, rc);
  7945. + goto out_err;
  7946. + }
  7947. +
  7948. + cdev_init(&vc_mem_cdev, &vc_mem_fops);
  7949. + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
  7950. + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
  7951. + goto out_unregister;
  7952. + }
  7953. +
  7954. + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
  7955. + if (IS_ERR(vc_mem_class)) {
  7956. + rc = PTR_ERR(vc_mem_class);
  7957. + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
  7958. + goto out_cdev_del;
  7959. + }
  7960. +
  7961. + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
  7962. + DRIVER_NAME);
  7963. + if (IS_ERR(dev)) {
  7964. + rc = PTR_ERR(dev);
  7965. + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
  7966. + goto out_class_destroy;
  7967. + }
  7968. +
  7969. +#ifdef CONFIG_DEBUG_FS
  7970. + /* don't fail if the debug entries cannot be created */
  7971. + vc_mem_debugfs_init(dev);
  7972. +#endif
  7973. +
  7974. + vc_mem_inited = 1;
  7975. + return 0;
  7976. +
  7977. + device_destroy(vc_mem_class, vc_mem_devnum);
  7978. +
  7979. + out_class_destroy:
  7980. + class_destroy(vc_mem_class);
  7981. + vc_mem_class = NULL;
  7982. +
  7983. + out_cdev_del:
  7984. + cdev_del(&vc_mem_cdev);
  7985. +
  7986. + out_unregister:
  7987. + unregister_chrdev_region(vc_mem_devnum, 1);
  7988. +
  7989. + out_err:
  7990. + return -1;
  7991. +}
  7992. +
  7993. +/****************************************************************************
  7994. +*
  7995. +* vc_mem_exit
  7996. +*
  7997. +***************************************************************************/
  7998. +
  7999. +static void __exit
  8000. +vc_mem_exit(void)
  8001. +{
  8002. + pr_debug("%s: called\n", __func__);
  8003. +
  8004. + if (vc_mem_inited) {
  8005. +#if CONFIG_DEBUG_FS
  8006. + vc_mem_debugfs_deinit();
  8007. +#endif
  8008. + device_destroy(vc_mem_class, vc_mem_devnum);
  8009. + class_destroy(vc_mem_class);
  8010. + cdev_del(&vc_mem_cdev);
  8011. + unregister_chrdev_region(vc_mem_devnum, 1);
  8012. + }
  8013. +}
  8014. +
  8015. +module_init(vc_mem_init);
  8016. +module_exit(vc_mem_exit);
  8017. +MODULE_LICENSE("GPL");
  8018. +MODULE_AUTHOR("Broadcom Corporation");
  8019. +
  8020. +module_param(phys_addr, uint, 0644);
  8021. +module_param(mem_size, uint, 0644);
  8022. +module_param(mem_base, uint, 0644);
  8023. +
  8024. diff -Nur linux-3.10.37/arch/arm/mach-bcm2708/vc_support.c linux-rpi/arch/arm/mach-bcm2708/vc_support.c
  8025. --- linux-3.10.37/arch/arm/mach-bcm2708/vc_support.c 1970-01-01 01:00:00.000000000 +0100
  8026. +++ linux-rpi/arch/arm/mach-bcm2708/vc_support.c 2014-04-24 15:35:00.777527936 +0200
  8027. @@ -0,0 +1,319 @@
  8028. +/*
  8029. + * vc_support.c
  8030. + *
  8031. + * Created on: 25 Nov 2012
  8032. + * Author: Simon
  8033. + */
  8034. +
  8035. +#include <linux/module.h>
  8036. +#include <mach/vcio.h>
  8037. +
  8038. +#ifdef ECLIPSE_IGNORE
  8039. +
  8040. +#define __user
  8041. +#define __init
  8042. +#define __exit
  8043. +#define __iomem
  8044. +#define KERN_DEBUG
  8045. +#define KERN_ERR
  8046. +#define KERN_WARNING
  8047. +#define KERN_INFO
  8048. +#define _IOWR(a, b, c) b
  8049. +#define _IOW(a, b, c) b
  8050. +#define _IO(a, b) b
  8051. +
  8052. +#endif
  8053. +
  8054. +/****** VC MAILBOX FUNCTIONALITY ******/
  8055. +unsigned int AllocateVcMemory(unsigned int *pHandle, unsigned int size, unsigned int alignment, unsigned int flags)
  8056. +{
  8057. + struct vc_msg
  8058. + {
  8059. + unsigned int m_msgSize;
  8060. + unsigned int m_response;
  8061. +
  8062. + struct vc_tag
  8063. + {
  8064. + unsigned int m_tagId;
  8065. + unsigned int m_sendBufferSize;
  8066. + union {
  8067. + unsigned int m_sendDataSize;
  8068. + unsigned int m_recvDataSize;
  8069. + };
  8070. +
  8071. + struct args
  8072. + {
  8073. + union {
  8074. + unsigned int m_size;
  8075. + unsigned int m_handle;
  8076. + };
  8077. + unsigned int m_alignment;
  8078. + unsigned int m_flags;
  8079. + } m_args;
  8080. + } m_tag;
  8081. +
  8082. + unsigned int m_endTag;
  8083. + } msg;
  8084. + int s;
  8085. +
  8086. + msg.m_msgSize = sizeof(msg);
  8087. + msg.m_response = 0;
  8088. + msg.m_endTag = 0;
  8089. +
  8090. + //fill in the tag for the allocation command
  8091. + msg.m_tag.m_tagId = 0x3000c;
  8092. + msg.m_tag.m_sendBufferSize = 12;
  8093. + msg.m_tag.m_sendDataSize = 12;
  8094. +
  8095. + //fill in our args
  8096. + msg.m_tag.m_args.m_size = size;
  8097. + msg.m_tag.m_args.m_alignment = alignment;
  8098. + msg.m_tag.m_args.m_flags = flags;
  8099. +
  8100. + //run the command
  8101. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8102. +
  8103. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004)
  8104. + {
  8105. + *pHandle = msg.m_tag.m_args.m_handle;
  8106. + return 0;
  8107. + }
  8108. + else
  8109. + {
  8110. + printk(KERN_ERR "failed to allocate vc memory: s=%d response=%08x recv data size=%08x\n",
  8111. + s, msg.m_response, msg.m_tag.m_recvDataSize);
  8112. + return 1;
  8113. + }
  8114. +}
  8115. +
  8116. +unsigned int ReleaseVcMemory(unsigned int handle)
  8117. +{
  8118. + struct vc_msg
  8119. + {
  8120. + unsigned int m_msgSize;
  8121. + unsigned int m_response;
  8122. +
  8123. + struct vc_tag
  8124. + {
  8125. + unsigned int m_tagId;
  8126. + unsigned int m_sendBufferSize;
  8127. + union {
  8128. + unsigned int m_sendDataSize;
  8129. + unsigned int m_recvDataSize;
  8130. + };
  8131. +
  8132. + struct args
  8133. + {
  8134. + union {
  8135. + unsigned int m_handle;
  8136. + unsigned int m_error;
  8137. + };
  8138. + } m_args;
  8139. + } m_tag;
  8140. +
  8141. + unsigned int m_endTag;
  8142. + } msg;
  8143. + int s;
  8144. +
  8145. + msg.m_msgSize = sizeof(msg);
  8146. + msg.m_response = 0;
  8147. + msg.m_endTag = 0;
  8148. +
  8149. + //fill in the tag for the release command
  8150. + msg.m_tag.m_tagId = 0x3000f;
  8151. + msg.m_tag.m_sendBufferSize = 4;
  8152. + msg.m_tag.m_sendDataSize = 4;
  8153. +
  8154. + //pass across the handle
  8155. + msg.m_tag.m_args.m_handle = handle;
  8156. +
  8157. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8158. +
  8159. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004 && msg.m_tag.m_args.m_error == 0)
  8160. + return 0;
  8161. + else
  8162. + {
  8163. + printk(KERN_ERR "failed to release vc memory: s=%d response=%08x recv data size=%08x error=%08x\n",
  8164. + s, msg.m_response, msg.m_tag.m_recvDataSize, msg.m_tag.m_args.m_error);
  8165. + return 1;
  8166. + }
  8167. +}
  8168. +
  8169. +unsigned int LockVcMemory(unsigned int *pBusAddress, unsigned int handle)
  8170. +{
  8171. + struct vc_msg
  8172. + {
  8173. + unsigned int m_msgSize;
  8174. + unsigned int m_response;
  8175. +
  8176. + struct vc_tag
  8177. + {
  8178. + unsigned int m_tagId;
  8179. + unsigned int m_sendBufferSize;
  8180. + union {
  8181. + unsigned int m_sendDataSize;
  8182. + unsigned int m_recvDataSize;
  8183. + };
  8184. +
  8185. + struct args
  8186. + {
  8187. + union {
  8188. + unsigned int m_handle;
  8189. + unsigned int m_busAddress;
  8190. + };
  8191. + } m_args;
  8192. + } m_tag;
  8193. +
  8194. + unsigned int m_endTag;
  8195. + } msg;
  8196. + int s;
  8197. +
  8198. + msg.m_msgSize = sizeof(msg);
  8199. + msg.m_response = 0;
  8200. + msg.m_endTag = 0;
  8201. +
  8202. + //fill in the tag for the lock command
  8203. + msg.m_tag.m_tagId = 0x3000d;
  8204. + msg.m_tag.m_sendBufferSize = 4;
  8205. + msg.m_tag.m_sendDataSize = 4;
  8206. +
  8207. + //pass across the handle
  8208. + msg.m_tag.m_args.m_handle = handle;
  8209. +
  8210. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8211. +
  8212. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004)
  8213. + {
  8214. + //pick out the bus address
  8215. + *pBusAddress = msg.m_tag.m_args.m_busAddress;
  8216. + return 0;
  8217. + }
  8218. + else
  8219. + {
  8220. + printk(KERN_ERR "failed to lock vc memory: s=%d response=%08x recv data size=%08x\n",
  8221. + s, msg.m_response, msg.m_tag.m_recvDataSize);
  8222. + return 1;
  8223. + }
  8224. +}
  8225. +
  8226. +unsigned int UnlockVcMemory(unsigned int handle)
  8227. +{
  8228. + struct vc_msg
  8229. + {
  8230. + unsigned int m_msgSize;
  8231. + unsigned int m_response;
  8232. +
  8233. + struct vc_tag
  8234. + {
  8235. + unsigned int m_tagId;
  8236. + unsigned int m_sendBufferSize;
  8237. + union {
  8238. + unsigned int m_sendDataSize;
  8239. + unsigned int m_recvDataSize;
  8240. + };
  8241. +
  8242. + struct args
  8243. + {
  8244. + union {
  8245. + unsigned int m_handle;
  8246. + unsigned int m_error;
  8247. + };
  8248. + } m_args;
  8249. + } m_tag;
  8250. +
  8251. + unsigned int m_endTag;
  8252. + } msg;
  8253. + int s;
  8254. +
  8255. + msg.m_msgSize = sizeof(msg);
  8256. + msg.m_response = 0;
  8257. + msg.m_endTag = 0;
  8258. +
  8259. + //fill in the tag for the unlock command
  8260. + msg.m_tag.m_tagId = 0x3000e;
  8261. + msg.m_tag.m_sendBufferSize = 4;
  8262. + msg.m_tag.m_sendDataSize = 4;
  8263. +
  8264. + //pass across the handle
  8265. + msg.m_tag.m_args.m_handle = handle;
  8266. +
  8267. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8268. +
  8269. + //check the error code too
  8270. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004 && msg.m_tag.m_args.m_error == 0)
  8271. + return 0;
  8272. + else
  8273. + {
  8274. + printk(KERN_ERR "failed to unlock vc memory: s=%d response=%08x recv data size=%08x error%08x\n",
  8275. + s, msg.m_response, msg.m_tag.m_recvDataSize, msg.m_tag.m_args.m_error);
  8276. + return 1;
  8277. + }
  8278. +}
  8279. +
  8280. +unsigned int ExecuteVcCode(unsigned int code,
  8281. + unsigned int r0, unsigned int r1, unsigned int r2, unsigned int r3, unsigned int r4, unsigned int r5)
  8282. +{
  8283. + struct vc_msg
  8284. + {
  8285. + unsigned int m_msgSize;
  8286. + unsigned int m_response;
  8287. +
  8288. + struct vc_tag
  8289. + {
  8290. + unsigned int m_tagId;
  8291. + unsigned int m_sendBufferSize;
  8292. + union {
  8293. + unsigned int m_sendDataSize;
  8294. + unsigned int m_recvDataSize;
  8295. + };
  8296. +
  8297. + struct args
  8298. + {
  8299. + union {
  8300. + unsigned int m_pCode;
  8301. + unsigned int m_return;
  8302. + };
  8303. + unsigned int m_r0;
  8304. + unsigned int m_r1;
  8305. + unsigned int m_r2;
  8306. + unsigned int m_r3;
  8307. + unsigned int m_r4;
  8308. + unsigned int m_r5;
  8309. + } m_args;
  8310. + } m_tag;
  8311. +
  8312. + unsigned int m_endTag;
  8313. + } msg;
  8314. + int s;
  8315. +
  8316. + msg.m_msgSize = sizeof(msg);
  8317. + msg.m_response = 0;
  8318. + msg.m_endTag = 0;
  8319. +
  8320. + //fill in the tag for the unlock command
  8321. + msg.m_tag.m_tagId = 0x30010;
  8322. + msg.m_tag.m_sendBufferSize = 28;
  8323. + msg.m_tag.m_sendDataSize = 28;
  8324. +
  8325. + //pass across the handle
  8326. + msg.m_tag.m_args.m_pCode = code;
  8327. + msg.m_tag.m_args.m_r0 = r0;
  8328. + msg.m_tag.m_args.m_r1 = r1;
  8329. + msg.m_tag.m_args.m_r2 = r2;
  8330. + msg.m_tag.m_args.m_r3 = r3;
  8331. + msg.m_tag.m_args.m_r4 = r4;
  8332. + msg.m_tag.m_args.m_r5 = r5;
  8333. +
  8334. + s = bcm_mailbox_property(&msg, sizeof(msg));
  8335. +
  8336. + //check the error code too
  8337. + if (s == 0 && msg.m_response == 0x80000000 && msg.m_tag.m_recvDataSize == 0x80000004)
  8338. + return msg.m_tag.m_args.m_return;
  8339. + else
  8340. + {
  8341. + printk(KERN_ERR "failed to execute: s=%d response=%08x recv data size=%08x\n",
  8342. + s, msg.m_response, msg.m_tag.m_recvDataSize);
  8343. + return 1;
  8344. + }
  8345. +}
  8346. +
  8347. diff -Nur linux-3.10.37/arch/arm/Makefile linux-rpi/arch/arm/Makefile
  8348. --- linux-3.10.37/arch/arm/Makefile 2014-04-14 15:42:31.000000000 +0200
  8349. +++ linux-rpi/arch/arm/Makefile 2014-04-24 15:35:00.677526821 +0200
  8350. @@ -139,6 +139,7 @@
  8351. # by CONFIG_* macro name.
  8352. machine-$(CONFIG_ARCH_AT91) += at91
  8353. machine-$(CONFIG_ARCH_BCM) += bcm
  8354. +machine-$(CONFIG_ARCH_BCM2708) += bcm2708
  8355. machine-$(CONFIG_ARCH_BCM2835) += bcm2835
  8356. machine-$(CONFIG_ARCH_CLPS711X) += clps711x
  8357. machine-$(CONFIG_ARCH_CNS3XXX) += cns3xxx
  8358. diff -Nur linux-3.10.37/arch/arm/mm/Kconfig linux-rpi/arch/arm/mm/Kconfig
  8359. --- linux-3.10.37/arch/arm/mm/Kconfig 2014-04-14 15:42:31.000000000 +0200
  8360. +++ linux-rpi/arch/arm/mm/Kconfig 2014-04-24 15:35:00.965530031 +0200
  8361. @@ -358,7 +358,7 @@
  8362. # ARMv6
  8363. config CPU_V6
  8364. - bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX
  8365. + bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX || MACH_BCM2708
  8366. select CPU_32v6
  8367. select CPU_ABRT_EV6
  8368. select CPU_CACHE_V6
  8369. diff -Nur linux-3.10.37/arch/arm/mm/proc-v6.S linux-rpi/arch/arm/mm/proc-v6.S
  8370. --- linux-3.10.37/arch/arm/mm/proc-v6.S 2014-04-14 15:42:31.000000000 +0200
  8371. +++ linux-rpi/arch/arm/mm/proc-v6.S 2014-04-24 15:35:00.969530076 +0200
  8372. @@ -73,10 +73,19 @@
  8373. *
  8374. * IRQs are already disabled.
  8375. */
  8376. +
  8377. +/* See jira SW-5991 for details of this workaround */
  8378. ENTRY(cpu_v6_do_idle)
  8379. - mov r1, #0
  8380. - mcr p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  8381. - mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt
  8382. + .align 5
  8383. + mov r1, #2
  8384. +1: subs r1, #1
  8385. + nop
  8386. + mcreq p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  8387. + mcreq p15, 0, r1, c7, c0, 4 @ wait for interrupt
  8388. + nop
  8389. + nop
  8390. + nop
  8391. + bne 1b
  8392. mov pc, lr
  8393. ENTRY(cpu_v6_dcache_clean_area)
  8394. diff -Nur linux-3.10.37/arch/arm/tools/mach-types linux-rpi/arch/arm/tools/mach-types
  8395. --- linux-3.10.37/arch/arm/tools/mach-types 2014-04-14 15:42:31.000000000 +0200
  8396. +++ linux-rpi/arch/arm/tools/mach-types 2014-04-24 15:35:00.985530254 +0200
  8397. @@ -522,6 +522,7 @@
  8398. prima2_evb MACH_PRIMA2_EVB PRIMA2_EVB 3103
  8399. paz00 MACH_PAZ00 PAZ00 3128
  8400. acmenetusfoxg20 MACH_ACMENETUSFOXG20 ACMENETUSFOXG20 3129
  8401. +bcm2708 MACH_BCM2708 BCM2708 3138
  8402. ag5evm MACH_AG5EVM AG5EVM 3189
  8403. ics_if_voip MACH_ICS_IF_VOIP ICS_IF_VOIP 3206
  8404. wlf_cragg_6410 MACH_WLF_CRAGG_6410 WLF_CRAGG_6410 3207
  8405. diff -Nur linux-3.10.37/Documentation/video4linux/bcm2835-v4l2.txt linux-rpi/Documentation/video4linux/bcm2835-v4l2.txt
  8406. --- linux-3.10.37/Documentation/video4linux/bcm2835-v4l2.txt 1970-01-01 01:00:00.000000000 +0100
  8407. +++ linux-rpi/Documentation/video4linux/bcm2835-v4l2.txt 2014-04-24 15:35:00.565525573 +0200
  8408. @@ -0,0 +1,60 @@
  8409. +
  8410. +BCM2835 (aka Raspberry Pi) V4L2 driver
  8411. +======================================
  8412. +
  8413. +1. Copyright
  8414. +============
  8415. +
  8416. +Copyright © 2013 Raspberry Pi (Trading) Ltd.
  8417. +
  8418. +2. License
  8419. +==========
  8420. +
  8421. +This program is free software; you can redistribute it and/or modify
  8422. +it under the terms of the GNU General Public License as published by
  8423. +the Free Software Foundation; either version 2 of the License, or
  8424. +(at your option) any later version.
  8425. +
  8426. +This program is distributed in the hope that it will be useful,
  8427. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  8428. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8429. +GNU General Public License for more details.
  8430. +
  8431. +You should have received a copy of the GNU General Public License
  8432. +along with this program; if not, write to the Free Software
  8433. +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  8434. +
  8435. +3. Quick Start
  8436. +==============
  8437. +
  8438. +You need a version 1.0 or later of v4l2-ctl, available from:
  8439. + git://git.linuxtv.org/v4l-utils.git
  8440. +
  8441. +$ sudo modprobe bcm2835-v4l2
  8442. +
  8443. +Turn on the overlay:
  8444. +
  8445. +$ v4l2-ctl --overlay=1
  8446. +
  8447. +Turn off the overlay:
  8448. +
  8449. +$ v4l2-ctl --overlay=0
  8450. +
  8451. +Set the capture format for video:
  8452. +
  8453. +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
  8454. +
  8455. +(Note: 1088 not 1080).
  8456. +
  8457. +Capture:
  8458. +
  8459. +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
  8460. +
  8461. +Stills capture:
  8462. +
  8463. +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
  8464. +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
  8465. +
  8466. +List of available formats:
  8467. +
  8468. +$ v4l2-ctl --list-formats
  8469. diff -Nur linux-3.10.37/drivers/char/broadcom/Kconfig linux-rpi/drivers/char/broadcom/Kconfig
  8470. --- linux-3.10.37/drivers/char/broadcom/Kconfig 1970-01-01 01:00:00.000000000 +0100
  8471. +++ linux-rpi/drivers/char/broadcom/Kconfig 2014-04-24 15:35:02.101542691 +0200
  8472. @@ -0,0 +1,16 @@
  8473. +#
  8474. +# Broadcom char driver config
  8475. +#
  8476. +
  8477. +menuconfig BRCM_CHAR_DRIVERS
  8478. + bool "Broadcom Char Drivers"
  8479. + help
  8480. + Broadcom's char drivers
  8481. +
  8482. +config BCM_VC_CMA
  8483. + bool "Videocore CMA"
  8484. + depends on CMA && BRCM_CHAR_DRIVERS
  8485. + default n
  8486. + help
  8487. + Helper for videocore CMA access.
  8488. +
  8489. diff -Nur linux-3.10.37/drivers/char/broadcom/Makefile linux-rpi/drivers/char/broadcom/Makefile
  8490. --- linux-3.10.37/drivers/char/broadcom/Makefile 1970-01-01 01:00:00.000000000 +0100
  8491. +++ linux-rpi/drivers/char/broadcom/Makefile 2014-04-24 15:35:02.101542691 +0200
  8492. @@ -0,0 +1,2 @@
  8493. +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
  8494. +
  8495. diff -Nur linux-3.10.37/drivers/char/broadcom/vc_cma/Makefile linux-rpi/drivers/char/broadcom/vc_cma/Makefile
  8496. --- linux-3.10.37/drivers/char/broadcom/vc_cma/Makefile 1970-01-01 01:00:00.000000000 +0100
  8497. +++ linux-rpi/drivers/char/broadcom/vc_cma/Makefile 2014-04-24 15:35:02.101542691 +0200
  8498. @@ -0,0 +1,14 @@
  8499. +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
  8500. +ccflags-y += -Werror
  8501. +ccflags-y += -Idrivers/misc/vc04_services
  8502. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
  8503. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
  8504. +
  8505. +ccflags-y += -D__KERNEL__
  8506. +ccflags-y += -D__linux__
  8507. +ccflags-y += -Werror
  8508. +
  8509. +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
  8510. +
  8511. +vc-cma-objs := vc_cma.o
  8512. +
  8513. diff -Nur linux-3.10.37/drivers/char/broadcom/vc_cma/vc_cma.c linux-rpi/drivers/char/broadcom/vc_cma/vc_cma.c
  8514. --- linux-3.10.37/drivers/char/broadcom/vc_cma/vc_cma.c 1970-01-01 01:00:00.000000000 +0100
  8515. +++ linux-rpi/drivers/char/broadcom/vc_cma/vc_cma.c 2014-04-24 15:35:02.101542691 +0200
  8516. @@ -0,0 +1,1143 @@
  8517. +/**
  8518. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  8519. + *
  8520. + * Redistribution and use in source and binary forms, with or without
  8521. + * modification, are permitted provided that the following conditions
  8522. + * are met:
  8523. + * 1. Redistributions of source code must retain the above copyright
  8524. + * notice, this list of conditions, and the following disclaimer,
  8525. + * without modification.
  8526. + * 2. Redistributions in binary form must reproduce the above copyright
  8527. + * notice, this list of conditions and the following disclaimer in the
  8528. + * documentation and/or other materials provided with the distribution.
  8529. + * 3. The names of the above-listed copyright holders may not be used
  8530. + * to endorse or promote products derived from this software without
  8531. + * specific prior written permission.
  8532. + *
  8533. + * ALTERNATIVELY, this software may be distributed under the terms of the
  8534. + * GNU General Public License ("GPL") version 2, as published by the Free
  8535. + * Software Foundation.
  8536. + *
  8537. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  8538. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  8539. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  8540. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  8541. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  8542. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  8543. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  8544. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  8545. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  8546. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  8547. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8548. + */
  8549. +
  8550. +#include <linux/kernel.h>
  8551. +#include <linux/module.h>
  8552. +#include <linux/kthread.h>
  8553. +#include <linux/fs.h>
  8554. +#include <linux/device.h>
  8555. +#include <linux/cdev.h>
  8556. +#include <linux/mm.h>
  8557. +#include <linux/proc_fs.h>
  8558. +#include <linux/seq_file.h>
  8559. +#include <linux/dma-mapping.h>
  8560. +#include <linux/dma-contiguous.h>
  8561. +#include <linux/platform_device.h>
  8562. +#include <linux/uaccess.h>
  8563. +#include <asm/cacheflush.h>
  8564. +
  8565. +#include <linux/broadcom/vc_cma.h>
  8566. +
  8567. +#include "vchiq_util.h"
  8568. +#include "vchiq_connected.h"
  8569. +//#include "debug_sym.h"
  8570. +//#include "vc_mem.h"
  8571. +
  8572. +#define DRIVER_NAME "vc-cma"
  8573. +
  8574. +#define LOG_DBG(fmt, ...) \
  8575. + if (vc_cma_debug) \
  8576. + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
  8577. +#define LOG_ERR(fmt, ...) \
  8578. + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
  8579. +
  8580. +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
  8581. +#define VC_CMA_VERSION 2
  8582. +
  8583. +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
  8584. +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
  8585. +#define VC_CMA_MAX_PARAMS_PER_MSG \
  8586. + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
  8587. +#define VC_CMA_RESERVE_COUNT_MAX 16
  8588. +
  8589. +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
  8590. +
  8591. +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
  8592. +
  8593. +#define loud_error(...) \
  8594. + LOG_ERR("===== " __VA_ARGS__)
  8595. +
  8596. +enum {
  8597. + VC_CMA_MSG_QUIT,
  8598. + VC_CMA_MSG_OPEN,
  8599. + VC_CMA_MSG_TICK,
  8600. + VC_CMA_MSG_ALLOC, /* chunk count */
  8601. + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
  8602. + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
  8603. + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
  8604. + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
  8605. + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
  8606. + VC_CMA_MSG_UPDATE_RESERVE,
  8607. + VC_CMA_MSG_MAX
  8608. +};
  8609. +
  8610. +struct cma_msg {
  8611. + unsigned short type;
  8612. + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
  8613. +};
  8614. +
  8615. +struct vc_cma_reserve_user {
  8616. + unsigned int pid;
  8617. + unsigned int reserve;
  8618. +};
  8619. +
  8620. +/* Device (/dev) related variables */
  8621. +static dev_t vc_cma_devnum;
  8622. +static struct class *vc_cma_class;
  8623. +static struct cdev vc_cma_cdev;
  8624. +static int vc_cma_inited;
  8625. +static int vc_cma_debug;
  8626. +
  8627. +/* Proc entry */
  8628. +static struct proc_dir_entry *vc_cma_proc_entry;
  8629. +
  8630. +phys_addr_t vc_cma_base;
  8631. +struct page *vc_cma_base_page;
  8632. +unsigned int vc_cma_size;
  8633. +EXPORT_SYMBOL(vc_cma_size);
  8634. +unsigned int vc_cma_initial;
  8635. +unsigned int vc_cma_chunks;
  8636. +unsigned int vc_cma_chunks_used;
  8637. +unsigned int vc_cma_chunks_reserved;
  8638. +
  8639. +static int in_loud_error;
  8640. +
  8641. +unsigned int vc_cma_reserve_total;
  8642. +unsigned int vc_cma_reserve_count;
  8643. +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
  8644. +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
  8645. +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
  8646. +
  8647. +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
  8648. +static struct platform_device vc_cma_device = {
  8649. + .name = "vc-cma",
  8650. + .id = 0,
  8651. + .dev = {
  8652. + .dma_mask = &vc_cma_dma_mask,
  8653. + .coherent_dma_mask = DMA_BIT_MASK(32),
  8654. + },
  8655. +};
  8656. +
  8657. +static VCHIQ_INSTANCE_T cma_instance;
  8658. +static VCHIQ_SERVICE_HANDLE_T cma_service;
  8659. +static VCHIU_QUEUE_T cma_msg_queue;
  8660. +static struct task_struct *cma_worker;
  8661. +
  8662. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
  8663. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
  8664. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  8665. + VCHIQ_HEADER_T * header,
  8666. + VCHIQ_SERVICE_HANDLE_T service,
  8667. + void *bulk_userdata);
  8668. +static void send_vc_msg(unsigned short type,
  8669. + unsigned short param1, unsigned short param2);
  8670. +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
  8671. +
  8672. +static int early_vc_cma_mem(char *p)
  8673. +{
  8674. + unsigned int new_size;
  8675. + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
  8676. + vc_cma_size = memparse(p, &p);
  8677. + vc_cma_initial = vc_cma_size;
  8678. + if (*p == '/')
  8679. + vc_cma_size = memparse(p + 1, &p);
  8680. + if (*p == '@')
  8681. + vc_cma_base = memparse(p + 1, &p);
  8682. +
  8683. + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
  8684. + & ~(VC_CMA_CHUNK_SIZE - 1);
  8685. + if (new_size > vc_cma_size)
  8686. + vc_cma_size = 0;
  8687. + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
  8688. + & ~(VC_CMA_CHUNK_SIZE - 1);
  8689. + if (vc_cma_initial > vc_cma_size)
  8690. + vc_cma_initial = vc_cma_size;
  8691. + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
  8692. + & ~(VC_CMA_CHUNK_SIZE - 1);
  8693. +
  8694. + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
  8695. + vc_cma_size, (unsigned int)vc_cma_base);
  8696. +
  8697. + return 0;
  8698. +}
  8699. +
  8700. +early_param("vc-cma-mem", early_vc_cma_mem);
  8701. +
  8702. +void vc_cma_early_init(void)
  8703. +{
  8704. + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
  8705. + if (vc_cma_size) {
  8706. + int rc = platform_device_register(&vc_cma_device);
  8707. + LOG_DBG("platform_device_register -> %d", rc);
  8708. + }
  8709. +}
  8710. +
  8711. +void vc_cma_reserve(void)
  8712. +{
  8713. + /* if vc_cma_size is set, then declare vc CMA area of the same
  8714. + * size from the end of memory
  8715. + */
  8716. + if (vc_cma_size) {
  8717. + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
  8718. + vc_cma_base, 0) == 0) {
  8719. + } else {
  8720. + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
  8721. + vc_cma_size, (unsigned int)vc_cma_base);
  8722. + vc_cma_size = 0;
  8723. + }
  8724. + }
  8725. + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
  8726. +}
  8727. +
  8728. +/****************************************************************************
  8729. +*
  8730. +* vc_cma_open
  8731. +*
  8732. +***************************************************************************/
  8733. +
  8734. +static int vc_cma_open(struct inode *inode, struct file *file)
  8735. +{
  8736. + (void)inode;
  8737. + (void)file;
  8738. +
  8739. + return 0;
  8740. +}
  8741. +
  8742. +/****************************************************************************
  8743. +*
  8744. +* vc_cma_release
  8745. +*
  8746. +***************************************************************************/
  8747. +
  8748. +static int vc_cma_release(struct inode *inode, struct file *file)
  8749. +{
  8750. + (void)inode;
  8751. + (void)file;
  8752. +
  8753. + vc_cma_set_reserve(0, current->tgid);
  8754. +
  8755. + return 0;
  8756. +}
  8757. +
  8758. +/****************************************************************************
  8759. +*
  8760. +* vc_cma_ioctl
  8761. +*
  8762. +***************************************************************************/
  8763. +
  8764. +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  8765. +{
  8766. + int rc = 0;
  8767. +
  8768. + (void)cmd;
  8769. + (void)arg;
  8770. +
  8771. + switch (cmd) {
  8772. + case VC_CMA_IOC_RESERVE:
  8773. + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
  8774. + if (rc >= 0)
  8775. + rc = 0;
  8776. + break;
  8777. + default:
  8778. + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
  8779. + return -ENOTTY;
  8780. + }
  8781. +
  8782. + return rc;
  8783. +}
  8784. +
  8785. +/****************************************************************************
  8786. +*
  8787. +* File Operations for the driver.
  8788. +*
  8789. +***************************************************************************/
  8790. +
  8791. +static const struct file_operations vc_cma_fops = {
  8792. + .owner = THIS_MODULE,
  8793. + .open = vc_cma_open,
  8794. + .release = vc_cma_release,
  8795. + .unlocked_ioctl = vc_cma_ioctl,
  8796. +};
  8797. +
  8798. +/****************************************************************************
  8799. +*
  8800. +* vc_cma_proc_open
  8801. +*
  8802. +***************************************************************************/
  8803. +
  8804. +static int vc_cma_show_info(struct seq_file *m, void *v)
  8805. +{
  8806. + int i;
  8807. +
  8808. + seq_printf(m, "Videocore CMA:\n");
  8809. + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
  8810. + seq_printf(m, " Length : %08x\n", vc_cma_size);
  8811. + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
  8812. + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
  8813. + seq_printf(m, " Chunks : %4d (%d bytes)\n",
  8814. + (int)vc_cma_chunks,
  8815. + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
  8816. + seq_printf(m, " Used : %4d (%d bytes)\n",
  8817. + (int)vc_cma_chunks_used,
  8818. + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
  8819. + seq_printf(m, " Reserved : %4d (%d bytes)\n",
  8820. + (unsigned int)vc_cma_chunks_reserved,
  8821. + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
  8822. +
  8823. + for (i = 0; i < vc_cma_reserve_count; i++) {
  8824. + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
  8825. + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
  8826. + user->reserve);
  8827. + }
  8828. +
  8829. + seq_printf(m, "\n");
  8830. +
  8831. + return 0;
  8832. +}
  8833. +
  8834. +static int vc_cma_proc_open(struct inode *inode, struct file *file)
  8835. +{
  8836. + return single_open(file, vc_cma_show_info, NULL);
  8837. +}
  8838. +
  8839. +/****************************************************************************
  8840. +*
  8841. +* vc_cma_proc_write
  8842. +*
  8843. +***************************************************************************/
  8844. +
  8845. +static int vc_cma_proc_write(struct file *file,
  8846. + const char __user *buffer,
  8847. + size_t size, loff_t *ppos)
  8848. +{
  8849. + int rc = -EFAULT;
  8850. + char input_str[20];
  8851. +
  8852. + memset(input_str, 0, sizeof(input_str));
  8853. +
  8854. + if (size > sizeof(input_str)) {
  8855. + LOG_ERR("%s: input string length too long", __func__);
  8856. + goto out;
  8857. + }
  8858. +
  8859. + if (copy_from_user(input_str, buffer, size - 1)) {
  8860. + LOG_ERR("%s: failed to get input string", __func__);
  8861. + goto out;
  8862. + }
  8863. +#define ALLOC_STR "alloc"
  8864. +#define FREE_STR "free"
  8865. +#define DEBUG_STR "debug"
  8866. +#define RESERVE_STR "reserve"
  8867. + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
  8868. + int size;
  8869. + char *p = input_str + strlen(ALLOC_STR);
  8870. +
  8871. + while (*p == ' ')
  8872. + p++;
  8873. + size = memparse(p, NULL);
  8874. + LOG_ERR("/proc/vc-cma: alloc %d", size);
  8875. + if (size)
  8876. + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
  8877. + size / VC_CMA_CHUNK_SIZE, 0);
  8878. + else
  8879. + LOG_ERR("invalid size '%s'", p);
  8880. + rc = size;
  8881. + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
  8882. + int size;
  8883. + char *p = input_str + strlen(FREE_STR);
  8884. +
  8885. + while (*p == ' ')
  8886. + p++;
  8887. + size = memparse(p, NULL);
  8888. + LOG_ERR("/proc/vc-cma: free %d", size);
  8889. + if (size)
  8890. + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
  8891. + size / VC_CMA_CHUNK_SIZE, 0);
  8892. + else
  8893. + LOG_ERR("invalid size '%s'", p);
  8894. + rc = size;
  8895. + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
  8896. + char *p = input_str + strlen(DEBUG_STR);
  8897. + while (*p == ' ')
  8898. + p++;
  8899. + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
  8900. + vc_cma_debug = 1;
  8901. + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
  8902. + vc_cma_debug = 0;
  8903. + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
  8904. + rc = size;
  8905. + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
  8906. + int size;
  8907. + int reserved;
  8908. + char *p = input_str + strlen(RESERVE_STR);
  8909. + while (*p == ' ')
  8910. + p++;
  8911. + size = memparse(p, NULL);
  8912. +
  8913. + reserved = vc_cma_set_reserve(size, current->tgid);
  8914. + rc = (reserved >= 0) ? size : reserved;
  8915. + }
  8916. +
  8917. +out:
  8918. + return rc;
  8919. +}
  8920. +
  8921. +/****************************************************************************
  8922. +*
  8923. +* File Operations for /proc interface.
  8924. +*
  8925. +***************************************************************************/
  8926. +
  8927. +static const struct file_operations vc_cma_proc_fops = {
  8928. + .open = vc_cma_proc_open,
  8929. + .read = seq_read,
  8930. + .write = vc_cma_proc_write,
  8931. + .llseek = seq_lseek,
  8932. + .release = single_release
  8933. +};
  8934. +
  8935. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
  8936. +{
  8937. + struct vc_cma_reserve_user *user = NULL;
  8938. + int delta = 0;
  8939. + int i;
  8940. +
  8941. + if (down_interruptible(&vc_cma_reserve_mutex))
  8942. + return -ERESTARTSYS;
  8943. +
  8944. + for (i = 0; i < vc_cma_reserve_count; i++) {
  8945. + if (pid == vc_cma_reserve_users[i].pid) {
  8946. + user = &vc_cma_reserve_users[i];
  8947. + delta = reserve - user->reserve;
  8948. + if (reserve)
  8949. + user->reserve = reserve;
  8950. + else {
  8951. + /* Remove this entry by copying downwards */
  8952. + while ((i + 1) < vc_cma_reserve_count) {
  8953. + user[0].pid = user[1].pid;
  8954. + user[0].reserve = user[1].reserve;
  8955. + user++;
  8956. + i++;
  8957. + }
  8958. + vc_cma_reserve_count--;
  8959. + user = NULL;
  8960. + }
  8961. + break;
  8962. + }
  8963. + }
  8964. +
  8965. + if (reserve && !user) {
  8966. + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
  8967. + LOG_ERR("vc-cma: Too many reservations - "
  8968. + "increase CMA_RESERVE_COUNT_MAX");
  8969. + up(&vc_cma_reserve_mutex);
  8970. + return -EBUSY;
  8971. + }
  8972. + user = &vc_cma_reserve_users[vc_cma_reserve_count];
  8973. + user->pid = pid;
  8974. + user->reserve = reserve;
  8975. + delta = reserve;
  8976. + vc_cma_reserve_count++;
  8977. + }
  8978. +
  8979. + vc_cma_reserve_total += delta;
  8980. +
  8981. + send_vc_msg(VC_CMA_MSG_RESERVE,
  8982. + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
  8983. +
  8984. + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
  8985. +
  8986. + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
  8987. + reserve, pid, vc_cma_reserve_total);
  8988. +
  8989. + up(&vc_cma_reserve_mutex);
  8990. +
  8991. + return vc_cma_reserve_total;
  8992. +}
  8993. +
  8994. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  8995. + VCHIQ_HEADER_T * header,
  8996. + VCHIQ_SERVICE_HANDLE_T service,
  8997. + void *bulk_userdata)
  8998. +{
  8999. + switch (reason) {
  9000. + case VCHIQ_MESSAGE_AVAILABLE:
  9001. + if (!send_worker_msg(header))
  9002. + return VCHIQ_RETRY;
  9003. + break;
  9004. + case VCHIQ_SERVICE_CLOSED:
  9005. + LOG_DBG("CMA service closed");
  9006. + break;
  9007. + default:
  9008. + LOG_ERR("Unexpected CMA callback reason %d", reason);
  9009. + break;
  9010. + }
  9011. + return VCHIQ_SUCCESS;
  9012. +}
  9013. +
  9014. +static void send_vc_msg(unsigned short type,
  9015. + unsigned short param1, unsigned short param2)
  9016. +{
  9017. + unsigned short msg[] = { type, param1, param2 };
  9018. + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
  9019. + VCHIQ_STATUS_T ret;
  9020. + vchiq_use_service(cma_service);
  9021. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9022. + vchiq_release_service(cma_service);
  9023. + if (ret != VCHIQ_SUCCESS)
  9024. + LOG_ERR("vchiq_queue_message returned %x", ret);
  9025. +}
  9026. +
  9027. +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
  9028. +{
  9029. + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
  9030. + return false;
  9031. + vchiu_queue_push(&cma_msg_queue, msg);
  9032. + up(&vc_cma_worker_queue_push_mutex);
  9033. + return true;
  9034. +}
  9035. +
  9036. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
  9037. +{
  9038. + int i;
  9039. + for (i = 0; i < num_chunks; i++) {
  9040. + struct page *chunk;
  9041. + unsigned int chunk_num;
  9042. + uint8_t *chunk_addr;
  9043. + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
  9044. +
  9045. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  9046. + PAGES_PER_CHUNK,
  9047. + VC_CMA_CHUNK_ORDER);
  9048. + if (!chunk)
  9049. + break;
  9050. +
  9051. + chunk_addr = page_address(chunk);
  9052. + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
  9053. + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
  9054. + chunk_size);
  9055. +
  9056. + chunk_num =
  9057. + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
  9058. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  9059. + VC_CMA_CHUNK_SIZE) != 0);
  9060. + if (chunk_num >= vc_cma_chunks) {
  9061. + LOG_ERR("%s: ===============================",
  9062. + __func__);
  9063. + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
  9064. + "bad SPARSEMEM configuration?",
  9065. + __func__, (unsigned int)page_to_phys(chunk),
  9066. + vc_cma_base, vc_cma_base + vc_cma_size - 1);
  9067. + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
  9068. + vc_cma_device.dev.cma_area);
  9069. + LOG_ERR("%s: ===============================",
  9070. + __func__);
  9071. + break;
  9072. + }
  9073. + reply->params[i] = chunk_num;
  9074. + vc_cma_chunks_used++;
  9075. + }
  9076. +
  9077. + if (i < num_chunks) {
  9078. + LOG_ERR("%s: dma_alloc_from_contiguous failed "
  9079. + "for %x bytes (alloc %d of %d, %d free)",
  9080. + __func__, VC_CMA_CHUNK_SIZE, i,
  9081. + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
  9082. + num_chunks = i;
  9083. + }
  9084. +
  9085. + LOG_DBG("CMA allocated %d chunks -> %d used",
  9086. + num_chunks, vc_cma_chunks_used);
  9087. + reply->type = VC_CMA_MSG_ALLOCATED;
  9088. +
  9089. + {
  9090. + VCHIQ_ELEMENT_T elem = {
  9091. + reply,
  9092. + offsetof(struct cma_msg, params[0]) +
  9093. + num_chunks * sizeof(reply->params[0])
  9094. + };
  9095. + VCHIQ_STATUS_T ret;
  9096. + vchiq_use_service(cma_service);
  9097. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9098. + vchiq_release_service(cma_service);
  9099. + if (ret != VCHIQ_SUCCESS)
  9100. + LOG_ERR("vchiq_queue_message return " "%x", ret);
  9101. + }
  9102. +
  9103. + return num_chunks;
  9104. +}
  9105. +
  9106. +static int cma_worker_proc(void *param)
  9107. +{
  9108. + static struct cma_msg reply;
  9109. + (void)param;
  9110. +
  9111. + while (1) {
  9112. + VCHIQ_HEADER_T *msg;
  9113. + static struct cma_msg msg_copy;
  9114. + struct cma_msg *cma_msg = &msg_copy;
  9115. + int type, msg_size;
  9116. +
  9117. + msg = vchiu_queue_pop(&cma_msg_queue);
  9118. + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
  9119. + msg_size = msg->size;
  9120. + memcpy(&msg_copy, msg->data, msg_size);
  9121. + type = cma_msg->type;
  9122. + vchiq_release_message(cma_service, msg);
  9123. + } else {
  9124. + msg_size = 0;
  9125. + type = (int)msg;
  9126. + if (type == VC_CMA_MSG_QUIT)
  9127. + break;
  9128. + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
  9129. + msg = NULL;
  9130. + cma_msg = NULL;
  9131. + } else {
  9132. + BUG();
  9133. + continue;
  9134. + }
  9135. + }
  9136. +
  9137. + switch (type) {
  9138. + case VC_CMA_MSG_ALLOC:{
  9139. + int num_chunks, free_chunks;
  9140. + num_chunks = cma_msg->params[0];
  9141. + free_chunks =
  9142. + vc_cma_chunks - vc_cma_chunks_used;
  9143. + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
  9144. + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
  9145. + LOG_ERR
  9146. + ("CMA_MSG_ALLOC - chunk count (%d) "
  9147. + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
  9148. + num_chunks,
  9149. + VC_CMA_MAX_PARAMS_PER_MSG);
  9150. + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
  9151. + }
  9152. +
  9153. + if (num_chunks > free_chunks) {
  9154. + LOG_ERR
  9155. + ("CMA_MSG_ALLOC - chunk count (%d) "
  9156. + "exceeds free chunks (%d)",
  9157. + num_chunks, free_chunks);
  9158. + num_chunks = free_chunks;
  9159. + }
  9160. +
  9161. + vc_cma_alloc_chunks(num_chunks, &reply);
  9162. + }
  9163. + break;
  9164. +
  9165. + case VC_CMA_MSG_FREE:{
  9166. + int chunk_count =
  9167. + (msg_size -
  9168. + offsetof(struct cma_msg,
  9169. + params)) /
  9170. + sizeof(cma_msg->params[0]);
  9171. + int i;
  9172. + BUG_ON(chunk_count <= 0);
  9173. +
  9174. + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
  9175. + chunk_count, cma_msg->params[0]);
  9176. + for (i = 0; i < chunk_count; i++) {
  9177. + int chunk_num = cma_msg->params[i];
  9178. + struct page *page = vc_cma_base_page +
  9179. + chunk_num * PAGES_PER_CHUNK;
  9180. + if (chunk_num >= vc_cma_chunks) {
  9181. + LOG_ERR
  9182. + ("CMA_MSG_FREE - chunk %d of %d"
  9183. + " (value %x) exceeds maximum "
  9184. + "(%x)", i, chunk_count,
  9185. + chunk_num,
  9186. + vc_cma_chunks - 1);
  9187. + break;
  9188. + }
  9189. +
  9190. + if (!dma_release_from_contiguous
  9191. + (NULL /*&vc_cma_device.dev*/, page,
  9192. + PAGES_PER_CHUNK)) {
  9193. + LOG_ERR
  9194. + ("CMA_MSG_FREE - failed to "
  9195. + "release chunk %d (phys %x, "
  9196. + "page %x)", chunk_num,
  9197. + page_to_phys(page),
  9198. + (unsigned int)page);
  9199. + }
  9200. + vc_cma_chunks_used--;
  9201. + }
  9202. + LOG_DBG("CMA released %d chunks -> %d used",
  9203. + i, vc_cma_chunks_used);
  9204. + }
  9205. + break;
  9206. +
  9207. + case VC_CMA_MSG_UPDATE_RESERVE:{
  9208. + int chunks_needed =
  9209. + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
  9210. + 1)
  9211. + / VC_CMA_CHUNK_SIZE) -
  9212. + vc_cma_chunks_reserved;
  9213. +
  9214. + LOG_DBG
  9215. + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
  9216. + chunks_needed);
  9217. +
  9218. + /* Cap the reservations to what is available */
  9219. + if (chunks_needed > 0) {
  9220. + if (chunks_needed >
  9221. + (vc_cma_chunks -
  9222. + vc_cma_chunks_used))
  9223. + chunks_needed =
  9224. + (vc_cma_chunks -
  9225. + vc_cma_chunks_used);
  9226. +
  9227. + chunks_needed =
  9228. + vc_cma_alloc_chunks(chunks_needed,
  9229. + &reply);
  9230. + }
  9231. +
  9232. + LOG_DBG
  9233. + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
  9234. + chunks_needed);
  9235. + vc_cma_chunks_reserved += chunks_needed;
  9236. + }
  9237. + break;
  9238. +
  9239. + default:
  9240. + LOG_ERR("unexpected msg type %d", type);
  9241. + break;
  9242. + }
  9243. + }
  9244. +
  9245. + LOG_DBG("quitting...");
  9246. + return 0;
  9247. +}
  9248. +
  9249. +/****************************************************************************
  9250. +*
  9251. +* vc_cma_connected_init
  9252. +*
  9253. +* This function is called once the videocore has been connected.
  9254. +*
  9255. +***************************************************************************/
  9256. +
  9257. +static void vc_cma_connected_init(void)
  9258. +{
  9259. + VCHIQ_SERVICE_PARAMS_T service_params;
  9260. +
  9261. + LOG_DBG("vc_cma_connected_init");
  9262. +
  9263. + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
  9264. + LOG_ERR("could not create CMA msg queue");
  9265. + goto fail_queue;
  9266. + }
  9267. +
  9268. + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
  9269. + goto fail_vchiq_init;
  9270. +
  9271. + vchiq_connect(cma_instance);
  9272. +
  9273. + service_params.fourcc = VC_CMA_FOURCC;
  9274. + service_params.callback = cma_service_callback;
  9275. + service_params.userdata = NULL;
  9276. + service_params.version = VC_CMA_VERSION;
  9277. + service_params.version_min = VC_CMA_VERSION;
  9278. +
  9279. + if (vchiq_open_service(cma_instance, &service_params,
  9280. + &cma_service) != VCHIQ_SUCCESS) {
  9281. + LOG_ERR("failed to open service - already in use?");
  9282. + goto fail_vchiq_open;
  9283. + }
  9284. +
  9285. + vchiq_release_service(cma_service);
  9286. +
  9287. + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
  9288. + if (!cma_worker) {
  9289. + LOG_ERR("could not create CMA worker thread");
  9290. + goto fail_worker;
  9291. + }
  9292. + set_user_nice(cma_worker, -20);
  9293. + wake_up_process(cma_worker);
  9294. +
  9295. + return;
  9296. +
  9297. +fail_worker:
  9298. + vchiq_close_service(cma_service);
  9299. +fail_vchiq_open:
  9300. + vchiq_shutdown(cma_instance);
  9301. +fail_vchiq_init:
  9302. + vchiu_queue_delete(&cma_msg_queue);
  9303. +fail_queue:
  9304. + return;
  9305. +}
  9306. +
  9307. +void
  9308. +loud_error_header(void)
  9309. +{
  9310. + if (in_loud_error)
  9311. + return;
  9312. +
  9313. + LOG_ERR("============================================================"
  9314. + "================");
  9315. + LOG_ERR("============================================================"
  9316. + "================");
  9317. + LOG_ERR("=====");
  9318. +
  9319. + in_loud_error = 1;
  9320. +}
  9321. +
  9322. +void
  9323. +loud_error_footer(void)
  9324. +{
  9325. + if (!in_loud_error)
  9326. + return;
  9327. +
  9328. + LOG_ERR("=====");
  9329. + LOG_ERR("============================================================"
  9330. + "================");
  9331. + LOG_ERR("============================================================"
  9332. + "================");
  9333. +
  9334. + in_loud_error = 0;
  9335. +}
  9336. +
  9337. +#if 1
  9338. +static int check_cma_config(void) { return 1; }
  9339. +#else
  9340. +static int
  9341. +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
  9342. + const char *symbol,
  9343. + void *buf, size_t bufsize)
  9344. +{
  9345. + VC_MEM_ADDR_T vcMemAddr;
  9346. + size_t vcMemSize;
  9347. + uint8_t *mapAddr;
  9348. + off_t vcMapAddr;
  9349. +
  9350. + if (!LookupVideoCoreSymbol(handle, symbol,
  9351. + &vcMemAddr,
  9352. + &vcMemSize)) {
  9353. + loud_error_header();
  9354. + loud_error(
  9355. + "failed to find VC symbol \"%s\".",
  9356. + symbol);
  9357. + loud_error_footer();
  9358. + return 0;
  9359. + }
  9360. +
  9361. + if (vcMemSize != bufsize) {
  9362. + loud_error_header();
  9363. + loud_error(
  9364. + "VC symbol \"%s\" is the wrong size.",
  9365. + symbol);
  9366. + loud_error_footer();
  9367. + return 0;
  9368. + }
  9369. +
  9370. + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
  9371. + vcMapAddr += mm_vc_mem_phys_addr;
  9372. + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
  9373. + if (mapAddr == 0) {
  9374. + loud_error_header();
  9375. + loud_error(
  9376. + "failed to ioremap \"%s\" @ 0x%x "
  9377. + "(phys: 0x%x, size: %u).",
  9378. + symbol,
  9379. + (unsigned int)vcMapAddr,
  9380. + (unsigned int)vcMemAddr,
  9381. + (unsigned int)vcMemSize);
  9382. + loud_error_footer();
  9383. + return 0;
  9384. + }
  9385. +
  9386. + memcpy(buf, mapAddr, bufsize);
  9387. + iounmap(mapAddr);
  9388. +
  9389. + return 1;
  9390. +}
  9391. +
  9392. +
  9393. +static int
  9394. +check_cma_config(void)
  9395. +{
  9396. + VC_MEM_ACCESS_HANDLE_T mem_hndl;
  9397. + VC_MEM_ADDR_T mempool_start;
  9398. + VC_MEM_ADDR_T mempool_end;
  9399. + VC_MEM_ADDR_T mempool_offline_start;
  9400. + VC_MEM_ADDR_T mempool_offline_end;
  9401. + VC_MEM_ADDR_T cam_alloc_base;
  9402. + VC_MEM_ADDR_T cam_alloc_size;
  9403. + VC_MEM_ADDR_T cam_alloc_end;
  9404. + int success = 0;
  9405. +
  9406. + if (OpenVideoCoreMemory(&mem_hndl) != 0)
  9407. + goto out;
  9408. +
  9409. + /* Read the relevant VideoCore variables */
  9410. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
  9411. + &mempool_start,
  9412. + sizeof(mempool_start)))
  9413. + goto close;
  9414. +
  9415. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
  9416. + &mempool_end,
  9417. + sizeof(mempool_end)))
  9418. + goto close;
  9419. +
  9420. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
  9421. + &mempool_offline_start,
  9422. + sizeof(mempool_offline_start)))
  9423. + goto close;
  9424. +
  9425. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
  9426. + &mempool_offline_end,
  9427. + sizeof(mempool_offline_end)))
  9428. + goto close;
  9429. +
  9430. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
  9431. + &cam_alloc_base,
  9432. + sizeof(cam_alloc_base)))
  9433. + goto close;
  9434. +
  9435. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
  9436. + &cam_alloc_size,
  9437. + sizeof(cam_alloc_size)))
  9438. + goto close;
  9439. +
  9440. + cam_alloc_end = cam_alloc_base + cam_alloc_size;
  9441. +
  9442. + success = 1;
  9443. +
  9444. + /* Now the sanity checks */
  9445. + if (!mempool_offline_start)
  9446. + mempool_offline_start = mempool_start;
  9447. + if (!mempool_offline_end)
  9448. + mempool_offline_end = mempool_end;
  9449. +
  9450. + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
  9451. + loud_error_header();
  9452. + loud_error(
  9453. + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
  9454. + "vc_cma_base(%x)",
  9455. + mempool_offline_start,
  9456. + VCADDR_TO_PHYSADDR(mempool_offline_start),
  9457. + vc_cma_base);
  9458. + success = 0;
  9459. + }
  9460. +
  9461. + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
  9462. + (vc_cma_base + vc_cma_size)) {
  9463. + loud_error_header();
  9464. + loud_error(
  9465. + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
  9466. + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
  9467. + mempool_offline_start,
  9468. + VCADDR_TO_PHYSADDR(mempool_offline_end),
  9469. + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
  9470. + success = 0;
  9471. + }
  9472. +
  9473. + if (mempool_end < mempool_start) {
  9474. + loud_error_header();
  9475. + loud_error(
  9476. + "__MEMPOOL_END(%x) must not be before "
  9477. + "__MEMPOOL_START(%x)",
  9478. + mempool_end,
  9479. + mempool_start);
  9480. + success = 0;
  9481. + }
  9482. +
  9483. + if (mempool_offline_end < mempool_offline_start) {
  9484. + loud_error_header();
  9485. + loud_error(
  9486. + "__MEMPOOL_OFFLINE_END(%x) must not be before "
  9487. + "__MEMPOOL_OFFLINE_START(%x)",
  9488. + mempool_offline_end,
  9489. + mempool_offline_start);
  9490. + success = 0;
  9491. + }
  9492. +
  9493. + if (mempool_offline_start < mempool_start) {
  9494. + loud_error_header();
  9495. + loud_error(
  9496. + "__MEMPOOL_OFFLINE_START(%x) must not be before "
  9497. + "__MEMPOOL_START(%x)",
  9498. + mempool_offline_start,
  9499. + mempool_start);
  9500. + success = 0;
  9501. + }
  9502. +
  9503. + if (mempool_offline_end > mempool_end) {
  9504. + loud_error_header();
  9505. + loud_error(
  9506. + "__MEMPOOL_OFFLINE_END(%x) must not be after "
  9507. + "__MEMPOOL_END(%x)",
  9508. + mempool_offline_end,
  9509. + mempool_end);
  9510. + success = 0;
  9511. + }
  9512. +
  9513. + if ((cam_alloc_base < mempool_end) &&
  9514. + (cam_alloc_end > mempool_start)) {
  9515. + loud_error_header();
  9516. + loud_error(
  9517. + "cam_alloc pool(%x-%x) overlaps "
  9518. + "mempool(%x-%x)",
  9519. + cam_alloc_base, cam_alloc_end,
  9520. + mempool_start, mempool_end);
  9521. + success = 0;
  9522. + }
  9523. +
  9524. + loud_error_footer();
  9525. +
  9526. +close:
  9527. + CloseVideoCoreMemory(mem_hndl);
  9528. +
  9529. +out:
  9530. + return success;
  9531. +}
  9532. +#endif
  9533. +
  9534. +static int vc_cma_init(void)
  9535. +{
  9536. + int rc = -EFAULT;
  9537. + struct device *dev;
  9538. +
  9539. + if (!check_cma_config())
  9540. + goto out_release;
  9541. +
  9542. + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
  9543. + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
  9544. + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
  9545. + vc_cma_size, vc_cma_size / (1024 * 1024));
  9546. + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
  9547. + vc_cma_initial, vc_cma_initial / (1024 * 1024));
  9548. +
  9549. + vc_cma_base_page = phys_to_page(vc_cma_base);
  9550. +
  9551. + if (vc_cma_chunks) {
  9552. + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
  9553. +
  9554. + for (vc_cma_chunks_used = 0;
  9555. + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
  9556. + struct page *chunk;
  9557. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  9558. + PAGES_PER_CHUNK,
  9559. + VC_CMA_CHUNK_ORDER);
  9560. + if (!chunk)
  9561. + break;
  9562. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  9563. + VC_CMA_CHUNK_SIZE) != 0);
  9564. + }
  9565. + if (vc_cma_chunks_used != chunks_needed) {
  9566. + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
  9567. + "bytes, allocation %d of %d)",
  9568. + __func__, VC_CMA_CHUNK_SIZE,
  9569. + vc_cma_chunks_used, chunks_needed);
  9570. + goto out_release;
  9571. + }
  9572. +
  9573. + vchiq_add_connected_callback(vc_cma_connected_init);
  9574. + }
  9575. +
  9576. + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
  9577. + if (rc < 0) {
  9578. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  9579. + goto out_release;
  9580. + }
  9581. +
  9582. + cdev_init(&vc_cma_cdev, &vc_cma_fops);
  9583. + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
  9584. + if (rc != 0) {
  9585. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  9586. + goto out_unregister;
  9587. + }
  9588. +
  9589. + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
  9590. + if (IS_ERR(vc_cma_class)) {
  9591. + rc = PTR_ERR(vc_cma_class);
  9592. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  9593. + goto out_cdev_del;
  9594. + }
  9595. +
  9596. + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
  9597. + DRIVER_NAME);
  9598. + if (IS_ERR(dev)) {
  9599. + rc = PTR_ERR(dev);
  9600. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  9601. + goto out_class_destroy;
  9602. + }
  9603. +
  9604. + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
  9605. + if (vc_cma_proc_entry == NULL) {
  9606. + rc = -EFAULT;
  9607. + LOG_ERR("%s: proc_create failed", __func__);
  9608. + goto out_device_destroy;
  9609. + }
  9610. +
  9611. + vc_cma_inited = 1;
  9612. + return 0;
  9613. +
  9614. +out_device_destroy:
  9615. + device_destroy(vc_cma_class, vc_cma_devnum);
  9616. +
  9617. +out_class_destroy:
  9618. + class_destroy(vc_cma_class);
  9619. + vc_cma_class = NULL;
  9620. +
  9621. +out_cdev_del:
  9622. + cdev_del(&vc_cma_cdev);
  9623. +
  9624. +out_unregister:
  9625. + unregister_chrdev_region(vc_cma_devnum, 1);
  9626. +
  9627. +out_release:
  9628. + /* It is tempting to try to clean up by calling
  9629. + dma_release_from_contiguous for all allocated chunks, but it isn't
  9630. + a very safe thing to do. If vc_cma_initial is non-zero it is because
  9631. + VideoCore is already using that memory, so giving it back to Linux
  9632. + is likely to be fatal.
  9633. + */
  9634. + return -1;
  9635. +}
  9636. +
  9637. +/****************************************************************************
  9638. +*
  9639. +* vc_cma_exit
  9640. +*
  9641. +***************************************************************************/
  9642. +
  9643. +static void __exit vc_cma_exit(void)
  9644. +{
  9645. + LOG_DBG("%s: called", __func__);
  9646. +
  9647. + if (vc_cma_inited) {
  9648. + remove_proc_entry(DRIVER_NAME, NULL);
  9649. + device_destroy(vc_cma_class, vc_cma_devnum);
  9650. + class_destroy(vc_cma_class);
  9651. + cdev_del(&vc_cma_cdev);
  9652. + unregister_chrdev_region(vc_cma_devnum, 1);
  9653. + }
  9654. +}
  9655. +
  9656. +module_init(vc_cma_init);
  9657. +module_exit(vc_cma_exit);
  9658. +MODULE_LICENSE("GPL");
  9659. +MODULE_AUTHOR("Broadcom Corporation");
  9660. diff -Nur linux-3.10.37/drivers/char/hw_random/bcm2708-rng.c linux-rpi/drivers/char/hw_random/bcm2708-rng.c
  9661. --- linux-3.10.37/drivers/char/hw_random/bcm2708-rng.c 1970-01-01 01:00:00.000000000 +0100
  9662. +++ linux-rpi/drivers/char/hw_random/bcm2708-rng.c 2014-04-24 15:35:02.101542691 +0200
  9663. @@ -0,0 +1,117 @@
  9664. +/**
  9665. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9666. + *
  9667. + * Redistribution and use in source and binary forms, with or without
  9668. + * modification, are permitted provided that the following conditions
  9669. + * are met:
  9670. + * 1. Redistributions of source code must retain the above copyright
  9671. + * notice, this list of conditions, and the following disclaimer,
  9672. + * without modification.
  9673. + * 2. Redistributions in binary form must reproduce the above copyright
  9674. + * notice, this list of conditions and the following disclaimer in the
  9675. + * documentation and/or other materials provided with the distribution.
  9676. + * 3. The names of the above-listed copyright holders may not be used
  9677. + * to endorse or promote products derived from this software without
  9678. + * specific prior written permission.
  9679. + *
  9680. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9681. + * GNU General Public License ("GPL") version 2, as published by the Free
  9682. + * Software Foundation.
  9683. + *
  9684. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9685. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9686. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9687. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9688. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9689. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9690. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9691. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9692. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9693. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9694. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9695. + */
  9696. +
  9697. +#include <linux/kernel.h>
  9698. +#include <linux/module.h>
  9699. +#include <linux/init.h>
  9700. +#include <linux/hw_random.h>
  9701. +#include <linux/printk.h>
  9702. +
  9703. +#include <asm/io.h>
  9704. +#include <mach/hardware.h>
  9705. +#include <mach/platform.h>
  9706. +
  9707. +#define RNG_CTRL (0x0)
  9708. +#define RNG_STATUS (0x4)
  9709. +#define RNG_DATA (0x8)
  9710. +#define RNG_FF_THRESHOLD (0xc)
  9711. +
  9712. +/* enable rng */
  9713. +#define RNG_RBGEN 0x1
  9714. +/* double speed, less random mode */
  9715. +#define RNG_RBG2X 0x2
  9716. +
  9717. +/* the initial numbers generated are "less random" so will be discarded */
  9718. +#define RNG_WARMUP_COUNT 0x40000
  9719. +
  9720. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  9721. +{
  9722. + void __iomem *rng_base = (void __iomem *)rng->priv;
  9723. + unsigned words;
  9724. + /* wait for a random number to be in fifo */
  9725. + do {
  9726. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  9727. + }
  9728. + while (words == 0);
  9729. + /* read the random number */
  9730. + *buffer = __raw_readl(rng_base + RNG_DATA);
  9731. + return 4;
  9732. +}
  9733. +
  9734. +static struct hwrng bcm2708_rng_ops = {
  9735. + .name = "bcm2708",
  9736. + .data_read = bcm2708_rng_data_read,
  9737. +};
  9738. +
  9739. +static int __init bcm2708_rng_init(void)
  9740. +{
  9741. + void __iomem *rng_base;
  9742. + int err;
  9743. +
  9744. + /* map peripheral */
  9745. + rng_base = ioremap(RNG_BASE, 0x10);
  9746. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  9747. + if (!rng_base) {
  9748. + pr_err("bcm2708_rng_init failed to ioremap\n");
  9749. + return -ENOMEM;
  9750. + }
  9751. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  9752. + /* register driver */
  9753. + err = hwrng_register(&bcm2708_rng_ops);
  9754. + if (err) {
  9755. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  9756. + iounmap(rng_base);
  9757. + } else {
  9758. + /* set warm-up count & enable */
  9759. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  9760. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  9761. + }
  9762. + return err;
  9763. +}
  9764. +
  9765. +static void __exit bcm2708_rng_exit(void)
  9766. +{
  9767. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  9768. + pr_info("bcm2708_rng_exit\n");
  9769. + /* disable rng hardware */
  9770. + __raw_writel(0, rng_base + RNG_CTRL);
  9771. + /* unregister driver */
  9772. + hwrng_unregister(&bcm2708_rng_ops);
  9773. + iounmap(rng_base);
  9774. +}
  9775. +
  9776. +module_init(bcm2708_rng_init);
  9777. +module_exit(bcm2708_rng_exit);
  9778. +
  9779. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  9780. +MODULE_LICENSE("GPL and additional rights");
  9781. diff -Nur linux-3.10.37/drivers/char/hw_random/Kconfig linux-rpi/drivers/char/hw_random/Kconfig
  9782. --- linux-3.10.37/drivers/char/hw_random/Kconfig 2014-04-14 15:42:31.000000000 +0200
  9783. +++ linux-rpi/drivers/char/hw_random/Kconfig 2014-04-24 15:35:02.101542691 +0200
  9784. @@ -314,3 +314,15 @@
  9785. module will be called tpm-rng.
  9786. If unsure, say Y.
  9787. +
  9788. +config HW_RANDOM_BCM2708
  9789. + tristate "BCM2708 generic true random number generator support"
  9790. + depends on HW_RANDOM && ARCH_BCM2708
  9791. + ---help---
  9792. + This driver provides the kernel-side support for the BCM2708 hardware.
  9793. +
  9794. + To compile this driver as a module, choose M here: the
  9795. + module will be called bcm2708-rng.
  9796. +
  9797. + If unsure, say N.
  9798. +
  9799. diff -Nur linux-3.10.37/drivers/char/hw_random/Makefile linux-rpi/drivers/char/hw_random/Makefile
  9800. --- linux-3.10.37/drivers/char/hw_random/Makefile 2014-04-14 15:42:31.000000000 +0200
  9801. +++ linux-rpi/drivers/char/hw_random/Makefile 2014-04-24 15:35:02.101542691 +0200
  9802. @@ -27,3 +27,4 @@
  9803. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  9804. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  9805. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  9806. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  9807. diff -Nur linux-3.10.37/drivers/char/Kconfig linux-rpi/drivers/char/Kconfig
  9808. --- linux-3.10.37/drivers/char/Kconfig 2014-04-14 15:42:31.000000000 +0200
  9809. +++ linux-rpi/drivers/char/Kconfig 2014-04-24 15:35:02.093542602 +0200
  9810. @@ -586,6 +586,8 @@
  9811. source "drivers/s390/char/Kconfig"
  9812. +source "drivers/char/broadcom/Kconfig"
  9813. +
  9814. config MSM_SMD_PKT
  9815. bool "Enable device interface for some SMD packet ports"
  9816. default n
  9817. diff -Nur linux-3.10.37/drivers/char/Makefile linux-rpi/drivers/char/Makefile
  9818. --- linux-3.10.37/drivers/char/Makefile 2014-04-14 15:42:31.000000000 +0200
  9819. +++ linux-rpi/drivers/char/Makefile 2014-04-24 15:35:02.093542602 +0200
  9820. @@ -62,3 +62,6 @@
  9821. js-rtc-y = rtc.o
  9822. obj-$(CONFIG_TILE_SROM) += tile-srom.o
  9823. +
  9824. +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  9825. +
  9826. diff -Nur linux-3.10.37/drivers/cpufreq/bcm2835-cpufreq.c linux-rpi/drivers/cpufreq/bcm2835-cpufreq.c
  9827. --- linux-3.10.37/drivers/cpufreq/bcm2835-cpufreq.c 1970-01-01 01:00:00.000000000 +0100
  9828. +++ linux-rpi/drivers/cpufreq/bcm2835-cpufreq.c 2014-04-24 15:35:02.129543003 +0200
  9829. @@ -0,0 +1,239 @@
  9830. +/*****************************************************************************
  9831. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  9832. +*
  9833. +* Unless you and Broadcom execute a separate written software license
  9834. +* agreement governing use of this software, this software is licensed to you
  9835. +* under the terms of the GNU General Public License version 2, available at
  9836. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  9837. +*
  9838. +* Notwithstanding the above, under no circumstances may you combine this
  9839. +* software in any way with any other Broadcom software provided under a
  9840. +* license other than the GPL, without Broadcom's express prior written
  9841. +* consent.
  9842. +*****************************************************************************/
  9843. +
  9844. +/*****************************************************************************
  9845. +* FILENAME: bcm2835-cpufreq.h
  9846. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  9847. +* processor. Messages are sent to Videocore either setting or requesting the
  9848. +* frequency of the ARM in order to match an appropiate frequency to the current
  9849. +* usage of the processor. The policy which selects the frequency to use is
  9850. +* defined in the kernel .config file, but can be changed during runtime.
  9851. +*****************************************************************************/
  9852. +
  9853. +/* ---------- INCLUDES ---------- */
  9854. +#include <linux/kernel.h>
  9855. +#include <linux/init.h>
  9856. +#include <linux/module.h>
  9857. +#include <linux/cpufreq.h>
  9858. +#include <mach/vcio.h>
  9859. +
  9860. +/* ---------- DEFINES ---------- */
  9861. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  9862. +#define MODULE_NAME "bcm2835-cpufreq"
  9863. +
  9864. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  9865. +
  9866. +/* debug printk macros */
  9867. +#ifdef CPUFREQ_DEBUG_ENABLE
  9868. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  9869. +#else
  9870. +#define print_debug(fmt,...)
  9871. +#endif
  9872. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  9873. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  9874. +
  9875. +/* tag part of the message */
  9876. +struct vc_msg_tag {
  9877. + uint32_t tag_id; /* the message id */
  9878. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  9879. + uint32_t data_size; /* amount of data being sent or received */
  9880. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  9881. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  9882. +};
  9883. +
  9884. +/* message structure to be sent to videocore */
  9885. +struct vc_msg {
  9886. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  9887. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  9888. + struct vc_msg_tag tag; /* the tag structure above to make */
  9889. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  9890. +};
  9891. +
  9892. +/* ---------- GLOBALS ---------- */
  9893. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  9894. +
  9895. +/*
  9896. + ===============================================
  9897. + clk_rate either gets or sets the clock rates.
  9898. + ===============================================
  9899. +*/
  9900. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  9901. +{
  9902. + int s, actual_rate=0;
  9903. + struct vc_msg msg;
  9904. +
  9905. + /* wipe all previous message data */
  9906. + memset(&msg, 0, sizeof msg);
  9907. +
  9908. + msg.msg_size = sizeof msg;
  9909. +
  9910. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  9911. + msg.tag.buffer_size = 8;
  9912. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  9913. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  9914. + msg.tag.val = arm_rate * 1000;
  9915. +
  9916. + /* send the message */
  9917. + s = bcm_mailbox_property(&msg, sizeof msg);
  9918. +
  9919. + /* check if it was all ok and return the rate in KHz */
  9920. + if (s == 0 && (msg.request_code & 0x80000000))
  9921. + actual_rate = msg.tag.val/1000;
  9922. +
  9923. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  9924. + return actual_rate;
  9925. +}
  9926. +
  9927. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  9928. +{
  9929. + int s;
  9930. + int arm_rate = 0;
  9931. + struct vc_msg msg;
  9932. +
  9933. + /* wipe all previous message data */
  9934. + memset(&msg, 0, sizeof msg);
  9935. +
  9936. + msg.msg_size = sizeof msg;
  9937. + msg.tag.tag_id = tag;
  9938. + msg.tag.buffer_size = 8;
  9939. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  9940. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  9941. +
  9942. + /* send the message */
  9943. + s = bcm_mailbox_property(&msg, sizeof msg);
  9944. +
  9945. + /* check if it was all ok and return the rate in KHz */
  9946. + if (s == 0 && (msg.request_code & 0x80000000))
  9947. + arm_rate = msg.tag.val/1000;
  9948. +
  9949. + print_debug("%s frequency = %d\n",
  9950. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  9951. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  9952. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  9953. + "Unexpected", arm_rate);
  9954. +
  9955. + return arm_rate;
  9956. +}
  9957. +
  9958. +/*
  9959. + ====================================================
  9960. + Module Initialisation registers the cpufreq driver
  9961. + ====================================================
  9962. +*/
  9963. +static int __init bcm2835_cpufreq_module_init(void)
  9964. +{
  9965. + print_debug("IN\n");
  9966. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  9967. +}
  9968. +
  9969. +/*
  9970. + =============
  9971. + Module exit
  9972. + =============
  9973. +*/
  9974. +static void __exit bcm2835_cpufreq_module_exit(void)
  9975. +{
  9976. + print_debug("IN\n");
  9977. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  9978. + return;
  9979. +}
  9980. +
  9981. +/*
  9982. + ==============================================================
  9983. + Initialisation function sets up the CPU policy for first use
  9984. + ==============================================================
  9985. +*/
  9986. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  9987. +{
  9988. + /* measured value of how long it takes to change frequency */
  9989. + policy->cpuinfo.transition_latency = 355000; /* ns */
  9990. +
  9991. + /* now find out what the maximum and minimum frequencies are */
  9992. + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  9993. + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  9994. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  9995. +
  9996. + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
  9997. + return 0;
  9998. +}
  9999. +
  10000. +/*
  10001. + =================================================================================
  10002. + Target function chooses the most appropriate frequency from the table to enable
  10003. + =================================================================================
  10004. +*/
  10005. +
  10006. +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
  10007. +{
  10008. + unsigned int target = target_freq;
  10009. + unsigned int cur = policy->cur;
  10010. + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
  10011. +
  10012. + /* if we are above min and using ondemand, then just use max */
  10013. + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
  10014. + target = policy->max;
  10015. + /* if the frequency is the same, just quit */
  10016. + if (target == policy->cur)
  10017. + return 0;
  10018. +
  10019. + /* otherwise were good to set the clock frequency */
  10020. + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
  10021. +
  10022. + if (!policy->cur)
  10023. + {
  10024. + print_err("Error occurred setting a new frequency (%d)!\n", target);
  10025. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  10026. + return -EINVAL;
  10027. + }
  10028. + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
  10029. + return 0;
  10030. +}
  10031. +
  10032. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  10033. +{
  10034. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  10035. + print_debug("cpu=%d\n", actual_rate);
  10036. + return actual_rate;
  10037. +}
  10038. +
  10039. +/*
  10040. + =================================================================================
  10041. + Verify ensures that when a policy is changed, it is suitable for the CPU to use
  10042. + =================================================================================
  10043. +*/
  10044. +
  10045. +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
  10046. +{
  10047. + print_info("switching to governor %s\n", policy->governor->name);
  10048. + return 0;
  10049. +}
  10050. +
  10051. +
  10052. +/* the CPUFreq driver */
  10053. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  10054. + .name = "BCM2835 CPUFreq",
  10055. + .owner = THIS_MODULE,
  10056. + .init = bcm2835_cpufreq_driver_init,
  10057. + .verify = bcm2835_cpufreq_driver_verify,
  10058. + .target = bcm2835_cpufreq_driver_target,
  10059. + .get = bcm2835_cpufreq_driver_get
  10060. +};
  10061. +
  10062. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  10063. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  10064. +MODULE_LICENSE("GPL");
  10065. +
  10066. +module_init(bcm2835_cpufreq_module_init);
  10067. +module_exit(bcm2835_cpufreq_module_exit);
  10068. +
  10069. diff -Nur linux-3.10.37/drivers/cpufreq/Kconfig.arm linux-rpi/drivers/cpufreq/Kconfig.arm
  10070. --- linux-3.10.37/drivers/cpufreq/Kconfig.arm 2014-04-14 15:42:31.000000000 +0200
  10071. +++ linux-rpi/drivers/cpufreq/Kconfig.arm 2014-04-24 15:35:02.129543003 +0200
  10072. @@ -150,3 +150,11 @@
  10073. default y
  10074. help
  10075. This adds the CPUFreq driver support for SPEAr SOCs.
  10076. +
  10077. +config ARM_BCM2835_CPUFREQ
  10078. + bool "BCM2835 Driver"
  10079. + default y
  10080. + help
  10081. + This adds the CPUFreq driver for BCM2835
  10082. +
  10083. + If in doubt, say N.
  10084. diff -Nur linux-3.10.37/drivers/cpufreq/Makefile linux-rpi/drivers/cpufreq/Makefile
  10085. --- linux-3.10.37/drivers/cpufreq/Makefile 2014-04-14 15:42:31.000000000 +0200
  10086. +++ linux-rpi/drivers/cpufreq/Makefile 2014-04-24 15:35:02.129543003 +0200
  10087. @@ -72,6 +72,7 @@
  10088. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  10089. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  10090. obj-$(CONFIG_ARCH_TEGRA) += tegra-cpufreq.o
  10091. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  10092. ##################################################################################
  10093. # PowerPC platform drivers
  10094. diff -Nur linux-3.10.37/drivers/dma/bcm2708-dmaengine.c linux-rpi/drivers/dma/bcm2708-dmaengine.c
  10095. --- linux-3.10.37/drivers/dma/bcm2708-dmaengine.c 1970-01-01 01:00:00.000000000 +0100
  10096. +++ linux-rpi/drivers/dma/bcm2708-dmaengine.c 2014-04-24 15:35:02.153543270 +0200
  10097. @@ -0,0 +1,588 @@
  10098. +/*
  10099. + * BCM2708 DMA engine support
  10100. + *
  10101. + * This driver only supports cyclic DMA transfers
  10102. + * as needed for the I2S module.
  10103. + *
  10104. + * Author: Florian Meier <florian.meier@koalo.de>
  10105. + * Copyright 2013
  10106. + *
  10107. + * Based on
  10108. + * OMAP DMAengine support by Russell King
  10109. + *
  10110. + * BCM2708 DMA Driver
  10111. + * Copyright (C) 2010 Broadcom
  10112. + *
  10113. + * Raspberry Pi PCM I2S ALSA Driver
  10114. + * Copyright (c) by Phil Poole 2013
  10115. + *
  10116. + * MARVELL MMP Peripheral DMA Driver
  10117. + * Copyright 2012 Marvell International Ltd.
  10118. + *
  10119. + * This program is free software; you can redistribute it and/or modify
  10120. + * it under the terms of the GNU General Public License as published by
  10121. + * the Free Software Foundation; either version 2 of the License, or
  10122. + * (at your option) any later version.
  10123. + *
  10124. + * This program is distributed in the hope that it will be useful,
  10125. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10126. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10127. + * GNU General Public License for more details.
  10128. + */
  10129. +#include <linux/dmaengine.h>
  10130. +#include <linux/dma-mapping.h>
  10131. +#include <linux/err.h>
  10132. +#include <linux/init.h>
  10133. +#include <linux/interrupt.h>
  10134. +#include <linux/list.h>
  10135. +#include <linux/module.h>
  10136. +#include <linux/platform_device.h>
  10137. +#include <linux/slab.h>
  10138. +#include <linux/io.h>
  10139. +#include <linux/spinlock.h>
  10140. +#include <linux/irq.h>
  10141. +
  10142. +#include "virt-dma.h"
  10143. +
  10144. +#include <mach/dma.h>
  10145. +#include <mach/irqs.h>
  10146. +
  10147. +struct bcm2708_dmadev {
  10148. + struct dma_device ddev;
  10149. + spinlock_t lock;
  10150. + void __iomem *base;
  10151. + struct device_dma_parameters dma_parms;
  10152. +};
  10153. +
  10154. +struct bcm2708_chan {
  10155. + struct virt_dma_chan vc;
  10156. + struct list_head node;
  10157. +
  10158. + struct dma_slave_config cfg;
  10159. + bool cyclic;
  10160. +
  10161. + int ch;
  10162. + struct bcm2708_desc *desc;
  10163. +
  10164. + void __iomem *chan_base;
  10165. + int irq_number;
  10166. +};
  10167. +
  10168. +struct bcm2708_desc {
  10169. + struct virt_dma_desc vd;
  10170. + enum dma_transfer_direction dir;
  10171. +
  10172. + unsigned int control_block_size;
  10173. + struct bcm2708_dma_cb *control_block_base;
  10174. + dma_addr_t control_block_base_phys;
  10175. +
  10176. + unsigned frames;
  10177. + size_t size;
  10178. +};
  10179. +
  10180. +#define BCM2708_DMA_DATA_TYPE_S8 1
  10181. +#define BCM2708_DMA_DATA_TYPE_S16 2
  10182. +#define BCM2708_DMA_DATA_TYPE_S32 4
  10183. +#define BCM2708_DMA_DATA_TYPE_S128 16
  10184. +
  10185. +static inline struct bcm2708_dmadev *to_bcm2708_dma_dev(struct dma_device *d)
  10186. +{
  10187. + return container_of(d, struct bcm2708_dmadev, ddev);
  10188. +}
  10189. +
  10190. +static inline struct bcm2708_chan *to_bcm2708_dma_chan(struct dma_chan *c)
  10191. +{
  10192. + return container_of(c, struct bcm2708_chan, vc.chan);
  10193. +}
  10194. +
  10195. +static inline struct bcm2708_desc *to_bcm2708_dma_desc(
  10196. + struct dma_async_tx_descriptor *t)
  10197. +{
  10198. + return container_of(t, struct bcm2708_desc, vd.tx);
  10199. +}
  10200. +
  10201. +static void bcm2708_dma_desc_free(struct virt_dma_desc *vd)
  10202. +{
  10203. + struct bcm2708_desc *desc = container_of(vd, struct bcm2708_desc, vd);
  10204. + dma_free_coherent(desc->vd.tx.chan->device->dev,
  10205. + desc->control_block_size,
  10206. + desc->control_block_base,
  10207. + desc->control_block_base_phys);
  10208. + kfree(desc);
  10209. +}
  10210. +
  10211. +static void bcm2708_dma_start_desc(struct bcm2708_chan *c)
  10212. +{
  10213. + struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  10214. + struct bcm2708_desc *d;
  10215. +
  10216. + if (!vd) {
  10217. + c->desc = NULL;
  10218. + return;
  10219. + }
  10220. +
  10221. + list_del(&vd->node);
  10222. +
  10223. + c->desc = d = to_bcm2708_dma_desc(&vd->tx);
  10224. +
  10225. + bcm_dma_start(c->chan_base, d->control_block_base_phys);
  10226. +}
  10227. +
  10228. +static irqreturn_t bcm2708_dma_callback(int irq, void *data)
  10229. +{
  10230. + struct bcm2708_chan *c = data;
  10231. + struct bcm2708_desc *d;
  10232. + unsigned long flags;
  10233. +
  10234. + spin_lock_irqsave(&c->vc.lock, flags);
  10235. +
  10236. + /* Acknowledge interrupt */
  10237. + writel(BCM2708_DMA_INT, c->chan_base + BCM2708_DMA_CS);
  10238. +
  10239. + d = c->desc;
  10240. +
  10241. + if (d) {
  10242. + /* TODO Only works for cyclic DMA */
  10243. + vchan_cyclic_callback(&d->vd);
  10244. + }
  10245. +
  10246. + /* Keep the DMA engine running */
  10247. + dsb(); /* ARM synchronization barrier */
  10248. + writel(BCM2708_DMA_ACTIVE, c->chan_base + BCM2708_DMA_CS);
  10249. +
  10250. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10251. +
  10252. + return IRQ_HANDLED;
  10253. +}
  10254. +
  10255. +static int bcm2708_dma_alloc_chan_resources(struct dma_chan *chan)
  10256. +{
  10257. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10258. +
  10259. + return request_irq(c->irq_number,
  10260. + bcm2708_dma_callback, 0, "DMA IRQ", c);
  10261. +}
  10262. +
  10263. +static void bcm2708_dma_free_chan_resources(struct dma_chan *chan)
  10264. +{
  10265. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10266. +
  10267. + vchan_free_chan_resources(&c->vc);
  10268. + free_irq(c->irq_number, c);
  10269. +
  10270. + dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  10271. +}
  10272. +
  10273. +static size_t bcm2708_dma_desc_size(struct bcm2708_desc *d)
  10274. +{
  10275. + return d->size;
  10276. +}
  10277. +
  10278. +static size_t bcm2708_dma_desc_size_pos(struct bcm2708_desc *d, dma_addr_t addr)
  10279. +{
  10280. + unsigned i;
  10281. + size_t size;
  10282. +
  10283. + for (size = i = 0; i < d->frames; i++) {
  10284. + struct bcm2708_dma_cb *control_block =
  10285. + &d->control_block_base[i];
  10286. + size_t this_size = control_block->length;
  10287. + dma_addr_t dma;
  10288. +
  10289. + if (d->dir == DMA_DEV_TO_MEM)
  10290. + dma = control_block->dst;
  10291. + else
  10292. + dma = control_block->src;
  10293. +
  10294. + if (size)
  10295. + size += this_size;
  10296. + else if (addr >= dma && addr < dma + this_size)
  10297. + size += dma + this_size - addr;
  10298. + }
  10299. +
  10300. + return size;
  10301. +}
  10302. +
  10303. +static enum dma_status bcm2708_dma_tx_status(struct dma_chan *chan,
  10304. + dma_cookie_t cookie, struct dma_tx_state *txstate)
  10305. +{
  10306. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10307. + struct virt_dma_desc *vd;
  10308. + enum dma_status ret;
  10309. + unsigned long flags;
  10310. +
  10311. + ret = dma_cookie_status(chan, cookie, txstate);
  10312. + if (ret == DMA_SUCCESS || !txstate)
  10313. + return ret;
  10314. +
  10315. + spin_lock_irqsave(&c->vc.lock, flags);
  10316. + vd = vchan_find_desc(&c->vc, cookie);
  10317. + if (vd) {
  10318. + txstate->residue =
  10319. + bcm2708_dma_desc_size(to_bcm2708_dma_desc(&vd->tx));
  10320. + } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  10321. + struct bcm2708_desc *d = c->desc;
  10322. + dma_addr_t pos;
  10323. +
  10324. + if (d->dir == DMA_MEM_TO_DEV)
  10325. + pos = readl(c->chan_base + BCM2708_DMA_SOURCE_AD);
  10326. + else if (d->dir == DMA_DEV_TO_MEM)
  10327. + pos = readl(c->chan_base + BCM2708_DMA_DEST_AD);
  10328. + else
  10329. + pos = 0;
  10330. +
  10331. + txstate->residue = bcm2708_dma_desc_size_pos(d, pos);
  10332. + } else {
  10333. + txstate->residue = 0;
  10334. + }
  10335. +
  10336. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10337. +
  10338. + return ret;
  10339. +}
  10340. +
  10341. +static void bcm2708_dma_issue_pending(struct dma_chan *chan)
  10342. +{
  10343. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10344. + unsigned long flags;
  10345. +
  10346. + c->cyclic = true; /* Nothing else is implemented */
  10347. +
  10348. + spin_lock_irqsave(&c->vc.lock, flags);
  10349. + if (vchan_issue_pending(&c->vc) && !c->desc)
  10350. + bcm2708_dma_start_desc(c);
  10351. +
  10352. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10353. +}
  10354. +
  10355. +static struct dma_async_tx_descriptor *bcm2708_dma_prep_dma_cyclic(
  10356. + struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  10357. + size_t period_len, enum dma_transfer_direction direction,
  10358. + unsigned long flags, void *context)
  10359. +{
  10360. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10361. + enum dma_slave_buswidth dev_width;
  10362. + struct bcm2708_desc *d;
  10363. + dma_addr_t dev_addr;
  10364. + unsigned es, sync_type;
  10365. + unsigned frame;
  10366. +
  10367. + /* Grab configuration */
  10368. + if (direction == DMA_DEV_TO_MEM) {
  10369. + dev_addr = c->cfg.src_addr;
  10370. + dev_width = c->cfg.src_addr_width;
  10371. + sync_type = BCM2708_DMA_S_DREQ;
  10372. + } else if (direction == DMA_MEM_TO_DEV) {
  10373. + dev_addr = c->cfg.dst_addr;
  10374. + dev_width = c->cfg.dst_addr_width;
  10375. + sync_type = BCM2708_DMA_D_DREQ;
  10376. + } else {
  10377. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  10378. + return NULL;
  10379. + }
  10380. +
  10381. + /* Bus width translates to the element size (ES) */
  10382. + switch (dev_width) {
  10383. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  10384. + es = BCM2708_DMA_DATA_TYPE_S32;
  10385. + break;
  10386. + default:
  10387. + return NULL;
  10388. + }
  10389. +
  10390. + /* Now allocate and setup the descriptor. */
  10391. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  10392. + if (!d)
  10393. + return NULL;
  10394. +
  10395. + d->dir = direction;
  10396. + d->frames = buf_len / period_len;
  10397. +
  10398. + /* Allocate memory for control blocks */
  10399. + d->control_block_size = d->frames * sizeof(struct bcm2708_dma_cb);
  10400. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  10401. + d->control_block_size, &d->control_block_base_phys,
  10402. + GFP_NOWAIT);
  10403. +
  10404. + if (!d->control_block_base) {
  10405. + kfree(d);
  10406. + return NULL;
  10407. + }
  10408. +
  10409. + /*
  10410. + * Iterate over all frames, create a control block
  10411. + * for each frame and link them together.
  10412. + */
  10413. + for (frame = 0; frame < d->frames; frame++) {
  10414. + struct bcm2708_dma_cb *control_block =
  10415. + &d->control_block_base[frame];
  10416. +
  10417. + /* Setup adresses */
  10418. + if (d->dir == DMA_DEV_TO_MEM) {
  10419. + control_block->info = BCM2708_DMA_D_INC;
  10420. + control_block->src = dev_addr;
  10421. + control_block->dst = buf_addr + frame * period_len;
  10422. + } else {
  10423. + control_block->info = BCM2708_DMA_S_INC;
  10424. + control_block->src = buf_addr + frame * period_len;
  10425. + control_block->dst = dev_addr;
  10426. + }
  10427. +
  10428. + /* Enable interrupt */
  10429. + control_block->info |= BCM2708_DMA_INT_EN;
  10430. +
  10431. + /* Setup synchronization */
  10432. + if (sync_type != 0)
  10433. + control_block->info |= sync_type;
  10434. +
  10435. + /* Setup DREQ channel */
  10436. + if (c->cfg.slave_id != 0)
  10437. + control_block->info |=
  10438. + BCM2708_DMA_PER_MAP(c->cfg.slave_id);
  10439. +
  10440. + /* Length of a frame */
  10441. + control_block->length = period_len;
  10442. + d->size += control_block->length;
  10443. +
  10444. + /*
  10445. + * Next block is the next frame.
  10446. + * This DMA engine driver currently only supports cyclic DMA.
  10447. + * Therefore, wrap around at number of frames.
  10448. + */
  10449. + control_block->next = d->control_block_base_phys +
  10450. + sizeof(struct bcm2708_dma_cb)
  10451. + * ((frame + 1) % d->frames);
  10452. + }
  10453. +
  10454. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  10455. +}
  10456. +
  10457. +static int bcm2708_dma_slave_config(struct bcm2708_chan *c,
  10458. + struct dma_slave_config *cfg)
  10459. +{
  10460. + if ((cfg->direction == DMA_DEV_TO_MEM &&
  10461. + cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  10462. + (cfg->direction == DMA_MEM_TO_DEV &&
  10463. + cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  10464. + !is_slave_direction(cfg->direction)) {
  10465. + return -EINVAL;
  10466. + }
  10467. +
  10468. + c->cfg = *cfg;
  10469. +
  10470. + return 0;
  10471. +}
  10472. +
  10473. +static int bcm2708_dma_terminate_all(struct bcm2708_chan *c)
  10474. +{
  10475. + struct bcm2708_dmadev *d = to_bcm2708_dma_dev(c->vc.chan.device);
  10476. + unsigned long flags;
  10477. + int timeout = 10000;
  10478. + LIST_HEAD(head);
  10479. +
  10480. + spin_lock_irqsave(&c->vc.lock, flags);
  10481. +
  10482. + /* Prevent this channel being scheduled */
  10483. + spin_lock(&d->lock);
  10484. + list_del_init(&c->node);
  10485. + spin_unlock(&d->lock);
  10486. +
  10487. + /*
  10488. + * Stop DMA activity: we assume the callback will not be called
  10489. + * after bcm_dma_abort() returns (even if it does, it will see
  10490. + * c->desc is NULL and exit.)
  10491. + */
  10492. + if (c->desc) {
  10493. + c->desc = NULL;
  10494. + bcm_dma_abort(c->chan_base);
  10495. +
  10496. + /* Wait for stopping */
  10497. + while (timeout > 0) {
  10498. + timeout--;
  10499. + if (!(readl(c->chan_base + BCM2708_DMA_CS) &
  10500. + BCM2708_DMA_ACTIVE))
  10501. + break;
  10502. +
  10503. + cpu_relax();
  10504. + }
  10505. +
  10506. + if (timeout <= 0)
  10507. + dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  10508. + }
  10509. +
  10510. + vchan_get_all_descriptors(&c->vc, &head);
  10511. + spin_unlock_irqrestore(&c->vc.lock, flags);
  10512. + vchan_dma_desc_free_list(&c->vc, &head);
  10513. +
  10514. + return 0;
  10515. +}
  10516. +
  10517. +static int bcm2708_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  10518. + unsigned long arg)
  10519. +{
  10520. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  10521. +
  10522. + switch (cmd) {
  10523. + case DMA_SLAVE_CONFIG:
  10524. + return bcm2708_dma_slave_config(c,
  10525. + (struct dma_slave_config *)arg);
  10526. +
  10527. + case DMA_TERMINATE_ALL:
  10528. + return bcm2708_dma_terminate_all(c);
  10529. +
  10530. + default:
  10531. + return -ENXIO;
  10532. + }
  10533. +}
  10534. +
  10535. +static int bcm2708_dma_chan_init(struct bcm2708_dmadev *d, void __iomem* chan_base,
  10536. + int chan_id, int irq)
  10537. +{
  10538. + struct bcm2708_chan *c;
  10539. +
  10540. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  10541. + if (!c)
  10542. + return -ENOMEM;
  10543. +
  10544. + c->vc.desc_free = bcm2708_dma_desc_free;
  10545. + vchan_init(&c->vc, &d->ddev);
  10546. + INIT_LIST_HEAD(&c->node);
  10547. +
  10548. + d->ddev.chancnt++;
  10549. +
  10550. + c->chan_base = chan_base;
  10551. + c->ch = chan_id;
  10552. + c->irq_number = irq;
  10553. +
  10554. + return 0;
  10555. +}
  10556. +
  10557. +static void bcm2708_dma_free(struct bcm2708_dmadev *od)
  10558. +{
  10559. + while (!list_empty(&od->ddev.channels)) {
  10560. + struct bcm2708_chan *c = list_first_entry(&od->ddev.channels,
  10561. + struct bcm2708_chan, vc.chan.device_node);
  10562. +
  10563. + list_del(&c->vc.chan.device_node);
  10564. + tasklet_kill(&c->vc.task);
  10565. + }
  10566. +}
  10567. +
  10568. +static int bcm2708_dma_probe(struct platform_device *pdev)
  10569. +{
  10570. + struct bcm2708_dmadev *od;
  10571. + int rc, i;
  10572. +
  10573. + if (!pdev->dev.dma_mask)
  10574. + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  10575. +
  10576. + rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  10577. + if (rc)
  10578. + return rc;
  10579. + dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  10580. +
  10581. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  10582. + if (!od)
  10583. + return -ENOMEM;
  10584. +
  10585. + pdev->dev.dma_parms = &od->dma_parms;
  10586. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  10587. +
  10588. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  10589. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  10590. + od->ddev.device_alloc_chan_resources = bcm2708_dma_alloc_chan_resources;
  10591. + od->ddev.device_free_chan_resources = bcm2708_dma_free_chan_resources;
  10592. + od->ddev.device_tx_status = bcm2708_dma_tx_status;
  10593. + od->ddev.device_issue_pending = bcm2708_dma_issue_pending;
  10594. + od->ddev.device_prep_dma_cyclic = bcm2708_dma_prep_dma_cyclic;
  10595. + od->ddev.device_control = bcm2708_dma_control;
  10596. + od->ddev.dev = &pdev->dev;
  10597. + INIT_LIST_HEAD(&od->ddev.channels);
  10598. + spin_lock_init(&od->lock);
  10599. +
  10600. + platform_set_drvdata(pdev, od);
  10601. +
  10602. + for (i = 0; i < 16; i++) {
  10603. + void __iomem* chan_base;
  10604. + int chan_id, irq;
  10605. +
  10606. + chan_id = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  10607. + &chan_base,
  10608. + &irq);
  10609. +
  10610. + if (chan_id < 0)
  10611. + break;
  10612. +
  10613. + rc = bcm2708_dma_chan_init(od, chan_base, chan_id, irq);
  10614. + if (rc) {
  10615. + bcm2708_dma_free(od);
  10616. + return rc;
  10617. + }
  10618. + }
  10619. +
  10620. + rc = dma_async_device_register(&od->ddev);
  10621. + if (rc) {
  10622. + dev_err(&pdev->dev,
  10623. + "Failed to register slave DMA engine device: %d\n", rc);
  10624. + bcm2708_dma_free(od);
  10625. + return rc;
  10626. + }
  10627. +
  10628. + dev_dbg(&pdev->dev, "Load BCM2708 DMA engine driver\n");
  10629. +
  10630. + return rc;
  10631. +}
  10632. +
  10633. +static int bcm2708_dma_remove(struct platform_device *pdev)
  10634. +{
  10635. + struct bcm2708_dmadev *od = platform_get_drvdata(pdev);
  10636. +
  10637. + dma_async_device_unregister(&od->ddev);
  10638. + bcm2708_dma_free(od);
  10639. +
  10640. + return 0;
  10641. +}
  10642. +
  10643. +static struct platform_driver bcm2708_dma_driver = {
  10644. + .probe = bcm2708_dma_probe,
  10645. + .remove = bcm2708_dma_remove,
  10646. + .driver = {
  10647. + .name = "bcm2708-dmaengine",
  10648. + .owner = THIS_MODULE,
  10649. + },
  10650. +};
  10651. +
  10652. +static struct platform_device *pdev;
  10653. +
  10654. +static const struct platform_device_info bcm2708_dma_dev_info = {
  10655. + .name = "bcm2708-dmaengine",
  10656. + .id = -1,
  10657. +};
  10658. +
  10659. +static int bcm2708_dma_init(void)
  10660. +{
  10661. + int rc = platform_driver_register(&bcm2708_dma_driver);
  10662. +
  10663. + if (rc == 0) {
  10664. + pdev = platform_device_register_full(&bcm2708_dma_dev_info);
  10665. + if (IS_ERR(pdev)) {
  10666. + platform_driver_unregister(&bcm2708_dma_driver);
  10667. + rc = PTR_ERR(pdev);
  10668. + }
  10669. + }
  10670. +
  10671. + return rc;
  10672. +}
  10673. +subsys_initcall(bcm2708_dma_init);
  10674. +
  10675. +static void __exit bcm2708_dma_exit(void)
  10676. +{
  10677. + platform_device_unregister(pdev);
  10678. + platform_driver_unregister(&bcm2708_dma_driver);
  10679. +}
  10680. +module_exit(bcm2708_dma_exit);
  10681. +
  10682. +MODULE_ALIAS("platform:bcm2708-dma");
  10683. +MODULE_DESCRIPTION("BCM2708 DMA engine driver");
  10684. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  10685. +MODULE_LICENSE("GPL v2");
  10686. diff -Nur linux-3.10.37/drivers/dma/Kconfig linux-rpi/drivers/dma/Kconfig
  10687. --- linux-3.10.37/drivers/dma/Kconfig 2014-04-14 15:42:31.000000000 +0200
  10688. +++ linux-rpi/drivers/dma/Kconfig 2014-04-24 15:35:02.153543270 +0200
  10689. @@ -305,6 +305,12 @@
  10690. select DMA_ENGINE
  10691. select DMA_VIRTUAL_CHANNELS
  10692. +config DMA_BCM2708
  10693. + tristate "BCM2708 DMA engine support"
  10694. + depends on MACH_BCM2708
  10695. + select DMA_ENGINE
  10696. + select DMA_VIRTUAL_CHANNELS
  10697. +
  10698. config MMP_PDMA
  10699. bool "MMP PDMA support"
  10700. depends on (ARCH_MMP || ARCH_PXA)
  10701. diff -Nur linux-3.10.37/drivers/dma/Makefile linux-rpi/drivers/dma/Makefile
  10702. --- linux-3.10.37/drivers/dma/Makefile 2014-04-14 15:42:31.000000000 +0200
  10703. +++ linux-rpi/drivers/dma/Makefile 2014-04-24 15:35:02.153543270 +0200
  10704. @@ -37,4 +37,5 @@
  10705. obj-$(CONFIG_DMA_SA11X0) += sa11x0-dma.o
  10706. obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o
  10707. obj-$(CONFIG_DMA_OMAP) += omap-dma.o
  10708. +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
  10709. obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
  10710. diff -Nur linux-3.10.37/drivers/hwmon/bcm2835-hwmon.c linux-rpi/drivers/hwmon/bcm2835-hwmon.c
  10711. --- linux-3.10.37/drivers/hwmon/bcm2835-hwmon.c 1970-01-01 01:00:00.000000000 +0100
  10712. +++ linux-rpi/drivers/hwmon/bcm2835-hwmon.c 2014-04-24 15:35:02.349545454 +0200
  10713. @@ -0,0 +1,219 @@
  10714. +/*****************************************************************************
  10715. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  10716. +*
  10717. +* Unless you and Broadcom execute a separate written software license
  10718. +* agreement governing use of this software, this software is licensed to you
  10719. +* under the terms of the GNU General Public License version 2, available at
  10720. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  10721. +*
  10722. +* Notwithstanding the above, under no circumstances may you combine this
  10723. +* software in any way with any other Broadcom software provided under a
  10724. +* license other than the GPL, without Broadcom's express prior written
  10725. +* consent.
  10726. +*****************************************************************************/
  10727. +
  10728. +#include <linux/kernel.h>
  10729. +#include <linux/module.h>
  10730. +#include <linux/init.h>
  10731. +#include <linux/hwmon.h>
  10732. +#include <linux/hwmon-sysfs.h>
  10733. +#include <linux/platform_device.h>
  10734. +#include <linux/sysfs.h>
  10735. +#include <mach/vcio.h>
  10736. +#include <linux/slab.h>
  10737. +#include <linux/err.h>
  10738. +
  10739. +#define MODULE_NAME "bcm2835_hwmon"
  10740. +
  10741. +/*#define HWMON_DEBUG_ENABLE*/
  10742. +
  10743. +#ifdef HWMON_DEBUG_ENABLE
  10744. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  10745. +#else
  10746. +#define print_debug(fmt,...)
  10747. +#endif
  10748. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  10749. +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
  10750. +
  10751. +#define VC_TAG_GET_TEMP 0x00030006
  10752. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  10753. +
  10754. +/* --- STRUCTS --- */
  10755. +struct bcm2835_hwmon_data {
  10756. + struct device *hwmon_dev;
  10757. +};
  10758. +
  10759. +/* tag part of the message */
  10760. +struct vc_msg_tag {
  10761. + uint32_t tag_id; /* the tag ID for the temperature */
  10762. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  10763. + uint32_t request_code; /* identifies message as a request (should be 0) */
  10764. + uint32_t id; /* extra ID field (should be 0) */
  10765. + uint32_t val; /* returned value of the temperature */
  10766. +};
  10767. +
  10768. +/* message structure to be sent to videocore */
  10769. +struct vc_msg {
  10770. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  10771. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  10772. + struct vc_msg_tag tag; /* the tag structure above to make */
  10773. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  10774. +};
  10775. +
  10776. +typedef enum {
  10777. + TEMP,
  10778. + MAX_TEMP,
  10779. +} temp_type;
  10780. +
  10781. +/* --- PROTOTYPES --- */
  10782. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
  10783. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
  10784. +
  10785. +/* --- GLOBALS --- */
  10786. +
  10787. +static struct bcm2835_hwmon_data *bcm2835_data;
  10788. +static struct platform_driver bcm2835_hwmon_driver;
  10789. +
  10790. +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
  10791. +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
  10792. +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
  10793. +
  10794. +static struct attribute* bcm2835_attributes[] = {
  10795. + &sensor_dev_attr_name.dev_attr.attr,
  10796. + &sensor_dev_attr_temp1_input.dev_attr.attr,
  10797. + &sensor_dev_attr_temp1_max.dev_attr.attr,
  10798. + NULL,
  10799. +};
  10800. +
  10801. +static struct attribute_group bcm2835_attr_group = {
  10802. + .attrs = bcm2835_attributes,
  10803. +};
  10804. +
  10805. +/* --- FUNCTIONS --- */
  10806. +
  10807. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
  10808. +{
  10809. + return sprintf(buf,"bcm2835_hwmon\n");
  10810. +}
  10811. +
  10812. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
  10813. +{
  10814. + struct vc_msg msg;
  10815. + int result;
  10816. + uint temp = 0;
  10817. + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
  10818. +
  10819. + print_debug("IN");
  10820. +
  10821. + /* wipe all previous message data */
  10822. + memset(&msg, 0, sizeof msg);
  10823. +
  10824. + /* determine the message type */
  10825. + if(index == TEMP)
  10826. + msg.tag.tag_id = VC_TAG_GET_TEMP;
  10827. + else if (index == MAX_TEMP)
  10828. + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
  10829. + else
  10830. + {
  10831. + print_debug("Unknown temperature message!");
  10832. + return -EINVAL;
  10833. + }
  10834. +
  10835. + msg.msg_size = sizeof msg;
  10836. + msg.tag.buffer_size = 8;
  10837. +
  10838. + /* send the message */
  10839. + result = bcm_mailbox_property(&msg, sizeof msg);
  10840. +
  10841. + /* check if it was all ok and return the rate in milli degrees C */
  10842. + if (result == 0 && (msg.request_code & 0x80000000))
  10843. + temp = (uint)msg.tag.val;
  10844. + #ifdef HWMON_DEBUG_ENABLE
  10845. + else
  10846. + print_debug("Failed to get temperature!");
  10847. + #endif
  10848. + print_debug("Got temperature as %u",temp);
  10849. + print_debug("OUT");
  10850. + return sprintf(buf, "%u\n", temp);
  10851. +}
  10852. +
  10853. +
  10854. +static int bcm2835_hwmon_probe(struct platform_device *pdev)
  10855. +{
  10856. + int err;
  10857. +
  10858. + print_debug("IN");
  10859. + print_debug("HWMON Driver has been probed!");
  10860. +
  10861. + /* check that the device isn't null!*/
  10862. + if(pdev == NULL)
  10863. + {
  10864. + print_debug("Platform device is empty!");
  10865. + return -ENODEV;
  10866. + }
  10867. +
  10868. + /* allocate memory for neccessary data */
  10869. + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
  10870. + if(!bcm2835_data)
  10871. + {
  10872. + print_debug("Unable to allocate memory for hwmon data!");
  10873. + err = -ENOMEM;
  10874. + goto kzalloc_error;
  10875. + }
  10876. +
  10877. + /* create the sysfs files */
  10878. + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
  10879. + {
  10880. + print_debug("Unable to create sysfs files!");
  10881. + err = -EFAULT;
  10882. + goto sysfs_error;
  10883. + }
  10884. +
  10885. + /* register the hwmon device */
  10886. + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  10887. + if (IS_ERR(bcm2835_data->hwmon_dev))
  10888. + {
  10889. + err = PTR_ERR(bcm2835_data->hwmon_dev);
  10890. + goto hwmon_error;
  10891. + }
  10892. + print_debug("OUT");
  10893. + return 0;
  10894. +
  10895. + /* error goto's */
  10896. + hwmon_error:
  10897. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  10898. +
  10899. + sysfs_error:
  10900. + kfree(bcm2835_data);
  10901. +
  10902. + kzalloc_error:
  10903. +
  10904. + return err;
  10905. +
  10906. +}
  10907. +
  10908. +static int bcm2835_hwmon_remove(struct platform_device *pdev)
  10909. +{
  10910. + print_debug("IN");
  10911. + hwmon_device_unregister(bcm2835_data->hwmon_dev);
  10912. +
  10913. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  10914. + print_debug("OUT");
  10915. + return 0;
  10916. +}
  10917. +
  10918. +/* Hwmon Driver */
  10919. +static struct platform_driver bcm2835_hwmon_driver = {
  10920. + .probe = bcm2835_hwmon_probe,
  10921. + .remove = bcm2835_hwmon_remove,
  10922. + .driver = {
  10923. + .name = "bcm2835_hwmon",
  10924. + .owner = THIS_MODULE,
  10925. + },
  10926. +};
  10927. +
  10928. +MODULE_LICENSE("GPL");
  10929. +MODULE_AUTHOR("Dorian Peake");
  10930. +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
  10931. +
  10932. +module_platform_driver(bcm2835_hwmon_driver);
  10933. diff -Nur linux-3.10.37/drivers/hwmon/Kconfig linux-rpi/drivers/hwmon/Kconfig
  10934. --- linux-3.10.37/drivers/hwmon/Kconfig 2014-04-14 15:42:31.000000000 +0200
  10935. +++ linux-rpi/drivers/hwmon/Kconfig 2014-04-24 15:35:02.345545410 +0200
  10936. @@ -1528,6 +1528,16 @@
  10937. help
  10938. Support for the A/D converter on MC13783 and MC13892 PMIC.
  10939. +config SENSORS_BCM2835
  10940. + depends on THERMAL_BCM2835=n
  10941. + tristate "Broadcom BCM2835 HWMON Driver"
  10942. + help
  10943. + If you say yes here you get support for the hardware
  10944. + monitoring features of the BCM2835 Chip
  10945. +
  10946. + This driver can also be built as a module. If so, the module
  10947. + will be called bcm2835-hwmon.
  10948. +
  10949. if ACPI
  10950. comment "ACPI drivers"
  10951. diff -Nur linux-3.10.37/drivers/hwmon/Makefile linux-rpi/drivers/hwmon/Makefile
  10952. --- linux-3.10.37/drivers/hwmon/Makefile 2014-04-14 15:42:31.000000000 +0200
  10953. +++ linux-rpi/drivers/hwmon/Makefile 2014-04-24 15:35:02.345545410 +0200
  10954. @@ -140,6 +140,7 @@
  10955. obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
  10956. obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
  10957. obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
  10958. +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
  10959. obj-$(CONFIG_PMBUS) += pmbus/
  10960. diff -Nur linux-3.10.37/drivers/i2c/busses/i2c-bcm2708.c linux-rpi/drivers/i2c/busses/i2c-bcm2708.c
  10961. --- linux-3.10.37/drivers/i2c/busses/i2c-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  10962. +++ linux-rpi/drivers/i2c/busses/i2c-bcm2708.c 2014-04-24 15:35:02.373545722 +0200
  10963. @@ -0,0 +1,419 @@
  10964. +/*
  10965. + * Driver for Broadcom BCM2708 BSC Controllers
  10966. + *
  10967. + * Copyright (C) 2012 Chris Boot & Frank Buss
  10968. + *
  10969. + * This driver is inspired by:
  10970. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  10971. + *
  10972. + * This program is free software; you can redistribute it and/or modify
  10973. + * it under the terms of the GNU General Public License as published by
  10974. + * the Free Software Foundation; either version 2 of the License, or
  10975. + * (at your option) any later version.
  10976. + *
  10977. + * This program is distributed in the hope that it will be useful,
  10978. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10979. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10980. + * GNU General Public License for more details.
  10981. + *
  10982. + * You should have received a copy of the GNU General Public License
  10983. + * along with this program; if not, write to the Free Software
  10984. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  10985. + */
  10986. +
  10987. +#include <linux/kernel.h>
  10988. +#include <linux/module.h>
  10989. +#include <linux/spinlock.h>
  10990. +#include <linux/clk.h>
  10991. +#include <linux/err.h>
  10992. +#include <linux/platform_device.h>
  10993. +#include <linux/io.h>
  10994. +#include <linux/slab.h>
  10995. +#include <linux/i2c.h>
  10996. +#include <linux/interrupt.h>
  10997. +#include <linux/sched.h>
  10998. +#include <linux/wait.h>
  10999. +
  11000. +/* BSC register offsets */
  11001. +#define BSC_C 0x00
  11002. +#define BSC_S 0x04
  11003. +#define BSC_DLEN 0x08
  11004. +#define BSC_A 0x0c
  11005. +#define BSC_FIFO 0x10
  11006. +#define BSC_DIV 0x14
  11007. +#define BSC_DEL 0x18
  11008. +#define BSC_CLKT 0x1c
  11009. +
  11010. +/* Bitfields in BSC_C */
  11011. +#define BSC_C_I2CEN 0x00008000
  11012. +#define BSC_C_INTR 0x00000400
  11013. +#define BSC_C_INTT 0x00000200
  11014. +#define BSC_C_INTD 0x00000100
  11015. +#define BSC_C_ST 0x00000080
  11016. +#define BSC_C_CLEAR_1 0x00000020
  11017. +#define BSC_C_CLEAR_2 0x00000010
  11018. +#define BSC_C_READ 0x00000001
  11019. +
  11020. +/* Bitfields in BSC_S */
  11021. +#define BSC_S_CLKT 0x00000200
  11022. +#define BSC_S_ERR 0x00000100
  11023. +#define BSC_S_RXF 0x00000080
  11024. +#define BSC_S_TXE 0x00000040
  11025. +#define BSC_S_RXD 0x00000020
  11026. +#define BSC_S_TXD 0x00000010
  11027. +#define BSC_S_RXR 0x00000008
  11028. +#define BSC_S_TXW 0x00000004
  11029. +#define BSC_S_DONE 0x00000002
  11030. +#define BSC_S_TA 0x00000001
  11031. +
  11032. +#define I2C_TIMEOUT_MS 150
  11033. +
  11034. +#define DRV_NAME "bcm2708_i2c"
  11035. +
  11036. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  11037. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  11038. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  11039. +
  11040. +
  11041. +struct bcm2708_i2c {
  11042. + struct i2c_adapter adapter;
  11043. +
  11044. + spinlock_t lock;
  11045. + void __iomem *base;
  11046. + int irq;
  11047. + struct clk *clk;
  11048. +
  11049. + struct completion done;
  11050. +
  11051. + struct i2c_msg *msg;
  11052. + int pos;
  11053. + int nmsgs;
  11054. + bool error;
  11055. +};
  11056. +
  11057. +/*
  11058. + * This function sets the ALT mode on the I2C pins so that we can use them with
  11059. + * the BSC hardware.
  11060. + *
  11061. + * FIXME: This is a hack. Use pinmux / pinctrl.
  11062. + */
  11063. +static void bcm2708_i2c_init_pinmode(int id)
  11064. +{
  11065. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  11066. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  11067. +
  11068. + int pin;
  11069. + u32 *gpio = ioremap(0x20200000, SZ_16K);
  11070. +
  11071. + BUG_ON(id != 0 && id != 1);
  11072. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  11073. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  11074. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  11075. + INP_GPIO(pin); /* set mode to GPIO input first */
  11076. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  11077. + }
  11078. +
  11079. + iounmap(gpio);
  11080. +
  11081. +#undef INP_GPIO
  11082. +#undef SET_GPIO_ALT
  11083. +}
  11084. +
  11085. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  11086. +{
  11087. + return readl(bi->base + reg);
  11088. +}
  11089. +
  11090. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  11091. +{
  11092. + writel(val, bi->base + reg);
  11093. +}
  11094. +
  11095. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  11096. +{
  11097. + bcm2708_wr(bi, BSC_C, 0);
  11098. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  11099. +}
  11100. +
  11101. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  11102. +{
  11103. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  11104. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  11105. +}
  11106. +
  11107. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  11108. +{
  11109. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  11110. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  11111. +}
  11112. +
  11113. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  11114. +{
  11115. + unsigned long bus_hz;
  11116. + u32 cdiv;
  11117. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  11118. +
  11119. + bus_hz = clk_get_rate(bi->clk);
  11120. + cdiv = bus_hz / baudrate;
  11121. + if (cdiv > 0xffff)
  11122. + cdiv = 0xffff;
  11123. +
  11124. + if (bi->msg->flags & I2C_M_RD)
  11125. + c |= BSC_C_INTR | BSC_C_READ;
  11126. + else
  11127. + c |= BSC_C_INTT;
  11128. +
  11129. + bcm2708_wr(bi, BSC_DIV, cdiv);
  11130. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  11131. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  11132. + bcm2708_wr(bi, BSC_C, c);
  11133. +}
  11134. +
  11135. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  11136. +{
  11137. + struct bcm2708_i2c *bi = dev_id;
  11138. + bool handled = true;
  11139. + u32 s;
  11140. +
  11141. + spin_lock(&bi->lock);
  11142. +
  11143. + /* we may see camera interrupts on the "other" I2C channel
  11144. + Just return if we've not sent anything */
  11145. + if (!bi->nmsgs || !bi->msg )
  11146. + goto early_exit;
  11147. +
  11148. + s = bcm2708_rd(bi, BSC_S);
  11149. +
  11150. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  11151. + bcm2708_bsc_reset(bi);
  11152. + bi->error = true;
  11153. +
  11154. + /* wake up our bh */
  11155. + complete(&bi->done);
  11156. + } else if (s & BSC_S_DONE) {
  11157. + bi->nmsgs--;
  11158. +
  11159. + if (bi->msg->flags & I2C_M_RD)
  11160. + bcm2708_bsc_fifo_drain(bi);
  11161. +
  11162. + bcm2708_bsc_reset(bi);
  11163. +
  11164. + if (bi->nmsgs) {
  11165. + /* advance to next message */
  11166. + bi->msg++;
  11167. + bi->pos = 0;
  11168. + bcm2708_bsc_setup(bi);
  11169. + } else {
  11170. + /* wake up our bh */
  11171. + complete(&bi->done);
  11172. + }
  11173. + } else if (s & BSC_S_TXW) {
  11174. + bcm2708_bsc_fifo_fill(bi);
  11175. + } else if (s & BSC_S_RXR) {
  11176. + bcm2708_bsc_fifo_drain(bi);
  11177. + } else {
  11178. + handled = false;
  11179. + }
  11180. +
  11181. +early_exit:
  11182. + spin_unlock(&bi->lock);
  11183. +
  11184. + return handled ? IRQ_HANDLED : IRQ_NONE;
  11185. +}
  11186. +
  11187. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  11188. + struct i2c_msg *msgs, int num)
  11189. +{
  11190. + struct bcm2708_i2c *bi = adap->algo_data;
  11191. + unsigned long flags;
  11192. + int ret;
  11193. +
  11194. + spin_lock_irqsave(&bi->lock, flags);
  11195. +
  11196. + INIT_COMPLETION(bi->done);
  11197. + bi->msg = msgs;
  11198. + bi->pos = 0;
  11199. + bi->nmsgs = num;
  11200. + bi->error = false;
  11201. +
  11202. + spin_unlock_irqrestore(&bi->lock, flags);
  11203. +
  11204. + bcm2708_bsc_setup(bi);
  11205. +
  11206. + ret = wait_for_completion_timeout(&bi->done,
  11207. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  11208. + if (ret == 0) {
  11209. + dev_err(&adap->dev, "transfer timed out\n");
  11210. + spin_lock_irqsave(&bi->lock, flags);
  11211. + bcm2708_bsc_reset(bi);
  11212. + spin_unlock_irqrestore(&bi->lock, flags);
  11213. + return -ETIMEDOUT;
  11214. + }
  11215. +
  11216. + return bi->error ? -EIO : num;
  11217. +}
  11218. +
  11219. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  11220. +{
  11221. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  11222. +}
  11223. +
  11224. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  11225. + .master_xfer = bcm2708_i2c_master_xfer,
  11226. + .functionality = bcm2708_i2c_functionality,
  11227. +};
  11228. +
  11229. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  11230. +{
  11231. + struct resource *regs;
  11232. + int irq, err = -ENOMEM;
  11233. + struct clk *clk;
  11234. + struct bcm2708_i2c *bi;
  11235. + struct i2c_adapter *adap;
  11236. + unsigned long bus_hz;
  11237. + u32 cdiv;
  11238. +
  11239. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  11240. + if (!regs) {
  11241. + dev_err(&pdev->dev, "could not get IO memory\n");
  11242. + return -ENXIO;
  11243. + }
  11244. +
  11245. + irq = platform_get_irq(pdev, 0);
  11246. + if (irq < 0) {
  11247. + dev_err(&pdev->dev, "could not get IRQ\n");
  11248. + return irq;
  11249. + }
  11250. +
  11251. + clk = clk_get(&pdev->dev, NULL);
  11252. + if (IS_ERR(clk)) {
  11253. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  11254. + return PTR_ERR(clk);
  11255. + }
  11256. +
  11257. + bcm2708_i2c_init_pinmode(pdev->id);
  11258. +
  11259. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  11260. + if (!bi)
  11261. + goto out_clk_put;
  11262. +
  11263. + platform_set_drvdata(pdev, bi);
  11264. +
  11265. + adap = &bi->adapter;
  11266. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  11267. + adap->algo = &bcm2708_i2c_algorithm;
  11268. + adap->algo_data = bi;
  11269. + adap->dev.parent = &pdev->dev;
  11270. + adap->nr = pdev->id;
  11271. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  11272. +
  11273. + switch (pdev->id) {
  11274. + case 0:
  11275. + adap->class = I2C_CLASS_HWMON;
  11276. + break;
  11277. + case 1:
  11278. + adap->class = I2C_CLASS_DDC;
  11279. + break;
  11280. + default:
  11281. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  11282. + err = -ENXIO;
  11283. + goto out_free_bi;
  11284. + }
  11285. +
  11286. + spin_lock_init(&bi->lock);
  11287. + init_completion(&bi->done);
  11288. +
  11289. + bi->base = ioremap(regs->start, resource_size(regs));
  11290. + if (!bi->base) {
  11291. + dev_err(&pdev->dev, "could not remap memory\n");
  11292. + goto out_free_bi;
  11293. + }
  11294. +
  11295. + bi->irq = irq;
  11296. + bi->clk = clk;
  11297. +
  11298. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  11299. + dev_name(&pdev->dev), bi);
  11300. + if (err) {
  11301. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  11302. + goto out_iounmap;
  11303. + }
  11304. +
  11305. + bcm2708_bsc_reset(bi);
  11306. +
  11307. + err = i2c_add_numbered_adapter(adap);
  11308. + if (err < 0) {
  11309. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  11310. + goto out_free_irq;
  11311. + }
  11312. +
  11313. + bus_hz = clk_get_rate(bi->clk);
  11314. + cdiv = bus_hz / baudrate;
  11315. + if (cdiv > 0xffff) {
  11316. + cdiv = 0xffff;
  11317. + baudrate = bus_hz / cdiv;
  11318. + }
  11319. +
  11320. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
  11321. + pdev->id, (unsigned long)regs->start, irq, baudrate);
  11322. +
  11323. + return 0;
  11324. +
  11325. +out_free_irq:
  11326. + free_irq(bi->irq, bi);
  11327. +out_iounmap:
  11328. + iounmap(bi->base);
  11329. +out_free_bi:
  11330. + kfree(bi);
  11331. +out_clk_put:
  11332. + clk_put(clk);
  11333. + return err;
  11334. +}
  11335. +
  11336. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  11337. +{
  11338. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  11339. +
  11340. + platform_set_drvdata(pdev, NULL);
  11341. +
  11342. + i2c_del_adapter(&bi->adapter);
  11343. + free_irq(bi->irq, bi);
  11344. + iounmap(bi->base);
  11345. + clk_disable(bi->clk);
  11346. + clk_put(bi->clk);
  11347. + kfree(bi);
  11348. +
  11349. + return 0;
  11350. +}
  11351. +
  11352. +static struct platform_driver bcm2708_i2c_driver = {
  11353. + .driver = {
  11354. + .name = DRV_NAME,
  11355. + .owner = THIS_MODULE,
  11356. + },
  11357. + .probe = bcm2708_i2c_probe,
  11358. + .remove = bcm2708_i2c_remove,
  11359. +};
  11360. +
  11361. +// module_platform_driver(bcm2708_i2c_driver);
  11362. +
  11363. +
  11364. +static int __init bcm2708_i2c_init(void)
  11365. +{
  11366. + return platform_driver_register(&bcm2708_i2c_driver);
  11367. +}
  11368. +
  11369. +static void __exit bcm2708_i2c_exit(void)
  11370. +{
  11371. + platform_driver_unregister(&bcm2708_i2c_driver);
  11372. +}
  11373. +
  11374. +module_init(bcm2708_i2c_init);
  11375. +module_exit(bcm2708_i2c_exit);
  11376. +
  11377. +
  11378. +
  11379. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  11380. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  11381. +MODULE_LICENSE("GPL v2");
  11382. +MODULE_ALIAS("platform:" DRV_NAME);
  11383. diff -Nur linux-3.10.37/drivers/i2c/busses/Kconfig linux-rpi/drivers/i2c/busses/Kconfig
  11384. --- linux-3.10.37/drivers/i2c/busses/Kconfig 2014-04-14 15:42:31.000000000 +0200
  11385. +++ linux-rpi/drivers/i2c/busses/Kconfig 2014-04-24 15:35:02.373545722 +0200
  11386. @@ -345,6 +345,25 @@
  11387. This support is also available as a module. If so, the module
  11388. will be called i2c-bcm2835.
  11389. +config I2C_BCM2708
  11390. + tristate "BCM2708 BSC"
  11391. + depends on MACH_BCM2708
  11392. + help
  11393. + Enabling this option will add BSC (Broadcom Serial Controller)
  11394. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  11395. + with I2C/TWI/SMBus.
  11396. +
  11397. +config I2C_BCM2708_BAUDRATE
  11398. + prompt "BCM2708 I2C baudrate"
  11399. + depends on I2C_BCM2708
  11400. + int
  11401. + default 100000
  11402. + help
  11403. + Set the I2C baudrate. This will alter the default value. A
  11404. + different baudrate can be set by using a module parameter as well. If
  11405. + no parameter is provided when loading, this is the value that will be
  11406. + used.
  11407. +
  11408. config I2C_BLACKFIN_TWI
  11409. tristate "Blackfin TWI I2C support"
  11410. depends on BLACKFIN
  11411. diff -Nur linux-3.10.37/drivers/i2c/busses/Makefile linux-rpi/drivers/i2c/busses/Makefile
  11412. --- linux-3.10.37/drivers/i2c/busses/Makefile 2014-04-14 15:42:31.000000000 +0200
  11413. +++ linux-rpi/drivers/i2c/busses/Makefile 2014-04-24 15:35:02.373545722 +0200
  11414. @@ -32,6 +32,7 @@
  11415. obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  11416. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  11417. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  11418. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  11419. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  11420. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  11421. obj-$(CONFIG_I2C_CPM) += i2c-cpm.o
  11422. diff -Nur linux-3.10.37/drivers/media/dvb-core/dvb-usb-ids.h linux-rpi/drivers/media/dvb-core/dvb-usb-ids.h
  11423. --- linux-3.10.37/drivers/media/dvb-core/dvb-usb-ids.h 2014-04-14 15:42:31.000000000 +0200
  11424. +++ linux-rpi/drivers/media/dvb-core/dvb-usb-ids.h 2014-04-24 15:35:02.601548262 +0200
  11425. @@ -365,6 +365,7 @@
  11426. #define USB_PID_TERRATEC_DVBS2CI_V2 0x10ac
  11427. #define USB_PID_TECHNISAT_USB2_HDCI_V1 0x0001
  11428. #define USB_PID_TECHNISAT_USB2_HDCI_V2 0x0002
  11429. +#define USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI 0x0003
  11430. #define USB_PID_TECHNISAT_AIRSTAR_TELESTICK_2 0x0004
  11431. #define USB_PID_TECHNISAT_USB2_DVB_S2 0x0500
  11432. #endif
  11433. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/bcm2835-camera.c linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c
  11434. --- linux-3.10.37/drivers/media/platform/bcm2835/bcm2835-camera.c 1970-01-01 01:00:00.000000000 +0100
  11435. +++ linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c 2014-04-24 15:35:02.713549510 +0200
  11436. @@ -0,0 +1,1722 @@
  11437. +/*
  11438. + * Broadcom BM2835 V4L2 driver
  11439. + *
  11440. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  11441. + *
  11442. + * This file is subject to the terms and conditions of the GNU General Public
  11443. + * License. See the file COPYING in the main directory of this archive
  11444. + * for more details.
  11445. + *
  11446. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  11447. + * Dave Stevenson <dsteve@broadcom.com>
  11448. + * Simon Mellor <simellor@broadcom.com>
  11449. + * Luke Diamand <luked@broadcom.com>
  11450. + */
  11451. +
  11452. +#include <linux/errno.h>
  11453. +#include <linux/kernel.h>
  11454. +#include <linux/module.h>
  11455. +#include <linux/slab.h>
  11456. +#include <media/videobuf2-vmalloc.h>
  11457. +#include <media/videobuf2-dma-contig.h>
  11458. +#include <media/v4l2-device.h>
  11459. +#include <media/v4l2-ioctl.h>
  11460. +#include <media/v4l2-ctrls.h>
  11461. +#include <media/v4l2-fh.h>
  11462. +#include <media/v4l2-event.h>
  11463. +#include <media/v4l2-common.h>
  11464. +#include <linux/delay.h>
  11465. +
  11466. +#include "mmal-common.h"
  11467. +#include "mmal-encodings.h"
  11468. +#include "mmal-vchiq.h"
  11469. +#include "mmal-msg.h"
  11470. +#include "mmal-parameters.h"
  11471. +#include "bcm2835-camera.h"
  11472. +
  11473. +#define BM2835_MMAL_VERSION "0.0.2"
  11474. +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
  11475. +#define MIN_WIDTH 16
  11476. +#define MIN_HEIGHT 16
  11477. +#define MAX_WIDTH 2592
  11478. +#define MAX_HEIGHT 1944
  11479. +#define MIN_BUFFER_SIZE (80*1024)
  11480. +
  11481. +/* Max number of pixels supported whilst still being considered
  11482. + * a video mode by the GPU.
  11483. + */
  11484. +#define MAX_VIDEO_MODE_WIDTH 1280
  11485. +#define MAX_VIDEO_MODE_HEIGHT 720
  11486. +
  11487. +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
  11488. +MODULE_AUTHOR("Vincent Sanders");
  11489. +MODULE_LICENSE("GPL");
  11490. +MODULE_VERSION(BM2835_MMAL_VERSION);
  11491. +
  11492. +int bcm2835_v4l2_debug;
  11493. +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
  11494. +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
  11495. +
  11496. +static struct bm2835_mmal_dev *gdev; /* global device data */
  11497. +
  11498. +#define FPS_MIN 1
  11499. +#define FPS_MAX 90
  11500. +
  11501. +/* timeperframe: min/max and default */
  11502. +static const struct v4l2_fract
  11503. + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  11504. + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
  11505. + tpf_default = {.numerator = 1000, .denominator = 30000};
  11506. +
  11507. +/* video formats */
  11508. +static struct mmal_fmt formats[] = {
  11509. + {
  11510. + .name = "4:2:0, packed YUV",
  11511. + .fourcc = V4L2_PIX_FMT_YUV420,
  11512. + .flags = 0,
  11513. + .mmal = MMAL_ENCODING_I420,
  11514. + .depth = 12,
  11515. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11516. + },
  11517. + {
  11518. + .name = "4:2:2, packed, YUYV",
  11519. + .fourcc = V4L2_PIX_FMT_YUYV,
  11520. + .flags = 0,
  11521. + .mmal = MMAL_ENCODING_YUYV,
  11522. + .depth = 16,
  11523. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11524. + },
  11525. + {
  11526. + .name = "RGB24 (LE)",
  11527. + .fourcc = V4L2_PIX_FMT_RGB24,
  11528. + .flags = 0,
  11529. + .mmal = MMAL_ENCODING_BGR24,
  11530. + .depth = 24,
  11531. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11532. + },
  11533. + {
  11534. + .name = "JPEG",
  11535. + .fourcc = V4L2_PIX_FMT_JPEG,
  11536. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  11537. + .mmal = MMAL_ENCODING_JPEG,
  11538. + .depth = 8,
  11539. + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
  11540. + },
  11541. + {
  11542. + .name = "H264",
  11543. + .fourcc = V4L2_PIX_FMT_H264,
  11544. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  11545. + .mmal = MMAL_ENCODING_H264,
  11546. + .depth = 8,
  11547. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  11548. + },
  11549. + {
  11550. + .name = "MJPEG",
  11551. + .fourcc = V4L2_PIX_FMT_MJPEG,
  11552. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  11553. + .mmal = MMAL_ENCODING_MJPEG,
  11554. + .depth = 8,
  11555. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  11556. + },
  11557. + {
  11558. + .name = "4:2:2, packed, YVYU",
  11559. + .fourcc = V4L2_PIX_FMT_YVYU,
  11560. + .flags = 0,
  11561. + .mmal = MMAL_ENCODING_YVYU,
  11562. + .depth = 16,
  11563. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11564. + },
  11565. + {
  11566. + .name = "4:2:2, packed, VYUY",
  11567. + .fourcc = V4L2_PIX_FMT_VYUY,
  11568. + .flags = 0,
  11569. + .mmal = MMAL_ENCODING_VYUY,
  11570. + .depth = 16,
  11571. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11572. + },
  11573. + {
  11574. + .name = "4:2:2, packed, UYVY",
  11575. + .fourcc = V4L2_PIX_FMT_UYVY,
  11576. + .flags = 0,
  11577. + .mmal = MMAL_ENCODING_UYVY,
  11578. + .depth = 16,
  11579. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11580. + },
  11581. + {
  11582. + .name = "4:2:0, packed, NV12",
  11583. + .fourcc = V4L2_PIX_FMT_NV12,
  11584. + .flags = 0,
  11585. + .mmal = MMAL_ENCODING_NV12,
  11586. + .depth = 12,
  11587. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11588. + },
  11589. + {
  11590. + .name = "RGB24 (BE)",
  11591. + .fourcc = V4L2_PIX_FMT_BGR24,
  11592. + .flags = 0,
  11593. + .mmal = MMAL_ENCODING_RGB24,
  11594. + .depth = 24,
  11595. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11596. + },
  11597. + {
  11598. + .name = "4:2:0, packed YVU",
  11599. + .fourcc = V4L2_PIX_FMT_YVU420,
  11600. + .flags = 0,
  11601. + .mmal = MMAL_ENCODING_YV12,
  11602. + .depth = 12,
  11603. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11604. + },
  11605. + {
  11606. + .name = "4:2:0, packed, NV21",
  11607. + .fourcc = V4L2_PIX_FMT_NV21,
  11608. + .flags = 0,
  11609. + .mmal = MMAL_ENCODING_NV21,
  11610. + .depth = 12,
  11611. + .mmal_component = MMAL_COMPONENT_CAMERA,
  11612. + },
  11613. +};
  11614. +
  11615. +static struct mmal_fmt *get_format(struct v4l2_format *f)
  11616. +{
  11617. + struct mmal_fmt *fmt;
  11618. + unsigned int k;
  11619. +
  11620. + for (k = 0; k < ARRAY_SIZE(formats); k++) {
  11621. + fmt = &formats[k];
  11622. + if (fmt->fourcc == f->fmt.pix.pixelformat)
  11623. + break;
  11624. + }
  11625. +
  11626. + if (k == ARRAY_SIZE(formats))
  11627. + return NULL;
  11628. +
  11629. + return &formats[k];
  11630. +}
  11631. +
  11632. +/* ------------------------------------------------------------------
  11633. + Videobuf queue operations
  11634. + ------------------------------------------------------------------*/
  11635. +
  11636. +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  11637. + unsigned int *nbuffers, unsigned int *nplanes,
  11638. + unsigned int sizes[], void *alloc_ctxs[])
  11639. +{
  11640. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11641. + unsigned long size;
  11642. +
  11643. + /* refuse queue setup if port is not configured */
  11644. + if (dev->capture.port == NULL) {
  11645. + v4l2_err(&dev->v4l2_dev,
  11646. + "%s: capture port not configured\n", __func__);
  11647. + return -EINVAL;
  11648. + }
  11649. +
  11650. + size = dev->capture.port->current_buffer.size;
  11651. + if (size == 0) {
  11652. + v4l2_err(&dev->v4l2_dev,
  11653. + "%s: capture port buffer size is zero\n", __func__);
  11654. + return -EINVAL;
  11655. + }
  11656. +
  11657. + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
  11658. + *nbuffers = (dev->capture.port->current_buffer.num + 2);
  11659. +
  11660. + *nplanes = 1;
  11661. +
  11662. + sizes[0] = size;
  11663. +
  11664. + /*
  11665. + * videobuf2-vmalloc allocator is context-less so no need to set
  11666. + * alloc_ctxs array.
  11667. + */
  11668. +
  11669. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  11670. + __func__, dev);
  11671. +
  11672. + return 0;
  11673. +}
  11674. +
  11675. +static int buffer_prepare(struct vb2_buffer *vb)
  11676. +{
  11677. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  11678. + unsigned long size;
  11679. +
  11680. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  11681. + __func__, dev);
  11682. +
  11683. + BUG_ON(dev->capture.port == NULL);
  11684. + BUG_ON(dev->capture.fmt == NULL);
  11685. +
  11686. + size = dev->capture.stride * dev->capture.height;
  11687. + if (vb2_plane_size(vb, 0) < size) {
  11688. + v4l2_err(&dev->v4l2_dev,
  11689. + "%s data will not fit into plane (%lu < %lu)\n",
  11690. + __func__, vb2_plane_size(vb, 0), size);
  11691. + return -EINVAL;
  11692. + }
  11693. +
  11694. + return 0;
  11695. +}
  11696. +
  11697. +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
  11698. +{
  11699. + return dev->capture.camera_port ==
  11700. + &dev->
  11701. + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
  11702. +}
  11703. +
  11704. +static void buffer_cb(struct vchiq_mmal_instance *instance,
  11705. + struct vchiq_mmal_port *port,
  11706. + int status,
  11707. + struct mmal_buffer *buf,
  11708. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
  11709. +{
  11710. + struct bm2835_mmal_dev *dev = port->cb_ctx;
  11711. +
  11712. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11713. + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
  11714. + __func__, status, buf, length, mmal_flags, pts);
  11715. +
  11716. + if (status != 0) {
  11717. + /* error in transfer */
  11718. + if (buf != NULL) {
  11719. + /* there was a buffer with the error so return it */
  11720. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  11721. + }
  11722. + return;
  11723. + } else if (length == 0) {
  11724. + /* stream ended */
  11725. + if (buf != NULL) {
  11726. + /* this should only ever happen if the port is
  11727. + * disabled and there are buffers still queued
  11728. + */
  11729. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  11730. + pr_debug("Empty buffer");
  11731. + } else if (dev->capture.frame_count) {
  11732. + /* grab another frame */
  11733. + if (is_capturing(dev)) {
  11734. + pr_debug("Grab another frame");
  11735. + vchiq_mmal_port_parameter_set(
  11736. + instance,
  11737. + dev->capture.
  11738. + camera_port,
  11739. + MMAL_PARAMETER_CAPTURE,
  11740. + &dev->capture.
  11741. + frame_count,
  11742. + sizeof(dev->capture.frame_count));
  11743. + }
  11744. + } else {
  11745. + /* signal frame completion */
  11746. + complete(&dev->capture.frame_cmplt);
  11747. + }
  11748. + } else {
  11749. + if (dev->capture.frame_count) {
  11750. + if (dev->capture.vc_start_timestamp != -1 &&
  11751. + pts != 0) {
  11752. + s64 runtime_us = pts -
  11753. + dev->capture.vc_start_timestamp;
  11754. + u32 div = 0;
  11755. + u32 rem = 0;
  11756. +
  11757. + div =
  11758. + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
  11759. + buf->vb.v4l2_buf.timestamp.tv_sec =
  11760. + dev->capture.kernel_start_ts.tv_sec - 1 +
  11761. + div;
  11762. + buf->vb.v4l2_buf.timestamp.tv_usec =
  11763. + dev->capture.kernel_start_ts.tv_usec + rem;
  11764. +
  11765. + if (buf->vb.v4l2_buf.timestamp.tv_usec >=
  11766. + USEC_PER_SEC) {
  11767. + buf->vb.v4l2_buf.timestamp.tv_sec++;
  11768. + buf->vb.v4l2_buf.timestamp.tv_usec -=
  11769. + USEC_PER_SEC;
  11770. + }
  11771. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11772. + "Convert start time %d.%06d and %llu "
  11773. + "with offset %llu to %d.%06d\n",
  11774. + (int)dev->capture.kernel_start_ts.
  11775. + tv_sec,
  11776. + (int)dev->capture.kernel_start_ts.
  11777. + tv_usec,
  11778. + dev->capture.vc_start_timestamp, pts,
  11779. + (int)buf->vb.v4l2_buf.timestamp.tv_sec,
  11780. + (int)buf->vb.v4l2_buf.timestamp.
  11781. + tv_usec);
  11782. + } else {
  11783. + v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  11784. + }
  11785. +
  11786. + vb2_set_plane_payload(&buf->vb, 0, length);
  11787. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  11788. +
  11789. + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
  11790. + is_capturing(dev)) {
  11791. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11792. + "Grab another frame as buffer has EOS");
  11793. + vchiq_mmal_port_parameter_set(
  11794. + instance,
  11795. + dev->capture.
  11796. + camera_port,
  11797. + MMAL_PARAMETER_CAPTURE,
  11798. + &dev->capture.
  11799. + frame_count,
  11800. + sizeof(dev->capture.frame_count));
  11801. + }
  11802. + } else {
  11803. + /* signal frame completion */
  11804. + complete(&dev->capture.frame_cmplt);
  11805. + }
  11806. + }
  11807. +}
  11808. +
  11809. +static int enable_camera(struct bm2835_mmal_dev *dev)
  11810. +{
  11811. + int ret;
  11812. + if (!dev->camera_use_count) {
  11813. + ret = vchiq_mmal_component_enable(
  11814. + dev->instance,
  11815. + dev->component[MMAL_COMPONENT_CAMERA]);
  11816. + if (ret < 0) {
  11817. + v4l2_err(&dev->v4l2_dev,
  11818. + "Failed enabling camera, ret %d\n", ret);
  11819. + return -EINVAL;
  11820. + }
  11821. + }
  11822. + dev->camera_use_count++;
  11823. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11824. + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
  11825. + dev->camera_use_count);
  11826. + return 0;
  11827. +}
  11828. +
  11829. +static int disable_camera(struct bm2835_mmal_dev *dev)
  11830. +{
  11831. + int ret;
  11832. + if (!dev->camera_use_count) {
  11833. + v4l2_err(&dev->v4l2_dev,
  11834. + "Disabled the camera when already disabled\n");
  11835. + return -EINVAL;
  11836. + }
  11837. + dev->camera_use_count--;
  11838. + if (!dev->camera_use_count) {
  11839. + unsigned int i = 0xFFFFFFFF;
  11840. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11841. + "Disabling camera\n");
  11842. + ret =
  11843. + vchiq_mmal_component_disable(
  11844. + dev->instance,
  11845. + dev->component[MMAL_COMPONENT_CAMERA]);
  11846. + if (ret < 0) {
  11847. + v4l2_err(&dev->v4l2_dev,
  11848. + "Failed disabling camera, ret %d\n", ret);
  11849. + return -EINVAL;
  11850. + }
  11851. + vchiq_mmal_port_parameter_set(
  11852. + dev->instance,
  11853. + &dev->component[MMAL_COMPONENT_CAMERA]->control,
  11854. + MMAL_PARAMETER_CAMERA_NUM, &i,
  11855. + sizeof(i));
  11856. + }
  11857. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11858. + "Camera refcount now %d\n", dev->camera_use_count);
  11859. + return 0;
  11860. +}
  11861. +
  11862. +static void buffer_queue(struct vb2_buffer *vb)
  11863. +{
  11864. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  11865. + struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
  11866. + int ret;
  11867. +
  11868. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11869. + "%s: dev:%p buf:%p\n", __func__, dev, buf);
  11870. +
  11871. + buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
  11872. + buf->buffer_size = vb2_plane_size(&buf->vb, 0);
  11873. +
  11874. + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
  11875. + if (ret < 0)
  11876. + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
  11877. + __func__);
  11878. +}
  11879. +
  11880. +static int start_streaming(struct vb2_queue *vq, unsigned int count)
  11881. +{
  11882. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11883. + int ret;
  11884. + int parameter_size;
  11885. +
  11886. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  11887. + __func__, dev);
  11888. +
  11889. + /* ensure a format has actually been set */
  11890. + if (dev->capture.port == NULL)
  11891. + return -EINVAL;
  11892. +
  11893. + if (enable_camera(dev) < 0) {
  11894. + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
  11895. + return -EINVAL;
  11896. + }
  11897. +
  11898. + /*init_completion(&dev->capture.frame_cmplt); */
  11899. +
  11900. + /* enable frame capture */
  11901. + dev->capture.frame_count = 1;
  11902. +
  11903. + /* if the preview is not already running, wait for a few frames for AGC
  11904. + * to settle down.
  11905. + */
  11906. + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
  11907. + msleep(300);
  11908. +
  11909. + /* enable the connection from camera to encoder (if applicable) */
  11910. + if (dev->capture.camera_port != dev->capture.port
  11911. + && dev->capture.camera_port) {
  11912. + ret = vchiq_mmal_port_enable(dev->instance,
  11913. + dev->capture.camera_port, NULL);
  11914. + if (ret) {
  11915. + v4l2_err(&dev->v4l2_dev,
  11916. + "Failed to enable encode tunnel - error %d\n",
  11917. + ret);
  11918. + return -1;
  11919. + }
  11920. + }
  11921. +
  11922. + /* Get VC timestamp at this point in time */
  11923. + parameter_size = sizeof(dev->capture.vc_start_timestamp);
  11924. + if (vchiq_mmal_port_parameter_get(dev->instance,
  11925. + dev->capture.camera_port,
  11926. + MMAL_PARAMETER_SYSTEM_TIME,
  11927. + &dev->capture.vc_start_timestamp,
  11928. + &parameter_size)) {
  11929. + v4l2_err(&dev->v4l2_dev,
  11930. + "Failed to get VC start time - update your VC f/w\n");
  11931. +
  11932. + /* Flag to indicate just to rely on kernel timestamps */
  11933. + dev->capture.vc_start_timestamp = -1;
  11934. + } else
  11935. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11936. + "Start time %lld size %d\n",
  11937. + dev->capture.vc_start_timestamp, parameter_size);
  11938. +
  11939. + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
  11940. +
  11941. + /* enable the camera port */
  11942. + dev->capture.port->cb_ctx = dev;
  11943. + ret =
  11944. + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
  11945. + if (ret) {
  11946. + v4l2_err(&dev->v4l2_dev,
  11947. + "Failed to enable capture port - error %d. "
  11948. + "Disabling camera port again\n", ret);
  11949. +
  11950. + vchiq_mmal_port_disable(dev->instance,
  11951. + dev->capture.camera_port);
  11952. + if (disable_camera(dev) < 0) {
  11953. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  11954. + return -EINVAL;
  11955. + }
  11956. + return -1;
  11957. + }
  11958. +
  11959. + /* capture the first frame */
  11960. + vchiq_mmal_port_parameter_set(dev->instance,
  11961. + dev->capture.camera_port,
  11962. + MMAL_PARAMETER_CAPTURE,
  11963. + &dev->capture.frame_count,
  11964. + sizeof(dev->capture.frame_count));
  11965. + return 0;
  11966. +}
  11967. +
  11968. +/* abort streaming and wait for last buffer */
  11969. +static int stop_streaming(struct vb2_queue *vq)
  11970. +{
  11971. + int ret;
  11972. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11973. +
  11974. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  11975. + __func__, dev);
  11976. +
  11977. + init_completion(&dev->capture.frame_cmplt);
  11978. + dev->capture.frame_count = 0;
  11979. +
  11980. + /* ensure a format has actually been set */
  11981. + if (dev->capture.port == NULL)
  11982. + return -EINVAL;
  11983. +
  11984. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
  11985. +
  11986. + /* stop capturing frames */
  11987. + vchiq_mmal_port_parameter_set(dev->instance,
  11988. + dev->capture.camera_port,
  11989. + MMAL_PARAMETER_CAPTURE,
  11990. + &dev->capture.frame_count,
  11991. + sizeof(dev->capture.frame_count));
  11992. +
  11993. + /* wait for last frame to complete */
  11994. + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
  11995. + if (ret <= 0)
  11996. + v4l2_err(&dev->v4l2_dev,
  11997. + "error %d waiting for frame completion\n", ret);
  11998. +
  11999. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12000. + "disabling connection\n");
  12001. +
  12002. + /* disable the connection from camera to encoder */
  12003. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
  12004. + if (!ret && dev->capture.camera_port != dev->capture.port) {
  12005. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12006. + "disabling port\n");
  12007. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
  12008. + } else if (dev->capture.camera_port != dev->capture.port) {
  12009. + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
  12010. + ret);
  12011. + }
  12012. +
  12013. + if (disable_camera(dev) < 0) {
  12014. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  12015. + return -EINVAL;
  12016. + }
  12017. +
  12018. + return ret;
  12019. +}
  12020. +
  12021. +static void bm2835_mmal_lock(struct vb2_queue *vq)
  12022. +{
  12023. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12024. + mutex_lock(&dev->mutex);
  12025. +}
  12026. +
  12027. +static void bm2835_mmal_unlock(struct vb2_queue *vq)
  12028. +{
  12029. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  12030. + mutex_unlock(&dev->mutex);
  12031. +}
  12032. +
  12033. +static struct vb2_ops bm2835_mmal_video_qops = {
  12034. + .queue_setup = queue_setup,
  12035. + .buf_prepare = buffer_prepare,
  12036. + .buf_queue = buffer_queue,
  12037. + .start_streaming = start_streaming,
  12038. + .stop_streaming = stop_streaming,
  12039. + .wait_prepare = bm2835_mmal_unlock,
  12040. + .wait_finish = bm2835_mmal_lock,
  12041. +};
  12042. +
  12043. +/* ------------------------------------------------------------------
  12044. + IOCTL operations
  12045. + ------------------------------------------------------------------*/
  12046. +
  12047. +/* overlay ioctl */
  12048. +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  12049. + struct v4l2_fmtdesc *f)
  12050. +{
  12051. + struct mmal_fmt *fmt;
  12052. +
  12053. + if (f->index >= ARRAY_SIZE(formats))
  12054. + return -EINVAL;
  12055. +
  12056. + fmt = &formats[f->index];
  12057. +
  12058. + strlcpy(f->description, fmt->name, sizeof(f->description));
  12059. + f->pixelformat = fmt->fourcc;
  12060. + f->flags = fmt->flags;
  12061. +
  12062. + return 0;
  12063. +}
  12064. +
  12065. +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  12066. + struct v4l2_format *f)
  12067. +{
  12068. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12069. +
  12070. + f->fmt.win = dev->overlay;
  12071. +
  12072. + return 0;
  12073. +}
  12074. +
  12075. +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  12076. + struct v4l2_format *f)
  12077. +{
  12078. + /* Only support one format so get the current one. */
  12079. + vidioc_g_fmt_vid_overlay(file, priv, f);
  12080. +
  12081. + /* todo: allow the size and/or offset to be changed. */
  12082. + return 0;
  12083. +}
  12084. +
  12085. +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  12086. + struct v4l2_format *f)
  12087. +{
  12088. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12089. +
  12090. + vidioc_try_fmt_vid_overlay(file, priv, f);
  12091. +
  12092. + dev->overlay = f->fmt.win;
  12093. +
  12094. + /* todo: program the preview port parameters */
  12095. + return 0;
  12096. +}
  12097. +
  12098. +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
  12099. +{
  12100. + int ret;
  12101. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12102. + struct vchiq_mmal_port *src;
  12103. + struct vchiq_mmal_port *dst;
  12104. + struct mmal_parameter_displayregion prev_config = {
  12105. + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
  12106. + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
  12107. + .layer = PREVIEW_LAYER,
  12108. + .alpha = 255,
  12109. + .fullscreen = 0,
  12110. + .dest_rect = {
  12111. + .x = dev->overlay.w.left,
  12112. + .y = dev->overlay.w.top,
  12113. + .width = dev->overlay.w.width,
  12114. + .height = dev->overlay.w.height,
  12115. + },
  12116. + };
  12117. +
  12118. + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
  12119. + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
  12120. + return 0; /* already in requested state */
  12121. +
  12122. + src =
  12123. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12124. + output[MMAL_CAMERA_PORT_PREVIEW];
  12125. +
  12126. + if (!on) {
  12127. + /* disconnect preview ports and disable component */
  12128. + ret = vchiq_mmal_port_disable(dev->instance, src);
  12129. + if (!ret)
  12130. + ret =
  12131. + vchiq_mmal_port_connect_tunnel(dev->instance, src,
  12132. + NULL);
  12133. + if (ret >= 0)
  12134. + ret = vchiq_mmal_component_disable(
  12135. + dev->instance,
  12136. + dev->component[MMAL_COMPONENT_PREVIEW]);
  12137. +
  12138. + disable_camera(dev);
  12139. + return ret;
  12140. + }
  12141. +
  12142. + /* set preview port format and connect it to output */
  12143. + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
  12144. +
  12145. + ret = vchiq_mmal_port_set_format(dev->instance, src);
  12146. + if (ret < 0)
  12147. + goto error;
  12148. +
  12149. + ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
  12150. + MMAL_PARAMETER_DISPLAYREGION,
  12151. + &prev_config, sizeof(prev_config));
  12152. + if (ret < 0)
  12153. + goto error;
  12154. +
  12155. + if (enable_camera(dev) < 0)
  12156. + goto error;
  12157. +
  12158. + ret = vchiq_mmal_component_enable(
  12159. + dev->instance,
  12160. + dev->component[MMAL_COMPONENT_PREVIEW]);
  12161. + if (ret < 0)
  12162. + goto error;
  12163. +
  12164. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
  12165. + src, dst);
  12166. + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
  12167. + if (!ret)
  12168. + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
  12169. +error:
  12170. + return ret;
  12171. +}
  12172. +
  12173. +static int vidioc_g_fbuf(struct file *file, void *fh,
  12174. + struct v4l2_framebuffer *a)
  12175. +{
  12176. + /* The video overlay must stay within the framebuffer and can't be
  12177. + positioned independently. */
  12178. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12179. + struct vchiq_mmal_port *preview_port =
  12180. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12181. + output[MMAL_CAMERA_PORT_PREVIEW];
  12182. + a->flags = V4L2_FBUF_FLAG_OVERLAY;
  12183. + a->fmt.width = preview_port->es.video.width;
  12184. + a->fmt.height = preview_port->es.video.height;
  12185. + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
  12186. + a->fmt.bytesperline = (preview_port->es.video.width * 3)>>1;
  12187. + a->fmt.sizeimage = (preview_port->es.video.width *
  12188. + preview_port->es.video.height * 3)>>1;
  12189. + a->fmt.colorspace = V4L2_COLORSPACE_JPEG;
  12190. +
  12191. + return 0;
  12192. +}
  12193. +
  12194. +/* input ioctls */
  12195. +static int vidioc_enum_input(struct file *file, void *priv,
  12196. + struct v4l2_input *inp)
  12197. +{
  12198. + /* only a single camera input */
  12199. + if (inp->index != 0)
  12200. + return -EINVAL;
  12201. +
  12202. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  12203. + sprintf(inp->name, "Camera %u", inp->index);
  12204. + return 0;
  12205. +}
  12206. +
  12207. +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  12208. +{
  12209. + *i = 0;
  12210. + return 0;
  12211. +}
  12212. +
  12213. +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  12214. +{
  12215. + if (i != 0)
  12216. + return -EINVAL;
  12217. +
  12218. + return 0;
  12219. +}
  12220. +
  12221. +/* capture ioctls */
  12222. +static int vidioc_querycap(struct file *file, void *priv,
  12223. + struct v4l2_capability *cap)
  12224. +{
  12225. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12226. + u32 major;
  12227. + u32 minor;
  12228. +
  12229. + vchiq_mmal_version(dev->instance, &major, &minor);
  12230. +
  12231. + strcpy(cap->driver, "bm2835 mmal");
  12232. + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
  12233. + major, minor);
  12234. +
  12235. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  12236. + "platform:%s", dev->v4l2_dev.name);
  12237. + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
  12238. + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  12239. + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  12240. +
  12241. + return 0;
  12242. +}
  12243. +
  12244. +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  12245. + struct v4l2_fmtdesc *f)
  12246. +{
  12247. + struct mmal_fmt *fmt;
  12248. +
  12249. + if (f->index >= ARRAY_SIZE(formats))
  12250. + return -EINVAL;
  12251. +
  12252. + fmt = &formats[f->index];
  12253. +
  12254. + strlcpy(f->description, fmt->name, sizeof(f->description));
  12255. + f->pixelformat = fmt->fourcc;
  12256. + f->flags = fmt->flags;
  12257. +
  12258. + return 0;
  12259. +}
  12260. +
  12261. +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  12262. + struct v4l2_format *f)
  12263. +{
  12264. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12265. +
  12266. + f->fmt.pix.width = dev->capture.width;
  12267. + f->fmt.pix.height = dev->capture.height;
  12268. + f->fmt.pix.field = V4L2_FIELD_NONE;
  12269. + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
  12270. + f->fmt.pix.bytesperline =
  12271. + (f->fmt.pix.width * dev->capture.fmt->depth) >> 3;
  12272. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  12273. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG
  12274. + && f->fmt.pix.sizeimage < (100 << 10)) {
  12275. + /* Need a minimum size for JPEG to account for EXIF. */
  12276. + f->fmt.pix.sizeimage = (100 << 10);
  12277. + }
  12278. +
  12279. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_YUYV ||
  12280. + dev->capture.fmt->fourcc == V4L2_PIX_FMT_UYVY)
  12281. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  12282. + else
  12283. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  12284. + f->fmt.pix.priv = 0;
  12285. +
  12286. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  12287. + __func__);
  12288. + return 0;
  12289. +}
  12290. +
  12291. +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  12292. + struct v4l2_format *f)
  12293. +{
  12294. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12295. + struct mmal_fmt *mfmt;
  12296. +
  12297. + mfmt = get_format(f);
  12298. + if (!mfmt) {
  12299. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12300. + "Fourcc format (0x%08x) unknown.\n",
  12301. + f->fmt.pix.pixelformat);
  12302. + f->fmt.pix.pixelformat = formats[0].fourcc;
  12303. + mfmt = get_format(f);
  12304. + }
  12305. +
  12306. + f->fmt.pix.field = V4L2_FIELD_NONE;
  12307. + /* image must be a multiple of 32 pixels wide and 16 lines high */
  12308. + v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 5,
  12309. + &f->fmt.pix.height, 32, MAX_HEIGHT, 4, 0);
  12310. + f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth) >> 3;
  12311. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  12312. + if (f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
  12313. + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
  12314. +
  12315. + if (mfmt->fourcc == V4L2_PIX_FMT_YUYV ||
  12316. + mfmt->fourcc == V4L2_PIX_FMT_UYVY)
  12317. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  12318. + else
  12319. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  12320. + f->fmt.pix.priv = 0;
  12321. +
  12322. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  12323. + __func__);
  12324. + return 0;
  12325. +}
  12326. +
  12327. +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
  12328. + struct v4l2_format *f)
  12329. +{
  12330. + int ret;
  12331. + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
  12332. + struct vchiq_mmal_component *encode_component = NULL;
  12333. + struct mmal_fmt *mfmt = get_format(f);
  12334. +
  12335. + BUG_ON(!mfmt);
  12336. +
  12337. + if (dev->capture.encode_component) {
  12338. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12339. + "vid_cap - disconnect previous tunnel\n");
  12340. +
  12341. + /* Disconnect any previous connection */
  12342. + vchiq_mmal_port_connect_tunnel(dev->instance,
  12343. + dev->capture.camera_port, NULL);
  12344. + dev->capture.camera_port = NULL;
  12345. + ret = vchiq_mmal_component_disable(dev->instance,
  12346. + dev->capture.
  12347. + encode_component);
  12348. + if (ret)
  12349. + v4l2_err(&dev->v4l2_dev,
  12350. + "Failed to disable encode component %d\n",
  12351. + ret);
  12352. +
  12353. + dev->capture.encode_component = NULL;
  12354. + }
  12355. + /* format dependant port setup */
  12356. + switch (mfmt->mmal_component) {
  12357. + case MMAL_COMPONENT_CAMERA:
  12358. + /* Make a further decision on port based on resolution */
  12359. + if ((f->fmt.pix.width*f->fmt.pix.height) <=
  12360. + (MAX_VIDEO_MODE_WIDTH * MAX_VIDEO_MODE_HEIGHT))
  12361. + camera_port = port =
  12362. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12363. + output[MMAL_CAMERA_PORT_VIDEO];
  12364. + else
  12365. + camera_port = port =
  12366. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12367. + output[MMAL_CAMERA_PORT_CAPTURE];
  12368. + break;
  12369. + case MMAL_COMPONENT_IMAGE_ENCODE:
  12370. + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
  12371. + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  12372. + camera_port =
  12373. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12374. + output[MMAL_CAMERA_PORT_CAPTURE];
  12375. + break;
  12376. + case MMAL_COMPONENT_VIDEO_ENCODE:
  12377. + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
  12378. + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  12379. + camera_port =
  12380. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12381. + output[MMAL_CAMERA_PORT_VIDEO];
  12382. + break;
  12383. + default:
  12384. + break;
  12385. + }
  12386. +
  12387. + if (!port)
  12388. + return -EINVAL;
  12389. +
  12390. + if (encode_component)
  12391. + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
  12392. + else
  12393. + camera_port->format.encoding = mfmt->mmal;
  12394. +
  12395. + camera_port->format.encoding_variant = 0;
  12396. + camera_port->es.video.width = f->fmt.pix.width;
  12397. + camera_port->es.video.height = f->fmt.pix.height;
  12398. + camera_port->es.video.crop.x = 0;
  12399. + camera_port->es.video.crop.y = 0;
  12400. + camera_port->es.video.crop.width = f->fmt.pix.width;
  12401. + camera_port->es.video.crop.height = f->fmt.pix.height;
  12402. + camera_port->es.video.frame_rate.num = 0;
  12403. + camera_port->es.video.frame_rate.den = 1;
  12404. +
  12405. + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
  12406. +
  12407. + if (!ret
  12408. + && camera_port ==
  12409. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12410. + output[MMAL_CAMERA_PORT_VIDEO]) {
  12411. + bool overlay_enabled =
  12412. + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
  12413. + struct vchiq_mmal_port *preview_port =
  12414. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12415. + output[MMAL_CAMERA_PORT_PREVIEW];
  12416. + /* Preview and encode ports need to match on resolution */
  12417. + if (overlay_enabled) {
  12418. + /* Need to disable the overlay before we can update
  12419. + * the resolution
  12420. + */
  12421. + ret =
  12422. + vchiq_mmal_port_disable(dev->instance,
  12423. + preview_port);
  12424. + if (!ret)
  12425. + ret =
  12426. + vchiq_mmal_port_connect_tunnel(
  12427. + dev->instance,
  12428. + preview_port,
  12429. + NULL);
  12430. + }
  12431. + preview_port->es.video.width = f->fmt.pix.width;
  12432. + preview_port->es.video.height = f->fmt.pix.height;
  12433. + preview_port->es.video.crop.x = 0;
  12434. + preview_port->es.video.crop.y = 0;
  12435. + preview_port->es.video.crop.width = f->fmt.pix.width;
  12436. + preview_port->es.video.crop.height = f->fmt.pix.height;
  12437. + preview_port->es.video.frame_rate.num =
  12438. + dev->capture.timeperframe.denominator;
  12439. + preview_port->es.video.frame_rate.den =
  12440. + dev->capture.timeperframe.numerator;
  12441. + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
  12442. + if (overlay_enabled) {
  12443. + ret = vchiq_mmal_port_connect_tunnel(
  12444. + dev->instance,
  12445. + preview_port,
  12446. + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
  12447. + if (!ret)
  12448. + ret = vchiq_mmal_port_enable(dev->instance,
  12449. + preview_port,
  12450. + NULL);
  12451. + }
  12452. + }
  12453. +
  12454. + if (ret) {
  12455. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12456. + "%s failed to set format\n", __func__);
  12457. + /* ensure capture is not going to be tried */
  12458. + dev->capture.port = NULL;
  12459. + } else {
  12460. + if (encode_component) {
  12461. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12462. + "vid_cap - set up encode comp\n");
  12463. +
  12464. + /* configure buffering */
  12465. + camera_port->current_buffer.size =
  12466. + camera_port->recommended_buffer.size;
  12467. + camera_port->current_buffer.num =
  12468. + camera_port->recommended_buffer.num;
  12469. +
  12470. + ret =
  12471. + vchiq_mmal_port_connect_tunnel(
  12472. + dev->instance,
  12473. + camera_port,
  12474. + &encode_component->input[0]);
  12475. + if (ret) {
  12476. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12477. + &dev->v4l2_dev,
  12478. + "%s failed to create connection\n",
  12479. + __func__);
  12480. + /* ensure capture is not going to be tried */
  12481. + dev->capture.port = NULL;
  12482. + } else {
  12483. + port->es.video.width = f->fmt.pix.width;
  12484. + port->es.video.height = f->fmt.pix.height;
  12485. + port->es.video.crop.x = 0;
  12486. + port->es.video.crop.y = 0;
  12487. + port->es.video.crop.width = f->fmt.pix.width;
  12488. + port->es.video.crop.height = f->fmt.pix.height;
  12489. + port->es.video.frame_rate.num =
  12490. + dev->capture.timeperframe.denominator;
  12491. + port->es.video.frame_rate.den =
  12492. + dev->capture.timeperframe.numerator;
  12493. +
  12494. + port->format.encoding = mfmt->mmal;
  12495. + port->format.encoding_variant = 0;
  12496. + /* Set any encoding specific parameters */
  12497. + switch (mfmt->mmal_component) {
  12498. + case MMAL_COMPONENT_VIDEO_ENCODE:
  12499. + port->format.bitrate =
  12500. + dev->capture.encode_bitrate;
  12501. + break;
  12502. + case MMAL_COMPONENT_IMAGE_ENCODE:
  12503. + /* Could set EXIF parameters here */
  12504. + break;
  12505. + default:
  12506. + break;
  12507. + }
  12508. + ret = vchiq_mmal_port_set_format(dev->instance,
  12509. + port);
  12510. + if (ret)
  12511. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12512. + &dev->v4l2_dev,
  12513. + "%s failed to set format\n",
  12514. + __func__);
  12515. + }
  12516. +
  12517. + if (!ret) {
  12518. + ret = vchiq_mmal_component_enable(
  12519. + dev->instance,
  12520. + encode_component);
  12521. + if (ret) {
  12522. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12523. + &dev->v4l2_dev,
  12524. + "%s Failed to enable encode components\n",
  12525. + __func__);
  12526. + }
  12527. + }
  12528. + if (!ret) {
  12529. + /* configure buffering */
  12530. + port->current_buffer.num = 1;
  12531. + port->current_buffer.size =
  12532. + f->fmt.pix.sizeimage;
  12533. + if (port->format.encoding ==
  12534. + MMAL_ENCODING_JPEG) {
  12535. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12536. + &dev->v4l2_dev,
  12537. + "JPG - buf size now %d was %d\n",
  12538. + f->fmt.pix.sizeimage,
  12539. + port->current_buffer.size);
  12540. + port->current_buffer.size =
  12541. + (f->fmt.pix.sizeimage <
  12542. + (100 << 10))
  12543. + ? (100 << 10) : f->fmt.pix.
  12544. + sizeimage;
  12545. + }
  12546. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12547. + &dev->v4l2_dev,
  12548. + "vid_cap - cur_buf.size set to %d\n",
  12549. + f->fmt.pix.sizeimage);
  12550. + port->current_buffer.alignment = 0;
  12551. + }
  12552. + } else {
  12553. + /* configure buffering */
  12554. + camera_port->current_buffer.num = 1;
  12555. + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
  12556. + camera_port->current_buffer.alignment = 0;
  12557. + }
  12558. +
  12559. + if (!ret) {
  12560. + dev->capture.fmt = mfmt;
  12561. + dev->capture.stride = f->fmt.pix.bytesperline;
  12562. + dev->capture.width = camera_port->es.video.crop.width;
  12563. + dev->capture.height = camera_port->es.video.crop.height;
  12564. +
  12565. + /* select port for capture */
  12566. + dev->capture.port = port;
  12567. + dev->capture.camera_port = camera_port;
  12568. + dev->capture.encode_component = encode_component;
  12569. + v4l2_dbg(1, bcm2835_v4l2_debug,
  12570. + &dev->v4l2_dev,
  12571. + "Set dev->capture.fmt %08X, %dx%d, stride %d",
  12572. + port->format.encoding,
  12573. + dev->capture.width, dev->capture.height,
  12574. + dev->capture.stride);
  12575. + }
  12576. + }
  12577. +
  12578. + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
  12579. + return ret;
  12580. +}
  12581. +
  12582. +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  12583. + struct v4l2_format *f)
  12584. +{
  12585. + int ret;
  12586. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12587. + struct mmal_fmt *mfmt;
  12588. +
  12589. + /* try the format to set valid parameters */
  12590. + ret = vidioc_try_fmt_vid_cap(file, priv, f);
  12591. + if (ret) {
  12592. + v4l2_err(&dev->v4l2_dev,
  12593. + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
  12594. + return ret;
  12595. + }
  12596. +
  12597. + /* if a capture is running refuse to set format */
  12598. + if (vb2_is_busy(&dev->capture.vb_vidq)) {
  12599. + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
  12600. + return -EBUSY;
  12601. + }
  12602. +
  12603. + /* If the format is unsupported v4l2 says we should switch to
  12604. + * a supported one and not return an error. */
  12605. + mfmt = get_format(f);
  12606. + if (!mfmt) {
  12607. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  12608. + "Fourcc format (0x%08x) unknown.\n",
  12609. + f->fmt.pix.pixelformat);
  12610. + f->fmt.pix.pixelformat = formats[0].fourcc;
  12611. + mfmt = get_format(f);
  12612. + }
  12613. +
  12614. + ret = mmal_setup_components(dev, f);
  12615. + if (ret != 0) {
  12616. + v4l2_err(&dev->v4l2_dev,
  12617. + "%s: failed to setup mmal components: %d\n",
  12618. + __func__, ret);
  12619. + ret = -EINVAL;
  12620. + }
  12621. +
  12622. + return ret;
  12623. +}
  12624. +
  12625. +int vidioc_enum_framesizes(struct file *file, void *fh,
  12626. + struct v4l2_frmsizeenum *fsize)
  12627. +{
  12628. + static const struct v4l2_frmsize_stepwise sizes = {
  12629. + MIN_WIDTH, MAX_WIDTH, 2,
  12630. + MIN_HEIGHT, MAX_HEIGHT, 2
  12631. + };
  12632. + int i;
  12633. +
  12634. + if (fsize->index)
  12635. + return -EINVAL;
  12636. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  12637. + if (formats[i].fourcc == fsize->pixel_format)
  12638. + break;
  12639. + if (i == ARRAY_SIZE(formats))
  12640. + return -EINVAL;
  12641. + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  12642. + fsize->stepwise = sizes;
  12643. + return 0;
  12644. +}
  12645. +
  12646. +/* timeperframe is arbitrary and continous */
  12647. +static int vidioc_enum_frameintervals(struct file *file, void *priv,
  12648. + struct v4l2_frmivalenum *fival)
  12649. +{
  12650. + int i;
  12651. +
  12652. + if (fival->index)
  12653. + return -EINVAL;
  12654. +
  12655. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  12656. + if (formats[i].fourcc == fival->pixel_format)
  12657. + break;
  12658. + if (i == ARRAY_SIZE(formats))
  12659. + return -EINVAL;
  12660. +
  12661. + /* regarding width & height - we support any within range */
  12662. + if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH ||
  12663. + fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT)
  12664. + return -EINVAL;
  12665. +
  12666. + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  12667. +
  12668. + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
  12669. + fival->stepwise.min = tpf_min;
  12670. + fival->stepwise.max = tpf_max;
  12671. + fival->stepwise.step = (struct v4l2_fract) {1, 1};
  12672. +
  12673. + return 0;
  12674. +}
  12675. +
  12676. +static int vidioc_g_parm(struct file *file, void *priv,
  12677. + struct v4l2_streamparm *parm)
  12678. +{
  12679. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12680. +
  12681. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  12682. + return -EINVAL;
  12683. +
  12684. + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  12685. + parm->parm.capture.timeperframe = dev->capture.timeperframe;
  12686. + parm->parm.capture.readbuffers = 1;
  12687. + return 0;
  12688. +}
  12689. +
  12690. +#define FRACT_CMP(a, OP, b) \
  12691. + ((u64)(a).numerator * (b).denominator OP \
  12692. + (u64)(b).numerator * (a).denominator)
  12693. +
  12694. +static int vidioc_s_parm(struct file *file, void *priv,
  12695. + struct v4l2_streamparm *parm)
  12696. +{
  12697. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  12698. + struct v4l2_fract tpf;
  12699. + struct mmal_parameter_rational fps_param;
  12700. +
  12701. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  12702. + return -EINVAL;
  12703. +
  12704. + tpf = parm->parm.capture.timeperframe;
  12705. +
  12706. + /* tpf: {*, 0} resets timing; clip to [min, max]*/
  12707. + tpf = tpf.denominator ? tpf : tpf_default;
  12708. + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  12709. + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  12710. +
  12711. + dev->capture.timeperframe = tpf;
  12712. + parm->parm.capture.timeperframe = tpf;
  12713. + parm->parm.capture.readbuffers = 1;
  12714. +
  12715. + fps_param.num = 0; /* Select variable fps, and then use
  12716. + * FPS_RANGE to select the actual limits.
  12717. + */
  12718. + fps_param.den = 1;
  12719. + set_framerate_params(dev);
  12720. +
  12721. + return 0;
  12722. +}
  12723. +
  12724. +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
  12725. + /* overlay */
  12726. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  12727. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  12728. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  12729. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  12730. + .vidioc_overlay = vidioc_overlay,
  12731. + .vidioc_g_fbuf = vidioc_g_fbuf,
  12732. +
  12733. + /* inputs */
  12734. + .vidioc_enum_input = vidioc_enum_input,
  12735. + .vidioc_g_input = vidioc_g_input,
  12736. + .vidioc_s_input = vidioc_s_input,
  12737. +
  12738. + /* capture */
  12739. + .vidioc_querycap = vidioc_querycap,
  12740. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  12741. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  12742. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  12743. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  12744. +
  12745. + /* buffer management */
  12746. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  12747. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  12748. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  12749. + .vidioc_querybuf = vb2_ioctl_querybuf,
  12750. + .vidioc_qbuf = vb2_ioctl_qbuf,
  12751. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  12752. + .vidioc_enum_framesizes = vidioc_enum_framesizes,
  12753. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  12754. + .vidioc_g_parm = vidioc_g_parm,
  12755. + .vidioc_s_parm = vidioc_s_parm,
  12756. + .vidioc_streamon = vb2_ioctl_streamon,
  12757. + .vidioc_streamoff = vb2_ioctl_streamoff,
  12758. +
  12759. + .vidioc_log_status = v4l2_ctrl_log_status,
  12760. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  12761. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  12762. +};
  12763. +
  12764. +/* ------------------------------------------------------------------
  12765. + Driver init/finalise
  12766. + ------------------------------------------------------------------*/
  12767. +
  12768. +static const struct v4l2_file_operations camera0_fops = {
  12769. + .owner = THIS_MODULE,
  12770. + .open = v4l2_fh_open,
  12771. + .release = vb2_fop_release,
  12772. + .read = vb2_fop_read,
  12773. + .poll = vb2_fop_poll,
  12774. + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  12775. + .mmap = vb2_fop_mmap,
  12776. +};
  12777. +
  12778. +static struct video_device vdev_template = {
  12779. + .name = "camera0",
  12780. + .fops = &camera0_fops,
  12781. + .ioctl_ops = &camera0_ioctl_ops,
  12782. + .release = video_device_release_empty,
  12783. +};
  12784. +
  12785. +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
  12786. + struct vchiq_mmal_component *camera)
  12787. +{
  12788. + int ret;
  12789. + struct mmal_parameter_camera_config cam_config = {
  12790. + .max_stills_w = MAX_WIDTH,
  12791. + .max_stills_h = MAX_HEIGHT,
  12792. + .stills_yuv422 = 1,
  12793. + .one_shot_stills = 1,
  12794. + .max_preview_video_w = 1920,
  12795. + .max_preview_video_h = 1088,
  12796. + .num_preview_video_frames = 6,
  12797. + .stills_capture_circular_buffer_height = 0,
  12798. + .fast_preview_resume = 0,
  12799. + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
  12800. + };
  12801. +
  12802. + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
  12803. + MMAL_PARAMETER_CAMERA_CONFIG,
  12804. + &cam_config, sizeof(cam_config));
  12805. + return ret;
  12806. +}
  12807. +
  12808. +/* MMAL instance and component init */
  12809. +static int __init mmal_init(struct bm2835_mmal_dev *dev)
  12810. +{
  12811. + int ret;
  12812. + struct mmal_es_format *format;
  12813. +
  12814. + ret = vchiq_mmal_init(&dev->instance);
  12815. + if (ret < 0)
  12816. + return ret;
  12817. +
  12818. + /* get the camera component ready */
  12819. + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
  12820. + &dev->component[MMAL_COMPONENT_CAMERA]);
  12821. + if (ret < 0)
  12822. + goto unreg_mmal;
  12823. +
  12824. + if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  12825. + MMAL_CAMERA_PORT_COUNT) {
  12826. + ret = -EINVAL;
  12827. + goto unreg_camera;
  12828. + }
  12829. +
  12830. + ret = set_camera_parameters(dev->instance,
  12831. + dev->component[MMAL_COMPONENT_CAMERA]);
  12832. + if (ret < 0)
  12833. + goto unreg_camera;
  12834. +
  12835. + format =
  12836. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12837. + output[MMAL_CAMERA_PORT_PREVIEW].format;
  12838. +
  12839. + format->encoding = MMAL_ENCODING_OPAQUE;
  12840. + format->encoding_variant = MMAL_ENCODING_I420;
  12841. +
  12842. + format->es->video.width = 1024;
  12843. + format->es->video.height = 768;
  12844. + format->es->video.crop.x = 0;
  12845. + format->es->video.crop.y = 0;
  12846. + format->es->video.crop.width = 1024;
  12847. + format->es->video.crop.height = 768;
  12848. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  12849. + format->es->video.frame_rate.den = 1;
  12850. +
  12851. + format =
  12852. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12853. + output[MMAL_CAMERA_PORT_VIDEO].format;
  12854. +
  12855. + format->encoding = MMAL_ENCODING_OPAQUE;
  12856. + format->encoding_variant = MMAL_ENCODING_I420;
  12857. +
  12858. + format->es->video.width = 1024;
  12859. + format->es->video.height = 768;
  12860. + format->es->video.crop.x = 0;
  12861. + format->es->video.crop.y = 0;
  12862. + format->es->video.crop.width = 1024;
  12863. + format->es->video.crop.height = 768;
  12864. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  12865. + format->es->video.frame_rate.den = 1;
  12866. +
  12867. + format =
  12868. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12869. + output[MMAL_CAMERA_PORT_CAPTURE].format;
  12870. +
  12871. + format->encoding = MMAL_ENCODING_OPAQUE;
  12872. +
  12873. + format->es->video.width = 2592;
  12874. + format->es->video.height = 1944;
  12875. + format->es->video.crop.x = 0;
  12876. + format->es->video.crop.y = 0;
  12877. + format->es->video.crop.width = 2592;
  12878. + format->es->video.crop.height = 1944;
  12879. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  12880. + format->es->video.frame_rate.den = 1;
  12881. +
  12882. + dev->capture.width = format->es->video.width;
  12883. + dev->capture.height = format->es->video.height;
  12884. + dev->capture.fmt = &formats[0];
  12885. + dev->capture.encode_component = NULL;
  12886. + dev->capture.timeperframe = tpf_default;
  12887. + dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  12888. + dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  12889. +
  12890. + /* get the preview component ready */
  12891. + ret = vchiq_mmal_component_init(
  12892. + dev->instance, "ril.video_render",
  12893. + &dev->component[MMAL_COMPONENT_PREVIEW]);
  12894. + if (ret < 0)
  12895. + goto unreg_camera;
  12896. +
  12897. + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
  12898. + ret = -EINVAL;
  12899. + pr_debug("too few input ports %d needed %d\n",
  12900. + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
  12901. + goto unreg_preview;
  12902. + }
  12903. +
  12904. + /* get the image encoder component ready */
  12905. + ret = vchiq_mmal_component_init(
  12906. + dev->instance, "ril.image_encode",
  12907. + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  12908. + if (ret < 0)
  12909. + goto unreg_preview;
  12910. +
  12911. + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
  12912. + ret = -EINVAL;
  12913. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  12914. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
  12915. + 1);
  12916. + goto unreg_image_encoder;
  12917. + }
  12918. +
  12919. + /* get the video encoder component ready */
  12920. + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
  12921. + &dev->
  12922. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  12923. + if (ret < 0)
  12924. + goto unreg_image_encoder;
  12925. +
  12926. + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
  12927. + ret = -EINVAL;
  12928. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  12929. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
  12930. + 1);
  12931. + goto unreg_vid_encoder;
  12932. + }
  12933. +
  12934. + {
  12935. + struct vchiq_mmal_port *encoder_port =
  12936. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  12937. + encoder_port->format.encoding = MMAL_ENCODING_H264;
  12938. + ret = vchiq_mmal_port_set_format(dev->instance,
  12939. + encoder_port);
  12940. + }
  12941. +
  12942. + {
  12943. + unsigned int enable = 1;
  12944. + vchiq_mmal_port_parameter_set(
  12945. + dev->instance,
  12946. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  12947. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  12948. + &enable, sizeof(enable));
  12949. +
  12950. + vchiq_mmal_port_parameter_set(dev->instance,
  12951. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  12952. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  12953. + &enable,
  12954. + sizeof(enable));
  12955. + }
  12956. + ret = bm2835_mmal_set_all_camera_controls(dev);
  12957. + if (ret < 0)
  12958. + goto unreg_vid_encoder;
  12959. +
  12960. + return 0;
  12961. +
  12962. +unreg_vid_encoder:
  12963. + pr_err("Cleanup: Destroy video encoder\n");
  12964. + vchiq_mmal_component_finalise(
  12965. + dev->instance,
  12966. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
  12967. +
  12968. +unreg_image_encoder:
  12969. + pr_err("Cleanup: Destroy image encoder\n");
  12970. + vchiq_mmal_component_finalise(
  12971. + dev->instance,
  12972. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  12973. +
  12974. +unreg_preview:
  12975. + pr_err("Cleanup: Destroy video render\n");
  12976. + vchiq_mmal_component_finalise(dev->instance,
  12977. + dev->component[MMAL_COMPONENT_PREVIEW]);
  12978. +
  12979. +unreg_camera:
  12980. + pr_err("Cleanup: Destroy camera\n");
  12981. + vchiq_mmal_component_finalise(dev->instance,
  12982. + dev->component[MMAL_COMPONENT_CAMERA]);
  12983. +
  12984. +unreg_mmal:
  12985. + vchiq_mmal_finalise(dev->instance);
  12986. + return ret;
  12987. +}
  12988. +
  12989. +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
  12990. + struct video_device *vfd)
  12991. +{
  12992. + int ret;
  12993. +
  12994. + *vfd = vdev_template;
  12995. +
  12996. + vfd->v4l2_dev = &dev->v4l2_dev;
  12997. +
  12998. + vfd->lock = &dev->mutex;
  12999. +
  13000. + vfd->queue = &dev->capture.vb_vidq;
  13001. +
  13002. + set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  13003. +
  13004. + /* video device needs to be able to access instance data */
  13005. + video_set_drvdata(vfd, dev);
  13006. +
  13007. + ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  13008. + if (ret < 0)
  13009. + return ret;
  13010. +
  13011. + v4l2_info(vfd->v4l2_dev, "V4L2 device registered as %s\n",
  13012. + video_device_node_name(vfd));
  13013. +
  13014. + return 0;
  13015. +}
  13016. +
  13017. +static struct v4l2_format default_v4l2_format = {
  13018. + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
  13019. + .fmt.pix.width = 1024,
  13020. + .fmt.pix.bytesperline = 1024 * 3 / 2,
  13021. + .fmt.pix.height = 768,
  13022. + .fmt.pix.sizeimage = 1<<18,
  13023. +};
  13024. +
  13025. +static int __init bm2835_mmal_init(void)
  13026. +{
  13027. + int ret;
  13028. + struct bm2835_mmal_dev *dev;
  13029. + struct vb2_queue *q;
  13030. +
  13031. + dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  13032. + if (!dev)
  13033. + return -ENOMEM;
  13034. +
  13035. + /* setup device defaults */
  13036. + dev->overlay.w.left = 150;
  13037. + dev->overlay.w.top = 50;
  13038. + dev->overlay.w.width = 1024;
  13039. + dev->overlay.w.height = 768;
  13040. + dev->overlay.clipcount = 0;
  13041. + dev->overlay.field = V4L2_FIELD_NONE;
  13042. +
  13043. + dev->capture.fmt = &formats[3]; /* JPEG */
  13044. +
  13045. + /* v4l device registration */
  13046. + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  13047. + "%s", BM2835_MMAL_MODULE_NAME);
  13048. + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  13049. + if (ret)
  13050. + goto free_dev;
  13051. +
  13052. + /* setup v4l controls */
  13053. + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
  13054. + if (ret < 0)
  13055. + goto unreg_dev;
  13056. + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  13057. +
  13058. + /* mmal init */
  13059. + ret = mmal_init(dev);
  13060. + if (ret < 0)
  13061. + goto unreg_dev;
  13062. +
  13063. + /* initialize queue */
  13064. + q = &dev->capture.vb_vidq;
  13065. + memset(q, 0, sizeof(*q));
  13066. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  13067. + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  13068. + q->drv_priv = dev;
  13069. + q->buf_struct_size = sizeof(struct mmal_buffer);
  13070. + q->ops = &bm2835_mmal_video_qops;
  13071. + q->mem_ops = &vb2_vmalloc_memops;
  13072. + q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  13073. + ret = vb2_queue_init(q);
  13074. + if (ret < 0)
  13075. + goto unreg_dev;
  13076. +
  13077. + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
  13078. + mutex_init(&dev->mutex);
  13079. +
  13080. + /* initialise video devices */
  13081. + ret = bm2835_mmal_init_device(dev, &dev->vdev);
  13082. + if (ret < 0)
  13083. + goto unreg_dev;
  13084. +
  13085. + ret = mmal_setup_components(dev, &default_v4l2_format);
  13086. + if (ret < 0) {
  13087. + v4l2_err(&dev->v4l2_dev,
  13088. + "%s: could not setup components\n", __func__);
  13089. + goto unreg_dev;
  13090. + }
  13091. +
  13092. + v4l2_info(&dev->v4l2_dev,
  13093. + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
  13094. + BM2835_MMAL_VERSION);
  13095. +
  13096. + gdev = dev;
  13097. + return 0;
  13098. +
  13099. +unreg_dev:
  13100. + v4l2_ctrl_handler_free(&dev->ctrl_handler);
  13101. + v4l2_device_unregister(&dev->v4l2_dev);
  13102. +
  13103. +free_dev:
  13104. + kfree(dev);
  13105. +
  13106. + v4l2_err(&dev->v4l2_dev,
  13107. + "%s: error %d while loading driver\n",
  13108. + BM2835_MMAL_MODULE_NAME, ret);
  13109. +
  13110. + return ret;
  13111. +}
  13112. +
  13113. +static void __exit bm2835_mmal_exit(void)
  13114. +{
  13115. + if (!gdev)
  13116. + return;
  13117. +
  13118. + v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
  13119. + video_device_node_name(&gdev->vdev));
  13120. +
  13121. + video_unregister_device(&gdev->vdev);
  13122. +
  13123. + if (gdev->capture.encode_component) {
  13124. + v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
  13125. + "mmal_exit - disconnect tunnel\n");
  13126. + vchiq_mmal_port_connect_tunnel(gdev->instance,
  13127. + gdev->capture.camera_port, NULL);
  13128. + vchiq_mmal_component_disable(gdev->instance,
  13129. + gdev->capture.encode_component);
  13130. + }
  13131. + vchiq_mmal_component_disable(gdev->instance,
  13132. + gdev->component[MMAL_COMPONENT_CAMERA]);
  13133. +
  13134. + vchiq_mmal_component_finalise(gdev->instance,
  13135. + gdev->
  13136. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  13137. +
  13138. + vchiq_mmal_component_finalise(gdev->instance,
  13139. + gdev->
  13140. + component[MMAL_COMPONENT_IMAGE_ENCODE]);
  13141. +
  13142. + vchiq_mmal_component_finalise(gdev->instance,
  13143. + gdev->component[MMAL_COMPONENT_PREVIEW]);
  13144. +
  13145. + vchiq_mmal_component_finalise(gdev->instance,
  13146. + gdev->component[MMAL_COMPONENT_CAMERA]);
  13147. +
  13148. + vchiq_mmal_finalise(gdev->instance);
  13149. +
  13150. + v4l2_ctrl_handler_free(&gdev->ctrl_handler);
  13151. +
  13152. + v4l2_device_unregister(&gdev->v4l2_dev);
  13153. +
  13154. + kfree(gdev);
  13155. +}
  13156. +
  13157. +module_init(bm2835_mmal_init);
  13158. +module_exit(bm2835_mmal_exit);
  13159. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/bcm2835-camera.h linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h
  13160. --- linux-3.10.37/drivers/media/platform/bcm2835/bcm2835-camera.h 1970-01-01 01:00:00.000000000 +0100
  13161. +++ linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h 2014-04-24 15:35:02.713549510 +0200
  13162. @@ -0,0 +1,125 @@
  13163. +/*
  13164. + * Broadcom BM2835 V4L2 driver
  13165. + *
  13166. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  13167. + *
  13168. + * This file is subject to the terms and conditions of the GNU General Public
  13169. + * License. See the file COPYING in the main directory of this archive
  13170. + * for more details.
  13171. + *
  13172. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  13173. + * Dave Stevenson <dsteve@broadcom.com>
  13174. + * Simon Mellor <simellor@broadcom.com>
  13175. + * Luke Diamand <luked@broadcom.com>
  13176. + *
  13177. + * core driver device
  13178. + */
  13179. +
  13180. +#define V4L2_CTRL_COUNT 27 /* number of v4l controls */
  13181. +
  13182. +enum {
  13183. + MMAL_COMPONENT_CAMERA = 0,
  13184. + MMAL_COMPONENT_PREVIEW,
  13185. + MMAL_COMPONENT_IMAGE_ENCODE,
  13186. + MMAL_COMPONENT_VIDEO_ENCODE,
  13187. + MMAL_COMPONENT_COUNT
  13188. +};
  13189. +
  13190. +enum {
  13191. + MMAL_CAMERA_PORT_PREVIEW = 0,
  13192. + MMAL_CAMERA_PORT_VIDEO,
  13193. + MMAL_CAMERA_PORT_CAPTURE,
  13194. + MMAL_CAMERA_PORT_COUNT
  13195. +};
  13196. +
  13197. +#define PREVIEW_LAYER 2
  13198. +
  13199. +extern int bcm2835_v4l2_debug;
  13200. +
  13201. +struct bm2835_mmal_dev {
  13202. + /* v4l2 devices */
  13203. + struct v4l2_device v4l2_dev;
  13204. + struct video_device vdev;
  13205. + struct mutex mutex;
  13206. +
  13207. + /* controls */
  13208. + struct v4l2_ctrl_handler ctrl_handler;
  13209. + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
  13210. + enum v4l2_scene_mode scene_mode;
  13211. + struct mmal_colourfx colourfx;
  13212. + int hflip;
  13213. + int vflip;
  13214. + int red_gain;
  13215. + int blue_gain;
  13216. + enum mmal_parameter_exposuremode exposure_mode_user;
  13217. + enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
  13218. + /* active exposure mode may differ if selected via a scene mode */
  13219. + enum mmal_parameter_exposuremode exposure_mode_active;
  13220. + enum mmal_parameter_exposuremeteringmode metering_mode;
  13221. + unsigned int manual_shutter_speed;
  13222. + bool exp_auto_priority;
  13223. +
  13224. + /* allocated mmal instance and components */
  13225. + struct vchiq_mmal_instance *instance;
  13226. + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
  13227. + int camera_use_count;
  13228. +
  13229. + struct v4l2_window overlay;
  13230. +
  13231. + struct {
  13232. + unsigned int width; /* width */
  13233. + unsigned int height; /* height */
  13234. + unsigned int stride; /* stride */
  13235. + struct mmal_fmt *fmt;
  13236. + struct v4l2_fract timeperframe;
  13237. +
  13238. + /* H264 encode bitrate */
  13239. + int encode_bitrate;
  13240. + /* H264 bitrate mode. CBR/VBR */
  13241. + int encode_bitrate_mode;
  13242. + /* H264 profile */
  13243. + enum v4l2_mpeg_video_h264_profile enc_profile;
  13244. + /* H264 level */
  13245. + enum v4l2_mpeg_video_h264_level enc_level;
  13246. + /* JPEG Q-factor */
  13247. + int q_factor;
  13248. +
  13249. + struct vb2_queue vb_vidq;
  13250. +
  13251. + /* VC start timestamp for streaming */
  13252. + s64 vc_start_timestamp;
  13253. + /* Kernel start timestamp for streaming */
  13254. + struct timeval kernel_start_ts;
  13255. +
  13256. + struct vchiq_mmal_port *port; /* port being used for capture */
  13257. + /* camera port being used for capture */
  13258. + struct vchiq_mmal_port *camera_port;
  13259. + /* component being used for encode */
  13260. + struct vchiq_mmal_component *encode_component;
  13261. + /* number of frames remaining which driver should capture */
  13262. + unsigned int frame_count;
  13263. + /* last frame completion */
  13264. + struct completion frame_cmplt;
  13265. +
  13266. + } capture;
  13267. +
  13268. +};
  13269. +
  13270. +int bm2835_mmal_init_controls(
  13271. + struct bm2835_mmal_dev *dev,
  13272. + struct v4l2_ctrl_handler *hdl);
  13273. +
  13274. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
  13275. +int set_framerate_params(struct bm2835_mmal_dev *dev);
  13276. +
  13277. +/* Debug helpers */
  13278. +
  13279. +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
  13280. +{ \
  13281. + v4l2_dbg(level, debug, dev, \
  13282. +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
  13283. + desc == NULL ? "" : desc, \
  13284. + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
  13285. + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
  13286. + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
  13287. +}
  13288. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/controls.c linux-rpi/drivers/media/platform/bcm2835/controls.c
  13289. --- linux-3.10.37/drivers/media/platform/bcm2835/controls.c 1970-01-01 01:00:00.000000000 +0100
  13290. +++ linux-rpi/drivers/media/platform/bcm2835/controls.c 2014-04-24 15:35:02.713549510 +0200
  13291. @@ -0,0 +1,1315 @@
  13292. +/*
  13293. + * Broadcom BM2835 V4L2 driver
  13294. + *
  13295. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  13296. + *
  13297. + * This file is subject to the terms and conditions of the GNU General Public
  13298. + * License. See the file COPYING in the main directory of this archive
  13299. + * for more details.
  13300. + *
  13301. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  13302. + * Dave Stevenson <dsteve@broadcom.com>
  13303. + * Simon Mellor <simellor@broadcom.com>
  13304. + * Luke Diamand <luked@broadcom.com>
  13305. + */
  13306. +
  13307. +#include <linux/errno.h>
  13308. +#include <linux/kernel.h>
  13309. +#include <linux/module.h>
  13310. +#include <linux/slab.h>
  13311. +#include <media/videobuf2-vmalloc.h>
  13312. +#include <media/v4l2-device.h>
  13313. +#include <media/v4l2-ioctl.h>
  13314. +#include <media/v4l2-ctrls.h>
  13315. +#include <media/v4l2-fh.h>
  13316. +#include <media/v4l2-event.h>
  13317. +#include <media/v4l2-common.h>
  13318. +
  13319. +#include "mmal-common.h"
  13320. +#include "mmal-vchiq.h"
  13321. +#include "mmal-parameters.h"
  13322. +#include "bcm2835-camera.h"
  13323. +
  13324. +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
  13325. + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
  13326. + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
  13327. + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
  13328. + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
  13329. + * -4 to +4
  13330. + */
  13331. +static const s64 ev_bias_qmenu[] = {
  13332. + -4000, -3667, -3333,
  13333. + -3000, -2667, -2333,
  13334. + -2000, -1667, -1333,
  13335. + -1000, -667, -333,
  13336. + 0, 333, 667,
  13337. + 1000, 1333, 1667,
  13338. + 2000, 2333, 2667,
  13339. + 3000, 3333, 3667,
  13340. + 4000
  13341. +};
  13342. +
  13343. +/* Supported ISO values
  13344. + * ISOO = auto ISO
  13345. + */
  13346. +static const s64 iso_qmenu[] = {
  13347. + 0, 100, 200, 400, 800,
  13348. +};
  13349. +
  13350. +static const s64 mains_freq_qmenu[] = {
  13351. + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  13352. + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
  13353. + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  13354. + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
  13355. +};
  13356. +
  13357. +/* Supported video encode modes */
  13358. +static const s64 bitrate_mode_qmenu[] = {
  13359. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  13360. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  13361. +};
  13362. +
  13363. +enum bm2835_mmal_ctrl_type {
  13364. + MMAL_CONTROL_TYPE_STD,
  13365. + MMAL_CONTROL_TYPE_STD_MENU,
  13366. + MMAL_CONTROL_TYPE_INT_MENU,
  13367. + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
  13368. +};
  13369. +
  13370. +struct bm2835_mmal_v4l2_ctrl;
  13371. +
  13372. +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
  13373. + struct bm2835_mmal_dev *dev,
  13374. + struct v4l2_ctrl *ctrl,
  13375. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
  13376. +
  13377. +struct bm2835_mmal_v4l2_ctrl {
  13378. + u32 id; /* v4l2 control identifier */
  13379. + enum bm2835_mmal_ctrl_type type;
  13380. + /* control minimum value or
  13381. + * mask for MMAL_CONTROL_TYPE_STD_MENU */
  13382. + s32 min;
  13383. + s32 max; /* maximum value of control */
  13384. + s32 def; /* default value of control */
  13385. + s32 step; /* step size of the control */
  13386. + const s64 *imenu; /* integer menu array */
  13387. + u32 mmal_id; /* mmal parameter id */
  13388. + bm2835_mmal_v4l2_ctrl_cb *setter;
  13389. + bool ignore_errors;
  13390. +};
  13391. +
  13392. +struct v4l2_to_mmal_effects_setting {
  13393. + u32 v4l2_effect;
  13394. + u32 mmal_effect;
  13395. + s32 col_fx_enable;
  13396. + s32 col_fx_fixed_cbcr;
  13397. + u32 u;
  13398. + u32 v;
  13399. + u32 num_effect_params;
  13400. + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
  13401. +};
  13402. +
  13403. +static const struct v4l2_to_mmal_effects_setting
  13404. + v4l2_to_mmal_effects_values[] = {
  13405. + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
  13406. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13407. + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
  13408. + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
  13409. + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
  13410. + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
  13411. + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
  13412. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13413. + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
  13414. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13415. + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
  13416. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13417. + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
  13418. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13419. + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  13420. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13421. + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
  13422. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13423. + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
  13424. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13425. + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
  13426. + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
  13427. + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
  13428. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13429. + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
  13430. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  13431. + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
  13432. + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
  13433. + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  13434. + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
  13435. + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
  13436. + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
  13437. +};
  13438. +
  13439. +struct v4l2_mmal_scene_config {
  13440. + enum v4l2_scene_mode v4l2_scene;
  13441. + enum mmal_parameter_exposuremode exposure_mode;
  13442. + enum mmal_parameter_exposuremeteringmode metering_mode;
  13443. +};
  13444. +
  13445. +static const struct v4l2_mmal_scene_config scene_configs[] = {
  13446. + /* V4L2_SCENE_MODE_NONE automatically added */
  13447. + {
  13448. + V4L2_SCENE_MODE_NIGHT,
  13449. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  13450. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  13451. + },
  13452. + {
  13453. + V4L2_SCENE_MODE_SPORTS,
  13454. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  13455. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  13456. + },
  13457. +};
  13458. +
  13459. +/* control handlers*/
  13460. +
  13461. +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
  13462. + struct v4l2_ctrl *ctrl,
  13463. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13464. +{
  13465. + struct mmal_parameter_rational rational_value;
  13466. + struct vchiq_mmal_port *control;
  13467. +
  13468. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13469. +
  13470. + rational_value.num = ctrl->val;
  13471. + rational_value.den = 100;
  13472. +
  13473. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13474. + mmal_ctrl->mmal_id,
  13475. + &rational_value,
  13476. + sizeof(rational_value));
  13477. +}
  13478. +
  13479. +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
  13480. + struct v4l2_ctrl *ctrl,
  13481. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13482. +{
  13483. + u32 u32_value;
  13484. + struct vchiq_mmal_port *control;
  13485. +
  13486. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13487. +
  13488. + u32_value = ctrl->val;
  13489. +
  13490. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13491. + mmal_ctrl->mmal_id,
  13492. + &u32_value, sizeof(u32_value));
  13493. +}
  13494. +
  13495. +static int ctrl_set_value_menu(struct bm2835_mmal_dev *dev,
  13496. + struct v4l2_ctrl *ctrl,
  13497. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13498. +{
  13499. + u32 u32_value;
  13500. + struct vchiq_mmal_port *control;
  13501. +
  13502. + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
  13503. + return 1;
  13504. +
  13505. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13506. +
  13507. + u32_value = mmal_ctrl->imenu[ctrl->val];
  13508. +
  13509. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13510. + mmal_ctrl->mmal_id,
  13511. + &u32_value, sizeof(u32_value));
  13512. +}
  13513. +
  13514. +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
  13515. + struct v4l2_ctrl *ctrl,
  13516. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13517. +{
  13518. + s32 s32_value;
  13519. + struct vchiq_mmal_port *control;
  13520. +
  13521. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13522. +
  13523. + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
  13524. +
  13525. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13526. + mmal_ctrl->mmal_id,
  13527. + &s32_value, sizeof(s32_value));
  13528. +}
  13529. +
  13530. +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
  13531. + struct v4l2_ctrl *ctrl,
  13532. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13533. +{
  13534. + int ret;
  13535. + u32 u32_value;
  13536. + struct vchiq_mmal_component *camera;
  13537. +
  13538. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  13539. +
  13540. + u32_value = ((ctrl->val % 360) / 90) * 90;
  13541. +
  13542. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  13543. + mmal_ctrl->mmal_id,
  13544. + &u32_value, sizeof(u32_value));
  13545. + if (ret < 0)
  13546. + return ret;
  13547. +
  13548. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  13549. + mmal_ctrl->mmal_id,
  13550. + &u32_value, sizeof(u32_value));
  13551. + if (ret < 0)
  13552. + return ret;
  13553. +
  13554. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  13555. + mmal_ctrl->mmal_id,
  13556. + &u32_value, sizeof(u32_value));
  13557. +
  13558. + return ret;
  13559. +}
  13560. +
  13561. +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
  13562. + struct v4l2_ctrl *ctrl,
  13563. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13564. +{
  13565. + int ret;
  13566. + u32 u32_value;
  13567. + struct vchiq_mmal_component *camera;
  13568. +
  13569. + if (ctrl->id == V4L2_CID_HFLIP)
  13570. + dev->hflip = ctrl->val;
  13571. + else
  13572. + dev->vflip = ctrl->val;
  13573. +
  13574. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  13575. +
  13576. + if (dev->hflip && dev->vflip)
  13577. + u32_value = MMAL_PARAM_MIRROR_BOTH;
  13578. + else if (dev->hflip)
  13579. + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
  13580. + else if (dev->vflip)
  13581. + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
  13582. + else
  13583. + u32_value = MMAL_PARAM_MIRROR_NONE;
  13584. +
  13585. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  13586. + mmal_ctrl->mmal_id,
  13587. + &u32_value, sizeof(u32_value));
  13588. + if (ret < 0)
  13589. + return ret;
  13590. +
  13591. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  13592. + mmal_ctrl->mmal_id,
  13593. + &u32_value, sizeof(u32_value));
  13594. + if (ret < 0)
  13595. + return ret;
  13596. +
  13597. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  13598. + mmal_ctrl->mmal_id,
  13599. + &u32_value, sizeof(u32_value));
  13600. +
  13601. + return ret;
  13602. +
  13603. +}
  13604. +
  13605. +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
  13606. + struct v4l2_ctrl *ctrl,
  13607. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13608. +{
  13609. + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode_user;
  13610. + u32 shutter_speed = 0;
  13611. + struct vchiq_mmal_port *control;
  13612. + int ret = 0;
  13613. +
  13614. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13615. +
  13616. + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
  13617. + /* V4L2 is in 100usec increments.
  13618. + * MMAL is 1usec.
  13619. + */
  13620. + dev->manual_shutter_speed = ctrl->val * 100;
  13621. + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
  13622. + switch (ctrl->val) {
  13623. + case V4L2_EXPOSURE_AUTO:
  13624. + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
  13625. + break;
  13626. +
  13627. + case V4L2_EXPOSURE_MANUAL:
  13628. + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
  13629. + break;
  13630. + }
  13631. + dev->exposure_mode_user = exp_mode;
  13632. + dev->exposure_mode_v4l2_user = ctrl->val;
  13633. + } else if (mmal_ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
  13634. + dev->exp_auto_priority = ctrl->val;
  13635. + }
  13636. +
  13637. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  13638. + if (exp_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  13639. + shutter_speed = dev->manual_shutter_speed;
  13640. +
  13641. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  13642. + control,
  13643. + MMAL_PARAMETER_SHUTTER_SPEED,
  13644. + &shutter_speed,
  13645. + sizeof(shutter_speed));
  13646. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13647. + control,
  13648. + MMAL_PARAMETER_EXPOSURE_MODE,
  13649. + &exp_mode,
  13650. + sizeof(u32));
  13651. + dev->exposure_mode_active = exp_mode;
  13652. + }
  13653. + /* exposure_dynamic_framerate (V4L2_CID_EXPOSURE_AUTO_PRIORITY) should
  13654. + * always apply irrespective of scene mode.
  13655. + */
  13656. + ret += set_framerate_params(dev);
  13657. +
  13658. + return ret;
  13659. +}
  13660. +
  13661. +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
  13662. + struct v4l2_ctrl *ctrl,
  13663. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13664. +{
  13665. + switch (ctrl->val) {
  13666. + case V4L2_EXPOSURE_METERING_AVERAGE:
  13667. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  13668. + break;
  13669. +
  13670. + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  13671. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  13672. + break;
  13673. +
  13674. + case V4L2_EXPOSURE_METERING_SPOT:
  13675. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  13676. + break;
  13677. +
  13678. + /* todo matrix weighting not added to Linux API till 3.9
  13679. + case V4L2_EXPOSURE_METERING_MATRIX:
  13680. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  13681. + break;
  13682. + */
  13683. +
  13684. + }
  13685. +
  13686. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  13687. + struct vchiq_mmal_port *control;
  13688. + u32 u32_value = dev->metering_mode;
  13689. +
  13690. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13691. +
  13692. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13693. + mmal_ctrl->mmal_id,
  13694. + &u32_value, sizeof(u32_value));
  13695. + } else
  13696. + return 0;
  13697. +}
  13698. +
  13699. +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
  13700. + struct v4l2_ctrl *ctrl,
  13701. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13702. +{
  13703. + u32 u32_value;
  13704. + struct vchiq_mmal_port *control;
  13705. +
  13706. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13707. +
  13708. + switch (ctrl->val) {
  13709. + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  13710. + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
  13711. + break;
  13712. + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  13713. + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
  13714. + break;
  13715. + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  13716. + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
  13717. + break;
  13718. + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  13719. + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
  13720. + break;
  13721. + }
  13722. +
  13723. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13724. + mmal_ctrl->mmal_id,
  13725. + &u32_value, sizeof(u32_value));
  13726. +}
  13727. +
  13728. +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
  13729. + struct v4l2_ctrl *ctrl,
  13730. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13731. +{
  13732. + u32 u32_value;
  13733. + struct vchiq_mmal_port *control;
  13734. +
  13735. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13736. +
  13737. + switch (ctrl->val) {
  13738. + case V4L2_WHITE_BALANCE_MANUAL:
  13739. + u32_value = MMAL_PARAM_AWBMODE_OFF;
  13740. + break;
  13741. +
  13742. + case V4L2_WHITE_BALANCE_AUTO:
  13743. + u32_value = MMAL_PARAM_AWBMODE_AUTO;
  13744. + break;
  13745. +
  13746. + case V4L2_WHITE_BALANCE_INCANDESCENT:
  13747. + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
  13748. + break;
  13749. +
  13750. + case V4L2_WHITE_BALANCE_FLUORESCENT:
  13751. + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
  13752. + break;
  13753. +
  13754. + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
  13755. + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
  13756. + break;
  13757. +
  13758. + case V4L2_WHITE_BALANCE_HORIZON:
  13759. + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
  13760. + break;
  13761. +
  13762. + case V4L2_WHITE_BALANCE_DAYLIGHT:
  13763. + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
  13764. + break;
  13765. +
  13766. + case V4L2_WHITE_BALANCE_FLASH:
  13767. + u32_value = MMAL_PARAM_AWBMODE_FLASH;
  13768. + break;
  13769. +
  13770. + case V4L2_WHITE_BALANCE_CLOUDY:
  13771. + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
  13772. + break;
  13773. +
  13774. + case V4L2_WHITE_BALANCE_SHADE:
  13775. + u32_value = MMAL_PARAM_AWBMODE_SHADE;
  13776. + break;
  13777. +
  13778. + }
  13779. +
  13780. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13781. + mmal_ctrl->mmal_id,
  13782. + &u32_value, sizeof(u32_value));
  13783. +}
  13784. +
  13785. +static int ctrl_set_awb_gains(struct bm2835_mmal_dev *dev,
  13786. + struct v4l2_ctrl *ctrl,
  13787. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13788. +{
  13789. + struct vchiq_mmal_port *control;
  13790. + struct mmal_parameter_awbgains gains;
  13791. +
  13792. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13793. +
  13794. + if (ctrl->id == V4L2_CID_RED_BALANCE)
  13795. + dev->red_gain = ctrl->val;
  13796. + else if (ctrl->id == V4L2_CID_BLUE_BALANCE)
  13797. + dev->blue_gain = ctrl->val;
  13798. +
  13799. + gains.r_gain.num = dev->red_gain;
  13800. + gains.b_gain.num = dev->blue_gain;
  13801. + gains.r_gain.den = gains.b_gain.den = 1000;
  13802. +
  13803. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13804. + mmal_ctrl->mmal_id,
  13805. + &gains, sizeof(gains));
  13806. +}
  13807. +
  13808. +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
  13809. + struct v4l2_ctrl *ctrl,
  13810. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13811. +{
  13812. + int ret = -EINVAL;
  13813. + int i, j;
  13814. + struct vchiq_mmal_port *control;
  13815. + struct mmal_parameter_imagefx_parameters imagefx;
  13816. +
  13817. + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
  13818. + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
  13819. +
  13820. + imagefx.effect =
  13821. + v4l2_to_mmal_effects_values[i].mmal_effect;
  13822. + imagefx.num_effect_params =
  13823. + v4l2_to_mmal_effects_values[i].num_effect_params;
  13824. +
  13825. + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
  13826. + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
  13827. +
  13828. + for (j = 0; j < imagefx.num_effect_params; j++)
  13829. + imagefx.effect_parameter[j] =
  13830. + v4l2_to_mmal_effects_values[i].effect_params[j];
  13831. +
  13832. + dev->colourfx.enable =
  13833. + v4l2_to_mmal_effects_values[i].col_fx_enable;
  13834. + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
  13835. + dev->colourfx.u =
  13836. + v4l2_to_mmal_effects_values[i].u;
  13837. + dev->colourfx.v =
  13838. + v4l2_to_mmal_effects_values[i].v;
  13839. + }
  13840. +
  13841. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13842. +
  13843. + ret = vchiq_mmal_port_parameter_set(
  13844. + dev->instance, control,
  13845. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  13846. + &imagefx, sizeof(imagefx));
  13847. + if (ret)
  13848. + goto exit;
  13849. +
  13850. + ret = vchiq_mmal_port_parameter_set(
  13851. + dev->instance, control,
  13852. + MMAL_PARAMETER_COLOUR_EFFECT,
  13853. + &dev->colourfx, sizeof(dev->colourfx));
  13854. + }
  13855. + }
  13856. +
  13857. +exit:
  13858. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13859. + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
  13860. + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
  13861. + dev->colourfx.enable ? "true" : "false",
  13862. + dev->colourfx.u, dev->colourfx.v,
  13863. + ret, (ret == 0 ? 0 : -EINVAL));
  13864. + return (ret == 0 ? 0 : EINVAL);
  13865. +}
  13866. +
  13867. +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
  13868. + struct v4l2_ctrl *ctrl,
  13869. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13870. +{
  13871. + int ret = -EINVAL;
  13872. + struct vchiq_mmal_port *control;
  13873. +
  13874. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13875. +
  13876. + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
  13877. + dev->colourfx.enable = ctrl->val & 0xff;
  13878. +
  13879. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  13880. + MMAL_PARAMETER_COLOUR_EFFECT,
  13881. + &dev->colourfx, sizeof(dev->colourfx));
  13882. +
  13883. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13884. + "%s: After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
  13885. + __func__, mmal_ctrl, ctrl->id, ctrl->val, ret,
  13886. + (ret == 0 ? 0 : -EINVAL));
  13887. + return (ret == 0 ? 0 : EINVAL);
  13888. +}
  13889. +
  13890. +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
  13891. + struct v4l2_ctrl *ctrl,
  13892. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13893. +{
  13894. + int ret;
  13895. + struct vchiq_mmal_port *encoder_out;
  13896. +
  13897. + dev->capture.encode_bitrate = ctrl->val;
  13898. +
  13899. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  13900. +
  13901. + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  13902. + mmal_ctrl->mmal_id,
  13903. + &ctrl->val, sizeof(ctrl->val));
  13904. + ret = 0;
  13905. + return ret;
  13906. +}
  13907. +
  13908. +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
  13909. + struct v4l2_ctrl *ctrl,
  13910. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13911. +{
  13912. + u32 bitrate_mode;
  13913. + struct vchiq_mmal_port *encoder_out;
  13914. +
  13915. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  13916. +
  13917. + dev->capture.encode_bitrate_mode = ctrl->val;
  13918. + switch (ctrl->val) {
  13919. + default:
  13920. + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
  13921. + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
  13922. + break;
  13923. + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
  13924. + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
  13925. + break;
  13926. + }
  13927. +
  13928. + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  13929. + mmal_ctrl->mmal_id,
  13930. + &bitrate_mode,
  13931. + sizeof(bitrate_mode));
  13932. + return 0;
  13933. +}
  13934. +
  13935. +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
  13936. + struct v4l2_ctrl *ctrl,
  13937. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13938. +{
  13939. + u32 u32_value;
  13940. + struct vchiq_mmal_port *jpeg_out;
  13941. +
  13942. + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  13943. +
  13944. + u32_value = ctrl->val;
  13945. +
  13946. + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
  13947. + mmal_ctrl->mmal_id,
  13948. + &u32_value, sizeof(u32_value));
  13949. +}
  13950. +
  13951. +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
  13952. + struct v4l2_ctrl *ctrl,
  13953. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13954. +{
  13955. + u32 u32_value;
  13956. + struct vchiq_mmal_port *vid_enc_ctl;
  13957. +
  13958. + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  13959. +
  13960. + u32_value = ctrl->val;
  13961. +
  13962. + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
  13963. + mmal_ctrl->mmal_id,
  13964. + &u32_value, sizeof(u32_value));
  13965. +}
  13966. +
  13967. +static int ctrl_set_video_encode_profile_level(struct bm2835_mmal_dev *dev,
  13968. + struct v4l2_ctrl *ctrl,
  13969. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13970. +{
  13971. + struct mmal_parameter_video_profile param;
  13972. + int ret = 0;
  13973. +
  13974. + if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_PROFILE) {
  13975. + switch (ctrl->val) {
  13976. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  13977. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  13978. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  13979. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  13980. + dev->capture.enc_profile = ctrl->val;
  13981. + break;
  13982. + default:
  13983. + ret = -EINVAL;
  13984. + break;
  13985. + }
  13986. + } else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_LEVEL) {
  13987. + switch (ctrl->val) {
  13988. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  13989. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  13990. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  13991. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  13992. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  13993. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  13994. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  13995. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  13996. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  13997. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  13998. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  13999. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  14000. + dev->capture.enc_level = ctrl->val;
  14001. + break;
  14002. + default:
  14003. + ret = -EINVAL;
  14004. + break;
  14005. + }
  14006. + }
  14007. +
  14008. + if (!ret) {
  14009. + switch (dev->capture.enc_profile) {
  14010. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  14011. + param.profile = MMAL_VIDEO_PROFILE_H264_BASELINE;
  14012. + break;
  14013. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  14014. + param.profile =
  14015. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE;
  14016. + break;
  14017. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  14018. + param.profile = MMAL_VIDEO_PROFILE_H264_MAIN;
  14019. + break;
  14020. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  14021. + param.profile = MMAL_VIDEO_PROFILE_H264_HIGH;
  14022. + break;
  14023. + default:
  14024. + /* Should never get here */
  14025. + break;
  14026. + }
  14027. +
  14028. + switch (dev->capture.enc_level) {
  14029. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  14030. + param.level = MMAL_VIDEO_LEVEL_H264_1;
  14031. + break;
  14032. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  14033. + param.level = MMAL_VIDEO_LEVEL_H264_1b;
  14034. + break;
  14035. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  14036. + param.level = MMAL_VIDEO_LEVEL_H264_11;
  14037. + break;
  14038. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  14039. + param.level = MMAL_VIDEO_LEVEL_H264_12;
  14040. + break;
  14041. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  14042. + param.level = MMAL_VIDEO_LEVEL_H264_13;
  14043. + break;
  14044. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  14045. + param.level = MMAL_VIDEO_LEVEL_H264_2;
  14046. + break;
  14047. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  14048. + param.level = MMAL_VIDEO_LEVEL_H264_21;
  14049. + break;
  14050. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  14051. + param.level = MMAL_VIDEO_LEVEL_H264_22;
  14052. + break;
  14053. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  14054. + param.level = MMAL_VIDEO_LEVEL_H264_3;
  14055. + break;
  14056. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  14057. + param.level = MMAL_VIDEO_LEVEL_H264_31;
  14058. + break;
  14059. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  14060. + param.level = MMAL_VIDEO_LEVEL_H264_32;
  14061. + break;
  14062. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  14063. + param.level = MMAL_VIDEO_LEVEL_H264_4;
  14064. + break;
  14065. + default:
  14066. + /* Should never get here */
  14067. + break;
  14068. + }
  14069. +
  14070. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  14071. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0],
  14072. + mmal_ctrl->mmal_id,
  14073. + &param, sizeof(param));
  14074. + }
  14075. + return ret;
  14076. +}
  14077. +
  14078. +static int ctrl_set_scene_mode(struct bm2835_mmal_dev *dev,
  14079. + struct v4l2_ctrl *ctrl,
  14080. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  14081. +{
  14082. + int ret = 0;
  14083. + int shutter_speed;
  14084. + struct vchiq_mmal_port *control;
  14085. +
  14086. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14087. + "scene mode selected %d, was %d\n", ctrl->val,
  14088. + dev->scene_mode);
  14089. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  14090. +
  14091. + if (ctrl->val == dev->scene_mode)
  14092. + return 0;
  14093. +
  14094. + if (ctrl->val == V4L2_SCENE_MODE_NONE) {
  14095. + /* Restore all user selections */
  14096. + dev->scene_mode = V4L2_SCENE_MODE_NONE;
  14097. +
  14098. + if (dev->exposure_mode_user == MMAL_PARAM_EXPOSUREMODE_OFF)
  14099. + shutter_speed = dev->manual_shutter_speed;
  14100. + else
  14101. + shutter_speed = 0;
  14102. +
  14103. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14104. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  14105. + __func__, shutter_speed, dev->exposure_mode_user,
  14106. + dev->metering_mode);
  14107. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  14108. + control,
  14109. + MMAL_PARAMETER_SHUTTER_SPEED,
  14110. + &shutter_speed,
  14111. + sizeof(shutter_speed));
  14112. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  14113. + control,
  14114. + MMAL_PARAMETER_EXPOSURE_MODE,
  14115. + &dev->exposure_mode_user,
  14116. + sizeof(u32));
  14117. + dev->exposure_mode_active = dev->exposure_mode_user;
  14118. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  14119. + control,
  14120. + MMAL_PARAMETER_EXP_METERING_MODE,
  14121. + &dev->metering_mode,
  14122. + sizeof(u32));
  14123. + ret += set_framerate_params(dev);
  14124. + } else {
  14125. + /* Set up scene mode */
  14126. + int i;
  14127. + const struct v4l2_mmal_scene_config *scene = NULL;
  14128. + int shutter_speed;
  14129. + enum mmal_parameter_exposuremode exposure_mode;
  14130. + enum mmal_parameter_exposuremeteringmode metering_mode;
  14131. +
  14132. + for (i = 0; i < ARRAY_SIZE(scene_configs); i++) {
  14133. + if (scene_configs[i].v4l2_scene ==
  14134. + ctrl->val) {
  14135. + scene = &scene_configs[i];
  14136. + break;
  14137. + }
  14138. + }
  14139. + if (i >= ARRAY_SIZE(scene_configs))
  14140. + return -EINVAL;
  14141. +
  14142. + /* Set all the values */
  14143. + dev->scene_mode = ctrl->val;
  14144. +
  14145. + if (scene->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  14146. + shutter_speed = dev->manual_shutter_speed;
  14147. + else
  14148. + shutter_speed = 0;
  14149. + exposure_mode = scene->exposure_mode;
  14150. + metering_mode = scene->metering_mode;
  14151. +
  14152. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14153. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  14154. + __func__, shutter_speed, exposure_mode, metering_mode);
  14155. +
  14156. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  14157. + MMAL_PARAMETER_SHUTTER_SPEED,
  14158. + &shutter_speed,
  14159. + sizeof(shutter_speed));
  14160. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  14161. + control,
  14162. + MMAL_PARAMETER_EXPOSURE_MODE,
  14163. + &exposure_mode,
  14164. + sizeof(u32));
  14165. + dev->exposure_mode_active = exposure_mode;
  14166. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  14167. + MMAL_PARAMETER_EXPOSURE_MODE,
  14168. + &exposure_mode,
  14169. + sizeof(u32));
  14170. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  14171. + MMAL_PARAMETER_EXP_METERING_MODE,
  14172. + &metering_mode,
  14173. + sizeof(u32));
  14174. + ret += set_framerate_params(dev);
  14175. + }
  14176. + if (ret) {
  14177. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14178. + "%s: Setting scene to %d, ret=%d\n",
  14179. + __func__, ctrl->val, ret);
  14180. + ret = -EINVAL;
  14181. + }
  14182. + return 0;
  14183. +}
  14184. +
  14185. +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
  14186. +{
  14187. + struct bm2835_mmal_dev *dev =
  14188. + container_of(ctrl->handler, struct bm2835_mmal_dev,
  14189. + ctrl_handler);
  14190. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
  14191. + int ret;
  14192. +
  14193. + if ((mmal_ctrl == NULL) ||
  14194. + (mmal_ctrl->id != ctrl->id) ||
  14195. + (mmal_ctrl->setter == NULL)) {
  14196. + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
  14197. + return -EINVAL;
  14198. + }
  14199. +
  14200. + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
  14201. + if (ret)
  14202. + pr_warn("ctrl id:%d/MMAL param %08X- returned ret %d\n",
  14203. + ctrl->id, mmal_ctrl->mmal_id, ret);
  14204. + if (mmal_ctrl->ignore_errors)
  14205. + ret = 0;
  14206. + return ret;
  14207. +}
  14208. +
  14209. +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
  14210. + .s_ctrl = bm2835_mmal_s_ctrl,
  14211. +};
  14212. +
  14213. +
  14214. +
  14215. +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
  14216. + {
  14217. + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
  14218. + -100, 100, 0, 1, NULL,
  14219. + MMAL_PARAMETER_SATURATION,
  14220. + &ctrl_set_rational,
  14221. + false
  14222. + },
  14223. + {
  14224. + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
  14225. + -100, 100, 0, 1, NULL,
  14226. + MMAL_PARAMETER_SHARPNESS,
  14227. + &ctrl_set_rational,
  14228. + false
  14229. + },
  14230. + {
  14231. + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
  14232. + -100, 100, 0, 1, NULL,
  14233. + MMAL_PARAMETER_CONTRAST,
  14234. + &ctrl_set_rational,
  14235. + false
  14236. + },
  14237. + {
  14238. + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
  14239. + 0, 100, 50, 1, NULL,
  14240. + MMAL_PARAMETER_BRIGHTNESS,
  14241. + &ctrl_set_rational,
  14242. + false
  14243. + },
  14244. + {
  14245. + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
  14246. + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
  14247. + MMAL_PARAMETER_ISO,
  14248. + &ctrl_set_value_menu,
  14249. + false
  14250. + },
  14251. + {
  14252. + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
  14253. + 0, 1, 0, 1, NULL,
  14254. + MMAL_PARAMETER_VIDEO_STABILISATION,
  14255. + &ctrl_set_value,
  14256. + false
  14257. + },
  14258. +/* {
  14259. + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
  14260. + },
  14261. +*/ {
  14262. + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
  14263. + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
  14264. + MMAL_PARAMETER_EXPOSURE_MODE,
  14265. + &ctrl_set_exposure,
  14266. + false
  14267. + },
  14268. +/* todo this needs mixing in with set exposure
  14269. + {
  14270. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  14271. + },
  14272. + */
  14273. + {
  14274. + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
  14275. + /* Units of 100usecs */
  14276. + 1, 1*1000*10, 100*10, 1, NULL,
  14277. + MMAL_PARAMETER_SHUTTER_SPEED,
  14278. + &ctrl_set_exposure,
  14279. + false
  14280. + },
  14281. + {
  14282. + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
  14283. + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
  14284. + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
  14285. + MMAL_PARAMETER_EXPOSURE_COMP,
  14286. + &ctrl_set_value_ev,
  14287. + false
  14288. + },
  14289. + {
  14290. + V4L2_CID_EXPOSURE_AUTO_PRIORITY, MMAL_CONTROL_TYPE_STD,
  14291. + 0, 1,
  14292. + 0, 1, NULL,
  14293. + 0, /* Dummy MMAL ID as it gets mapped into FPS range*/
  14294. + &ctrl_set_exposure,
  14295. + false
  14296. + },
  14297. + {
  14298. + V4L2_CID_EXPOSURE_METERING,
  14299. + MMAL_CONTROL_TYPE_STD_MENU,
  14300. + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
  14301. + MMAL_PARAMETER_EXP_METERING_MODE,
  14302. + &ctrl_set_metering_mode,
  14303. + false
  14304. + },
  14305. + {
  14306. + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  14307. + MMAL_CONTROL_TYPE_STD_MENU,
  14308. + ~0x3ff, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
  14309. + MMAL_PARAMETER_AWB_MODE,
  14310. + &ctrl_set_awb_mode,
  14311. + false
  14312. + },
  14313. + {
  14314. + V4L2_CID_RED_BALANCE, MMAL_CONTROL_TYPE_STD,
  14315. + 1, 7999, 1000, 1, NULL,
  14316. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  14317. + &ctrl_set_awb_gains,
  14318. + false
  14319. + },
  14320. + {
  14321. + V4L2_CID_BLUE_BALANCE, MMAL_CONTROL_TYPE_STD,
  14322. + 1, 7999, 1000, 1, NULL,
  14323. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  14324. + &ctrl_set_awb_gains,
  14325. + false
  14326. + },
  14327. + {
  14328. + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
  14329. + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
  14330. + MMAL_PARAMETER_IMAGE_EFFECT,
  14331. + &ctrl_set_image_effect,
  14332. + false
  14333. + },
  14334. + {
  14335. + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
  14336. + 0, 0xffff, 0x8080, 1, NULL,
  14337. + MMAL_PARAMETER_COLOUR_EFFECT,
  14338. + &ctrl_set_colfx,
  14339. + false
  14340. + },
  14341. + {
  14342. + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
  14343. + 0, 360, 0, 90, NULL,
  14344. + MMAL_PARAMETER_ROTATION,
  14345. + &ctrl_set_rotate,
  14346. + false
  14347. + },
  14348. + {
  14349. + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
  14350. + 0, 1, 0, 1, NULL,
  14351. + MMAL_PARAMETER_MIRROR,
  14352. + &ctrl_set_flip,
  14353. + false
  14354. + },
  14355. + {
  14356. + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
  14357. + 0, 1, 0, 1, NULL,
  14358. + MMAL_PARAMETER_MIRROR,
  14359. + &ctrl_set_flip,
  14360. + false
  14361. + },
  14362. + {
  14363. + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  14364. + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
  14365. + 0, 0, bitrate_mode_qmenu,
  14366. + MMAL_PARAMETER_RATECONTROL,
  14367. + &ctrl_set_bitrate_mode,
  14368. + false
  14369. + },
  14370. + {
  14371. + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
  14372. + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
  14373. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  14374. + &ctrl_set_bitrate,
  14375. + false
  14376. + },
  14377. + {
  14378. + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
  14379. + 1, 100,
  14380. + 30, 1, NULL,
  14381. + MMAL_PARAMETER_JPEG_Q_FACTOR,
  14382. + &ctrl_set_image_encode_output,
  14383. + false
  14384. + },
  14385. + {
  14386. + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
  14387. + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
  14388. + 1, 1, NULL,
  14389. + MMAL_PARAMETER_FLICKER_AVOID,
  14390. + &ctrl_set_flicker_avoidance,
  14391. + false
  14392. + },
  14393. + {
  14394. + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
  14395. + 0, 1,
  14396. + 0, 1, NULL,
  14397. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
  14398. + &ctrl_set_video_encode_param_output,
  14399. + true /* Errors ignored as requires latest firmware to work */
  14400. + },
  14401. + {
  14402. + V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  14403. + MMAL_CONTROL_TYPE_STD_MENU,
  14404. + ~((1<<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  14405. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
  14406. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  14407. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  14408. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  14409. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1, NULL,
  14410. + MMAL_PARAMETER_PROFILE,
  14411. + &ctrl_set_video_encode_profile_level,
  14412. + false
  14413. + },
  14414. + {
  14415. + V4L2_CID_MPEG_VIDEO_H264_LEVEL, MMAL_CONTROL_TYPE_STD_MENU,
  14416. + ~((1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
  14417. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1B) |
  14418. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_1) |
  14419. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_2) |
  14420. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_3) |
  14421. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  14422. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_1) |
  14423. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_2) |
  14424. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  14425. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  14426. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  14427. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
  14428. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  14429. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1, NULL,
  14430. + MMAL_PARAMETER_PROFILE,
  14431. + &ctrl_set_video_encode_profile_level,
  14432. + false
  14433. + },
  14434. + {
  14435. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  14436. + -1, /* Min is computed at runtime */
  14437. + V4L2_SCENE_MODE_TEXT,
  14438. + V4L2_SCENE_MODE_NONE, 1, NULL,
  14439. + MMAL_PARAMETER_PROFILE,
  14440. + &ctrl_set_scene_mode,
  14441. + false
  14442. + },
  14443. +};
  14444. +
  14445. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
  14446. +{
  14447. + int c;
  14448. + int ret = 0;
  14449. +
  14450. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  14451. + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
  14452. + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
  14453. + &v4l2_ctrls[c]);
  14454. + if (!v4l2_ctrls[c].ignore_errors && ret) {
  14455. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14456. + "Failed when setting default values for ctrl %d\n",
  14457. + c);
  14458. + break;
  14459. + }
  14460. + }
  14461. + }
  14462. + return ret;
  14463. +}
  14464. +
  14465. +int set_framerate_params(struct bm2835_mmal_dev *dev)
  14466. +{
  14467. + struct mmal_parameter_fps_range fps_range;
  14468. + int ret;
  14469. +
  14470. + if ((dev->exposure_mode_active != MMAL_PARAM_EXPOSUREMODE_OFF) &&
  14471. + (dev->exp_auto_priority)) {
  14472. + /* Variable FPS. Define min FPS as 1fps.
  14473. + * Max as max defined FPS.
  14474. + */
  14475. + fps_range.fps_low.num = 1;
  14476. + fps_range.fps_low.den = 1;
  14477. + fps_range.fps_high.num = dev->capture.timeperframe.denominator;
  14478. + fps_range.fps_high.den = dev->capture.timeperframe.numerator;
  14479. + } else {
  14480. + /* Fixed FPS - set min and max to be the same */
  14481. + fps_range.fps_low.num = fps_range.fps_high.num =
  14482. + dev->capture.timeperframe.denominator;
  14483. + fps_range.fps_low.den = fps_range.fps_high.den =
  14484. + dev->capture.timeperframe.numerator;
  14485. + }
  14486. +
  14487. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14488. + "Set fps range to %d/%d to %d/%d\n",
  14489. + fps_range.fps_low.num,
  14490. + fps_range.fps_low.den,
  14491. + fps_range.fps_high.num,
  14492. + fps_range.fps_high.den
  14493. + );
  14494. +
  14495. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  14496. + &dev->component[MMAL_COMPONENT_CAMERA]->
  14497. + output[MMAL_CAMERA_PORT_PREVIEW],
  14498. + MMAL_PARAMETER_FPS_RANGE,
  14499. + &fps_range, sizeof(fps_range));
  14500. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  14501. + &dev->component[MMAL_COMPONENT_CAMERA]->
  14502. + output[MMAL_CAMERA_PORT_VIDEO],
  14503. + MMAL_PARAMETER_FPS_RANGE,
  14504. + &fps_range, sizeof(fps_range));
  14505. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  14506. + &dev->component[MMAL_COMPONENT_CAMERA]->
  14507. + output[MMAL_CAMERA_PORT_CAPTURE],
  14508. + MMAL_PARAMETER_FPS_RANGE,
  14509. + &fps_range, sizeof(fps_range));
  14510. + if (ret)
  14511. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  14512. + "Failed to set fps ret %d\n",
  14513. + ret);
  14514. +
  14515. + return ret;
  14516. +
  14517. +}
  14518. +
  14519. +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
  14520. + struct v4l2_ctrl_handler *hdl)
  14521. +{
  14522. + int c;
  14523. + const struct bm2835_mmal_v4l2_ctrl *ctrl;
  14524. +
  14525. + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
  14526. +
  14527. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  14528. + ctrl = &v4l2_ctrls[c];
  14529. +
  14530. + switch (ctrl->type) {
  14531. + case MMAL_CONTROL_TYPE_STD:
  14532. + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
  14533. + &bm2835_mmal_ctrl_ops, ctrl->id,
  14534. + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
  14535. + break;
  14536. +
  14537. + case MMAL_CONTROL_TYPE_STD_MENU:
  14538. + {
  14539. + int mask = ctrl->min;
  14540. +
  14541. + if (ctrl->id == V4L2_CID_SCENE_MODE) {
  14542. + /* Special handling to work out the mask
  14543. + * value based on the scene_configs array
  14544. + * at runtime. Reduces the chance of
  14545. + * mismatches.
  14546. + */
  14547. + int i;
  14548. + mask = 1<<V4L2_SCENE_MODE_NONE;
  14549. + for (i = 0;
  14550. + i < ARRAY_SIZE(scene_configs);
  14551. + i++) {
  14552. + mask |= 1<<scene_configs[i].v4l2_scene;
  14553. + }
  14554. + mask = ~mask;
  14555. + }
  14556. +
  14557. + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
  14558. + &bm2835_mmal_ctrl_ops, ctrl->id,
  14559. + ctrl->max, mask, ctrl->def);
  14560. + break;
  14561. + }
  14562. +
  14563. + case MMAL_CONTROL_TYPE_INT_MENU:
  14564. + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
  14565. + &bm2835_mmal_ctrl_ops, ctrl->id,
  14566. + ctrl->max, ctrl->def, ctrl->imenu);
  14567. + break;
  14568. +
  14569. + case MMAL_CONTROL_TYPE_CLUSTER:
  14570. + /* skip this entry when constructing controls */
  14571. + continue;
  14572. + }
  14573. +
  14574. + if (hdl->error)
  14575. + break;
  14576. +
  14577. + dev->ctrls[c]->priv = (void *)ctrl;
  14578. + }
  14579. +
  14580. + if (hdl->error) {
  14581. + pr_err("error adding control %d/%d id 0x%x\n", c,
  14582. + V4L2_CTRL_COUNT, ctrl->id);
  14583. + return hdl->error;
  14584. + }
  14585. +
  14586. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  14587. + ctrl = &v4l2_ctrls[c];
  14588. +
  14589. + switch (ctrl->type) {
  14590. + case MMAL_CONTROL_TYPE_CLUSTER:
  14591. + v4l2_ctrl_auto_cluster(ctrl->min,
  14592. + &dev->ctrls[c+1],
  14593. + ctrl->max,
  14594. + ctrl->def);
  14595. + break;
  14596. +
  14597. + case MMAL_CONTROL_TYPE_STD:
  14598. + case MMAL_CONTROL_TYPE_STD_MENU:
  14599. + case MMAL_CONTROL_TYPE_INT_MENU:
  14600. + break;
  14601. + }
  14602. +
  14603. + }
  14604. +
  14605. + return 0;
  14606. +}
  14607. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/Kconfig linux-rpi/drivers/media/platform/bcm2835/Kconfig
  14608. --- linux-3.10.37/drivers/media/platform/bcm2835/Kconfig 1970-01-01 01:00:00.000000000 +0100
  14609. +++ linux-rpi/drivers/media/platform/bcm2835/Kconfig 2014-04-24 15:35:02.713549510 +0200
  14610. @@ -0,0 +1,25 @@
  14611. +# Broadcom VideoCore IV v4l2 camera support
  14612. +
  14613. +config VIDEO_BCM2835
  14614. + bool "Broadcom BCM2835 camera interface driver"
  14615. + depends on VIDEO_V4L2 && ARCH_BCM2708
  14616. + ---help---
  14617. + Say Y here to enable camera host interface devices for
  14618. + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
  14619. + to a service running on VideoCore.
  14620. +
  14621. +
  14622. +if VIDEO_BCM2835
  14623. +
  14624. +config VIDEO_BCM2835_MMAL
  14625. + tristate "Broadcom BM2835 MMAL camera interface driver"
  14626. + depends on BCM2708_VCHIQ
  14627. + select VIDEOBUF2_VMALLOC
  14628. + ---help---
  14629. + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
  14630. +
  14631. + To compile this driver as a module, choose M here: the
  14632. + module will be called bcm2835-v4l2.o
  14633. +
  14634. +
  14635. +endif # VIDEO_BM2835
  14636. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/Makefile linux-rpi/drivers/media/platform/bcm2835/Makefile
  14637. --- linux-3.10.37/drivers/media/platform/bcm2835/Makefile 1970-01-01 01:00:00.000000000 +0100
  14638. +++ linux-rpi/drivers/media/platform/bcm2835/Makefile 2014-04-24 15:35:02.713549510 +0200
  14639. @@ -0,0 +1,5 @@
  14640. +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
  14641. +
  14642. +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
  14643. +
  14644. +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  14645. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-common.h linux-rpi/drivers/media/platform/bcm2835/mmal-common.h
  14646. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-common.h 1970-01-01 01:00:00.000000000 +0100
  14647. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-common.h 2014-04-24 15:35:02.713549510 +0200
  14648. @@ -0,0 +1,53 @@
  14649. +/*
  14650. + * Broadcom BM2835 V4L2 driver
  14651. + *
  14652. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14653. + *
  14654. + * This file is subject to the terms and conditions of the GNU General Public
  14655. + * License. See the file COPYING in the main directory of this archive
  14656. + * for more details.
  14657. + *
  14658. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14659. + * Dave Stevenson <dsteve@broadcom.com>
  14660. + * Simon Mellor <simellor@broadcom.com>
  14661. + * Luke Diamand <luked@broadcom.com>
  14662. + *
  14663. + * MMAL structures
  14664. + *
  14665. + */
  14666. +
  14667. +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
  14668. +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
  14669. +
  14670. +/** Special value signalling that time is not known */
  14671. +#define MMAL_TIME_UNKNOWN (1LL<<63)
  14672. +
  14673. +/* mapping between v4l and mmal video modes */
  14674. +struct mmal_fmt {
  14675. + char *name;
  14676. + u32 fourcc; /* v4l2 format id */
  14677. + int flags; /* v4l2 flags field */
  14678. + u32 mmal;
  14679. + int depth;
  14680. + u32 mmal_component; /* MMAL component index to be used to encode */
  14681. +};
  14682. +
  14683. +/* buffer for one video frame */
  14684. +struct mmal_buffer {
  14685. + /* v4l buffer data -- must be first */
  14686. + struct vb2_buffer vb;
  14687. +
  14688. + /* list of buffers available */
  14689. + struct list_head list;
  14690. +
  14691. + void *buffer; /* buffer pointer */
  14692. + unsigned long buffer_size; /* size of allocated buffer */
  14693. +};
  14694. +
  14695. +/* */
  14696. +struct mmal_colourfx {
  14697. + s32 enable;
  14698. + u32 u;
  14699. + u32 v;
  14700. +};
  14701. +
  14702. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-encodings.h linux-rpi/drivers/media/platform/bcm2835/mmal-encodings.h
  14703. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-encodings.h 1970-01-01 01:00:00.000000000 +0100
  14704. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-encodings.h 2014-04-24 15:35:02.713549510 +0200
  14705. @@ -0,0 +1,94 @@
  14706. +/*
  14707. + * Broadcom BM2835 V4L2 driver
  14708. + *
  14709. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14710. + *
  14711. + * This file is subject to the terms and conditions of the GNU General Public
  14712. + * License. See the file COPYING in the main directory of this archive
  14713. + * for more details.
  14714. + *
  14715. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14716. + * Dave Stevenson <dsteve@broadcom.com>
  14717. + * Simon Mellor <simellor@broadcom.com>
  14718. + * Luke Diamand <luked@broadcom.com>
  14719. + */
  14720. +
  14721. +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
  14722. +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
  14723. +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
  14724. +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
  14725. +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
  14726. +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
  14727. +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
  14728. +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
  14729. +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
  14730. +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
  14731. +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
  14732. +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
  14733. +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
  14734. +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
  14735. +#define MMAL_ENCODING_MJPEG MMAL_FOURCC('M', 'J', 'P', 'G')
  14736. +
  14737. +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
  14738. +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
  14739. +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
  14740. +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
  14741. +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
  14742. +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
  14743. +
  14744. +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
  14745. +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
  14746. +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
  14747. +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
  14748. +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
  14749. +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
  14750. +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
  14751. +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
  14752. +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
  14753. +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
  14754. +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
  14755. +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
  14756. +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
  14757. +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
  14758. +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
  14759. +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
  14760. +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
  14761. +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
  14762. +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
  14763. +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
  14764. +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
  14765. +
  14766. +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
  14767. + * This format is *not* opaque - if requested you will receive full frames
  14768. + * of YUV_UV video.
  14769. + */
  14770. +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
  14771. +
  14772. +/** VideoCore opaque image format, image handles are returned to
  14773. + * the host but not the actual image data.
  14774. + */
  14775. +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
  14776. +
  14777. +/** An EGL image handle
  14778. + */
  14779. +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
  14780. +
  14781. +/* }@ */
  14782. +
  14783. +/** \name Pre-defined audio encodings */
  14784. +/* @{ */
  14785. +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
  14786. +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
  14787. +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
  14788. +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
  14789. +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
  14790. +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
  14791. +
  14792. +/* Pre-defined H264 encoding variants */
  14793. +
  14794. +/** ISO 14496-10 Annex B byte stream format */
  14795. +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
  14796. +/** ISO 14496-15 AVC stream format */
  14797. +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
  14798. +/** Implicitly delineated NAL units without emulation prevention */
  14799. +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
  14800. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg-common.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h
  14801. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg-common.h 1970-01-01 01:00:00.000000000 +0100
  14802. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h 2014-04-24 15:35:02.713549510 +0200
  14803. @@ -0,0 +1,50 @@
  14804. +/*
  14805. + * Broadcom BM2835 V4L2 driver
  14806. + *
  14807. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14808. + *
  14809. + * This file is subject to the terms and conditions of the GNU General Public
  14810. + * License. See the file COPYING in the main directory of this archive
  14811. + * for more details.
  14812. + *
  14813. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14814. + * Dave Stevenson <dsteve@broadcom.com>
  14815. + * Simon Mellor <simellor@broadcom.com>
  14816. + * Luke Diamand <luked@broadcom.com>
  14817. + */
  14818. +
  14819. +#ifndef MMAL_MSG_COMMON_H
  14820. +#define MMAL_MSG_COMMON_H
  14821. +
  14822. +enum mmal_msg_status {
  14823. + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
  14824. + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
  14825. + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
  14826. + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
  14827. + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
  14828. + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
  14829. + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
  14830. + MMAL_MSG_STATUS_EIO, /**< I/O error */
  14831. + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
  14832. + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
  14833. + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
  14834. + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
  14835. + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
  14836. + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
  14837. + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
  14838. + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
  14839. +};
  14840. +
  14841. +struct mmal_rect {
  14842. + s32 x; /**< x coordinate (from left) */
  14843. + s32 y; /**< y coordinate (from top) */
  14844. + s32 width; /**< width */
  14845. + s32 height; /**< height */
  14846. +};
  14847. +
  14848. +struct mmal_rational {
  14849. + s32 num; /**< Numerator */
  14850. + s32 den; /**< Denominator */
  14851. +};
  14852. +
  14853. +#endif /* MMAL_MSG_COMMON_H */
  14854. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg-format.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h
  14855. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg-format.h 1970-01-01 01:00:00.000000000 +0100
  14856. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h 2014-04-24 15:35:02.713549510 +0200
  14857. @@ -0,0 +1,81 @@
  14858. +/*
  14859. + * Broadcom BM2835 V4L2 driver
  14860. + *
  14861. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14862. + *
  14863. + * This file is subject to the terms and conditions of the GNU General Public
  14864. + * License. See the file COPYING in the main directory of this archive
  14865. + * for more details.
  14866. + *
  14867. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14868. + * Dave Stevenson <dsteve@broadcom.com>
  14869. + * Simon Mellor <simellor@broadcom.com>
  14870. + * Luke Diamand <luked@broadcom.com>
  14871. + */
  14872. +
  14873. +#ifndef MMAL_MSG_FORMAT_H
  14874. +#define MMAL_MSG_FORMAT_H
  14875. +
  14876. +#include "mmal-msg-common.h"
  14877. +
  14878. +/* MMAL_ES_FORMAT_T */
  14879. +
  14880. +
  14881. +struct mmal_audio_format {
  14882. + u32 channels; /**< Number of audio channels */
  14883. + u32 sample_rate; /**< Sample rate */
  14884. +
  14885. + u32 bits_per_sample; /**< Bits per sample */
  14886. + u32 block_align; /**< Size of a block of data */
  14887. +};
  14888. +
  14889. +struct mmal_video_format {
  14890. + u32 width; /**< Width of frame in pixels */
  14891. + u32 height; /**< Height of frame in rows of pixels */
  14892. + struct mmal_rect crop; /**< Visible region of the frame */
  14893. + struct mmal_rational frame_rate; /**< Frame rate */
  14894. + struct mmal_rational par; /**< Pixel aspect ratio */
  14895. +
  14896. + /* FourCC specifying the color space of the video stream. See the
  14897. + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
  14898. + */
  14899. + u32 color_space;
  14900. +};
  14901. +
  14902. +struct mmal_subpicture_format {
  14903. + u32 x_offset;
  14904. + u32 y_offset;
  14905. +};
  14906. +
  14907. +union mmal_es_specific_format {
  14908. + struct mmal_audio_format audio;
  14909. + struct mmal_video_format video;
  14910. + struct mmal_subpicture_format subpicture;
  14911. +};
  14912. +
  14913. +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
  14914. +struct mmal_es_format {
  14915. + u32 type; /* enum mmal_es_type */
  14916. +
  14917. + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
  14918. + u32 encoding_variant; /* FourCC specifying the specific
  14919. + * encoding variant of the elementary
  14920. + * stream.
  14921. + */
  14922. +
  14923. + union mmal_es_specific_format *es; /* TODO: pointers in
  14924. + * message serialisation?!?
  14925. + */
  14926. + /* Type specific
  14927. + * information for the
  14928. + * elementary stream
  14929. + */
  14930. +
  14931. + u32 bitrate; /**< Bitrate in bits per second */
  14932. + u32 flags; /**< Flags describing properties of the elementary stream. */
  14933. +
  14934. + u32 extradata_size; /**< Size of the codec specific data */
  14935. + u8 *extradata; /**< Codec specific data */
  14936. +};
  14937. +
  14938. +#endif /* MMAL_MSG_FORMAT_H */
  14939. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg.h
  14940. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg.h 1970-01-01 01:00:00.000000000 +0100
  14941. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg.h 2014-04-24 15:35:02.713549510 +0200
  14942. @@ -0,0 +1,404 @@
  14943. +/*
  14944. + * Broadcom BM2835 V4L2 driver
  14945. + *
  14946. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14947. + *
  14948. + * This file is subject to the terms and conditions of the GNU General Public
  14949. + * License. See the file COPYING in the main directory of this archive
  14950. + * for more details.
  14951. + *
  14952. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14953. + * Dave Stevenson <dsteve@broadcom.com>
  14954. + * Simon Mellor <simellor@broadcom.com>
  14955. + * Luke Diamand <luked@broadcom.com>
  14956. + */
  14957. +
  14958. +/* all the data structures which serialise the MMAL protocol. note
  14959. + * these are directly mapped onto the recived message data.
  14960. + *
  14961. + * BEWARE: They seem to *assume* pointers are u32 and that there is no
  14962. + * structure padding!
  14963. + *
  14964. + * NOTE: this implementation uses kernel types to ensure sizes. Rather
  14965. + * than assigning values to enums to force their size the
  14966. + * implementation uses fixed size types and not the enums (though the
  14967. + * comments have the actual enum type
  14968. + */
  14969. +
  14970. +#define VC_MMAL_VER 15
  14971. +#define VC_MMAL_MIN_VER 10
  14972. +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
  14973. +
  14974. +/* max total message size is 512 bytes */
  14975. +#define MMAL_MSG_MAX_SIZE 512
  14976. +/* with six 32bit header elements max payload is therefore 488 bytes */
  14977. +#define MMAL_MSG_MAX_PAYLOAD 488
  14978. +
  14979. +#include "mmal-msg-common.h"
  14980. +#include "mmal-msg-format.h"
  14981. +#include "mmal-msg-port.h"
  14982. +
  14983. +enum mmal_msg_type {
  14984. + MMAL_MSG_TYPE_QUIT = 1,
  14985. + MMAL_MSG_TYPE_SERVICE_CLOSED,
  14986. + MMAL_MSG_TYPE_GET_VERSION,
  14987. + MMAL_MSG_TYPE_COMPONENT_CREATE,
  14988. + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
  14989. + MMAL_MSG_TYPE_COMPONENT_ENABLE,
  14990. + MMAL_MSG_TYPE_COMPONENT_DISABLE,
  14991. + MMAL_MSG_TYPE_PORT_INFO_GET,
  14992. + MMAL_MSG_TYPE_PORT_INFO_SET,
  14993. + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
  14994. + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
  14995. + MMAL_MSG_TYPE_BUFFER_TO_HOST,
  14996. + MMAL_MSG_TYPE_GET_STATS,
  14997. + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
  14998. + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
  14999. + MMAL_MSG_TYPE_EVENT_TO_HOST,
  15000. + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
  15001. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
  15002. + MMAL_MSG_TYPE_CONSUME_MEM,
  15003. + MMAL_MSG_TYPE_LMK, /* 20 */
  15004. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
  15005. + MMAL_MSG_TYPE_DRM_GET_LHS32,
  15006. + MMAL_MSG_TYPE_DRM_GET_TIME,
  15007. + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
  15008. + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
  15009. + MMAL_MSG_TYPE_HOST_LOG,
  15010. + MMAL_MSG_TYPE_MSG_LAST
  15011. +};
  15012. +
  15013. +/* port action request messages differ depending on the action type */
  15014. +enum mmal_msg_port_action_type {
  15015. + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
  15016. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
  15017. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
  15018. + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
  15019. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
  15020. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
  15021. + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
  15022. +};
  15023. +
  15024. +struct mmal_msg_header {
  15025. + u32 magic;
  15026. + u32 type; /** enum mmal_msg_type */
  15027. +
  15028. + /* Opaque handle to the control service */
  15029. + struct mmal_control_service *control_service;
  15030. +
  15031. + struct mmal_msg_context *context; /** a u32 per message context */
  15032. + u32 status; /** The status of the vchiq operation */
  15033. + u32 padding;
  15034. +};
  15035. +
  15036. +/* Send from VC to host to report version */
  15037. +struct mmal_msg_version {
  15038. + u32 flags;
  15039. + u32 major;
  15040. + u32 minor;
  15041. + u32 minimum;
  15042. +};
  15043. +
  15044. +/* request to VC to create component */
  15045. +struct mmal_msg_component_create {
  15046. + void *client_component; /* component context */
  15047. + char name[128];
  15048. + u32 pid; /* For debug */
  15049. +};
  15050. +
  15051. +/* reply from VC to component creation request */
  15052. +struct mmal_msg_component_create_reply {
  15053. + u32 status; /** enum mmal_msg_status - how does this differ to
  15054. + * the one in the header?
  15055. + */
  15056. + u32 component_handle; /* VideoCore handle for component */
  15057. + u32 input_num; /* Number of input ports */
  15058. + u32 output_num; /* Number of output ports */
  15059. + u32 clock_num; /* Number of clock ports */
  15060. +};
  15061. +
  15062. +/* request to VC to destroy a component */
  15063. +struct mmal_msg_component_destroy {
  15064. + u32 component_handle;
  15065. +};
  15066. +
  15067. +struct mmal_msg_component_destroy_reply {
  15068. + u32 status; /** The component destruction status */
  15069. +};
  15070. +
  15071. +
  15072. +/* request and reply to VC to enable a component */
  15073. +struct mmal_msg_component_enable {
  15074. + u32 component_handle;
  15075. +};
  15076. +
  15077. +struct mmal_msg_component_enable_reply {
  15078. + u32 status; /** The component enable status */
  15079. +};
  15080. +
  15081. +
  15082. +/* request and reply to VC to disable a component */
  15083. +struct mmal_msg_component_disable {
  15084. + u32 component_handle;
  15085. +};
  15086. +
  15087. +struct mmal_msg_component_disable_reply {
  15088. + u32 status; /** The component disable status */
  15089. +};
  15090. +
  15091. +/* request to VC to get port information */
  15092. +struct mmal_msg_port_info_get {
  15093. + u32 component_handle; /* component handle port is associated with */
  15094. + u32 port_type; /* enum mmal_msg_port_type */
  15095. + u32 index; /* port index to query */
  15096. +};
  15097. +
  15098. +/* reply from VC to get port info request */
  15099. +struct mmal_msg_port_info_get_reply {
  15100. + u32 status; /** enum mmal_msg_status */
  15101. + u32 component_handle; /* component handle port is associated with */
  15102. + u32 port_type; /* enum mmal_msg_port_type */
  15103. + u32 port_index; /* port indexed in query */
  15104. + s32 found; /* unused */
  15105. + u32 port_handle; /**< Handle to use for this port */
  15106. + struct mmal_port port;
  15107. + struct mmal_es_format format; /* elementry stream format */
  15108. + union mmal_es_specific_format es; /* es type specific data */
  15109. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
  15110. +};
  15111. +
  15112. +/* request to VC to set port information */
  15113. +struct mmal_msg_port_info_set {
  15114. + u32 component_handle;
  15115. + u32 port_type; /* enum mmal_msg_port_type */
  15116. + u32 port_index; /* port indexed in query */
  15117. + struct mmal_port port;
  15118. + struct mmal_es_format format;
  15119. + union mmal_es_specific_format es;
  15120. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  15121. +};
  15122. +
  15123. +/* reply from VC to port info set request */
  15124. +struct mmal_msg_port_info_set_reply {
  15125. + u32 status;
  15126. + u32 component_handle; /* component handle port is associated with */
  15127. + u32 port_type; /* enum mmal_msg_port_type */
  15128. + u32 index; /* port indexed in query */
  15129. + s32 found; /* unused */
  15130. + u32 port_handle; /**< Handle to use for this port */
  15131. + struct mmal_port port;
  15132. + struct mmal_es_format format;
  15133. + union mmal_es_specific_format es;
  15134. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  15135. +};
  15136. +
  15137. +
  15138. +/* port action requests that take a mmal_port as a parameter */
  15139. +struct mmal_msg_port_action_port {
  15140. + u32 component_handle;
  15141. + u32 port_handle;
  15142. + u32 action; /* enum mmal_msg_port_action_type */
  15143. + struct mmal_port port;
  15144. +};
  15145. +
  15146. +/* port action requests that take handles as a parameter */
  15147. +struct mmal_msg_port_action_handle {
  15148. + u32 component_handle;
  15149. + u32 port_handle;
  15150. + u32 action; /* enum mmal_msg_port_action_type */
  15151. + u32 connect_component_handle;
  15152. + u32 connect_port_handle;
  15153. +};
  15154. +
  15155. +struct mmal_msg_port_action_reply {
  15156. + u32 status; /** The port action operation status */
  15157. +};
  15158. +
  15159. +
  15160. +
  15161. +
  15162. +/* MMAL buffer transfer */
  15163. +
  15164. +/** Size of space reserved in a buffer message for short messages. */
  15165. +#define MMAL_VC_SHORT_DATA 128
  15166. +
  15167. +/** Signals that the current payload is the end of the stream of data */
  15168. +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
  15169. +/** Signals that the start of the current payload starts a frame */
  15170. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
  15171. +/** Signals that the end of the current payload ends a frame */
  15172. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
  15173. +/** Signals that the current payload contains only complete frames (>1) */
  15174. +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
  15175. + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  15176. +/** Signals that the current payload is a keyframe (i.e. self decodable) */
  15177. +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
  15178. +/** Signals a discontinuity in the stream of data (e.g. after a seek).
  15179. + * Can be used for instance by a decoder to reset its state */
  15180. +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
  15181. +/** Signals a buffer containing some kind of config data for the component
  15182. + * (e.g. codec config data) */
  15183. +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
  15184. +/** Signals an encrypted payload */
  15185. +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
  15186. +/** Signals a buffer containing side information */
  15187. +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
  15188. +/** Signals a buffer which is the snapshot/postview image from a stills
  15189. + * capture
  15190. + */
  15191. +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
  15192. +/** Signals a buffer which contains data known to be corrupted */
  15193. +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
  15194. +/** Signals that a buffer failed to be transmitted */
  15195. +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
  15196. +
  15197. +struct mmal_driver_buffer {
  15198. + u32 magic;
  15199. + u32 component_handle;
  15200. + u32 port_handle;
  15201. + void *client_context;
  15202. +};
  15203. +
  15204. +/* buffer header */
  15205. +struct mmal_buffer_header {
  15206. + struct mmal_buffer_header *next; /* next header */
  15207. + void *priv; /* framework private data */
  15208. + u32 cmd;
  15209. + void *data;
  15210. + u32 alloc_size;
  15211. + u32 length;
  15212. + u32 offset;
  15213. + u32 flags;
  15214. + s64 pts;
  15215. + s64 dts;
  15216. + void *type;
  15217. + void *user_data;
  15218. +};
  15219. +
  15220. +struct mmal_buffer_header_type_specific {
  15221. + union {
  15222. + struct {
  15223. + u32 planes;
  15224. + u32 offset[4];
  15225. + u32 pitch[4];
  15226. + u32 flags;
  15227. + } video;
  15228. + } u;
  15229. +};
  15230. +
  15231. +struct mmal_msg_buffer_from_host {
  15232. + /* The front 32 bytes of the buffer header are copied
  15233. + * back to us in the reply to allow for context. This
  15234. + * area is used to store two mmal_driver_buffer structures to
  15235. + * allow for multiple concurrent service users.
  15236. + */
  15237. + /* control data */
  15238. + struct mmal_driver_buffer drvbuf;
  15239. +
  15240. + /* referenced control data for passthrough buffer management */
  15241. + struct mmal_driver_buffer drvbuf_ref;
  15242. + struct mmal_buffer_header buffer_header; /* buffer header itself */
  15243. + struct mmal_buffer_header_type_specific buffer_header_type_specific;
  15244. + s32 is_zero_copy;
  15245. + s32 has_reference;
  15246. +
  15247. + /** allows short data to be xfered in control message */
  15248. + u32 payload_in_message;
  15249. + u8 short_data[MMAL_VC_SHORT_DATA];
  15250. +};
  15251. +
  15252. +
  15253. +/* port parameter setting */
  15254. +
  15255. +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
  15256. +
  15257. +struct mmal_msg_port_parameter_set {
  15258. + u32 component_handle; /* component */
  15259. + u32 port_handle; /* port */
  15260. + u32 id; /* Parameter ID */
  15261. + u32 size; /* Parameter size */
  15262. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  15263. +};
  15264. +
  15265. +struct mmal_msg_port_parameter_set_reply {
  15266. + u32 status; /** enum mmal_msg_status todo: how does this
  15267. + * differ to the one in the header?
  15268. + */
  15269. +};
  15270. +
  15271. +/* port parameter getting */
  15272. +
  15273. +struct mmal_msg_port_parameter_get {
  15274. + u32 component_handle; /* component */
  15275. + u32 port_handle; /* port */
  15276. + u32 id; /* Parameter ID */
  15277. + u32 size; /* Parameter size */
  15278. +};
  15279. +
  15280. +struct mmal_msg_port_parameter_get_reply {
  15281. + u32 status; /* Status of mmal_port_parameter_get call */
  15282. + u32 id; /* Parameter ID */
  15283. + u32 size; /* Parameter size */
  15284. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  15285. +};
  15286. +
  15287. +/* event messages */
  15288. +#define MMAL_WORKER_EVENT_SPACE 256
  15289. +
  15290. +struct mmal_msg_event_to_host {
  15291. + void *client_component; /* component context */
  15292. +
  15293. + u32 port_type;
  15294. + u32 port_num;
  15295. +
  15296. + u32 cmd;
  15297. + u32 length;
  15298. + u8 data[MMAL_WORKER_EVENT_SPACE];
  15299. + struct mmal_buffer_header *delayed_buffer;
  15300. +};
  15301. +
  15302. +/* all mmal messages are serialised through this structure */
  15303. +struct mmal_msg {
  15304. + /* header */
  15305. + struct mmal_msg_header h;
  15306. + /* payload */
  15307. + union {
  15308. + struct mmal_msg_version version;
  15309. +
  15310. + struct mmal_msg_component_create component_create;
  15311. + struct mmal_msg_component_create_reply component_create_reply;
  15312. +
  15313. + struct mmal_msg_component_destroy component_destroy;
  15314. + struct mmal_msg_component_destroy_reply component_destroy_reply;
  15315. +
  15316. + struct mmal_msg_component_enable component_enable;
  15317. + struct mmal_msg_component_enable_reply component_enable_reply;
  15318. +
  15319. + struct mmal_msg_component_disable component_disable;
  15320. + struct mmal_msg_component_disable_reply component_disable_reply;
  15321. +
  15322. + struct mmal_msg_port_info_get port_info_get;
  15323. + struct mmal_msg_port_info_get_reply port_info_get_reply;
  15324. +
  15325. + struct mmal_msg_port_info_set port_info_set;
  15326. + struct mmal_msg_port_info_set_reply port_info_set_reply;
  15327. +
  15328. + struct mmal_msg_port_action_port port_action_port;
  15329. + struct mmal_msg_port_action_handle port_action_handle;
  15330. + struct mmal_msg_port_action_reply port_action_reply;
  15331. +
  15332. + struct mmal_msg_buffer_from_host buffer_from_host;
  15333. +
  15334. + struct mmal_msg_port_parameter_set port_parameter_set;
  15335. + struct mmal_msg_port_parameter_set_reply
  15336. + port_parameter_set_reply;
  15337. + struct mmal_msg_port_parameter_get
  15338. + port_parameter_get;
  15339. + struct mmal_msg_port_parameter_get_reply
  15340. + port_parameter_get_reply;
  15341. +
  15342. + struct mmal_msg_event_to_host event_to_host;
  15343. +
  15344. + u8 payload[MMAL_MSG_MAX_PAYLOAD];
  15345. + } u;
  15346. +};
  15347. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg-port.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h
  15348. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-msg-port.h 1970-01-01 01:00:00.000000000 +0100
  15349. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h 2014-04-24 15:35:02.713549510 +0200
  15350. @@ -0,0 +1,107 @@
  15351. +/*
  15352. + * Broadcom BM2835 V4L2 driver
  15353. + *
  15354. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15355. + *
  15356. + * This file is subject to the terms and conditions of the GNU General Public
  15357. + * License. See the file COPYING in the main directory of this archive
  15358. + * for more details.
  15359. + *
  15360. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15361. + * Dave Stevenson <dsteve@broadcom.com>
  15362. + * Simon Mellor <simellor@broadcom.com>
  15363. + * Luke Diamand <luked@broadcom.com>
  15364. + */
  15365. +
  15366. +/* MMAL_PORT_TYPE_T */
  15367. +enum mmal_port_type {
  15368. + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
  15369. + MMAL_PORT_TYPE_CONTROL, /**< Control port */
  15370. + MMAL_PORT_TYPE_INPUT, /**< Input port */
  15371. + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
  15372. + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
  15373. +};
  15374. +
  15375. +/** The port is pass-through and doesn't need buffer headers allocated */
  15376. +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
  15377. +/** The port wants to allocate the buffer payloads.
  15378. + * This signals a preference that payload allocation should be done
  15379. + * on this port for efficiency reasons. */
  15380. +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
  15381. +/** The port supports format change events.
  15382. + * This applies to input ports and is used to let the client know
  15383. + * whether the port supports being reconfigured via a format
  15384. + * change event (i.e. without having to disable the port). */
  15385. +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
  15386. +
  15387. +/* mmal port structure (MMAL_PORT_T)
  15388. + *
  15389. + * most elements are informational only, the pointer values for
  15390. + * interogation messages are generally provided as additional
  15391. + * strucures within the message. When used to set values only teh
  15392. + * buffer_num, buffer_size and userdata parameters are writable.
  15393. + */
  15394. +struct mmal_port {
  15395. + void *priv; /* Private member used by the framework */
  15396. + const char *name; /* Port name. Used for debugging purposes (RO) */
  15397. +
  15398. + u32 type; /* Type of the port (RO) enum mmal_port_type */
  15399. + u16 index; /* Index of the port in its type list (RO) */
  15400. + u16 index_all; /* Index of the port in the list of all ports (RO) */
  15401. +
  15402. + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
  15403. + struct mmal_es_format *format; /* Format of the elementary stream */
  15404. +
  15405. + u32 buffer_num_min; /* Minimum number of buffers the port
  15406. + * requires (RO). This is set by the
  15407. + * component.
  15408. + */
  15409. +
  15410. + u32 buffer_size_min; /* Minimum size of buffers the port
  15411. + * requires (RO). This is set by the
  15412. + * component.
  15413. + */
  15414. +
  15415. + u32 buffer_alignment_min; /* Minimum alignment requirement for
  15416. + * the buffers (RO). A value of
  15417. + * zero means no special alignment
  15418. + * requirements. This is set by the
  15419. + * component.
  15420. + */
  15421. +
  15422. + u32 buffer_num_recommended; /* Number of buffers the port
  15423. + * recommends for optimal
  15424. + * performance (RO). A value of
  15425. + * zero means no special
  15426. + * recommendation. This is set
  15427. + * by the component.
  15428. + */
  15429. +
  15430. + u32 buffer_size_recommended; /* Size of buffers the port
  15431. + * recommends for optimal
  15432. + * performance (RO). A value of
  15433. + * zero means no special
  15434. + * recommendation. This is set
  15435. + * by the component.
  15436. + */
  15437. +
  15438. + u32 buffer_num; /* Actual number of buffers the port will use.
  15439. + * This is set by the client.
  15440. + */
  15441. +
  15442. + u32 buffer_size; /* Actual maximum size of the buffers that
  15443. + * will be sent to the port. This is set by
  15444. + * the client.
  15445. + */
  15446. +
  15447. + void *component; /* Component this port belongs to (Read Only) */
  15448. +
  15449. + void *userdata; /* Field reserved for use by the client */
  15450. +
  15451. + u32 capabilities; /* Flags describing the capabilities of a
  15452. + * port (RO). Bitwise combination of \ref
  15453. + * portcapabilities "Port capabilities"
  15454. + * values.
  15455. + */
  15456. +
  15457. +};
  15458. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-parameters.h linux-rpi/drivers/media/platform/bcm2835/mmal-parameters.h
  15459. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-parameters.h 1970-01-01 01:00:00.000000000 +0100
  15460. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-parameters.h 2014-04-24 15:35:02.713549510 +0200
  15461. @@ -0,0 +1,655 @@
  15462. +/*
  15463. + * Broadcom BM2835 V4L2 driver
  15464. + *
  15465. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15466. + *
  15467. + * This file is subject to the terms and conditions of the GNU General Public
  15468. + * License. See the file COPYING in the main directory of this archive
  15469. + * for more details.
  15470. + *
  15471. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15472. + * Dave Stevenson <dsteve@broadcom.com>
  15473. + * Simon Mellor <simellor@broadcom.com>
  15474. + * Luke Diamand <luked@broadcom.com>
  15475. + */
  15476. +
  15477. +/* common parameters */
  15478. +
  15479. +/** @name Parameter groups
  15480. + * Parameters are divided into groups, and then allocated sequentially within
  15481. + * a group using an enum.
  15482. + * @{
  15483. + */
  15484. +
  15485. +/** Common parameter ID group, used with many types of component. */
  15486. +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
  15487. +/** Camera-specific parameter ID group. */
  15488. +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
  15489. +/** Video-specific parameter ID group. */
  15490. +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
  15491. +/** Audio-specific parameter ID group. */
  15492. +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
  15493. +/** Clock-specific parameter ID group. */
  15494. +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
  15495. +/** Miracast-specific parameter ID group. */
  15496. +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
  15497. +
  15498. +/* Common parameters */
  15499. +enum mmal_parameter_common_type {
  15500. + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
  15501. + = MMAL_PARAMETER_GROUP_COMMON,
  15502. + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
  15503. + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
  15504. +
  15505. + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
  15506. + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
  15507. +
  15508. + /** MMAL_PARAMETER_BOOLEAN_T */
  15509. + MMAL_PARAMETER_ZERO_COPY,
  15510. +
  15511. + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
  15512. + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
  15513. +
  15514. + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
  15515. + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
  15516. + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
  15517. + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
  15518. + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
  15519. + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
  15520. + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
  15521. + MMAL_PARAMETER_SYSTEM_TIME /**< MMAL_PARAMETER_UINT64_T */
  15522. +};
  15523. +
  15524. +/* camera parameters */
  15525. +
  15526. +enum mmal_parameter_camera_type {
  15527. + /* 0 */
  15528. + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
  15529. + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
  15530. + = MMAL_PARAMETER_GROUP_CAMERA,
  15531. + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
  15532. + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
  15533. + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15534. + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
  15535. + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
  15536. + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
  15537. + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
  15538. + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
  15539. + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
  15540. + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
  15541. + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
  15542. + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
  15543. + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  15544. + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
  15545. + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
  15546. +
  15547. + /* 0x10 */
  15548. + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
  15549. + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15550. + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
  15551. + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
  15552. + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
  15553. + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
  15554. + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
  15555. + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
  15556. + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15557. + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
  15558. + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
  15559. + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
  15560. + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
  15561. + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15562. + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
  15563. + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15564. +
  15565. + /* 0x20 */
  15566. + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
  15567. + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15568. + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15569. + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
  15570. + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
  15571. + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
  15572. + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
  15573. + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
  15574. + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
  15575. + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15576. + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
  15577. + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
  15578. + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15579. + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15580. + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15581. + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  15582. +
  15583. + /* 0x30 */
  15584. + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
  15585. + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15586. +
  15587. + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
  15588. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  15589. +
  15590. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15591. + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
  15592. +
  15593. + /** @ref MMAL_PARAMETER_UINT32_T */
  15594. + MMAL_PARAMETER_CAMERA_MIN_ISO,
  15595. +
  15596. + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
  15597. + MMAL_PARAMETER_CAMERA_USE_CASE,
  15598. +
  15599. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15600. + MMAL_PARAMETER_CAPTURE_STATS_PASS,
  15601. +
  15602. + /** @ref MMAL_PARAMETER_UINT32_T */
  15603. + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
  15604. +
  15605. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15606. + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
  15607. +
  15608. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15609. + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
  15610. +
  15611. + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
  15612. + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
  15613. +
  15614. + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
  15615. + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
  15616. + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15617. + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
  15618. + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
  15619. + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  15620. +
  15621. + /* 0x40 */
  15622. + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15623. + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15624. + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15625. + MMAL_PARAMETER_SHUTTER_SPEED, /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
  15626. + MMAL_PARAMETER_CUSTOM_AWB_GAINS, /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
  15627. +};
  15628. +
  15629. +struct mmal_parameter_rational {
  15630. + s32 num; /**< Numerator */
  15631. + s32 den; /**< Denominator */
  15632. +};
  15633. +
  15634. +enum mmal_parameter_camera_config_timestamp_mode {
  15635. + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
  15636. + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
  15637. + * for the frame timestamp
  15638. + */
  15639. + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
  15640. + * but subtract the
  15641. + * timestamp of the first
  15642. + * frame sent to give a
  15643. + * zero based timestamp.
  15644. + */
  15645. +};
  15646. +
  15647. +struct mmal_parameter_fps_range {
  15648. + /**< Low end of the permitted framerate range */
  15649. + struct mmal_parameter_rational fps_low;
  15650. + /**< High end of the permitted framerate range */
  15651. + struct mmal_parameter_rational fps_high;
  15652. +};
  15653. +
  15654. +
  15655. +/* camera configuration parameter */
  15656. +struct mmal_parameter_camera_config {
  15657. + /* Parameters for setting up the image pools */
  15658. + u32 max_stills_w; /* Max size of stills capture */
  15659. + u32 max_stills_h;
  15660. + u32 stills_yuv422; /* Allow YUV422 stills capture */
  15661. + u32 one_shot_stills; /* Continuous or one shot stills captures. */
  15662. +
  15663. + u32 max_preview_video_w; /* Max size of the preview or video
  15664. + * capture frames
  15665. + */
  15666. + u32 max_preview_video_h;
  15667. + u32 num_preview_video_frames;
  15668. +
  15669. + /** Sets the height of the circular buffer for stills capture. */
  15670. + u32 stills_capture_circular_buffer_height;
  15671. +
  15672. + /** Allows preview/encode to resume as fast as possible after the stills
  15673. + * input frame has been received, and then processes the still frame in
  15674. + * the background whilst preview/encode has resumed.
  15675. + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
  15676. + */
  15677. + u32 fast_preview_resume;
  15678. +
  15679. + /** Selects algorithm for timestamping frames if
  15680. + * there is no clock component connected.
  15681. + * enum mmal_parameter_camera_config_timestamp_mode
  15682. + */
  15683. + s32 use_stc_timestamp;
  15684. +};
  15685. +
  15686. +
  15687. +enum mmal_parameter_exposuremode {
  15688. + MMAL_PARAM_EXPOSUREMODE_OFF,
  15689. + MMAL_PARAM_EXPOSUREMODE_AUTO,
  15690. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  15691. + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
  15692. + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
  15693. + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
  15694. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  15695. + MMAL_PARAM_EXPOSUREMODE_SNOW,
  15696. + MMAL_PARAM_EXPOSUREMODE_BEACH,
  15697. + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
  15698. + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
  15699. + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
  15700. + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
  15701. +};
  15702. +
  15703. +enum mmal_parameter_exposuremeteringmode {
  15704. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
  15705. + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
  15706. + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
  15707. + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
  15708. +};
  15709. +
  15710. +enum mmal_parameter_awbmode {
  15711. + MMAL_PARAM_AWBMODE_OFF,
  15712. + MMAL_PARAM_AWBMODE_AUTO,
  15713. + MMAL_PARAM_AWBMODE_SUNLIGHT,
  15714. + MMAL_PARAM_AWBMODE_CLOUDY,
  15715. + MMAL_PARAM_AWBMODE_SHADE,
  15716. + MMAL_PARAM_AWBMODE_TUNGSTEN,
  15717. + MMAL_PARAM_AWBMODE_FLUORESCENT,
  15718. + MMAL_PARAM_AWBMODE_INCANDESCENT,
  15719. + MMAL_PARAM_AWBMODE_FLASH,
  15720. + MMAL_PARAM_AWBMODE_HORIZON,
  15721. +};
  15722. +
  15723. +enum mmal_parameter_imagefx {
  15724. + MMAL_PARAM_IMAGEFX_NONE,
  15725. + MMAL_PARAM_IMAGEFX_NEGATIVE,
  15726. + MMAL_PARAM_IMAGEFX_SOLARIZE,
  15727. + MMAL_PARAM_IMAGEFX_POSTERIZE,
  15728. + MMAL_PARAM_IMAGEFX_WHITEBOARD,
  15729. + MMAL_PARAM_IMAGEFX_BLACKBOARD,
  15730. + MMAL_PARAM_IMAGEFX_SKETCH,
  15731. + MMAL_PARAM_IMAGEFX_DENOISE,
  15732. + MMAL_PARAM_IMAGEFX_EMBOSS,
  15733. + MMAL_PARAM_IMAGEFX_OILPAINT,
  15734. + MMAL_PARAM_IMAGEFX_HATCH,
  15735. + MMAL_PARAM_IMAGEFX_GPEN,
  15736. + MMAL_PARAM_IMAGEFX_PASTEL,
  15737. + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  15738. + MMAL_PARAM_IMAGEFX_FILM,
  15739. + MMAL_PARAM_IMAGEFX_BLUR,
  15740. + MMAL_PARAM_IMAGEFX_SATURATION,
  15741. + MMAL_PARAM_IMAGEFX_COLOURSWAP,
  15742. + MMAL_PARAM_IMAGEFX_WASHEDOUT,
  15743. + MMAL_PARAM_IMAGEFX_POSTERISE,
  15744. + MMAL_PARAM_IMAGEFX_COLOURPOINT,
  15745. + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  15746. + MMAL_PARAM_IMAGEFX_CARTOON,
  15747. +};
  15748. +
  15749. +enum MMAL_PARAM_FLICKERAVOID_T {
  15750. + MMAL_PARAM_FLICKERAVOID_OFF,
  15751. + MMAL_PARAM_FLICKERAVOID_AUTO,
  15752. + MMAL_PARAM_FLICKERAVOID_50HZ,
  15753. + MMAL_PARAM_FLICKERAVOID_60HZ,
  15754. + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
  15755. +};
  15756. +
  15757. +struct mmal_parameter_awbgains {
  15758. + struct mmal_parameter_rational r_gain; /**< Red gain */
  15759. + struct mmal_parameter_rational b_gain; /**< Blue gain */
  15760. +};
  15761. +
  15762. +/** Manner of video rate control */
  15763. +enum mmal_parameter_rate_control_mode {
  15764. + MMAL_VIDEO_RATECONTROL_DEFAULT,
  15765. + MMAL_VIDEO_RATECONTROL_VARIABLE,
  15766. + MMAL_VIDEO_RATECONTROL_CONSTANT,
  15767. + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
  15768. + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
  15769. +};
  15770. +
  15771. +enum mmal_video_profile {
  15772. + MMAL_VIDEO_PROFILE_H263_BASELINE,
  15773. + MMAL_VIDEO_PROFILE_H263_H320CODING,
  15774. + MMAL_VIDEO_PROFILE_H263_BACKWARDCOMPATIBLE,
  15775. + MMAL_VIDEO_PROFILE_H263_ISWV2,
  15776. + MMAL_VIDEO_PROFILE_H263_ISWV3,
  15777. + MMAL_VIDEO_PROFILE_H263_HIGHCOMPRESSION,
  15778. + MMAL_VIDEO_PROFILE_H263_INTERNET,
  15779. + MMAL_VIDEO_PROFILE_H263_INTERLACE,
  15780. + MMAL_VIDEO_PROFILE_H263_HIGHLATENCY,
  15781. + MMAL_VIDEO_PROFILE_MP4V_SIMPLE,
  15782. + MMAL_VIDEO_PROFILE_MP4V_SIMPLESCALABLE,
  15783. + MMAL_VIDEO_PROFILE_MP4V_CORE,
  15784. + MMAL_VIDEO_PROFILE_MP4V_MAIN,
  15785. + MMAL_VIDEO_PROFILE_MP4V_NBIT,
  15786. + MMAL_VIDEO_PROFILE_MP4V_SCALABLETEXTURE,
  15787. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFACE,
  15788. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFBA,
  15789. + MMAL_VIDEO_PROFILE_MP4V_BASICANIMATED,
  15790. + MMAL_VIDEO_PROFILE_MP4V_HYBRID,
  15791. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDREALTIME,
  15792. + MMAL_VIDEO_PROFILE_MP4V_CORESCALABLE,
  15793. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCODING,
  15794. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCORE,
  15795. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSCALABLE,
  15796. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSIMPLE,
  15797. + MMAL_VIDEO_PROFILE_H264_BASELINE,
  15798. + MMAL_VIDEO_PROFILE_H264_MAIN,
  15799. + MMAL_VIDEO_PROFILE_H264_EXTENDED,
  15800. + MMAL_VIDEO_PROFILE_H264_HIGH,
  15801. + MMAL_VIDEO_PROFILE_H264_HIGH10,
  15802. + MMAL_VIDEO_PROFILE_H264_HIGH422,
  15803. + MMAL_VIDEO_PROFILE_H264_HIGH444,
  15804. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
  15805. + MMAL_VIDEO_PROFILE_DUMMY = 0x7FFFFFFF
  15806. +};
  15807. +
  15808. +enum mmal_video_level {
  15809. + MMAL_VIDEO_LEVEL_H263_10,
  15810. + MMAL_VIDEO_LEVEL_H263_20,
  15811. + MMAL_VIDEO_LEVEL_H263_30,
  15812. + MMAL_VIDEO_LEVEL_H263_40,
  15813. + MMAL_VIDEO_LEVEL_H263_45,
  15814. + MMAL_VIDEO_LEVEL_H263_50,
  15815. + MMAL_VIDEO_LEVEL_H263_60,
  15816. + MMAL_VIDEO_LEVEL_H263_70,
  15817. + MMAL_VIDEO_LEVEL_MP4V_0,
  15818. + MMAL_VIDEO_LEVEL_MP4V_0b,
  15819. + MMAL_VIDEO_LEVEL_MP4V_1,
  15820. + MMAL_VIDEO_LEVEL_MP4V_2,
  15821. + MMAL_VIDEO_LEVEL_MP4V_3,
  15822. + MMAL_VIDEO_LEVEL_MP4V_4,
  15823. + MMAL_VIDEO_LEVEL_MP4V_4a,
  15824. + MMAL_VIDEO_LEVEL_MP4V_5,
  15825. + MMAL_VIDEO_LEVEL_MP4V_6,
  15826. + MMAL_VIDEO_LEVEL_H264_1,
  15827. + MMAL_VIDEO_LEVEL_H264_1b,
  15828. + MMAL_VIDEO_LEVEL_H264_11,
  15829. + MMAL_VIDEO_LEVEL_H264_12,
  15830. + MMAL_VIDEO_LEVEL_H264_13,
  15831. + MMAL_VIDEO_LEVEL_H264_2,
  15832. + MMAL_VIDEO_LEVEL_H264_21,
  15833. + MMAL_VIDEO_LEVEL_H264_22,
  15834. + MMAL_VIDEO_LEVEL_H264_3,
  15835. + MMAL_VIDEO_LEVEL_H264_31,
  15836. + MMAL_VIDEO_LEVEL_H264_32,
  15837. + MMAL_VIDEO_LEVEL_H264_4,
  15838. + MMAL_VIDEO_LEVEL_H264_41,
  15839. + MMAL_VIDEO_LEVEL_H264_42,
  15840. + MMAL_VIDEO_LEVEL_H264_5,
  15841. + MMAL_VIDEO_LEVEL_H264_51,
  15842. + MMAL_VIDEO_LEVEL_DUMMY = 0x7FFFFFFF
  15843. +};
  15844. +
  15845. +struct mmal_parameter_video_profile {
  15846. + enum mmal_video_profile profile;
  15847. + enum mmal_video_level level;
  15848. +};
  15849. +
  15850. +/* video parameters */
  15851. +
  15852. +enum mmal_parameter_video_type {
  15853. + /** @ref MMAL_DISPLAYREGION_T */
  15854. + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
  15855. +
  15856. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  15857. + MMAL_PARAMETER_SUPPORTED_PROFILES,
  15858. +
  15859. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  15860. + MMAL_PARAMETER_PROFILE,
  15861. +
  15862. + /** @ref MMAL_PARAMETER_UINT32_T */
  15863. + MMAL_PARAMETER_INTRAPERIOD,
  15864. +
  15865. + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
  15866. + MMAL_PARAMETER_RATECONTROL,
  15867. +
  15868. + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
  15869. + MMAL_PARAMETER_NALUNITFORMAT,
  15870. +
  15871. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15872. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  15873. +
  15874. + /** @ref MMAL_PARAMETER_UINT32_T.
  15875. + * Setting the value to zero resets to the default (one slice per frame).
  15876. + */
  15877. + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
  15878. +
  15879. + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
  15880. + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
  15881. +
  15882. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
  15883. + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
  15884. +
  15885. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
  15886. + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
  15887. +
  15888. + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
  15889. + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
  15890. + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
  15891. + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
  15892. +
  15893. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15894. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  15895. +
  15896. + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
  15897. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  15898. +
  15899. + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
  15900. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  15901. +
  15902. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15903. + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
  15904. +
  15905. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15906. + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
  15907. +
  15908. + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
  15909. + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
  15910. +
  15911. + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
  15912. + /** @ref MMAL_PARAMETER_UINT32_T.
  15913. + * Changing this parameter from the default can reduce frame rate
  15914. + * because image buffers need to be re-pitched.
  15915. + */
  15916. + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
  15917. +
  15918. + /** @ref MMAL_PARAMETER_UINT32_T.
  15919. + * Changing this parameter from the default can reduce frame rate
  15920. + * because image buffers need to be re-pitched.
  15921. + */
  15922. + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
  15923. +
  15924. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15925. + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
  15926. +
  15927. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15928. + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
  15929. +
  15930. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  15931. + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
  15932. +
  15933. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  15934. + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
  15935. +
  15936. + /** @ref MMAL_PARAMETER_UINT32_T */
  15937. + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
  15938. +
  15939. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15940. + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
  15941. +
  15942. + /* H264 specific parameters */
  15943. +
  15944. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15945. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
  15946. +
  15947. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15948. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
  15949. +
  15950. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15951. + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
  15952. +
  15953. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15954. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
  15955. +
  15956. + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
  15957. + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
  15958. +
  15959. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15960. + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
  15961. +
  15962. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15963. + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
  15964. +
  15965. + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
  15966. + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
  15967. +
  15968. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15969. + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
  15970. +
  15971. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15972. + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
  15973. +
  15974. + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
  15975. + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
  15976. +
  15977. + /** @ref MMAL_PARAMETER_BYTES_T */
  15978. + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
  15979. +
  15980. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15981. + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
  15982. +
  15983. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15984. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
  15985. +
  15986. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15987. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
  15988. +};
  15989. +
  15990. +/** Valid mirror modes */
  15991. +enum mmal_parameter_mirror {
  15992. + MMAL_PARAM_MIRROR_NONE,
  15993. + MMAL_PARAM_MIRROR_VERTICAL,
  15994. + MMAL_PARAM_MIRROR_HORIZONTAL,
  15995. + MMAL_PARAM_MIRROR_BOTH,
  15996. +};
  15997. +
  15998. +enum mmal_parameter_displaytransform {
  15999. + MMAL_DISPLAY_ROT0 = 0,
  16000. + MMAL_DISPLAY_MIRROR_ROT0 = 1,
  16001. + MMAL_DISPLAY_MIRROR_ROT180 = 2,
  16002. + MMAL_DISPLAY_ROT180 = 3,
  16003. + MMAL_DISPLAY_MIRROR_ROT90 = 4,
  16004. + MMAL_DISPLAY_ROT270 = 5,
  16005. + MMAL_DISPLAY_ROT90 = 6,
  16006. + MMAL_DISPLAY_MIRROR_ROT270 = 7,
  16007. +};
  16008. +
  16009. +enum mmal_parameter_displaymode {
  16010. + MMAL_DISPLAY_MODE_FILL = 0,
  16011. + MMAL_DISPLAY_MODE_LETTERBOX = 1,
  16012. +};
  16013. +
  16014. +enum mmal_parameter_displayset {
  16015. + MMAL_DISPLAY_SET_NONE = 0,
  16016. + MMAL_DISPLAY_SET_NUM = 1,
  16017. + MMAL_DISPLAY_SET_FULLSCREEN = 2,
  16018. + MMAL_DISPLAY_SET_TRANSFORM = 4,
  16019. + MMAL_DISPLAY_SET_DEST_RECT = 8,
  16020. + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
  16021. + MMAL_DISPLAY_SET_MODE = 0x20,
  16022. + MMAL_DISPLAY_SET_PIXEL = 0x40,
  16023. + MMAL_DISPLAY_SET_NOASPECT = 0x80,
  16024. + MMAL_DISPLAY_SET_LAYER = 0x100,
  16025. + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
  16026. + MMAL_DISPLAY_SET_ALPHA = 0x400,
  16027. +};
  16028. +
  16029. +struct mmal_parameter_displayregion {
  16030. + /** Bitfield that indicates which fields are set and should be
  16031. + * used. All other fields will maintain their current value.
  16032. + * \ref MMAL_DISPLAYSET_T defines the bits that can be
  16033. + * combined.
  16034. + */
  16035. + u32 set;
  16036. +
  16037. + /** Describes the display output device, with 0 typically
  16038. + * being a directly connected LCD display. The actual values
  16039. + * will depend on the hardware. Code using hard-wired numbers
  16040. + * (e.g. 2) is certain to fail.
  16041. + */
  16042. +
  16043. + u32 display_num;
  16044. + /** Indicates that we are using the full device screen area,
  16045. + * rather than a window of the display. If zero, then
  16046. + * dest_rect is used to specify a region of the display to
  16047. + * use.
  16048. + */
  16049. +
  16050. + s32 fullscreen;
  16051. + /** Indicates any rotation or flipping used to map frames onto
  16052. + * the natural display orientation.
  16053. + */
  16054. + u32 transform; /* enum mmal_parameter_displaytransform */
  16055. +
  16056. + /** Where to display the frame within the screen, if
  16057. + * fullscreen is zero.
  16058. + */
  16059. + struct vchiq_mmal_rect dest_rect;
  16060. +
  16061. + /** Indicates which area of the frame to display. If all
  16062. + * values are zero, the whole frame will be used.
  16063. + */
  16064. + struct vchiq_mmal_rect src_rect;
  16065. +
  16066. + /** If set to non-zero, indicates that any display scaling
  16067. + * should disregard the aspect ratio of the frame region being
  16068. + * displayed.
  16069. + */
  16070. + s32 noaspect;
  16071. +
  16072. + /** Indicates how the image should be scaled to fit the
  16073. + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
  16074. + * that the image should fill the screen by potentially
  16075. + * cropping the frames. Setting \code mode \endcode to \code
  16076. + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
  16077. + * source region should be displayed and black bars added if
  16078. + * necessary.
  16079. + */
  16080. + u32 mode; /* enum mmal_parameter_displaymode */
  16081. +
  16082. + /** If non-zero, defines the width of a source pixel relative
  16083. + * to \code pixel_y \endcode. If zero, then pixels default to
  16084. + * being square.
  16085. + */
  16086. + u32 pixel_x;
  16087. +
  16088. + /** If non-zero, defines the height of a source pixel relative
  16089. + * to \code pixel_x \endcode. If zero, then pixels default to
  16090. + * being square.
  16091. + */
  16092. + u32 pixel_y;
  16093. +
  16094. + /** Sets the relative depth of the images, with greater values
  16095. + * being in front of smaller values.
  16096. + */
  16097. + u32 layer;
  16098. +
  16099. + /** Set to non-zero to ensure copy protection is used on
  16100. + * output.
  16101. + */
  16102. + s32 copyprotect_required;
  16103. +
  16104. + /** Level of opacity of the layer, where zero is fully
  16105. + * transparent and 255 is fully opaque.
  16106. + */
  16107. + u32 alpha;
  16108. +};
  16109. +
  16110. +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
  16111. +
  16112. +struct mmal_parameter_imagefx_parameters {
  16113. + enum mmal_parameter_imagefx effect;
  16114. + u32 num_effect_params;
  16115. + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
  16116. +};
  16117. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-vchiq.c linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c
  16118. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  16119. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c 2014-04-24 15:35:02.713549510 +0200
  16120. @@ -0,0 +1,1916 @@
  16121. +/*
  16122. + * Broadcom BM2835 V4L2 driver
  16123. + *
  16124. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  16125. + *
  16126. + * This file is subject to the terms and conditions of the GNU General Public
  16127. + * License. See the file COPYING in the main directory of this archive
  16128. + * for more details.
  16129. + *
  16130. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  16131. + * Dave Stevenson <dsteve@broadcom.com>
  16132. + * Simon Mellor <simellor@broadcom.com>
  16133. + * Luke Diamand <luked@broadcom.com>
  16134. + *
  16135. + * V4L2 driver MMAL vchiq interface code
  16136. + */
  16137. +
  16138. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16139. +
  16140. +#include <linux/errno.h>
  16141. +#include <linux/kernel.h>
  16142. +#include <linux/mutex.h>
  16143. +#include <linux/mm.h>
  16144. +#include <linux/slab.h>
  16145. +#include <linux/completion.h>
  16146. +#include <linux/vmalloc.h>
  16147. +#include <asm/cacheflush.h>
  16148. +#include <media/videobuf2-vmalloc.h>
  16149. +
  16150. +#include "mmal-common.h"
  16151. +#include "mmal-vchiq.h"
  16152. +#include "mmal-msg.h"
  16153. +
  16154. +#define USE_VCHIQ_ARM
  16155. +#include "interface/vchi/vchi.h"
  16156. +
  16157. +/* maximum number of components supported */
  16158. +#define VCHIQ_MMAL_MAX_COMPONENTS 4
  16159. +
  16160. +/*#define FULL_MSG_DUMP 1*/
  16161. +
  16162. +#ifdef DEBUG
  16163. +static const char *const msg_type_names[] = {
  16164. + "UNKNOWN",
  16165. + "QUIT",
  16166. + "SERVICE_CLOSED",
  16167. + "GET_VERSION",
  16168. + "COMPONENT_CREATE",
  16169. + "COMPONENT_DESTROY",
  16170. + "COMPONENT_ENABLE",
  16171. + "COMPONENT_DISABLE",
  16172. + "PORT_INFO_GET",
  16173. + "PORT_INFO_SET",
  16174. + "PORT_ACTION",
  16175. + "BUFFER_FROM_HOST",
  16176. + "BUFFER_TO_HOST",
  16177. + "GET_STATS",
  16178. + "PORT_PARAMETER_SET",
  16179. + "PORT_PARAMETER_GET",
  16180. + "EVENT_TO_HOST",
  16181. + "GET_CORE_STATS_FOR_PORT",
  16182. + "OPAQUE_ALLOCATOR",
  16183. + "CONSUME_MEM",
  16184. + "LMK",
  16185. + "OPAQUE_ALLOCATOR_DESC",
  16186. + "DRM_GET_LHS32",
  16187. + "DRM_GET_TIME",
  16188. + "BUFFER_FROM_HOST_ZEROLEN",
  16189. + "PORT_FLUSH",
  16190. + "HOST_LOG",
  16191. +};
  16192. +#endif
  16193. +
  16194. +static const char *const port_action_type_names[] = {
  16195. + "UNKNOWN",
  16196. + "ENABLE",
  16197. + "DISABLE",
  16198. + "FLUSH",
  16199. + "CONNECT",
  16200. + "DISCONNECT",
  16201. + "SET_REQUIREMENTS",
  16202. +};
  16203. +
  16204. +#if defined(DEBUG)
  16205. +#if defined(FULL_MSG_DUMP)
  16206. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  16207. + do { \
  16208. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  16209. + msg_type_names[(MSG)->h.type], \
  16210. + (MSG)->h.type, (MSG_LEN)); \
  16211. + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
  16212. + 16, 4, (MSG), \
  16213. + sizeof(struct mmal_msg_header), 1); \
  16214. + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
  16215. + 16, 4, \
  16216. + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
  16217. + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
  16218. + } while (0)
  16219. +#else
  16220. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  16221. + { \
  16222. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  16223. + msg_type_names[(MSG)->h.type], \
  16224. + (MSG)->h.type, (MSG_LEN)); \
  16225. + }
  16226. +#endif
  16227. +#else
  16228. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
  16229. +#endif
  16230. +
  16231. +/* normal message context */
  16232. +struct mmal_msg_context {
  16233. + union {
  16234. + struct {
  16235. + /* work struct for defered callback - must come first */
  16236. + struct work_struct work;
  16237. + /* mmal instance */
  16238. + struct vchiq_mmal_instance *instance;
  16239. + /* mmal port */
  16240. + struct vchiq_mmal_port *port;
  16241. + /* actual buffer used to store bulk reply */
  16242. + struct mmal_buffer *buffer;
  16243. + /* amount of buffer used */
  16244. + unsigned long buffer_used;
  16245. + /* MMAL buffer flags */
  16246. + u32 mmal_flags;
  16247. + /* Presentation and Decode timestamps */
  16248. + s64 pts;
  16249. + s64 dts;
  16250. +
  16251. + int status; /* context status */
  16252. +
  16253. + } bulk; /* bulk data */
  16254. +
  16255. + struct {
  16256. + /* message handle to release */
  16257. + VCHI_HELD_MSG_T msg_handle;
  16258. + /* pointer to received message */
  16259. + struct mmal_msg *msg;
  16260. + /* received message length */
  16261. + u32 msg_len;
  16262. + /* completion upon reply */
  16263. + struct completion cmplt;
  16264. + } sync; /* synchronous response */
  16265. + } u;
  16266. +
  16267. +};
  16268. +
  16269. +struct vchiq_mmal_instance {
  16270. + VCHI_SERVICE_HANDLE_T handle;
  16271. +
  16272. + /* ensure serialised access to service */
  16273. + struct mutex vchiq_mutex;
  16274. +
  16275. + /* ensure serialised access to bulk operations */
  16276. + struct mutex bulk_mutex;
  16277. +
  16278. + /* vmalloc page to receive scratch bulk xfers into */
  16279. + void *bulk_scratch;
  16280. +
  16281. + /* component to use next */
  16282. + int component_idx;
  16283. + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
  16284. +};
  16285. +
  16286. +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
  16287. + *instance)
  16288. +{
  16289. + struct mmal_msg_context *msg_context;
  16290. +
  16291. + /* todo: should this be allocated from a pool to avoid kmalloc */
  16292. + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
  16293. + memset(msg_context, 0, sizeof(*msg_context));
  16294. +
  16295. + return msg_context;
  16296. +}
  16297. +
  16298. +static void release_msg_context(struct mmal_msg_context *msg_context)
  16299. +{
  16300. + kfree(msg_context);
  16301. +}
  16302. +
  16303. +/* deals with receipt of event to host message */
  16304. +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
  16305. + struct mmal_msg *msg, u32 msg_len)
  16306. +{
  16307. + pr_debug("unhandled event\n");
  16308. + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
  16309. + msg->u.event_to_host.client_component,
  16310. + msg->u.event_to_host.port_type,
  16311. + msg->u.event_to_host.port_num,
  16312. + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
  16313. +}
  16314. +
  16315. +/* workqueue scheduled callback
  16316. + *
  16317. + * we do this because it is important we do not call any other vchiq
  16318. + * sync calls from witin the message delivery thread
  16319. + */
  16320. +static void buffer_work_cb(struct work_struct *work)
  16321. +{
  16322. + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
  16323. +
  16324. + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
  16325. + msg_context->u.bulk.port,
  16326. + msg_context->u.bulk.status,
  16327. + msg_context->u.bulk.buffer,
  16328. + msg_context->u.bulk.buffer_used,
  16329. + msg_context->u.bulk.mmal_flags,
  16330. + msg_context->u.bulk.dts,
  16331. + msg_context->u.bulk.pts);
  16332. +
  16333. + /* release message context */
  16334. + release_msg_context(msg_context);
  16335. +}
  16336. +
  16337. +/* enqueue a bulk receive for a given message context */
  16338. +static int bulk_receive(struct vchiq_mmal_instance *instance,
  16339. + struct mmal_msg *msg,
  16340. + struct mmal_msg_context *msg_context)
  16341. +{
  16342. + unsigned long rd_len;
  16343. + unsigned long flags = 0;
  16344. + int ret;
  16345. +
  16346. + /* bulk mutex stops other bulk operations while we have a
  16347. + * receive in progress - released in callback
  16348. + */
  16349. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  16350. + if (ret != 0)
  16351. + return ret;
  16352. +
  16353. + rd_len = msg->u.buffer_from_host.buffer_header.length;
  16354. +
  16355. + /* take buffer from queue */
  16356. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  16357. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  16358. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16359. + pr_err("buffer list empty trying to submit bulk receive\n");
  16360. +
  16361. + /* todo: this is a serious error, we should never have
  16362. + * commited a buffer_to_host operation to the mmal
  16363. + * port without the buffer to back it up (underflow
  16364. + * handling) and there is no obvious way to deal with
  16365. + * this - how is the mmal servie going to react when
  16366. + * we fail to do the xfer and reschedule a buffer when
  16367. + * it arrives? perhaps a starved flag to indicate a
  16368. + * waiting bulk receive?
  16369. + */
  16370. +
  16371. + mutex_unlock(&instance->bulk_mutex);
  16372. +
  16373. + return -EINVAL;
  16374. + }
  16375. +
  16376. + msg_context->u.bulk.buffer =
  16377. + list_entry(msg_context->u.bulk.port->buffers.next,
  16378. + struct mmal_buffer, list);
  16379. + list_del(&msg_context->u.bulk.buffer->list);
  16380. +
  16381. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16382. +
  16383. + /* ensure we do not overrun the available buffer */
  16384. + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
  16385. + rd_len = msg_context->u.bulk.buffer->buffer_size;
  16386. + pr_warn("short read as not enough receive buffer space\n");
  16387. + /* todo: is this the correct response, what happens to
  16388. + * the rest of the message data?
  16389. + */
  16390. + }
  16391. +
  16392. + /* store length */
  16393. + msg_context->u.bulk.buffer_used = rd_len;
  16394. + msg_context->u.bulk.mmal_flags =
  16395. + msg->u.buffer_from_host.buffer_header.flags;
  16396. + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
  16397. + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
  16398. +
  16399. + // only need to flush L1 cache here, as VCHIQ takes care of the L2
  16400. + // cache.
  16401. + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
  16402. +
  16403. + /* queue the bulk submission */
  16404. + vchi_service_use(instance->handle);
  16405. + ret = vchi_bulk_queue_receive(instance->handle,
  16406. + msg_context->u.bulk.buffer->buffer,
  16407. + /* Actual receive needs to be a multiple
  16408. + * of 4 bytes
  16409. + */
  16410. + (rd_len + 3) & ~3,
  16411. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  16412. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  16413. + msg_context);
  16414. +
  16415. + vchi_service_release(instance->handle);
  16416. +
  16417. + if (ret != 0) {
  16418. + /* callback will not be clearing the mutex */
  16419. + mutex_unlock(&instance->bulk_mutex);
  16420. + }
  16421. +
  16422. + return ret;
  16423. +}
  16424. +
  16425. +/* enque a dummy bulk receive for a given message context */
  16426. +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
  16427. + struct mmal_msg_context *msg_context)
  16428. +{
  16429. + int ret;
  16430. +
  16431. + /* bulk mutex stops other bulk operations while we have a
  16432. + * receive in progress - released in callback
  16433. + */
  16434. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  16435. + if (ret != 0)
  16436. + return ret;
  16437. +
  16438. + /* zero length indicates this was a dummy transfer */
  16439. + msg_context->u.bulk.buffer_used = 0;
  16440. +
  16441. + /* queue the bulk submission */
  16442. + vchi_service_use(instance->handle);
  16443. +
  16444. + ret = vchi_bulk_queue_receive(instance->handle,
  16445. + instance->bulk_scratch,
  16446. + 8,
  16447. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  16448. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  16449. + msg_context);
  16450. +
  16451. + vchi_service_release(instance->handle);
  16452. +
  16453. + if (ret != 0) {
  16454. + /* callback will not be clearing the mutex */
  16455. + mutex_unlock(&instance->bulk_mutex);
  16456. + }
  16457. +
  16458. + return ret;
  16459. +}
  16460. +
  16461. +/* data in message, memcpy from packet into output buffer */
  16462. +static int inline_receive(struct vchiq_mmal_instance *instance,
  16463. + struct mmal_msg *msg,
  16464. + struct mmal_msg_context *msg_context)
  16465. +{
  16466. + unsigned long flags = 0;
  16467. +
  16468. + /* take buffer from queue */
  16469. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  16470. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  16471. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16472. + pr_err("buffer list empty trying to receive inline\n");
  16473. +
  16474. + /* todo: this is a serious error, we should never have
  16475. + * commited a buffer_to_host operation to the mmal
  16476. + * port without the buffer to back it up (with
  16477. + * underflow handling) and there is no obvious way to
  16478. + * deal with this. Less bad than the bulk case as we
  16479. + * can just drop this on the floor but...unhelpful
  16480. + */
  16481. + return -EINVAL;
  16482. + }
  16483. +
  16484. + msg_context->u.bulk.buffer =
  16485. + list_entry(msg_context->u.bulk.port->buffers.next,
  16486. + struct mmal_buffer, list);
  16487. + list_del(&msg_context->u.bulk.buffer->list);
  16488. +
  16489. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  16490. +
  16491. + memcpy(msg_context->u.bulk.buffer->buffer,
  16492. + msg->u.buffer_from_host.short_data,
  16493. + msg->u.buffer_from_host.payload_in_message);
  16494. +
  16495. + msg_context->u.bulk.buffer_used =
  16496. + msg->u.buffer_from_host.payload_in_message;
  16497. +
  16498. + return 0;
  16499. +}
  16500. +
  16501. +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
  16502. +static int
  16503. +buffer_from_host(struct vchiq_mmal_instance *instance,
  16504. + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
  16505. +{
  16506. + struct mmal_msg_context *msg_context;
  16507. + struct mmal_msg m;
  16508. + int ret;
  16509. +
  16510. + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
  16511. +
  16512. + /* bulk mutex stops other bulk operations while we
  16513. + * have a receive in progress
  16514. + */
  16515. + if (mutex_lock_interruptible(&instance->bulk_mutex))
  16516. + return -EINTR;
  16517. +
  16518. + /* get context */
  16519. + msg_context = get_msg_context(instance);
  16520. + if (msg_context == NULL)
  16521. + return -ENOMEM;
  16522. +
  16523. + /* store bulk message context for when data arrives */
  16524. + msg_context->u.bulk.instance = instance;
  16525. + msg_context->u.bulk.port = port;
  16526. + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
  16527. + msg_context->u.bulk.buffer_used = 0;
  16528. +
  16529. + /* initialise work structure ready to schedule callback */
  16530. + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
  16531. +
  16532. + /* prep the buffer from host message */
  16533. + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
  16534. +
  16535. + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
  16536. + m.h.magic = MMAL_MAGIC;
  16537. + m.h.context = msg_context;
  16538. + m.h.status = 0;
  16539. +
  16540. + /* drvbuf is our private data passed back */
  16541. + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
  16542. + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
  16543. + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
  16544. + m.u.buffer_from_host.drvbuf.client_context = msg_context;
  16545. +
  16546. + /* buffer header */
  16547. + m.u.buffer_from_host.buffer_header.cmd = 0;
  16548. + m.u.buffer_from_host.buffer_header.data = buf->buffer;
  16549. + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
  16550. + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
  16551. + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
  16552. + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
  16553. + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
  16554. + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
  16555. +
  16556. + /* clear buffer type sepecific data */
  16557. + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
  16558. + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
  16559. +
  16560. + /* no payload in message */
  16561. + m.u.buffer_from_host.payload_in_message = 0;
  16562. +
  16563. + vchi_service_use(instance->handle);
  16564. +
  16565. + ret = vchi_msg_queue(instance->handle, &m,
  16566. + sizeof(struct mmal_msg_header) +
  16567. + sizeof(m.u.buffer_from_host),
  16568. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  16569. +
  16570. + if (ret != 0) {
  16571. + release_msg_context(msg_context);
  16572. + /* todo: is this correct error value? */
  16573. + }
  16574. +
  16575. + vchi_service_release(instance->handle);
  16576. +
  16577. + mutex_unlock(&instance->bulk_mutex);
  16578. +
  16579. + return ret;
  16580. +}
  16581. +
  16582. +/* submit a buffer to the mmal sevice
  16583. + *
  16584. + * the buffer_from_host uses size data from the ports next available
  16585. + * mmal_buffer and deals with there being no buffer available by
  16586. + * incrementing the underflow for later
  16587. + */
  16588. +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
  16589. + struct vchiq_mmal_port *port)
  16590. +{
  16591. + int ret;
  16592. + struct mmal_buffer *buf;
  16593. + unsigned long flags = 0;
  16594. +
  16595. + if (!port->enabled)
  16596. + return -EINVAL;
  16597. +
  16598. + /* peek buffer from queue */
  16599. + spin_lock_irqsave(&port->slock, flags);
  16600. + if (list_empty(&port->buffers)) {
  16601. + port->buffer_underflow++;
  16602. + spin_unlock_irqrestore(&port->slock, flags);
  16603. + return -ENOSPC;
  16604. + }
  16605. +
  16606. + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
  16607. +
  16608. + spin_unlock_irqrestore(&port->slock, flags);
  16609. +
  16610. + /* issue buffer to mmal service */
  16611. + ret = buffer_from_host(instance, port, buf);
  16612. + if (ret) {
  16613. + pr_err("adding buffer header failed\n");
  16614. + /* todo: how should this be dealt with */
  16615. + }
  16616. +
  16617. + return ret;
  16618. +}
  16619. +
  16620. +/* deals with receipt of buffer to host message */
  16621. +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
  16622. + struct mmal_msg *msg, u32 msg_len)
  16623. +{
  16624. + struct mmal_msg_context *msg_context;
  16625. +
  16626. + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
  16627. + instance, msg, msg_len);
  16628. +
  16629. + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
  16630. + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
  16631. + } else {
  16632. + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
  16633. + return;
  16634. + }
  16635. +
  16636. + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
  16637. + /* message reception had an error */
  16638. + pr_warn("error %d in reply\n", msg->h.status);
  16639. +
  16640. + msg_context->u.bulk.status = msg->h.status;
  16641. +
  16642. + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
  16643. + /* empty buffer */
  16644. + if (msg->u.buffer_from_host.buffer_header.flags &
  16645. + MMAL_BUFFER_HEADER_FLAG_EOS) {
  16646. + msg_context->u.bulk.status =
  16647. + dummy_bulk_receive(instance, msg_context);
  16648. + if (msg_context->u.bulk.status == 0)
  16649. + return; /* successful bulk submission, bulk
  16650. + * completion will trigger callback
  16651. + */
  16652. + } else {
  16653. + /* do callback with empty buffer - not EOS though */
  16654. + msg_context->u.bulk.status = 0;
  16655. + msg_context->u.bulk.buffer_used = 0;
  16656. + }
  16657. + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
  16658. + /* data is not in message, queue a bulk receive */
  16659. + msg_context->u.bulk.status =
  16660. + bulk_receive(instance, msg, msg_context);
  16661. + if (msg_context->u.bulk.status == 0)
  16662. + return; /* successful bulk submission, bulk
  16663. + * completion will trigger callback
  16664. + */
  16665. +
  16666. + /* failed to submit buffer, this will end badly */
  16667. + pr_err("error %d on bulk submission\n",
  16668. + msg_context->u.bulk.status);
  16669. +
  16670. + } else if (msg->u.buffer_from_host.payload_in_message <=
  16671. + MMAL_VC_SHORT_DATA) {
  16672. + /* data payload within message */
  16673. + msg_context->u.bulk.status = inline_receive(instance, msg,
  16674. + msg_context);
  16675. + } else {
  16676. + pr_err("message with invalid short payload\n");
  16677. +
  16678. + /* signal error */
  16679. + msg_context->u.bulk.status = -EINVAL;
  16680. + msg_context->u.bulk.buffer_used =
  16681. + msg->u.buffer_from_host.payload_in_message;
  16682. + }
  16683. +
  16684. + /* replace the buffer header */
  16685. + port_buffer_from_host(instance, msg_context->u.bulk.port);
  16686. +
  16687. + /* schedule the port callback */
  16688. + schedule_work(&msg_context->u.bulk.work);
  16689. +}
  16690. +
  16691. +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
  16692. + struct mmal_msg_context *msg_context)
  16693. +{
  16694. + /* bulk receive operation complete */
  16695. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  16696. +
  16697. + /* replace the buffer header */
  16698. + port_buffer_from_host(msg_context->u.bulk.instance,
  16699. + msg_context->u.bulk.port);
  16700. +
  16701. + msg_context->u.bulk.status = 0;
  16702. +
  16703. + /* schedule the port callback */
  16704. + schedule_work(&msg_context->u.bulk.work);
  16705. +}
  16706. +
  16707. +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
  16708. + struct mmal_msg_context *msg_context)
  16709. +{
  16710. + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
  16711. +
  16712. + /* bulk receive operation complete */
  16713. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  16714. +
  16715. + /* replace the buffer header */
  16716. + port_buffer_from_host(msg_context->u.bulk.instance,
  16717. + msg_context->u.bulk.port);
  16718. +
  16719. + msg_context->u.bulk.status = -EINTR;
  16720. +
  16721. + schedule_work(&msg_context->u.bulk.work);
  16722. +}
  16723. +
  16724. +/* incoming event service callback */
  16725. +static void service_callback(void *param,
  16726. + const VCHI_CALLBACK_REASON_T reason,
  16727. + void *bulk_ctx)
  16728. +{
  16729. + struct vchiq_mmal_instance *instance = param;
  16730. + int status;
  16731. + u32 msg_len;
  16732. + struct mmal_msg *msg;
  16733. + VCHI_HELD_MSG_T msg_handle;
  16734. +
  16735. + if (!instance) {
  16736. + pr_err("Message callback passed NULL instance\n");
  16737. + return;
  16738. + }
  16739. +
  16740. + switch (reason) {
  16741. + case VCHI_CALLBACK_MSG_AVAILABLE:
  16742. + status = vchi_msg_hold(instance->handle, (void **)&msg,
  16743. + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
  16744. + if (status) {
  16745. + pr_err("Unable to dequeue a message (%d)\n", status);
  16746. + break;
  16747. + }
  16748. +
  16749. + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
  16750. +
  16751. + /* handling is different for buffer messages */
  16752. + switch (msg->h.type) {
  16753. +
  16754. + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
  16755. + vchi_held_msg_release(&msg_handle);
  16756. + break;
  16757. +
  16758. + case MMAL_MSG_TYPE_EVENT_TO_HOST:
  16759. + event_to_host_cb(instance, msg, msg_len);
  16760. + vchi_held_msg_release(&msg_handle);
  16761. +
  16762. + break;
  16763. +
  16764. + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
  16765. + buffer_to_host_cb(instance, msg, msg_len);
  16766. + vchi_held_msg_release(&msg_handle);
  16767. + break;
  16768. +
  16769. + default:
  16770. + /* messages dependant on header context to complete */
  16771. +
  16772. + /* todo: the msg.context really ought to be sanity
  16773. + * checked before we just use it, afaict it comes back
  16774. + * and is used raw from the videocore. Perhaps it
  16775. + * should be verified the address lies in the kernel
  16776. + * address space.
  16777. + */
  16778. + if (msg->h.context == NULL) {
  16779. + pr_err("received message context was null!\n");
  16780. + vchi_held_msg_release(&msg_handle);
  16781. + break;
  16782. + }
  16783. +
  16784. + /* fill in context values */
  16785. + msg->h.context->u.sync.msg_handle = msg_handle;
  16786. + msg->h.context->u.sync.msg = msg;
  16787. + msg->h.context->u.sync.msg_len = msg_len;
  16788. +
  16789. + /* todo: should this check (completion_done()
  16790. + * == 1) for no one waiting? or do we need a
  16791. + * flag to tell us the completion has been
  16792. + * interrupted so we can free the message and
  16793. + * its context. This probably also solves the
  16794. + * message arriving after interruption todo
  16795. + * below
  16796. + */
  16797. +
  16798. + /* complete message so caller knows it happened */
  16799. + complete(&msg->h.context->u.sync.cmplt);
  16800. + break;
  16801. + }
  16802. +
  16803. + break;
  16804. +
  16805. + case VCHI_CALLBACK_BULK_RECEIVED:
  16806. + bulk_receive_cb(instance, bulk_ctx);
  16807. + break;
  16808. +
  16809. + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
  16810. + bulk_abort_cb(instance, bulk_ctx);
  16811. + break;
  16812. +
  16813. + case VCHI_CALLBACK_SERVICE_CLOSED:
  16814. + /* TODO: consider if this requires action if received when
  16815. + * driver is not explicitly closing the service
  16816. + */
  16817. + break;
  16818. +
  16819. + default:
  16820. + pr_err("Received unhandled message reason %d\n", reason);
  16821. + break;
  16822. + }
  16823. +}
  16824. +
  16825. +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
  16826. + struct mmal_msg *msg,
  16827. + unsigned int payload_len,
  16828. + struct mmal_msg **msg_out,
  16829. + VCHI_HELD_MSG_T *msg_handle_out)
  16830. +{
  16831. + struct mmal_msg_context msg_context;
  16832. + int ret;
  16833. +
  16834. + /* payload size must not cause message to exceed max size */
  16835. + if (payload_len >
  16836. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
  16837. + pr_err("payload length %d exceeds max:%d\n", payload_len,
  16838. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
  16839. + return -EINVAL;
  16840. + }
  16841. +
  16842. + init_completion(&msg_context.u.sync.cmplt);
  16843. +
  16844. + msg->h.magic = MMAL_MAGIC;
  16845. + msg->h.context = &msg_context;
  16846. + msg->h.status = 0;
  16847. +
  16848. + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
  16849. + ">>> sync message");
  16850. +
  16851. + vchi_service_use(instance->handle);
  16852. +
  16853. + ret = vchi_msg_queue(instance->handle,
  16854. + msg,
  16855. + sizeof(struct mmal_msg_header) + payload_len,
  16856. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  16857. +
  16858. + vchi_service_release(instance->handle);
  16859. +
  16860. + if (ret) {
  16861. + pr_err("error %d queuing message\n", ret);
  16862. + return ret;
  16863. + }
  16864. +
  16865. + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, 3*HZ);
  16866. + if (ret <= 0) {
  16867. + pr_err("error %d waiting for sync completion\n", ret);
  16868. + if (ret == 0)
  16869. + ret = -ETIME;
  16870. + /* todo: what happens if the message arrives after aborting */
  16871. + return ret;
  16872. + }
  16873. +
  16874. + *msg_out = msg_context.u.sync.msg;
  16875. + *msg_handle_out = msg_context.u.sync.msg_handle;
  16876. +
  16877. + return 0;
  16878. +}
  16879. +
  16880. +static void dump_port_info(struct vchiq_mmal_port *port)
  16881. +{
  16882. + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
  16883. +
  16884. + pr_debug("buffer minimum num:%d size:%d align:%d\n",
  16885. + port->minimum_buffer.num,
  16886. + port->minimum_buffer.size, port->minimum_buffer.alignment);
  16887. +
  16888. + pr_debug("buffer recommended num:%d size:%d align:%d\n",
  16889. + port->recommended_buffer.num,
  16890. + port->recommended_buffer.size,
  16891. + port->recommended_buffer.alignment);
  16892. +
  16893. + pr_debug("buffer current values num:%d size:%d align:%d\n",
  16894. + port->current_buffer.num,
  16895. + port->current_buffer.size, port->current_buffer.alignment);
  16896. +
  16897. + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
  16898. + port->format.type,
  16899. + port->format.encoding, port->format.encoding_variant);
  16900. +
  16901. + pr_debug(" bitrate:%d flags:0x%x\n",
  16902. + port->format.bitrate, port->format.flags);
  16903. +
  16904. + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
  16905. + pr_debug
  16906. + ("es video format: width:%d height:%d colourspace:0x%x\n",
  16907. + port->es.video.width, port->es.video.height,
  16908. + port->es.video.color_space);
  16909. +
  16910. + pr_debug(" : crop xywh %d,%d,%d,%d\n",
  16911. + port->es.video.crop.x,
  16912. + port->es.video.crop.y,
  16913. + port->es.video.crop.width, port->es.video.crop.height);
  16914. + pr_debug(" : framerate %d/%d aspect %d/%d\n",
  16915. + port->es.video.frame_rate.num,
  16916. + port->es.video.frame_rate.den,
  16917. + port->es.video.par.num, port->es.video.par.den);
  16918. + }
  16919. +}
  16920. +
  16921. +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
  16922. +{
  16923. +
  16924. + /* todo do readonly fields need setting at all? */
  16925. + p->type = port->type;
  16926. + p->index = port->index;
  16927. + p->index_all = 0;
  16928. + p->is_enabled = port->enabled;
  16929. + p->buffer_num_min = port->minimum_buffer.num;
  16930. + p->buffer_size_min = port->minimum_buffer.size;
  16931. + p->buffer_alignment_min = port->minimum_buffer.alignment;
  16932. + p->buffer_num_recommended = port->recommended_buffer.num;
  16933. + p->buffer_size_recommended = port->recommended_buffer.size;
  16934. +
  16935. + /* only three writable fields in a port */
  16936. + p->buffer_num = port->current_buffer.num;
  16937. + p->buffer_size = port->current_buffer.size;
  16938. + p->userdata = port;
  16939. +}
  16940. +
  16941. +static int port_info_set(struct vchiq_mmal_instance *instance,
  16942. + struct vchiq_mmal_port *port)
  16943. +{
  16944. + int ret;
  16945. + struct mmal_msg m;
  16946. + struct mmal_msg *rmsg;
  16947. + VCHI_HELD_MSG_T rmsg_handle;
  16948. +
  16949. + pr_debug("setting port info port %p\n", port);
  16950. + if (!port)
  16951. + return -1;
  16952. + dump_port_info(port);
  16953. +
  16954. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
  16955. +
  16956. + m.u.port_info_set.component_handle = port->component->handle;
  16957. + m.u.port_info_set.port_type = port->type;
  16958. + m.u.port_info_set.port_index = port->index;
  16959. +
  16960. + port_to_mmal_msg(port, &m.u.port_info_set.port);
  16961. +
  16962. + /* elementry stream format setup */
  16963. + m.u.port_info_set.format.type = port->format.type;
  16964. + m.u.port_info_set.format.encoding = port->format.encoding;
  16965. + m.u.port_info_set.format.encoding_variant =
  16966. + port->format.encoding_variant;
  16967. + m.u.port_info_set.format.bitrate = port->format.bitrate;
  16968. + m.u.port_info_set.format.flags = port->format.flags;
  16969. +
  16970. + memcpy(&m.u.port_info_set.es, &port->es,
  16971. + sizeof(union mmal_es_specific_format));
  16972. +
  16973. + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
  16974. + memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
  16975. + port->format.extradata_size);
  16976. +
  16977. + ret = send_synchronous_mmal_msg(instance, &m,
  16978. + sizeof(m.u.port_info_set),
  16979. + &rmsg, &rmsg_handle);
  16980. + if (ret)
  16981. + return ret;
  16982. +
  16983. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
  16984. + /* got an unexpected message type in reply */
  16985. + ret = -EINVAL;
  16986. + goto release_msg;
  16987. + }
  16988. +
  16989. + /* return operation status */
  16990. + ret = -rmsg->u.port_info_get_reply.status;
  16991. +
  16992. + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
  16993. + port->component->handle, port->handle);
  16994. +
  16995. +release_msg:
  16996. + vchi_held_msg_release(&rmsg_handle);
  16997. +
  16998. + return ret;
  16999. +
  17000. +}
  17001. +
  17002. +/* use port info get message to retrive port information */
  17003. +static int port_info_get(struct vchiq_mmal_instance *instance,
  17004. + struct vchiq_mmal_port *port)
  17005. +{
  17006. + int ret;
  17007. + struct mmal_msg m;
  17008. + struct mmal_msg *rmsg;
  17009. + VCHI_HELD_MSG_T rmsg_handle;
  17010. +
  17011. + /* port info time */
  17012. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
  17013. + m.u.port_info_get.component_handle = port->component->handle;
  17014. + m.u.port_info_get.port_type = port->type;
  17015. + m.u.port_info_get.index = port->index;
  17016. +
  17017. + ret = send_synchronous_mmal_msg(instance, &m,
  17018. + sizeof(m.u.port_info_get),
  17019. + &rmsg, &rmsg_handle);
  17020. + if (ret)
  17021. + return ret;
  17022. +
  17023. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
  17024. + /* got an unexpected message type in reply */
  17025. + ret = -EINVAL;
  17026. + goto release_msg;
  17027. + }
  17028. +
  17029. + /* return operation status */
  17030. + ret = -rmsg->u.port_info_get_reply.status;
  17031. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  17032. + goto release_msg;
  17033. +
  17034. + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
  17035. + port->enabled = false;
  17036. + else
  17037. + port->enabled = true;
  17038. +
  17039. + /* copy the values out of the message */
  17040. + port->handle = rmsg->u.port_info_get_reply.port_handle;
  17041. +
  17042. + /* port type and index cached to use on port info set becuase
  17043. + * it does not use a port handle
  17044. + */
  17045. + port->type = rmsg->u.port_info_get_reply.port_type;
  17046. + port->index = rmsg->u.port_info_get_reply.port_index;
  17047. +
  17048. + port->minimum_buffer.num =
  17049. + rmsg->u.port_info_get_reply.port.buffer_num_min;
  17050. + port->minimum_buffer.size =
  17051. + rmsg->u.port_info_get_reply.port.buffer_size_min;
  17052. + port->minimum_buffer.alignment =
  17053. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  17054. +
  17055. + port->recommended_buffer.alignment =
  17056. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  17057. + port->recommended_buffer.num =
  17058. + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
  17059. +
  17060. + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
  17061. + port->current_buffer.size =
  17062. + rmsg->u.port_info_get_reply.port.buffer_size;
  17063. +
  17064. + /* stream format */
  17065. + port->format.type = rmsg->u.port_info_get_reply.format.type;
  17066. + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
  17067. + port->format.encoding_variant =
  17068. + rmsg->u.port_info_get_reply.format.encoding_variant;
  17069. + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
  17070. + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
  17071. +
  17072. + /* elementry stream format */
  17073. + memcpy(&port->es,
  17074. + &rmsg->u.port_info_get_reply.es,
  17075. + sizeof(union mmal_es_specific_format));
  17076. + port->format.es = &port->es;
  17077. +
  17078. + port->format.extradata_size =
  17079. + rmsg->u.port_info_get_reply.format.extradata_size;
  17080. + memcpy(port->format.extradata,
  17081. + rmsg->u.port_info_get_reply.extradata,
  17082. + port->format.extradata_size);
  17083. +
  17084. + pr_debug("received port info\n");
  17085. + dump_port_info(port);
  17086. +
  17087. +release_msg:
  17088. +
  17089. + pr_debug("%s:result:%d component:0x%x port:%d\n",
  17090. + __func__, ret, port->component->handle, port->handle);
  17091. +
  17092. + vchi_held_msg_release(&rmsg_handle);
  17093. +
  17094. + return ret;
  17095. +}
  17096. +
  17097. +/* create comonent on vc */
  17098. +static int create_component(struct vchiq_mmal_instance *instance,
  17099. + struct vchiq_mmal_component *component,
  17100. + const char *name)
  17101. +{
  17102. + int ret;
  17103. + struct mmal_msg m;
  17104. + struct mmal_msg *rmsg;
  17105. + VCHI_HELD_MSG_T rmsg_handle;
  17106. +
  17107. + /* build component create message */
  17108. + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
  17109. + m.u.component_create.client_component = component;
  17110. + strncpy(m.u.component_create.name, name,
  17111. + sizeof(m.u.component_create.name));
  17112. +
  17113. + ret = send_synchronous_mmal_msg(instance, &m,
  17114. + sizeof(m.u.component_create),
  17115. + &rmsg, &rmsg_handle);
  17116. + if (ret)
  17117. + return ret;
  17118. +
  17119. + if (rmsg->h.type != m.h.type) {
  17120. + /* got an unexpected message type in reply */
  17121. + ret = -EINVAL;
  17122. + goto release_msg;
  17123. + }
  17124. +
  17125. + ret = -rmsg->u.component_create_reply.status;
  17126. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  17127. + goto release_msg;
  17128. +
  17129. + /* a valid component response received */
  17130. + component->handle = rmsg->u.component_create_reply.component_handle;
  17131. + component->inputs = rmsg->u.component_create_reply.input_num;
  17132. + component->outputs = rmsg->u.component_create_reply.output_num;
  17133. + component->clocks = rmsg->u.component_create_reply.clock_num;
  17134. +
  17135. + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
  17136. + component->handle,
  17137. + component->inputs, component->outputs, component->clocks);
  17138. +
  17139. +release_msg:
  17140. + vchi_held_msg_release(&rmsg_handle);
  17141. +
  17142. + return ret;
  17143. +}
  17144. +
  17145. +/* destroys a component on vc */
  17146. +static int destroy_component(struct vchiq_mmal_instance *instance,
  17147. + struct vchiq_mmal_component *component)
  17148. +{
  17149. + int ret;
  17150. + struct mmal_msg m;
  17151. + struct mmal_msg *rmsg;
  17152. + VCHI_HELD_MSG_T rmsg_handle;
  17153. +
  17154. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
  17155. + m.u.component_destroy.component_handle = component->handle;
  17156. +
  17157. + ret = send_synchronous_mmal_msg(instance, &m,
  17158. + sizeof(m.u.component_destroy),
  17159. + &rmsg, &rmsg_handle);
  17160. + if (ret)
  17161. + return ret;
  17162. +
  17163. + if (rmsg->h.type != m.h.type) {
  17164. + /* got an unexpected message type in reply */
  17165. + ret = -EINVAL;
  17166. + goto release_msg;
  17167. + }
  17168. +
  17169. + ret = -rmsg->u.component_destroy_reply.status;
  17170. +
  17171. +release_msg:
  17172. +
  17173. + vchi_held_msg_release(&rmsg_handle);
  17174. +
  17175. + return ret;
  17176. +}
  17177. +
  17178. +/* enable a component on vc */
  17179. +static int enable_component(struct vchiq_mmal_instance *instance,
  17180. + struct vchiq_mmal_component *component)
  17181. +{
  17182. + int ret;
  17183. + struct mmal_msg m;
  17184. + struct mmal_msg *rmsg;
  17185. + VCHI_HELD_MSG_T rmsg_handle;
  17186. +
  17187. + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
  17188. + m.u.component_enable.component_handle = component->handle;
  17189. +
  17190. + ret = send_synchronous_mmal_msg(instance, &m,
  17191. + sizeof(m.u.component_enable),
  17192. + &rmsg, &rmsg_handle);
  17193. + if (ret)
  17194. + return ret;
  17195. +
  17196. + if (rmsg->h.type != m.h.type) {
  17197. + /* got an unexpected message type in reply */
  17198. + ret = -EINVAL;
  17199. + goto release_msg;
  17200. + }
  17201. +
  17202. + ret = -rmsg->u.component_enable_reply.status;
  17203. +
  17204. +release_msg:
  17205. + vchi_held_msg_release(&rmsg_handle);
  17206. +
  17207. + return ret;
  17208. +}
  17209. +
  17210. +/* disable a component on vc */
  17211. +static int disable_component(struct vchiq_mmal_instance *instance,
  17212. + struct vchiq_mmal_component *component)
  17213. +{
  17214. + int ret;
  17215. + struct mmal_msg m;
  17216. + struct mmal_msg *rmsg;
  17217. + VCHI_HELD_MSG_T rmsg_handle;
  17218. +
  17219. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
  17220. + m.u.component_disable.component_handle = component->handle;
  17221. +
  17222. + ret = send_synchronous_mmal_msg(instance, &m,
  17223. + sizeof(m.u.component_disable),
  17224. + &rmsg, &rmsg_handle);
  17225. + if (ret)
  17226. + return ret;
  17227. +
  17228. + if (rmsg->h.type != m.h.type) {
  17229. + /* got an unexpected message type in reply */
  17230. + ret = -EINVAL;
  17231. + goto release_msg;
  17232. + }
  17233. +
  17234. + ret = -rmsg->u.component_disable_reply.status;
  17235. +
  17236. +release_msg:
  17237. +
  17238. + vchi_held_msg_release(&rmsg_handle);
  17239. +
  17240. + return ret;
  17241. +}
  17242. +
  17243. +/* get version of mmal implementation */
  17244. +static int get_version(struct vchiq_mmal_instance *instance,
  17245. + u32 *major_out, u32 *minor_out)
  17246. +{
  17247. + int ret;
  17248. + struct mmal_msg m;
  17249. + struct mmal_msg *rmsg;
  17250. + VCHI_HELD_MSG_T rmsg_handle;
  17251. +
  17252. + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
  17253. +
  17254. + ret = send_synchronous_mmal_msg(instance, &m,
  17255. + sizeof(m.u.version),
  17256. + &rmsg, &rmsg_handle);
  17257. + if (ret)
  17258. + return ret;
  17259. +
  17260. + if (rmsg->h.type != m.h.type) {
  17261. + /* got an unexpected message type in reply */
  17262. + ret = -EINVAL;
  17263. + goto release_msg;
  17264. + }
  17265. +
  17266. + *major_out = rmsg->u.version.major;
  17267. + *minor_out = rmsg->u.version.minor;
  17268. +
  17269. +release_msg:
  17270. + vchi_held_msg_release(&rmsg_handle);
  17271. +
  17272. + return ret;
  17273. +}
  17274. +
  17275. +/* do a port action with a port as a parameter */
  17276. +static int port_action_port(struct vchiq_mmal_instance *instance,
  17277. + struct vchiq_mmal_port *port,
  17278. + enum mmal_msg_port_action_type action_type)
  17279. +{
  17280. + int ret;
  17281. + struct mmal_msg m;
  17282. + struct mmal_msg *rmsg;
  17283. + VCHI_HELD_MSG_T rmsg_handle;
  17284. +
  17285. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  17286. + m.u.port_action_port.component_handle = port->component->handle;
  17287. + m.u.port_action_port.port_handle = port->handle;
  17288. + m.u.port_action_port.action = action_type;
  17289. +
  17290. + port_to_mmal_msg(port, &m.u.port_action_port.port);
  17291. +
  17292. + ret = send_synchronous_mmal_msg(instance, &m,
  17293. + sizeof(m.u.port_action_port),
  17294. + &rmsg, &rmsg_handle);
  17295. + if (ret)
  17296. + return ret;
  17297. +
  17298. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  17299. + /* got an unexpected message type in reply */
  17300. + ret = -EINVAL;
  17301. + goto release_msg;
  17302. + }
  17303. +
  17304. + ret = -rmsg->u.port_action_reply.status;
  17305. +
  17306. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
  17307. + __func__,
  17308. + ret, port->component->handle, port->handle,
  17309. + port_action_type_names[action_type], action_type);
  17310. +
  17311. +release_msg:
  17312. + vchi_held_msg_release(&rmsg_handle);
  17313. +
  17314. + return ret;
  17315. +}
  17316. +
  17317. +/* do a port action with handles as parameters */
  17318. +static int port_action_handle(struct vchiq_mmal_instance *instance,
  17319. + struct vchiq_mmal_port *port,
  17320. + enum mmal_msg_port_action_type action_type,
  17321. + u32 connect_component_handle,
  17322. + u32 connect_port_handle)
  17323. +{
  17324. + int ret;
  17325. + struct mmal_msg m;
  17326. + struct mmal_msg *rmsg;
  17327. + VCHI_HELD_MSG_T rmsg_handle;
  17328. +
  17329. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  17330. +
  17331. + m.u.port_action_handle.component_handle = port->component->handle;
  17332. + m.u.port_action_handle.port_handle = port->handle;
  17333. + m.u.port_action_handle.action = action_type;
  17334. +
  17335. + m.u.port_action_handle.connect_component_handle =
  17336. + connect_component_handle;
  17337. + m.u.port_action_handle.connect_port_handle = connect_port_handle;
  17338. +
  17339. + ret = send_synchronous_mmal_msg(instance, &m,
  17340. + sizeof(m.u.port_action_handle),
  17341. + &rmsg, &rmsg_handle);
  17342. + if (ret)
  17343. + return ret;
  17344. +
  17345. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  17346. + /* got an unexpected message type in reply */
  17347. + ret = -EINVAL;
  17348. + goto release_msg;
  17349. + }
  17350. +
  17351. + ret = -rmsg->u.port_action_reply.status;
  17352. +
  17353. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
  17354. + " connect component:0x%x connect port:%d\n",
  17355. + __func__,
  17356. + ret, port->component->handle, port->handle,
  17357. + port_action_type_names[action_type],
  17358. + action_type, connect_component_handle, connect_port_handle);
  17359. +
  17360. +release_msg:
  17361. + vchi_held_msg_release(&rmsg_handle);
  17362. +
  17363. + return ret;
  17364. +}
  17365. +
  17366. +static int port_parameter_set(struct vchiq_mmal_instance *instance,
  17367. + struct vchiq_mmal_port *port,
  17368. + u32 parameter_id, void *value, u32 value_size)
  17369. +{
  17370. + int ret;
  17371. + struct mmal_msg m;
  17372. + struct mmal_msg *rmsg;
  17373. + VCHI_HELD_MSG_T rmsg_handle;
  17374. +
  17375. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
  17376. +
  17377. + m.u.port_parameter_set.component_handle = port->component->handle;
  17378. + m.u.port_parameter_set.port_handle = port->handle;
  17379. + m.u.port_parameter_set.id = parameter_id;
  17380. + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
  17381. + memcpy(&m.u.port_parameter_set.value, value, value_size);
  17382. +
  17383. + ret = send_synchronous_mmal_msg(instance, &m,
  17384. + (4 * sizeof(u32)) + value_size,
  17385. + &rmsg, &rmsg_handle);
  17386. + if (ret)
  17387. + return ret;
  17388. +
  17389. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
  17390. + /* got an unexpected message type in reply */
  17391. + ret = -EINVAL;
  17392. + goto release_msg;
  17393. + }
  17394. +
  17395. + ret = -rmsg->u.port_parameter_set_reply.status;
  17396. +
  17397. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
  17398. + __func__,
  17399. + ret, port->component->handle, port->handle, parameter_id);
  17400. +
  17401. +release_msg:
  17402. + vchi_held_msg_release(&rmsg_handle);
  17403. +
  17404. + return ret;
  17405. +}
  17406. +
  17407. +static int port_parameter_get(struct vchiq_mmal_instance *instance,
  17408. + struct vchiq_mmal_port *port,
  17409. + u32 parameter_id, void *value, u32 *value_size)
  17410. +{
  17411. + int ret;
  17412. + struct mmal_msg m;
  17413. + struct mmal_msg *rmsg;
  17414. + VCHI_HELD_MSG_T rmsg_handle;
  17415. +
  17416. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
  17417. +
  17418. + m.u.port_parameter_get.component_handle = port->component->handle;
  17419. + m.u.port_parameter_get.port_handle = port->handle;
  17420. + m.u.port_parameter_get.id = parameter_id;
  17421. + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
  17422. +
  17423. + ret = send_synchronous_mmal_msg(instance, &m,
  17424. + sizeof(struct
  17425. + mmal_msg_port_parameter_get),
  17426. + &rmsg, &rmsg_handle);
  17427. + if (ret)
  17428. + return ret;
  17429. +
  17430. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
  17431. + /* got an unexpected message type in reply */
  17432. + pr_err("Incorrect reply type %d\n", rmsg->h.type);
  17433. + ret = -EINVAL;
  17434. + goto release_msg;
  17435. + }
  17436. +
  17437. + ret = -rmsg->u.port_parameter_get_reply.status;
  17438. + if (ret) {
  17439. + /* Copy only as much as we have space for
  17440. + * but report true size of parameter
  17441. + */
  17442. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  17443. + *value_size);
  17444. + *value_size = rmsg->u.port_parameter_get_reply.size;
  17445. + } else
  17446. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  17447. + rmsg->u.port_parameter_get_reply.size);
  17448. +
  17449. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
  17450. + ret, port->component->handle, port->handle, parameter_id);
  17451. +
  17452. +release_msg:
  17453. + vchi_held_msg_release(&rmsg_handle);
  17454. +
  17455. + return ret;
  17456. +}
  17457. +
  17458. +/* disables a port and drains buffers from it */
  17459. +static int port_disable(struct vchiq_mmal_instance *instance,
  17460. + struct vchiq_mmal_port *port)
  17461. +{
  17462. + int ret;
  17463. + struct list_head *q, *buf_head;
  17464. + unsigned long flags = 0;
  17465. +
  17466. + if (!port->enabled)
  17467. + return 0;
  17468. +
  17469. + port->enabled = false;
  17470. +
  17471. + ret = port_action_port(instance, port,
  17472. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
  17473. + if (ret == 0) {
  17474. +
  17475. + /* drain all queued buffers on port */
  17476. + spin_lock_irqsave(&port->slock, flags);
  17477. +
  17478. + list_for_each_safe(buf_head, q, &port->buffers) {
  17479. + struct mmal_buffer *mmalbuf;
  17480. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  17481. + list);
  17482. + list_del(buf_head);
  17483. + if (port->buffer_cb)
  17484. + port->buffer_cb(instance,
  17485. + port, 0, mmalbuf, 0, 0,
  17486. + MMAL_TIME_UNKNOWN,
  17487. + MMAL_TIME_UNKNOWN);
  17488. + }
  17489. +
  17490. + spin_unlock_irqrestore(&port->slock, flags);
  17491. +
  17492. + ret = port_info_get(instance, port);
  17493. + }
  17494. +
  17495. + return ret;
  17496. +}
  17497. +
  17498. +/* enable a port */
  17499. +static int port_enable(struct vchiq_mmal_instance *instance,
  17500. + struct vchiq_mmal_port *port)
  17501. +{
  17502. + unsigned int hdr_count;
  17503. + struct list_head *buf_head;
  17504. + int ret;
  17505. +
  17506. + if (port->enabled)
  17507. + return 0;
  17508. +
  17509. + /* ensure there are enough buffers queued to cover the buffer headers */
  17510. + if (port->buffer_cb != NULL) {
  17511. + hdr_count = 0;
  17512. + list_for_each(buf_head, &port->buffers) {
  17513. + hdr_count++;
  17514. + }
  17515. + if (hdr_count < port->current_buffer.num)
  17516. + return -ENOSPC;
  17517. + }
  17518. +
  17519. + ret = port_action_port(instance, port,
  17520. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
  17521. + if (ret)
  17522. + goto done;
  17523. +
  17524. + port->enabled = true;
  17525. +
  17526. + if (port->buffer_cb) {
  17527. + /* send buffer headers to videocore */
  17528. + hdr_count = 1;
  17529. + list_for_each(buf_head, &port->buffers) {
  17530. + struct mmal_buffer *mmalbuf;
  17531. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  17532. + list);
  17533. + ret = buffer_from_host(instance, port, mmalbuf);
  17534. + if (ret)
  17535. + goto done;
  17536. +
  17537. + hdr_count++;
  17538. + if (hdr_count > port->current_buffer.num)
  17539. + break;
  17540. + }
  17541. + }
  17542. +
  17543. + ret = port_info_get(instance, port);
  17544. +
  17545. +done:
  17546. + return ret;
  17547. +}
  17548. +
  17549. +/* ------------------------------------------------------------------
  17550. + * Exported API
  17551. + *------------------------------------------------------------------*/
  17552. +
  17553. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  17554. + struct vchiq_mmal_port *port)
  17555. +{
  17556. + int ret;
  17557. +
  17558. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17559. + return -EINTR;
  17560. +
  17561. + ret = port_info_set(instance, port);
  17562. + if (ret)
  17563. + goto release_unlock;
  17564. +
  17565. + /* read what has actually been set */
  17566. + ret = port_info_get(instance, port);
  17567. +
  17568. +release_unlock:
  17569. + mutex_unlock(&instance->vchiq_mutex);
  17570. +
  17571. + return ret;
  17572. +
  17573. +}
  17574. +
  17575. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  17576. + struct vchiq_mmal_port *port,
  17577. + u32 parameter, void *value, u32 value_size)
  17578. +{
  17579. + int ret;
  17580. +
  17581. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17582. + return -EINTR;
  17583. +
  17584. + ret = port_parameter_set(instance, port, parameter, value, value_size);
  17585. +
  17586. + mutex_unlock(&instance->vchiq_mutex);
  17587. +
  17588. + return ret;
  17589. +}
  17590. +
  17591. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  17592. + struct vchiq_mmal_port *port,
  17593. + u32 parameter, void *value, u32 *value_size)
  17594. +{
  17595. + int ret;
  17596. +
  17597. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17598. + return -EINTR;
  17599. +
  17600. + ret = port_parameter_get(instance, port, parameter, value, value_size);
  17601. +
  17602. + mutex_unlock(&instance->vchiq_mutex);
  17603. +
  17604. + return ret;
  17605. +}
  17606. +
  17607. +/* enable a port
  17608. + *
  17609. + * enables a port and queues buffers for satisfying callbacks if we
  17610. + * provide a callback handler
  17611. + */
  17612. +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
  17613. + struct vchiq_mmal_port *port,
  17614. + vchiq_mmal_buffer_cb buffer_cb)
  17615. +{
  17616. + int ret;
  17617. +
  17618. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17619. + return -EINTR;
  17620. +
  17621. + /* already enabled - noop */
  17622. + if (port->enabled) {
  17623. + ret = 0;
  17624. + goto unlock;
  17625. + }
  17626. +
  17627. + port->buffer_cb = buffer_cb;
  17628. +
  17629. + ret = port_enable(instance, port);
  17630. +
  17631. +unlock:
  17632. + mutex_unlock(&instance->vchiq_mutex);
  17633. +
  17634. + return ret;
  17635. +}
  17636. +
  17637. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  17638. + struct vchiq_mmal_port *port)
  17639. +{
  17640. + int ret;
  17641. +
  17642. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17643. + return -EINTR;
  17644. +
  17645. + if (!port->enabled) {
  17646. + mutex_unlock(&instance->vchiq_mutex);
  17647. + return 0;
  17648. + }
  17649. +
  17650. + ret = port_disable(instance, port);
  17651. +
  17652. + mutex_unlock(&instance->vchiq_mutex);
  17653. +
  17654. + return ret;
  17655. +}
  17656. +
  17657. +/* ports will be connected in a tunneled manner so data buffers
  17658. + * are not handled by client.
  17659. + */
  17660. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  17661. + struct vchiq_mmal_port *src,
  17662. + struct vchiq_mmal_port *dst)
  17663. +{
  17664. + int ret;
  17665. +
  17666. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17667. + return -EINTR;
  17668. +
  17669. + /* disconnect ports if connected */
  17670. + if (src->connected != NULL) {
  17671. + ret = port_disable(instance, src);
  17672. + if (ret) {
  17673. + pr_err("failed disabling src port(%d)\n", ret);
  17674. + goto release_unlock;
  17675. + }
  17676. +
  17677. + /* do not need to disable the destination port as they
  17678. + * are connected and it is done automatically
  17679. + */
  17680. +
  17681. + ret = port_action_handle(instance, src,
  17682. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
  17683. + src->connected->component->handle,
  17684. + src->connected->handle);
  17685. + if (ret < 0) {
  17686. + pr_err("failed disconnecting src port\n");
  17687. + goto release_unlock;
  17688. + }
  17689. + src->connected->enabled = false;
  17690. + src->connected = NULL;
  17691. + }
  17692. +
  17693. + if (dst == NULL) {
  17694. + /* do not make new connection */
  17695. + ret = 0;
  17696. + pr_debug("not making new connection\n");
  17697. + goto release_unlock;
  17698. + }
  17699. +
  17700. + /* copy src port format to dst */
  17701. + dst->format.encoding = src->format.encoding;
  17702. + dst->es.video.width = src->es.video.width;
  17703. + dst->es.video.height = src->es.video.height;
  17704. + dst->es.video.crop.x = src->es.video.crop.x;
  17705. + dst->es.video.crop.y = src->es.video.crop.y;
  17706. + dst->es.video.crop.width = src->es.video.crop.width;
  17707. + dst->es.video.crop.height = src->es.video.crop.height;
  17708. + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
  17709. + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
  17710. +
  17711. + /* set new format */
  17712. + ret = port_info_set(instance, dst);
  17713. + if (ret) {
  17714. + pr_debug("setting port info failed\n");
  17715. + goto release_unlock;
  17716. + }
  17717. +
  17718. + /* read what has actually been set */
  17719. + ret = port_info_get(instance, dst);
  17720. + if (ret) {
  17721. + pr_debug("read back port info failed\n");
  17722. + goto release_unlock;
  17723. + }
  17724. +
  17725. + /* connect two ports together */
  17726. + ret = port_action_handle(instance, src,
  17727. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
  17728. + dst->component->handle, dst->handle);
  17729. + if (ret < 0) {
  17730. + pr_debug("connecting port %d:%d to %d:%d failed\n",
  17731. + src->component->handle, src->handle,
  17732. + dst->component->handle, dst->handle);
  17733. + goto release_unlock;
  17734. + }
  17735. + src->connected = dst;
  17736. +
  17737. +release_unlock:
  17738. +
  17739. + mutex_unlock(&instance->vchiq_mutex);
  17740. +
  17741. + return ret;
  17742. +}
  17743. +
  17744. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  17745. + struct vchiq_mmal_port *port,
  17746. + struct mmal_buffer *buffer)
  17747. +{
  17748. + unsigned long flags = 0;
  17749. +
  17750. + spin_lock_irqsave(&port->slock, flags);
  17751. + list_add_tail(&buffer->list, &port->buffers);
  17752. + spin_unlock_irqrestore(&port->slock, flags);
  17753. +
  17754. + /* the port previously underflowed because it was missing a
  17755. + * mmal_buffer which has just been added, submit that buffer
  17756. + * to the mmal service.
  17757. + */
  17758. + if (port->buffer_underflow) {
  17759. + port_buffer_from_host(instance, port);
  17760. + port->buffer_underflow--;
  17761. + }
  17762. +
  17763. + return 0;
  17764. +}
  17765. +
  17766. +/* Initialise a mmal component and its ports
  17767. + *
  17768. + */
  17769. +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
  17770. + const char *name,
  17771. + struct vchiq_mmal_component **component_out)
  17772. +{
  17773. + int ret;
  17774. + int idx; /* port index */
  17775. + struct vchiq_mmal_component *component;
  17776. +
  17777. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17778. + return -EINTR;
  17779. +
  17780. + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
  17781. + ret = -EINVAL; /* todo is this correct error? */
  17782. + goto unlock;
  17783. + }
  17784. +
  17785. + component = &instance->component[instance->component_idx];
  17786. +
  17787. + ret = create_component(instance, component, name);
  17788. + if (ret < 0)
  17789. + goto unlock;
  17790. +
  17791. + /* ports info needs gathering */
  17792. + component->control.type = MMAL_PORT_TYPE_CONTROL;
  17793. + component->control.index = 0;
  17794. + component->control.component = component;
  17795. + spin_lock_init(&component->control.slock);
  17796. + INIT_LIST_HEAD(&component->control.buffers);
  17797. + ret = port_info_get(instance, &component->control);
  17798. + if (ret < 0)
  17799. + goto release_component;
  17800. +
  17801. + for (idx = 0; idx < component->inputs; idx++) {
  17802. + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
  17803. + component->input[idx].index = idx;
  17804. + component->input[idx].component = component;
  17805. + spin_lock_init(&component->input[idx].slock);
  17806. + INIT_LIST_HEAD(&component->input[idx].buffers);
  17807. + ret = port_info_get(instance, &component->input[idx]);
  17808. + if (ret < 0)
  17809. + goto release_component;
  17810. + }
  17811. +
  17812. + for (idx = 0; idx < component->outputs; idx++) {
  17813. + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
  17814. + component->output[idx].index = idx;
  17815. + component->output[idx].component = component;
  17816. + spin_lock_init(&component->output[idx].slock);
  17817. + INIT_LIST_HEAD(&component->output[idx].buffers);
  17818. + ret = port_info_get(instance, &component->output[idx]);
  17819. + if (ret < 0)
  17820. + goto release_component;
  17821. + }
  17822. +
  17823. + for (idx = 0; idx < component->clocks; idx++) {
  17824. + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
  17825. + component->clock[idx].index = idx;
  17826. + component->clock[idx].component = component;
  17827. + spin_lock_init(&component->clock[idx].slock);
  17828. + INIT_LIST_HEAD(&component->clock[idx].buffers);
  17829. + ret = port_info_get(instance, &component->clock[idx]);
  17830. + if (ret < 0)
  17831. + goto release_component;
  17832. + }
  17833. +
  17834. + instance->component_idx++;
  17835. +
  17836. + *component_out = component;
  17837. +
  17838. + mutex_unlock(&instance->vchiq_mutex);
  17839. +
  17840. + return 0;
  17841. +
  17842. +release_component:
  17843. + destroy_component(instance, component);
  17844. +unlock:
  17845. + mutex_unlock(&instance->vchiq_mutex);
  17846. +
  17847. + return ret;
  17848. +}
  17849. +
  17850. +/*
  17851. + * cause a mmal component to be destroyed
  17852. + */
  17853. +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
  17854. + struct vchiq_mmal_component *component)
  17855. +{
  17856. + int ret;
  17857. +
  17858. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17859. + return -EINTR;
  17860. +
  17861. + if (component->enabled)
  17862. + ret = disable_component(instance, component);
  17863. +
  17864. + ret = destroy_component(instance, component);
  17865. +
  17866. + mutex_unlock(&instance->vchiq_mutex);
  17867. +
  17868. + return ret;
  17869. +}
  17870. +
  17871. +/*
  17872. + * cause a mmal component to be enabled
  17873. + */
  17874. +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
  17875. + struct vchiq_mmal_component *component)
  17876. +{
  17877. + int ret;
  17878. +
  17879. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17880. + return -EINTR;
  17881. +
  17882. + if (component->enabled) {
  17883. + mutex_unlock(&instance->vchiq_mutex);
  17884. + return 0;
  17885. + }
  17886. +
  17887. + ret = enable_component(instance, component);
  17888. + if (ret == 0)
  17889. + component->enabled = true;
  17890. +
  17891. + mutex_unlock(&instance->vchiq_mutex);
  17892. +
  17893. + return ret;
  17894. +}
  17895. +
  17896. +/*
  17897. + * cause a mmal component to be enabled
  17898. + */
  17899. +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
  17900. + struct vchiq_mmal_component *component)
  17901. +{
  17902. + int ret;
  17903. +
  17904. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17905. + return -EINTR;
  17906. +
  17907. + if (!component->enabled) {
  17908. + mutex_unlock(&instance->vchiq_mutex);
  17909. + return 0;
  17910. + }
  17911. +
  17912. + ret = disable_component(instance, component);
  17913. + if (ret == 0)
  17914. + component->enabled = false;
  17915. +
  17916. + mutex_unlock(&instance->vchiq_mutex);
  17917. +
  17918. + return ret;
  17919. +}
  17920. +
  17921. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  17922. + u32 *major_out, u32 *minor_out)
  17923. +{
  17924. + int ret;
  17925. +
  17926. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17927. + return -EINTR;
  17928. +
  17929. + ret = get_version(instance, major_out, minor_out);
  17930. +
  17931. + mutex_unlock(&instance->vchiq_mutex);
  17932. +
  17933. + return ret;
  17934. +}
  17935. +
  17936. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
  17937. +{
  17938. + int status = 0;
  17939. +
  17940. + if (instance == NULL)
  17941. + return -EINVAL;
  17942. +
  17943. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17944. + return -EINTR;
  17945. +
  17946. + vchi_service_use(instance->handle);
  17947. +
  17948. + status = vchi_service_close(instance->handle);
  17949. + if (status != 0)
  17950. + pr_err("mmal-vchiq: VCHIQ close failed");
  17951. +
  17952. + mutex_unlock(&instance->vchiq_mutex);
  17953. +
  17954. + vfree(instance->bulk_scratch);
  17955. +
  17956. + kfree(instance);
  17957. +
  17958. + return status;
  17959. +}
  17960. +
  17961. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
  17962. +{
  17963. + int status;
  17964. + struct vchiq_mmal_instance *instance;
  17965. + static VCHI_CONNECTION_T *vchi_connection;
  17966. + static VCHI_INSTANCE_T vchi_instance;
  17967. + SERVICE_CREATION_T params = {
  17968. + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
  17969. + VC_MMAL_SERVER_NAME,
  17970. + vchi_connection,
  17971. + 0, /* rx fifo size (unused) */
  17972. + 0, /* tx fifo size (unused) */
  17973. + service_callback,
  17974. + NULL, /* service callback parameter */
  17975. + 1, /* unaligned bulk receives */
  17976. + 1, /* unaligned bulk transmits */
  17977. + 0 /* want crc check on bulk transfers */
  17978. + };
  17979. +
  17980. + /* compile time checks to ensure structure size as they are
  17981. + * directly (de)serialised from memory.
  17982. + */
  17983. +
  17984. + /* ensure the header structure has packed to the correct size */
  17985. + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
  17986. +
  17987. + /* ensure message structure does not exceed maximum length */
  17988. + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
  17989. +
  17990. + /* mmal port struct is correct size */
  17991. + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
  17992. +
  17993. + /* create a vchi instance */
  17994. + status = vchi_initialise(&vchi_instance);
  17995. + if (status) {
  17996. + pr_err("Failed to initialise VCHI instance (status=%d)\n",
  17997. + status);
  17998. + return -EIO;
  17999. + }
  18000. +
  18001. + status = vchi_connect(NULL, 0, vchi_instance);
  18002. + if (status) {
  18003. + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
  18004. + return -EIO;
  18005. + }
  18006. +
  18007. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  18008. + memset(instance, 0, sizeof(*instance));
  18009. +
  18010. + mutex_init(&instance->vchiq_mutex);
  18011. + mutex_init(&instance->bulk_mutex);
  18012. +
  18013. + instance->bulk_scratch = vmalloc(PAGE_SIZE);
  18014. +
  18015. + params.callback_param = instance;
  18016. +
  18017. + status = vchi_service_open(vchi_instance, &params, &instance->handle);
  18018. + if (status) {
  18019. + pr_err("Failed to open VCHI service connection (status=%d)\n",
  18020. + status);
  18021. + goto err_close_services;
  18022. + }
  18023. +
  18024. + vchi_service_release(instance->handle);
  18025. +
  18026. + *out_instance = instance;
  18027. +
  18028. + return 0;
  18029. +
  18030. +err_close_services:
  18031. +
  18032. + vchi_service_close(instance->handle);
  18033. + vfree(instance->bulk_scratch);
  18034. + kfree(instance);
  18035. + return -ENODEV;
  18036. +}
  18037. diff -Nur linux-3.10.37/drivers/media/platform/bcm2835/mmal-vchiq.h linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h
  18038. --- linux-3.10.37/drivers/media/platform/bcm2835/mmal-vchiq.h 1970-01-01 01:00:00.000000000 +0100
  18039. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h 2014-04-24 15:35:02.713549510 +0200
  18040. @@ -0,0 +1,178 @@
  18041. +/*
  18042. + * Broadcom BM2835 V4L2 driver
  18043. + *
  18044. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  18045. + *
  18046. + * This file is subject to the terms and conditions of the GNU General Public
  18047. + * License. See the file COPYING in the main directory of this archive
  18048. + * for more details.
  18049. + *
  18050. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  18051. + * Dave Stevenson <dsteve@broadcom.com>
  18052. + * Simon Mellor <simellor@broadcom.com>
  18053. + * Luke Diamand <luked@broadcom.com>
  18054. + *
  18055. + * MMAL interface to VCHIQ message passing
  18056. + */
  18057. +
  18058. +#ifndef MMAL_VCHIQ_H
  18059. +#define MMAL_VCHIQ_H
  18060. +
  18061. +#include "mmal-msg-format.h"
  18062. +
  18063. +#define MAX_PORT_COUNT 4
  18064. +
  18065. +/* Maximum size of the format extradata. */
  18066. +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
  18067. +
  18068. +struct vchiq_mmal_instance;
  18069. +
  18070. +enum vchiq_mmal_es_type {
  18071. + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
  18072. + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
  18073. + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
  18074. + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
  18075. + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
  18076. +};
  18077. +
  18078. +/* rectangle, used lots so it gets its own struct */
  18079. +struct vchiq_mmal_rect {
  18080. + s32 x;
  18081. + s32 y;
  18082. + s32 width;
  18083. + s32 height;
  18084. +};
  18085. +
  18086. +struct vchiq_mmal_port_buffer {
  18087. + unsigned int num; /* number of buffers */
  18088. + u32 size; /* size of buffers */
  18089. + u32 alignment; /* alignment of buffers */
  18090. +};
  18091. +
  18092. +struct vchiq_mmal_port;
  18093. +
  18094. +typedef void (*vchiq_mmal_buffer_cb)(
  18095. + struct vchiq_mmal_instance *instance,
  18096. + struct vchiq_mmal_port *port,
  18097. + int status, struct mmal_buffer *buffer,
  18098. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
  18099. +
  18100. +struct vchiq_mmal_port {
  18101. + bool enabled;
  18102. + u32 handle;
  18103. + u32 type; /* port type, cached to use on port info set */
  18104. + u32 index; /* port index, cached to use on port info set */
  18105. +
  18106. + /* component port belongs to, allows simple deref */
  18107. + struct vchiq_mmal_component *component;
  18108. +
  18109. + struct vchiq_mmal_port *connected; /* port conencted to */
  18110. +
  18111. + /* buffer info */
  18112. + struct vchiq_mmal_port_buffer minimum_buffer;
  18113. + struct vchiq_mmal_port_buffer recommended_buffer;
  18114. + struct vchiq_mmal_port_buffer current_buffer;
  18115. +
  18116. + /* stream format */
  18117. + struct mmal_es_format format;
  18118. + /* elementry stream format */
  18119. + union mmal_es_specific_format es;
  18120. +
  18121. + /* data buffers to fill */
  18122. + struct list_head buffers;
  18123. + /* lock to serialise adding and removing buffers from list */
  18124. + spinlock_t slock;
  18125. + /* count of how many buffer header refils have failed because
  18126. + * there was no buffer to satisfy them
  18127. + */
  18128. + int buffer_underflow;
  18129. + /* callback on buffer completion */
  18130. + vchiq_mmal_buffer_cb buffer_cb;
  18131. + /* callback context */
  18132. + void *cb_ctx;
  18133. +};
  18134. +
  18135. +struct vchiq_mmal_component {
  18136. + bool enabled;
  18137. + u32 handle; /* VideoCore handle for component */
  18138. + u32 inputs; /* Number of input ports */
  18139. + u32 outputs; /* Number of output ports */
  18140. + u32 clocks; /* Number of clock ports */
  18141. + struct vchiq_mmal_port control; /* control port */
  18142. + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
  18143. + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
  18144. + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
  18145. +};
  18146. +
  18147. +
  18148. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
  18149. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
  18150. +
  18151. +/* Initialise a mmal component and its ports
  18152. +*
  18153. +*/
  18154. +int vchiq_mmal_component_init(
  18155. + struct vchiq_mmal_instance *instance,
  18156. + const char *name,
  18157. + struct vchiq_mmal_component **component_out);
  18158. +
  18159. +int vchiq_mmal_component_finalise(
  18160. + struct vchiq_mmal_instance *instance,
  18161. + struct vchiq_mmal_component *component);
  18162. +
  18163. +int vchiq_mmal_component_enable(
  18164. + struct vchiq_mmal_instance *instance,
  18165. + struct vchiq_mmal_component *component);
  18166. +
  18167. +int vchiq_mmal_component_disable(
  18168. + struct vchiq_mmal_instance *instance,
  18169. + struct vchiq_mmal_component *component);
  18170. +
  18171. +
  18172. +
  18173. +/* enable a mmal port
  18174. + *
  18175. + * enables a port and if a buffer callback provided enque buffer
  18176. + * headers as apropriate for the port.
  18177. + */
  18178. +int vchiq_mmal_port_enable(
  18179. + struct vchiq_mmal_instance *instance,
  18180. + struct vchiq_mmal_port *port,
  18181. + vchiq_mmal_buffer_cb buffer_cb);
  18182. +
  18183. +/* disable a port
  18184. + *
  18185. + * disable a port will dequeue any pending buffers
  18186. + */
  18187. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  18188. + struct vchiq_mmal_port *port);
  18189. +
  18190. +
  18191. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  18192. + struct vchiq_mmal_port *port,
  18193. + u32 parameter,
  18194. + void *value,
  18195. + u32 value_size);
  18196. +
  18197. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  18198. + struct vchiq_mmal_port *port,
  18199. + u32 parameter,
  18200. + void *value,
  18201. + u32 *value_size);
  18202. +
  18203. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  18204. + struct vchiq_mmal_port *port);
  18205. +
  18206. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  18207. + struct vchiq_mmal_port *src,
  18208. + struct vchiq_mmal_port *dst);
  18209. +
  18210. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  18211. + u32 *major_out,
  18212. + u32 *minor_out);
  18213. +
  18214. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  18215. + struct vchiq_mmal_port *port,
  18216. + struct mmal_buffer *buf);
  18217. +
  18218. +#endif /* MMAL_VCHIQ_H */
  18219. diff -Nur linux-3.10.37/drivers/media/platform/Kconfig linux-rpi/drivers/media/platform/Kconfig
  18220. --- linux-3.10.37/drivers/media/platform/Kconfig 2014-04-14 15:42:31.000000000 +0200
  18221. +++ linux-rpi/drivers/media/platform/Kconfig 2014-04-24 15:35:02.713549510 +0200
  18222. @@ -124,6 +124,7 @@
  18223. source "drivers/media/platform/soc_camera/Kconfig"
  18224. source "drivers/media/platform/exynos4-is/Kconfig"
  18225. source "drivers/media/platform/s5p-tv/Kconfig"
  18226. +source "drivers/media/platform/bcm2835/Kconfig"
  18227. endif # V4L_PLATFORM_DRIVERS
  18228. diff -Nur linux-3.10.37/drivers/media/platform/Makefile linux-rpi/drivers/media/platform/Makefile
  18229. --- linux-3.10.37/drivers/media/platform/Makefile 2014-04-14 15:42:31.000000000 +0200
  18230. +++ linux-rpi/drivers/media/platform/Makefile 2014-04-24 15:35:02.713549510 +0200
  18231. @@ -50,4 +50,6 @@
  18232. obj-$(CONFIG_ARCH_OMAP) += omap/
  18233. +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
  18234. +
  18235. ccflags-y += -I$(srctree)/drivers/media/i2c
  18236. diff -Nur linux-3.10.37/drivers/media/usb/dvb-usb-v2/az6007.c linux-rpi/drivers/media/usb/dvb-usb-v2/az6007.c
  18237. --- linux-3.10.37/drivers/media/usb/dvb-usb-v2/az6007.c 2014-04-14 15:42:31.000000000 +0200
  18238. +++ linux-rpi/drivers/media/usb/dvb-usb-v2/az6007.c 2014-04-24 15:35:02.781550268 +0200
  18239. @@ -68,6 +68,19 @@
  18240. .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
  18241. };
  18242. +static struct drxk_config cablestar_hdci_drxk = {
  18243. + .adr = 0x29,
  18244. + .parallel_ts = true,
  18245. + .dynamic_clk = true,
  18246. + .single_master = true,
  18247. + .enable_merr_cfg = true,
  18248. + .no_i2c_bridge = false,
  18249. + .chunk_size = 64,
  18250. + .mpeg_out_clk_strength = 0x02,
  18251. + .qam_demod_parameter_count = 2,
  18252. + .microcode_name = "dvb-usb-technisat-cablestar-hdci-drxk.fw",
  18253. +};
  18254. +
  18255. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  18256. {
  18257. struct az6007_device_state *st = fe_to_priv(fe);
  18258. @@ -630,6 +643,27 @@
  18259. return 0;
  18260. }
  18261. +static int az6007_cablestar_hdci_frontend_attach(struct dvb_usb_adapter *adap)
  18262. +{
  18263. + struct az6007_device_state *st = adap_to_priv(adap);
  18264. + struct dvb_usb_device *d = adap_to_d(adap);
  18265. +
  18266. + pr_debug("attaching demod drxk\n");
  18267. +
  18268. + adap->fe[0] = dvb_attach(drxk_attach, &cablestar_hdci_drxk,
  18269. + &d->i2c_adap);
  18270. + if (!adap->fe[0])
  18271. + return -EINVAL;
  18272. +
  18273. + adap->fe[0]->sec_priv = adap;
  18274. + st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
  18275. + adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  18276. +
  18277. + az6007_ci_init(adap);
  18278. +
  18279. + return 0;
  18280. +}
  18281. +
  18282. static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
  18283. {
  18284. struct dvb_usb_device *d = adap_to_d(adap);
  18285. @@ -868,6 +902,29 @@
  18286. }
  18287. };
  18288. +static struct dvb_usb_device_properties az6007_cablestar_hdci_props = {
  18289. + .driver_name = KBUILD_MODNAME,
  18290. + .owner = THIS_MODULE,
  18291. + .firmware = AZ6007_FIRMWARE,
  18292. +
  18293. + .adapter_nr = adapter_nr,
  18294. + .size_of_priv = sizeof(struct az6007_device_state),
  18295. + .i2c_algo = &az6007_i2c_algo,
  18296. + .tuner_attach = az6007_tuner_attach,
  18297. + .frontend_attach = az6007_cablestar_hdci_frontend_attach,
  18298. + .streaming_ctrl = az6007_streaming_ctrl,
  18299. +/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
  18300. + .get_rc_config = NULL,
  18301. + .read_mac_address = az6007_read_mac_addr,
  18302. + .download_firmware = az6007_download_firmware,
  18303. + .identify_state = az6007_identify_state,
  18304. + .power_ctrl = az6007_power_ctrl,
  18305. + .num_adapters = 1,
  18306. + .adapter = {
  18307. + { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
  18308. + }
  18309. +};
  18310. +
  18311. static struct usb_device_id az6007_usb_table[] = {
  18312. {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
  18313. &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
  18314. @@ -875,6 +932,8 @@
  18315. &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
  18316. {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
  18317. &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
  18318. + {DVB_USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI,
  18319. + &az6007_cablestar_hdci_props, "Technisat CableStar Combo HD CI", RC_MAP_EMPTY)},
  18320. {0},
  18321. };
  18322. diff -Nur linux-3.10.37/drivers/media/usb/dvb-usb-v2/rtl28xxu.c linux-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
  18323. --- linux-3.10.37/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-04-14 15:42:31.000000000 +0200
  18324. +++ linux-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-04-24 15:35:02.785550313 +0200
  18325. @@ -1408,6 +1408,10 @@
  18326. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  18327. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  18328. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  18329. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xb803 /*USB_PID_AUGUST_DVBT205*/,
  18330. + &rtl2832u_props, "August DVB-T 205", NULL) },
  18331. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xa803 /*USB_PID_AUGUST_DVBT205*/,
  18332. + &rtl2832u_props, "August DVB-T 205", NULL) },
  18333. { }
  18334. };
  18335. MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
  18336. diff -Nur linux-3.10.37/drivers/misc/Kconfig linux-rpi/drivers/misc/Kconfig
  18337. --- linux-3.10.37/drivers/misc/Kconfig 2014-04-14 15:42:31.000000000 +0200
  18338. +++ linux-rpi/drivers/misc/Kconfig 2014-04-24 15:35:02.873551293 +0200
  18339. @@ -536,4 +536,6 @@
  18340. source "drivers/misc/altera-stapl/Kconfig"
  18341. source "drivers/misc/mei/Kconfig"
  18342. source "drivers/misc/vmw_vmci/Kconfig"
  18343. +source "drivers/misc/vc04_services/Kconfig"
  18344. endmenu
  18345. +
  18346. diff -Nur linux-3.10.37/drivers/misc/Makefile linux-rpi/drivers/misc/Makefile
  18347. --- linux-3.10.37/drivers/misc/Makefile 2014-04-14 15:42:31.000000000 +0200
  18348. +++ linux-rpi/drivers/misc/Makefile 2014-04-24 15:35:02.873551293 +0200
  18349. @@ -53,3 +53,4 @@
  18350. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  18351. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  18352. obj-$(CONFIG_SRAM) += sram.o
  18353. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  18354. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/connections/connection.h linux-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  18355. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/connections/connection.h 1970-01-01 01:00:00.000000000 +0100
  18356. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h 2014-04-24 15:35:02.889551471 +0200
  18357. @@ -0,0 +1,328 @@
  18358. +/**
  18359. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18360. + *
  18361. + * Redistribution and use in source and binary forms, with or without
  18362. + * modification, are permitted provided that the following conditions
  18363. + * are met:
  18364. + * 1. Redistributions of source code must retain the above copyright
  18365. + * notice, this list of conditions, and the following disclaimer,
  18366. + * without modification.
  18367. + * 2. Redistributions in binary form must reproduce the above copyright
  18368. + * notice, this list of conditions and the following disclaimer in the
  18369. + * documentation and/or other materials provided with the distribution.
  18370. + * 3. The names of the above-listed copyright holders may not be used
  18371. + * to endorse or promote products derived from this software without
  18372. + * specific prior written permission.
  18373. + *
  18374. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18375. + * GNU General Public License ("GPL") version 2, as published by the Free
  18376. + * Software Foundation.
  18377. + *
  18378. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18379. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18380. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18381. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18382. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18383. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18384. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18385. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18386. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18387. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18388. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18389. + */
  18390. +
  18391. +#ifndef CONNECTION_H_
  18392. +#define CONNECTION_H_
  18393. +
  18394. +#include <linux/kernel.h>
  18395. +#include <linux/types.h>
  18396. +#include <linux/semaphore.h>
  18397. +
  18398. +#include "interface/vchi/vchi_cfg_internal.h"
  18399. +#include "interface/vchi/vchi_common.h"
  18400. +#include "interface/vchi/message_drivers/message.h"
  18401. +
  18402. +/******************************************************************************
  18403. + Global defs
  18404. + *****************************************************************************/
  18405. +
  18406. +// Opaque handle for a connection / service pair
  18407. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  18408. +
  18409. +// opaque handle to the connection state information
  18410. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  18411. +
  18412. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  18413. +
  18414. +
  18415. +/******************************************************************************
  18416. + API
  18417. + *****************************************************************************/
  18418. +
  18419. +// Routine to init a connection with a particular low level driver
  18420. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  18421. + const VCHI_MESSAGE_DRIVER_T * driver );
  18422. +
  18423. +// Routine to control CRC enabling at a connection level
  18424. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  18425. + VCHI_CRC_CONTROL_T control );
  18426. +
  18427. +// Routine to create a service
  18428. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  18429. + int32_t service_id,
  18430. + uint32_t rx_fifo_size,
  18431. + uint32_t tx_fifo_size,
  18432. + int server,
  18433. + VCHI_CALLBACK_T callback,
  18434. + void *callback_param,
  18435. + int32_t want_crc,
  18436. + int32_t want_unaligned_bulk_rx,
  18437. + int32_t want_unaligned_bulk_tx,
  18438. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  18439. +
  18440. +// Routine to close a service
  18441. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  18442. +
  18443. +// Routine to queue a message
  18444. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18445. + const void *data,
  18446. + uint32_t data_size,
  18447. + VCHI_FLAGS_T flags,
  18448. + void *msg_handle );
  18449. +
  18450. +// scatter-gather (vector) message queueing
  18451. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18452. + VCHI_MSG_VECTOR_T *vector,
  18453. + uint32_t count,
  18454. + VCHI_FLAGS_T flags,
  18455. + void *msg_handle );
  18456. +
  18457. +// Routine to dequeue a message
  18458. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18459. + void *data,
  18460. + uint32_t max_data_size_to_read,
  18461. + uint32_t *actual_msg_size,
  18462. + VCHI_FLAGS_T flags );
  18463. +
  18464. +// Routine to peek at a message
  18465. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18466. + void **data,
  18467. + uint32_t *msg_size,
  18468. + VCHI_FLAGS_T flags );
  18469. +
  18470. +// Routine to hold a message
  18471. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18472. + void **data,
  18473. + uint32_t *msg_size,
  18474. + VCHI_FLAGS_T flags,
  18475. + void **message_handle );
  18476. +
  18477. +// Routine to initialise a received message iterator
  18478. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18479. + VCHI_MSG_ITER_T *iter,
  18480. + VCHI_FLAGS_T flags );
  18481. +
  18482. +// Routine to release a held message
  18483. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18484. + void *message_handle );
  18485. +
  18486. +// Routine to get info on a held message
  18487. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18488. + void *message_handle,
  18489. + void **data,
  18490. + int32_t *msg_size,
  18491. + uint32_t *tx_timestamp,
  18492. + uint32_t *rx_timestamp );
  18493. +
  18494. +// Routine to check whether the iterator has a next message
  18495. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18496. + const VCHI_MSG_ITER_T *iter );
  18497. +
  18498. +// Routine to advance the iterator
  18499. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18500. + VCHI_MSG_ITER_T *iter,
  18501. + void **data,
  18502. + uint32_t *msg_size );
  18503. +
  18504. +// Routine to remove the last message returned by the iterator
  18505. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18506. + VCHI_MSG_ITER_T *iter );
  18507. +
  18508. +// Routine to hold the last message returned by the iterator
  18509. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  18510. + VCHI_MSG_ITER_T *iter,
  18511. + void **msg_handle );
  18512. +
  18513. +// Routine to transmit bulk data
  18514. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18515. + const void *data_src,
  18516. + uint32_t data_size,
  18517. + VCHI_FLAGS_T flags,
  18518. + void *bulk_handle );
  18519. +
  18520. +// Routine to receive data
  18521. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  18522. + void *data_dst,
  18523. + uint32_t data_size,
  18524. + VCHI_FLAGS_T flags,
  18525. + void *bulk_handle );
  18526. +
  18527. +// Routine to report if a server is available
  18528. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  18529. +
  18530. +// Routine to report the number of RX slots available
  18531. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  18532. +
  18533. +// Routine to report the RX slot size
  18534. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  18535. +
  18536. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  18537. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  18538. + int32_t service,
  18539. + uint32_t length,
  18540. + MESSAGE_TX_CHANNEL_T channel,
  18541. + uint32_t channel_params,
  18542. + uint32_t data_length,
  18543. + uint32_t data_offset);
  18544. +
  18545. +// Callback to inform a service that a Xon or Xoff message has been received
  18546. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  18547. +
  18548. +// Callback to inform a service that a server available reply message has been received
  18549. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  18550. +
  18551. +// Callback to indicate that bulk auxiliary messages have arrived
  18552. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  18553. +
  18554. +// Callback to indicate that bulk auxiliary messages have arrived
  18555. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  18556. +
  18557. +// Callback with all the connection info you require
  18558. +typedef void (*VCHI_CONNECTION_INFO)(VCHI_CONNECTION_STATE_T *state, uint32_t protocol_version, uint32_t slot_size, uint32_t num_slots, uint32_t min_bulk_size);
  18559. +
  18560. +// Callback to inform of a disconnect
  18561. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  18562. +
  18563. +// Callback to inform of a power control request
  18564. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  18565. +
  18566. +// allocate memory suitably aligned for this connection
  18567. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  18568. +
  18569. +// free memory allocated by buffer_allocate
  18570. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  18571. +
  18572. +
  18573. +/******************************************************************************
  18574. + System driver struct
  18575. + *****************************************************************************/
  18576. +
  18577. +struct opaque_vchi_connection_api_t
  18578. +{
  18579. + // Routine to init the connection
  18580. + VCHI_CONNECTION_INIT_T init;
  18581. +
  18582. + // Connection-level CRC control
  18583. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  18584. +
  18585. + // Routine to connect to or create service
  18586. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  18587. +
  18588. + // Routine to disconnect from a service
  18589. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  18590. +
  18591. + // Routine to queue a message
  18592. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  18593. +
  18594. + // scatter-gather (vector) message queue
  18595. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  18596. +
  18597. + // Routine to dequeue a message
  18598. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  18599. +
  18600. + // Routine to peek at a message
  18601. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  18602. +
  18603. + // Routine to hold a message
  18604. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  18605. +
  18606. + // Routine to initialise a received message iterator
  18607. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  18608. +
  18609. + // Routine to release a message
  18610. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  18611. +
  18612. + // Routine to get information on a held message
  18613. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  18614. +
  18615. + // Routine to check for next message on iterator
  18616. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  18617. +
  18618. + // Routine to get next message on iterator
  18619. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  18620. +
  18621. + // Routine to remove the last message returned by iterator
  18622. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  18623. +
  18624. + // Routine to hold the last message returned by iterator
  18625. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  18626. +
  18627. + // Routine to transmit bulk data
  18628. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  18629. +
  18630. + // Routine to receive data
  18631. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  18632. +
  18633. + // Routine to report the available servers
  18634. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  18635. +
  18636. + // Routine to report the number of RX slots available
  18637. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  18638. +
  18639. + // Routine to report the RX slot size
  18640. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  18641. +
  18642. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  18643. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  18644. +
  18645. + // Callback to inform a service that a Xon or Xoff message has been received
  18646. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  18647. +
  18648. + // Callback to inform a service that a server available reply message has been received
  18649. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  18650. +
  18651. + // Callback to indicate that bulk auxiliary messages have arrived
  18652. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  18653. +
  18654. + // Callback to indicate that a bulk auxiliary message has been transmitted
  18655. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  18656. +
  18657. + // Callback to provide information about the connection
  18658. + VCHI_CONNECTION_INFO connection_info;
  18659. +
  18660. + // Callback to notify that peer has requested disconnect
  18661. + VCHI_CONNECTION_DISCONNECT disconnect;
  18662. +
  18663. + // Callback to notify that peer has requested power change
  18664. + VCHI_CONNECTION_POWER_CONTROL power_control;
  18665. +
  18666. + // allocate memory suitably aligned for this connection
  18667. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  18668. +
  18669. + // free memory allocated by buffer_allocate
  18670. + VCHI_BUFFER_FREE buffer_free;
  18671. +
  18672. +};
  18673. +
  18674. +struct vchi_connection_t {
  18675. + const VCHI_CONNECTION_API_T *api;
  18676. + VCHI_CONNECTION_STATE_T *state;
  18677. +#ifdef VCHI_COARSE_LOCKING
  18678. + struct semaphore sem;
  18679. +#endif
  18680. +};
  18681. +
  18682. +
  18683. +#endif /* CONNECTION_H_ */
  18684. +
  18685. +/****************************** End of file **********************************/
  18686. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h linux-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  18687. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 1970-01-01 01:00:00.000000000 +0100
  18688. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 2014-04-24 15:35:02.889551471 +0200
  18689. @@ -0,0 +1,204 @@
  18690. +/**
  18691. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18692. + *
  18693. + * Redistribution and use in source and binary forms, with or without
  18694. + * modification, are permitted provided that the following conditions
  18695. + * are met:
  18696. + * 1. Redistributions of source code must retain the above copyright
  18697. + * notice, this list of conditions, and the following disclaimer,
  18698. + * without modification.
  18699. + * 2. Redistributions in binary form must reproduce the above copyright
  18700. + * notice, this list of conditions and the following disclaimer in the
  18701. + * documentation and/or other materials provided with the distribution.
  18702. + * 3. The names of the above-listed copyright holders may not be used
  18703. + * to endorse or promote products derived from this software without
  18704. + * specific prior written permission.
  18705. + *
  18706. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18707. + * GNU General Public License ("GPL") version 2, as published by the Free
  18708. + * Software Foundation.
  18709. + *
  18710. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18711. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18712. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18713. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18714. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18715. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18716. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18717. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18718. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18719. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18720. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18721. + */
  18722. +
  18723. +#ifndef _VCHI_MESSAGE_H_
  18724. +#define _VCHI_MESSAGE_H_
  18725. +
  18726. +#include <linux/kernel.h>
  18727. +#include <linux/types.h>
  18728. +#include <linux/semaphore.h>
  18729. +
  18730. +#include "interface/vchi/vchi_cfg_internal.h"
  18731. +#include "interface/vchi/vchi_common.h"
  18732. +
  18733. +
  18734. +typedef enum message_event_type {
  18735. + MESSAGE_EVENT_NONE,
  18736. + MESSAGE_EVENT_NOP,
  18737. + MESSAGE_EVENT_MESSAGE,
  18738. + MESSAGE_EVENT_SLOT_COMPLETE,
  18739. + MESSAGE_EVENT_RX_BULK_PAUSED,
  18740. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  18741. + MESSAGE_EVENT_TX_COMPLETE,
  18742. + MESSAGE_EVENT_MSG_DISCARDED
  18743. +} MESSAGE_EVENT_TYPE_T;
  18744. +
  18745. +typedef enum vchi_msg_flags
  18746. +{
  18747. + VCHI_MSG_FLAGS_NONE = 0x0,
  18748. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  18749. +} VCHI_MSG_FLAGS_T;
  18750. +
  18751. +typedef enum message_tx_channel
  18752. +{
  18753. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  18754. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  18755. +} MESSAGE_TX_CHANNEL_T;
  18756. +
  18757. +// Macros used for cycling through bulk channels
  18758. +#define MESSAGE_TX_CHANNEL_BULK_PREV(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION-1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  18759. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  18760. +
  18761. +typedef enum message_rx_channel
  18762. +{
  18763. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  18764. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  18765. +} MESSAGE_RX_CHANNEL_T;
  18766. +
  18767. +// Message receive slot information
  18768. +typedef struct rx_msg_slot_info {
  18769. +
  18770. + struct rx_msg_slot_info *next;
  18771. + //struct slot_info *prev;
  18772. +#if !defined VCHI_COARSE_LOCKING
  18773. + struct semaphore sem;
  18774. +#endif
  18775. +
  18776. + uint8_t *addr; // base address of slot
  18777. + uint32_t len; // length of slot in bytes
  18778. +
  18779. + uint32_t write_ptr; // hardware causes this to advance
  18780. + uint32_t read_ptr; // this module does the reading
  18781. + int active; // is this slot in the hardware dma fifo?
  18782. + uint32_t msgs_parsed; // count how many messages are in this slot
  18783. + uint32_t msgs_released; // how many messages have been released
  18784. + void *state; // connection state information
  18785. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  18786. +} RX_MSG_SLOTINFO_T;
  18787. +
  18788. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  18789. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  18790. +// driver will be tasked with sending the aligned core section.
  18791. +typedef struct rx_bulk_slotinfo_t {
  18792. + struct rx_bulk_slotinfo_t *next;
  18793. +
  18794. + struct semaphore *blocking;
  18795. +
  18796. + // needed by DMA
  18797. + void *addr;
  18798. + uint32_t len;
  18799. +
  18800. + // needed for the callback
  18801. + void *service;
  18802. + void *handle;
  18803. + VCHI_FLAGS_T flags;
  18804. +} RX_BULK_SLOTINFO_T;
  18805. +
  18806. +
  18807. +/* ----------------------------------------------------------------------
  18808. + * each connection driver will have a pool of the following struct.
  18809. + *
  18810. + * the pool will be managed by vchi_qman_*
  18811. + * this means there will be multiple queues (single linked lists)
  18812. + * a given struct message_info will be on exactly one of these queues
  18813. + * at any one time
  18814. + * -------------------------------------------------------------------- */
  18815. +typedef struct rx_message_info {
  18816. +
  18817. + struct message_info *next;
  18818. + //struct message_info *prev;
  18819. +
  18820. + uint8_t *addr;
  18821. + uint32_t len;
  18822. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  18823. + uint32_t tx_timestamp;
  18824. + uint32_t rx_timestamp;
  18825. +
  18826. +} RX_MESSAGE_INFO_T;
  18827. +
  18828. +typedef struct {
  18829. + MESSAGE_EVENT_TYPE_T type;
  18830. +
  18831. + struct {
  18832. + // for messages
  18833. + void *addr; // address of message
  18834. + uint16_t slot_delta; // whether this message indicated slot delta
  18835. + uint32_t len; // length of message
  18836. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  18837. + int32_t service; // service id this message is destined for
  18838. + uint32_t tx_timestamp; // timestamp from the header
  18839. + uint32_t rx_timestamp; // timestamp when we parsed it
  18840. + } message;
  18841. +
  18842. + // FIXME: cleanup slot reporting...
  18843. + RX_MSG_SLOTINFO_T *rx_msg;
  18844. + RX_BULK_SLOTINFO_T *rx_bulk;
  18845. + void *tx_handle;
  18846. + MESSAGE_TX_CHANNEL_T tx_channel;
  18847. +
  18848. +} MESSAGE_EVENT_T;
  18849. +
  18850. +
  18851. +// callbacks
  18852. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  18853. +
  18854. +typedef struct {
  18855. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  18856. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  18857. +
  18858. +
  18859. +// handle to this instance of message driver (as returned by ->open)
  18860. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  18861. +
  18862. +struct opaque_vchi_message_driver_t {
  18863. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  18864. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  18865. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  18866. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  18867. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  18868. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  18869. + int32_t (*send)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, VCHI_MSG_FLAGS_T flags, void *send_handle ); // tx (message & bulk)
  18870. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  18871. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  18872. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  18873. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  18874. +
  18875. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  18876. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  18877. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  18878. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  18879. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  18880. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  18881. +
  18882. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18883. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18884. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18885. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  18886. + void (*form_bulk_aux)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, uint32_t chunk_size, const void **aux_data, int32_t *aux_len );
  18887. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  18888. +};
  18889. +
  18890. +
  18891. +#endif // _VCHI_MESSAGE_H_
  18892. +
  18893. +/****************************** End of file ***********************************/
  18894. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  18895. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 1970-01-01 01:00:00.000000000 +0100
  18896. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 2014-04-24 15:35:02.889551471 +0200
  18897. @@ -0,0 +1,224 @@
  18898. +/**
  18899. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18900. + *
  18901. + * Redistribution and use in source and binary forms, with or without
  18902. + * modification, are permitted provided that the following conditions
  18903. + * are met:
  18904. + * 1. Redistributions of source code must retain the above copyright
  18905. + * notice, this list of conditions, and the following disclaimer,
  18906. + * without modification.
  18907. + * 2. Redistributions in binary form must reproduce the above copyright
  18908. + * notice, this list of conditions and the following disclaimer in the
  18909. + * documentation and/or other materials provided with the distribution.
  18910. + * 3. The names of the above-listed copyright holders may not be used
  18911. + * to endorse or promote products derived from this software without
  18912. + * specific prior written permission.
  18913. + *
  18914. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18915. + * GNU General Public License ("GPL") version 2, as published by the Free
  18916. + * Software Foundation.
  18917. + *
  18918. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18919. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18920. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18921. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18922. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18923. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18924. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18925. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18926. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18927. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18928. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18929. + */
  18930. +
  18931. +#ifndef VCHI_CFG_H_
  18932. +#define VCHI_CFG_H_
  18933. +
  18934. +/****************************************************************************************
  18935. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  18936. + * services.
  18937. + ***************************************************************************************/
  18938. +
  18939. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  18940. +/* Really determined by the message driver, and should be available from a run-time call. */
  18941. +#ifndef VCHI_BULK_ALIGN
  18942. +# if __VCCOREVER__ >= 0x04000000
  18943. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  18944. +# else
  18945. +# define VCHI_BULK_ALIGN 16
  18946. +# endif
  18947. +#endif
  18948. +
  18949. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  18950. +/* May be less than or greater than VCHI_BULK_ALIGN */
  18951. +/* Really determined by the message driver, and should be available from a run-time call. */
  18952. +#ifndef VCHI_BULK_GRANULARITY
  18953. +# if __VCCOREVER__ >= 0x04000000
  18954. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  18955. +# else
  18956. +# define VCHI_BULK_GRANULARITY 16
  18957. +# endif
  18958. +#endif
  18959. +
  18960. +/* The largest possible message to be queued with vchi_msg_queue. */
  18961. +#ifndef VCHI_MAX_MSG_SIZE
  18962. +# if defined VCHI_LOCAL_HOST_PORT
  18963. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  18964. +# else
  18965. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  18966. +# endif
  18967. +#endif
  18968. +
  18969. +/******************************************************************************************
  18970. + * Defines below are system configuration options, and should not be used by VCHI services.
  18971. + *****************************************************************************************/
  18972. +
  18973. +/* How many connections can we support? A localhost implementation uses 2 connections,
  18974. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  18975. + * driver. */
  18976. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  18977. +# define VCHI_MAX_NUM_CONNECTIONS 3
  18978. +#endif
  18979. +
  18980. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  18981. + * amount of static memory. */
  18982. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  18983. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  18984. +#endif
  18985. +
  18986. +/* Adjust if using a message driver that supports more logical TX channels */
  18987. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  18988. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  18989. +#endif
  18990. +
  18991. +/* Adjust if using a message driver that supports more logical RX channels */
  18992. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  18993. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  18994. +#endif
  18995. +
  18996. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  18997. + * receive queue space, less message headers. */
  18998. +#ifndef VCHI_NUM_READ_SLOTS
  18999. +# if defined(VCHI_LOCAL_HOST_PORT)
  19000. +# define VCHI_NUM_READ_SLOTS 4
  19001. +# else
  19002. +# define VCHI_NUM_READ_SLOTS 48
  19003. +# endif
  19004. +#endif
  19005. +
  19006. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  19007. + * performance. Only define on VideoCore end, talking to host.
  19008. + */
  19009. +//#define VCHI_MSG_RX_OVERRUN
  19010. +
  19011. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  19012. + * underneath VCHI will usually have its own buffering. */
  19013. +#ifndef VCHI_NUM_WRITE_SLOTS
  19014. +# define VCHI_NUM_WRITE_SLOTS 4
  19015. +#endif
  19016. +
  19017. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  19018. + * then it's taking up too much buffer space, and the peer service will be told to stop
  19019. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  19020. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  19021. + * is too high. */
  19022. +#ifndef VCHI_XOFF_THRESHOLD
  19023. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  19024. +#endif
  19025. +
  19026. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  19027. + * service has dequeued/released enough messages that it's now occupying
  19028. + * VCHI_XON_THRESHOLD slots or fewer. */
  19029. +#ifndef VCHI_XON_THRESHOLD
  19030. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  19031. +#endif
  19032. +
  19033. +/* A size below which a bulk transfer omits the handshake completely and always goes
  19034. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  19035. + * can guarantee this by enabling unaligned transmits).
  19036. + * Not API. */
  19037. +#ifndef VCHI_MIN_BULK_SIZE
  19038. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  19039. +#endif
  19040. +
  19041. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  19042. + * speed and latency; the smaller the chunk size the better change of messages and other
  19043. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  19044. + * break transmissions into chunks.
  19045. + */
  19046. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  19047. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  19048. +#endif
  19049. +
  19050. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  19051. + * with multiple-line frames. Only use if the receiver can cope. */
  19052. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  19053. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  19054. +#endif
  19055. +
  19056. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  19057. + * vchi_msg_queue will be blocked. */
  19058. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  19059. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  19060. +#endif
  19061. +
  19062. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  19063. + * will be suspended until older messages are dequeued/released. */
  19064. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  19065. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  19066. +#endif
  19067. +
  19068. +/* Really should be able to cope if we run out of received message descriptors, by
  19069. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  19070. + * under the carpet. */
  19071. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  19072. +# undef VCHI_RX_MSG_QUEUE_SIZE
  19073. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  19074. +#endif
  19075. +
  19076. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  19077. + * will be blocked. */
  19078. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  19079. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  19080. +#endif
  19081. +
  19082. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  19083. + * will be blocked. */
  19084. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  19085. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  19086. +#endif
  19087. +
  19088. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  19089. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  19090. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  19091. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  19092. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  19093. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  19094. +#endif
  19095. +
  19096. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  19097. + * transmitter on and off.
  19098. + */
  19099. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  19100. +
  19101. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  19102. +
  19103. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  19104. + * negative for no IDLE.
  19105. + */
  19106. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  19107. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  19108. +# endif
  19109. +
  19110. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  19111. + * negative for no OFF.
  19112. + */
  19113. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  19114. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  19115. +# endif
  19116. +
  19117. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  19118. +
  19119. +#endif /* VCHI_CFG_H_ */
  19120. +
  19121. +/****************************** End of file **********************************/
  19122. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  19123. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 1970-01-01 01:00:00.000000000 +0100
  19124. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 2014-04-24 15:35:02.889551471 +0200
  19125. @@ -0,0 +1,71 @@
  19126. +/**
  19127. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19128. + *
  19129. + * Redistribution and use in source and binary forms, with or without
  19130. + * modification, are permitted provided that the following conditions
  19131. + * are met:
  19132. + * 1. Redistributions of source code must retain the above copyright
  19133. + * notice, this list of conditions, and the following disclaimer,
  19134. + * without modification.
  19135. + * 2. Redistributions in binary form must reproduce the above copyright
  19136. + * notice, this list of conditions and the following disclaimer in the
  19137. + * documentation and/or other materials provided with the distribution.
  19138. + * 3. The names of the above-listed copyright holders may not be used
  19139. + * to endorse or promote products derived from this software without
  19140. + * specific prior written permission.
  19141. + *
  19142. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19143. + * GNU General Public License ("GPL") version 2, as published by the Free
  19144. + * Software Foundation.
  19145. + *
  19146. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19147. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19148. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19149. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19150. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19151. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19152. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19153. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19154. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19155. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19156. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19157. + */
  19158. +
  19159. +#ifndef VCHI_CFG_INTERNAL_H_
  19160. +#define VCHI_CFG_INTERNAL_H_
  19161. +
  19162. +/****************************************************************************************
  19163. + * Control optimisation attempts.
  19164. + ***************************************************************************************/
  19165. +
  19166. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  19167. +#define VCHI_COARSE_LOCKING
  19168. +
  19169. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  19170. +// (only relevant if VCHI_COARSE_LOCKING)
  19171. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  19172. +
  19173. +// Avoid lock on non-blocking peek
  19174. +// (only relevant if VCHI_COARSE_LOCKING)
  19175. +#define VCHI_AVOID_PEEK_LOCK
  19176. +
  19177. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  19178. +#define VCHI_MULTIPLE_HANDLER_THREADS
  19179. +
  19180. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  19181. +// our way through the pool of descriptors.
  19182. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  19183. +
  19184. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  19185. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  19186. +
  19187. +// Don't use message descriptors for TX messages that don't need them
  19188. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  19189. +
  19190. +// Nano-locks for multiqueue
  19191. +//#define VCHI_MQUEUE_NANOLOCKS
  19192. +
  19193. +// Lock-free(er) dequeuing
  19194. +//#define VCHI_RX_NANOLOCKS
  19195. +
  19196. +#endif /*VCHI_CFG_INTERNAL_H_*/
  19197. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_common.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  19198. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_common.h 1970-01-01 01:00:00.000000000 +0100
  19199. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h 2014-04-24 15:35:02.889551471 +0200
  19200. @@ -0,0 +1,163 @@
  19201. +/**
  19202. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19203. + *
  19204. + * Redistribution and use in source and binary forms, with or without
  19205. + * modification, are permitted provided that the following conditions
  19206. + * are met:
  19207. + * 1. Redistributions of source code must retain the above copyright
  19208. + * notice, this list of conditions, and the following disclaimer,
  19209. + * without modification.
  19210. + * 2. Redistributions in binary form must reproduce the above copyright
  19211. + * notice, this list of conditions and the following disclaimer in the
  19212. + * documentation and/or other materials provided with the distribution.
  19213. + * 3. The names of the above-listed copyright holders may not be used
  19214. + * to endorse or promote products derived from this software without
  19215. + * specific prior written permission.
  19216. + *
  19217. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19218. + * GNU General Public License ("GPL") version 2, as published by the Free
  19219. + * Software Foundation.
  19220. + *
  19221. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19222. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19223. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19224. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19225. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19226. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19227. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19228. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19229. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19230. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19231. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19232. + */
  19233. +
  19234. +#ifndef VCHI_COMMON_H_
  19235. +#define VCHI_COMMON_H_
  19236. +
  19237. +
  19238. +//flags used when sending messages (must be bitmapped)
  19239. +typedef enum
  19240. +{
  19241. + VCHI_FLAGS_NONE = 0x0,
  19242. + VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE = 0x1, // waits for message to be received, or sent (NB. not the same as being seen on other side)
  19243. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  19244. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  19245. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  19246. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  19247. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  19248. +
  19249. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  19250. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  19251. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  19252. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  19253. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  19254. + VCHI_FLAGS_INTERNAL = 0xFF0000
  19255. +} VCHI_FLAGS_T;
  19256. +
  19257. +// constants for vchi_crc_control()
  19258. +typedef enum {
  19259. + VCHI_CRC_NOTHING = -1,
  19260. + VCHI_CRC_PER_SERVICE = 0,
  19261. + VCHI_CRC_EVERYTHING = 1,
  19262. +} VCHI_CRC_CONTROL_T;
  19263. +
  19264. +//callback reasons when an event occurs on a service
  19265. +typedef enum
  19266. +{
  19267. + VCHI_CALLBACK_REASON_MIN,
  19268. +
  19269. + //This indicates that there is data available
  19270. + //handle is the msg id that was transmitted with the data
  19271. + // When a message is received and there was no FULL message available previously, send callback
  19272. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  19273. + VCHI_CALLBACK_MSG_AVAILABLE,
  19274. + VCHI_CALLBACK_MSG_SENT,
  19275. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  19276. +
  19277. + // This indicates that a transfer from the other side has completed
  19278. + VCHI_CALLBACK_BULK_RECEIVED,
  19279. + //This indicates that data queued up to be sent has now gone
  19280. + //handle is the msg id that was used when sending the data
  19281. + VCHI_CALLBACK_BULK_SENT,
  19282. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  19283. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  19284. +
  19285. + VCHI_CALLBACK_SERVICE_CLOSED,
  19286. +
  19287. + // this side has sent XOFF to peer due to lack of data consumption by service
  19288. + // (suggests the service may need to take some recovery action if it has
  19289. + // been deliberately holding off consuming data)
  19290. + VCHI_CALLBACK_SENT_XOFF,
  19291. + VCHI_CALLBACK_SENT_XON,
  19292. +
  19293. + // indicates that a bulk transfer has finished reading the source buffer
  19294. + VCHI_CALLBACK_BULK_DATA_READ,
  19295. +
  19296. + // power notification events (currently host side only)
  19297. + VCHI_CALLBACK_PEER_OFF,
  19298. + VCHI_CALLBACK_PEER_SUSPENDED,
  19299. + VCHI_CALLBACK_PEER_ON,
  19300. + VCHI_CALLBACK_PEER_RESUMED,
  19301. + VCHI_CALLBACK_FORCED_POWER_OFF,
  19302. +
  19303. +#ifdef USE_VCHIQ_ARM
  19304. + // some extra notifications provided by vchiq_arm
  19305. + VCHI_CALLBACK_SERVICE_OPENED,
  19306. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  19307. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  19308. +#endif
  19309. +
  19310. + VCHI_CALLBACK_REASON_MAX
  19311. +} VCHI_CALLBACK_REASON_T;
  19312. +
  19313. +//Calback used by all services / bulk transfers
  19314. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  19315. + VCHI_CALLBACK_REASON_T reason,
  19316. + void *handle ); //for transmitting msg's only
  19317. +
  19318. +
  19319. +
  19320. +/*
  19321. + * Define vector struct for scatter-gather (vector) operations
  19322. + * Vectors can be nested - if a vector element has negative length, then
  19323. + * the data pointer is treated as pointing to another vector array, with
  19324. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  19325. + * you can do this:
  19326. + *
  19327. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  19328. + * {
  19329. + * VCHI_MSG_VECTOR_T nv[2];
  19330. + * nv[0].vec_base = my_header;
  19331. + * nv[0].vec_len = sizeof my_header;
  19332. + * nv[1].vec_base = v;
  19333. + * nv[1].vec_len = -n;
  19334. + * ...
  19335. + *
  19336. + */
  19337. +typedef struct vchi_msg_vector {
  19338. + const void *vec_base;
  19339. + int32_t vec_len;
  19340. +} VCHI_MSG_VECTOR_T;
  19341. +
  19342. +// Opaque type for a connection API
  19343. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  19344. +
  19345. +// Opaque type for a message driver
  19346. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  19347. +
  19348. +
  19349. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  19350. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  19351. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  19352. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  19353. +// is used again after messages for that service are removed/dequeued by any
  19354. +// means other than vchi_msg_iter_... calls on the iterator itself.
  19355. +typedef struct {
  19356. + struct opaque_vchi_service_t *service;
  19357. + void *last;
  19358. + void *next;
  19359. + void *remove;
  19360. +} VCHI_MSG_ITER_T;
  19361. +
  19362. +
  19363. +#endif // VCHI_COMMON_H_
  19364. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h
  19365. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi.h 1970-01-01 01:00:00.000000000 +0100
  19366. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h 2014-04-24 15:35:02.889551471 +0200
  19367. @@ -0,0 +1,373 @@
  19368. +/**
  19369. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19370. + *
  19371. + * Redistribution and use in source and binary forms, with or without
  19372. + * modification, are permitted provided that the following conditions
  19373. + * are met:
  19374. + * 1. Redistributions of source code must retain the above copyright
  19375. + * notice, this list of conditions, and the following disclaimer,
  19376. + * without modification.
  19377. + * 2. Redistributions in binary form must reproduce the above copyright
  19378. + * notice, this list of conditions and the following disclaimer in the
  19379. + * documentation and/or other materials provided with the distribution.
  19380. + * 3. The names of the above-listed copyright holders may not be used
  19381. + * to endorse or promote products derived from this software without
  19382. + * specific prior written permission.
  19383. + *
  19384. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19385. + * GNU General Public License ("GPL") version 2, as published by the Free
  19386. + * Software Foundation.
  19387. + *
  19388. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19389. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19390. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19391. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19392. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19393. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19394. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19395. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19396. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19397. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19398. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19399. + */
  19400. +
  19401. +#ifndef VCHI_H_
  19402. +#define VCHI_H_
  19403. +
  19404. +#include "interface/vchi/vchi_cfg.h"
  19405. +#include "interface/vchi/vchi_common.h"
  19406. +#include "interface/vchi/connections/connection.h"
  19407. +#include "vchi_mh.h"
  19408. +
  19409. +
  19410. +/******************************************************************************
  19411. + Global defs
  19412. + *****************************************************************************/
  19413. +
  19414. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  19415. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  19416. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  19417. +
  19418. +#ifdef USE_VCHIQ_ARM
  19419. +#define VCHI_BULK_ALIGNED(x) 1
  19420. +#else
  19421. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  19422. +#endif
  19423. +
  19424. +struct vchi_version {
  19425. + uint32_t version;
  19426. + uint32_t version_min;
  19427. +};
  19428. +#define VCHI_VERSION(v_) { v_, v_ }
  19429. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  19430. +
  19431. +typedef enum
  19432. +{
  19433. + VCHI_VEC_POINTER,
  19434. + VCHI_VEC_HANDLE,
  19435. + VCHI_VEC_LIST
  19436. +} VCHI_MSG_VECTOR_TYPE_T;
  19437. +
  19438. +typedef struct vchi_msg_vector_ex {
  19439. +
  19440. + VCHI_MSG_VECTOR_TYPE_T type;
  19441. + union
  19442. + {
  19443. + // a memory handle
  19444. + struct
  19445. + {
  19446. + VCHI_MEM_HANDLE_T handle;
  19447. + uint32_t offset;
  19448. + int32_t vec_len;
  19449. + } handle;
  19450. +
  19451. + // an ordinary data pointer
  19452. + struct
  19453. + {
  19454. + const void *vec_base;
  19455. + int32_t vec_len;
  19456. + } ptr;
  19457. +
  19458. + // a nested vector list
  19459. + struct
  19460. + {
  19461. + struct vchi_msg_vector_ex *vec;
  19462. + uint32_t vec_len;
  19463. + } list;
  19464. + } u;
  19465. +} VCHI_MSG_VECTOR_EX_T;
  19466. +
  19467. +
  19468. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  19469. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  19470. +
  19471. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  19472. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  19473. +
  19474. +// Macros to manipulate 'FOURCC' values
  19475. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  19476. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  19477. +
  19478. +
  19479. +// Opaque service information
  19480. +struct opaque_vchi_service_t;
  19481. +
  19482. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  19483. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  19484. +typedef struct
  19485. +{
  19486. + struct opaque_vchi_service_t *service;
  19487. + void *message;
  19488. +} VCHI_HELD_MSG_T;
  19489. +
  19490. +
  19491. +
  19492. +// structure used to provide the information needed to open a server or a client
  19493. +typedef struct {
  19494. + struct vchi_version version;
  19495. + int32_t service_id;
  19496. + VCHI_CONNECTION_T *connection;
  19497. + uint32_t rx_fifo_size;
  19498. + uint32_t tx_fifo_size;
  19499. + VCHI_CALLBACK_T callback;
  19500. + void *callback_param;
  19501. + /* client intends to receive bulk transfers of
  19502. + odd lengths or into unaligned buffers */
  19503. + int32_t want_unaligned_bulk_rx;
  19504. + /* client intends to transmit bulk transfers of
  19505. + odd lengths or out of unaligned buffers */
  19506. + int32_t want_unaligned_bulk_tx;
  19507. + /* client wants to check CRCs on (bulk) xfers.
  19508. + Only needs to be set at 1 end - will do both directions. */
  19509. + int32_t want_crc;
  19510. +} SERVICE_CREATION_T;
  19511. +
  19512. +// Opaque handle for a VCHI instance
  19513. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  19514. +
  19515. +// Opaque handle for a server or client
  19516. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  19517. +
  19518. +// Service registration & startup
  19519. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  19520. +
  19521. +typedef struct service_info_tag {
  19522. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  19523. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  19524. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  19525. +} SERVICE_INFO_T;
  19526. +
  19527. +/******************************************************************************
  19528. + Global funcs - implementation is specific to which side you are on (local / remote)
  19529. + *****************************************************************************/
  19530. +
  19531. +#ifdef __cplusplus
  19532. +extern "C" {
  19533. +#endif
  19534. +
  19535. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  19536. + const VCHI_MESSAGE_DRIVER_T * low_level);
  19537. +
  19538. +
  19539. +// Routine used to initialise the vchi on both local + remote connections
  19540. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  19541. +
  19542. +extern int32_t vchi_exit( void );
  19543. +
  19544. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  19545. + const uint32_t num_connections,
  19546. + VCHI_INSTANCE_T instance_handle );
  19547. +
  19548. +//When this is called, ensure that all services have no data pending.
  19549. +//Bulk transfers can remain 'queued'
  19550. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  19551. +
  19552. +// Global control over bulk CRC checking
  19553. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  19554. + VCHI_CRC_CONTROL_T control );
  19555. +
  19556. +// helper functions
  19557. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  19558. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  19559. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  19560. +
  19561. +
  19562. +/******************************************************************************
  19563. + Global service API
  19564. + *****************************************************************************/
  19565. +// Routine to create a named service
  19566. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  19567. + SERVICE_CREATION_T *setup,
  19568. + VCHI_SERVICE_HANDLE_T *handle );
  19569. +
  19570. +// Routine to destory a service
  19571. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  19572. +
  19573. +// Routine to open a named service
  19574. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  19575. + SERVICE_CREATION_T *setup,
  19576. + VCHI_SERVICE_HANDLE_T *handle);
  19577. +
  19578. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  19579. + short *peer_version );
  19580. +
  19581. +// Routine to close a named service
  19582. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  19583. +
  19584. +// Routine to increment ref count on a named service
  19585. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  19586. +
  19587. +// Routine to decrement ref count on a named service
  19588. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  19589. +
  19590. +// Routine to send a message accross a service
  19591. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  19592. + const void *data,
  19593. + uint32_t data_size,
  19594. + VCHI_FLAGS_T flags,
  19595. + void *msg_handle );
  19596. +
  19597. +// scatter-gather (vector) and send message
  19598. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  19599. + VCHI_MSG_VECTOR_EX_T *vector,
  19600. + uint32_t count,
  19601. + VCHI_FLAGS_T flags,
  19602. + void *msg_handle );
  19603. +
  19604. +// legacy scatter-gather (vector) and send message, only handles pointers
  19605. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  19606. + VCHI_MSG_VECTOR_T *vector,
  19607. + uint32_t count,
  19608. + VCHI_FLAGS_T flags,
  19609. + void *msg_handle );
  19610. +
  19611. +// Routine to receive a msg from a service
  19612. +// Dequeue is equivalent to hold, copy into client buffer, release
  19613. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  19614. + void *data,
  19615. + uint32_t max_data_size_to_read,
  19616. + uint32_t *actual_msg_size,
  19617. + VCHI_FLAGS_T flags );
  19618. +
  19619. +// Routine to look at a message in place.
  19620. +// The message is not dequeued, so a subsequent call to peek or dequeue
  19621. +// will return the same message.
  19622. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  19623. + void **data,
  19624. + uint32_t *msg_size,
  19625. + VCHI_FLAGS_T flags );
  19626. +
  19627. +// Routine to remove a message after it has been read in place with peek
  19628. +// The first message on the queue is dequeued.
  19629. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  19630. +
  19631. +// Routine to look at a message in place.
  19632. +// The message is dequeued, so the caller is left holding it; the descriptor is
  19633. +// filled in and must be released when the user has finished with the message.
  19634. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  19635. + void **data, // } may be NULL, as info can be
  19636. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  19637. + VCHI_FLAGS_T flags,
  19638. + VCHI_HELD_MSG_T *message_descriptor );
  19639. +
  19640. +// Initialise an iterator to look through messages in place
  19641. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  19642. + VCHI_MSG_ITER_T *iter,
  19643. + VCHI_FLAGS_T flags );
  19644. +
  19645. +/******************************************************************************
  19646. + Global service support API - operations on held messages and message iterators
  19647. + *****************************************************************************/
  19648. +
  19649. +// Routine to get the address of a held message
  19650. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  19651. +
  19652. +// Routine to get the size of a held message
  19653. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  19654. +
  19655. +// Routine to get the transmit timestamp as written into the header by the peer
  19656. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  19657. +
  19658. +// Routine to get the reception timestamp, written as we parsed the header
  19659. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  19660. +
  19661. +// Routine to release a held message after it has been processed
  19662. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  19663. +
  19664. +// Indicates whether the iterator has a next message.
  19665. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  19666. +
  19667. +// Return the pointer and length for the next message and advance the iterator.
  19668. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  19669. + void **data,
  19670. + uint32_t *msg_size );
  19671. +
  19672. +// Remove the last message returned by vchi_msg_iter_next.
  19673. +// Can only be called once after each call to vchi_msg_iter_next.
  19674. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  19675. +
  19676. +// Hold the last message returned by vchi_msg_iter_next.
  19677. +// Can only be called once after each call to vchi_msg_iter_next.
  19678. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  19679. + VCHI_HELD_MSG_T *message );
  19680. +
  19681. +// Return information for the next message, and hold it, advancing the iterator.
  19682. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  19683. + void **data, // } may be NULL
  19684. + uint32_t *msg_size, // }
  19685. + VCHI_HELD_MSG_T *message );
  19686. +
  19687. +
  19688. +/******************************************************************************
  19689. + Global bulk API
  19690. + *****************************************************************************/
  19691. +
  19692. +// Routine to prepare interface for a transfer from the other side
  19693. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  19694. + void *data_dst,
  19695. + uint32_t data_size,
  19696. + VCHI_FLAGS_T flags,
  19697. + void *transfer_handle );
  19698. +
  19699. +
  19700. +// Prepare interface for a transfer from the other side into relocatable memory.
  19701. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  19702. + VCHI_MEM_HANDLE_T h_dst,
  19703. + uint32_t offset,
  19704. + uint32_t data_size,
  19705. + const VCHI_FLAGS_T flags,
  19706. + void * const bulk_handle );
  19707. +
  19708. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  19709. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  19710. + const void *data_src,
  19711. + uint32_t data_size,
  19712. + VCHI_FLAGS_T flags,
  19713. + void *transfer_handle );
  19714. +
  19715. +
  19716. +/******************************************************************************
  19717. + Configuration plumbing
  19718. + *****************************************************************************/
  19719. +
  19720. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  19721. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  19722. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  19723. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  19724. +
  19725. +// declare all message drivers here
  19726. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  19727. +
  19728. +#ifdef __cplusplus
  19729. +}
  19730. +#endif
  19731. +
  19732. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  19733. + VCHI_MEM_HANDLE_T h_src,
  19734. + uint32_t offset,
  19735. + uint32_t data_size,
  19736. + VCHI_FLAGS_T flags,
  19737. + void *transfer_handle );
  19738. +#endif /* VCHI_H_ */
  19739. +
  19740. +/****************************** End of file **********************************/
  19741. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_mh.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  19742. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 1970-01-01 01:00:00.000000000 +0100
  19743. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 2014-04-24 15:35:02.893551516 +0200
  19744. @@ -0,0 +1,42 @@
  19745. +/**
  19746. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19747. + *
  19748. + * Redistribution and use in source and binary forms, with or without
  19749. + * modification, are permitted provided that the following conditions
  19750. + * are met:
  19751. + * 1. Redistributions of source code must retain the above copyright
  19752. + * notice, this list of conditions, and the following disclaimer,
  19753. + * without modification.
  19754. + * 2. Redistributions in binary form must reproduce the above copyright
  19755. + * notice, this list of conditions and the following disclaimer in the
  19756. + * documentation and/or other materials provided with the distribution.
  19757. + * 3. The names of the above-listed copyright holders may not be used
  19758. + * to endorse or promote products derived from this software without
  19759. + * specific prior written permission.
  19760. + *
  19761. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19762. + * GNU General Public License ("GPL") version 2, as published by the Free
  19763. + * Software Foundation.
  19764. + *
  19765. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19766. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19767. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19768. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19769. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19770. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19771. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19772. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19773. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19774. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19775. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19776. + */
  19777. +
  19778. +#ifndef VCHI_MH_H_
  19779. +#define VCHI_MH_H_
  19780. +
  19781. +#include <linux/types.h>
  19782. +
  19783. +typedef int32_t VCHI_MEM_HANDLE_T;
  19784. +#define VCHI_MEM_HANDLE_INVALID 0
  19785. +
  19786. +#endif
  19787. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  19788. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 1970-01-01 01:00:00.000000000 +0100
  19789. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 2014-04-24 15:35:02.893551516 +0200
  19790. @@ -0,0 +1,561 @@
  19791. +/**
  19792. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19793. + *
  19794. + * Redistribution and use in source and binary forms, with or without
  19795. + * modification, are permitted provided that the following conditions
  19796. + * are met:
  19797. + * 1. Redistributions of source code must retain the above copyright
  19798. + * notice, this list of conditions, and the following disclaimer,
  19799. + * without modification.
  19800. + * 2. Redistributions in binary form must reproduce the above copyright
  19801. + * notice, this list of conditions and the following disclaimer in the
  19802. + * documentation and/or other materials provided with the distribution.
  19803. + * 3. The names of the above-listed copyright holders may not be used
  19804. + * to endorse or promote products derived from this software without
  19805. + * specific prior written permission.
  19806. + *
  19807. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19808. + * GNU General Public License ("GPL") version 2, as published by the Free
  19809. + * Software Foundation.
  19810. + *
  19811. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19812. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19813. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19814. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19815. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19816. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19817. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19818. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19819. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19820. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19821. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19822. + */
  19823. +
  19824. +#include <linux/kernel.h>
  19825. +#include <linux/types.h>
  19826. +#include <linux/errno.h>
  19827. +#include <linux/interrupt.h>
  19828. +#include <linux/irq.h>
  19829. +#include <linux/pagemap.h>
  19830. +#include <linux/dma-mapping.h>
  19831. +#include <linux/version.h>
  19832. +#include <linux/io.h>
  19833. +#include <linux/uaccess.h>
  19834. +#include <asm/pgtable.h>
  19835. +
  19836. +#include <mach/irqs.h>
  19837. +
  19838. +#include <mach/platform.h>
  19839. +#include <mach/vcio.h>
  19840. +
  19841. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  19842. +
  19843. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  19844. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  19845. +
  19846. +#include "vchiq_arm.h"
  19847. +#include "vchiq_2835.h"
  19848. +#include "vchiq_connected.h"
  19849. +
  19850. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  19851. +
  19852. +typedef struct vchiq_2835_state_struct {
  19853. + int inited;
  19854. + VCHIQ_ARM_STATE_T arm_state;
  19855. +} VCHIQ_2835_ARM_STATE_T;
  19856. +
  19857. +static char *g_slot_mem;
  19858. +static int g_slot_mem_size;
  19859. +dma_addr_t g_slot_phys;
  19860. +static FRAGMENTS_T *g_fragments_base;
  19861. +static FRAGMENTS_T *g_free_fragments;
  19862. +struct semaphore g_free_fragments_sema;
  19863. +
  19864. +extern int vchiq_arm_log_level;
  19865. +
  19866. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  19867. +
  19868. +static irqreturn_t
  19869. +vchiq_doorbell_irq(int irq, void *dev_id);
  19870. +
  19871. +static int
  19872. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  19873. + struct task_struct *task, PAGELIST_T ** ppagelist);
  19874. +
  19875. +static void
  19876. +free_pagelist(PAGELIST_T *pagelist, int actual);
  19877. +
  19878. +int __init
  19879. +vchiq_platform_init(VCHIQ_STATE_T *state)
  19880. +{
  19881. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  19882. + int frag_mem_size;
  19883. + int err;
  19884. + int i;
  19885. +
  19886. + /* Allocate space for the channels in coherent memory */
  19887. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  19888. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  19889. +
  19890. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  19891. + &g_slot_phys, GFP_ATOMIC);
  19892. +
  19893. + if (!g_slot_mem) {
  19894. + vchiq_log_error(vchiq_arm_log_level,
  19895. + "Unable to allocate channel memory");
  19896. + err = -ENOMEM;
  19897. + goto failed_alloc;
  19898. + }
  19899. +
  19900. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  19901. +
  19902. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  19903. + if (!vchiq_slot_zero) {
  19904. + err = -EINVAL;
  19905. + goto failed_init_slots;
  19906. + }
  19907. +
  19908. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  19909. + (int)g_slot_phys + g_slot_mem_size;
  19910. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  19911. + MAX_FRAGMENTS;
  19912. +
  19913. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  19914. + g_slot_mem_size += frag_mem_size;
  19915. +
  19916. + g_free_fragments = g_fragments_base;
  19917. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  19918. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  19919. + &g_fragments_base[i + 1];
  19920. + }
  19921. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  19922. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  19923. +
  19924. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  19925. + VCHIQ_SUCCESS) {
  19926. + err = -EINVAL;
  19927. + goto failed_vchiq_init;
  19928. + }
  19929. +
  19930. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  19931. + IRQF_IRQPOLL, "VCHIQ doorbell",
  19932. + state);
  19933. + if (err < 0) {
  19934. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  19935. + "irq=%d err=%d", __func__,
  19936. + VCHIQ_DOORBELL_IRQ, err);
  19937. + goto failed_request_irq;
  19938. + }
  19939. +
  19940. + /* Send the base address of the slots to VideoCore */
  19941. +
  19942. + dsb(); /* Ensure all writes have completed */
  19943. +
  19944. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  19945. +
  19946. + vchiq_log_info(vchiq_arm_log_level,
  19947. + "vchiq_init - done (slots %x, phys %x)",
  19948. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  19949. +
  19950. + vchiq_call_connected_callbacks();
  19951. +
  19952. + return 0;
  19953. +
  19954. +failed_request_irq:
  19955. +failed_vchiq_init:
  19956. +failed_init_slots:
  19957. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  19958. +
  19959. +failed_alloc:
  19960. + return err;
  19961. +}
  19962. +
  19963. +void __exit
  19964. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  19965. +{
  19966. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  19967. + dma_free_coherent(NULL, g_slot_mem_size,
  19968. + g_slot_mem, g_slot_phys);
  19969. +}
  19970. +
  19971. +
  19972. +VCHIQ_STATUS_T
  19973. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  19974. +{
  19975. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  19976. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  19977. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  19978. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  19979. + if(status != VCHIQ_SUCCESS)
  19980. + {
  19981. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  19982. + }
  19983. + return status;
  19984. +}
  19985. +
  19986. +VCHIQ_ARM_STATE_T*
  19987. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  19988. +{
  19989. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  19990. + {
  19991. + BUG();
  19992. + }
  19993. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  19994. +}
  19995. +
  19996. +void
  19997. +remote_event_signal(REMOTE_EVENT_T *event)
  19998. +{
  19999. + wmb();
  20000. +
  20001. + event->fired = 1;
  20002. +
  20003. + dsb(); /* data barrier operation */
  20004. +
  20005. + if (event->armed) {
  20006. + /* trigger vc interrupt */
  20007. +
  20008. + writel(0, __io_address(ARM_0_BELL2));
  20009. + }
  20010. +}
  20011. +
  20012. +int
  20013. +vchiq_copy_from_user(void *dst, const void *src, int size)
  20014. +{
  20015. + if ((uint32_t)src < TASK_SIZE) {
  20016. + return copy_from_user(dst, src, size);
  20017. + } else {
  20018. + memcpy(dst, src, size);
  20019. + return 0;
  20020. + }
  20021. +}
  20022. +
  20023. +VCHIQ_STATUS_T
  20024. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  20025. + void *offset, int size, int dir)
  20026. +{
  20027. + PAGELIST_T *pagelist;
  20028. + int ret;
  20029. +
  20030. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  20031. +
  20032. + ret = create_pagelist((char __user *)offset, size,
  20033. + (dir == VCHIQ_BULK_RECEIVE)
  20034. + ? PAGELIST_READ
  20035. + : PAGELIST_WRITE,
  20036. + current,
  20037. + &pagelist);
  20038. + if (ret != 0)
  20039. + return VCHIQ_ERROR;
  20040. +
  20041. + bulk->handle = memhandle;
  20042. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  20043. +
  20044. + /* Store the pagelist address in remote_data, which isn't used by the
  20045. + slave. */
  20046. + bulk->remote_data = pagelist;
  20047. +
  20048. + return VCHIQ_SUCCESS;
  20049. +}
  20050. +
  20051. +void
  20052. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  20053. +{
  20054. + if (bulk && bulk->remote_data && bulk->actual)
  20055. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  20056. +}
  20057. +
  20058. +void
  20059. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  20060. +{
  20061. + /*
  20062. + * This should only be called on the master (VideoCore) side, but
  20063. + * provide an implementation to avoid the need for ifdefery.
  20064. + */
  20065. + BUG();
  20066. +}
  20067. +
  20068. +void
  20069. +vchiq_dump_platform_state(void *dump_context)
  20070. +{
  20071. + char buf[80];
  20072. + int len;
  20073. + len = snprintf(buf, sizeof(buf),
  20074. + " Platform: 2835 (VC master)");
  20075. + vchiq_dump(dump_context, buf, len + 1);
  20076. +}
  20077. +
  20078. +VCHIQ_STATUS_T
  20079. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  20080. +{
  20081. + return VCHIQ_ERROR;
  20082. +}
  20083. +
  20084. +VCHIQ_STATUS_T
  20085. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  20086. +{
  20087. + return VCHIQ_SUCCESS;
  20088. +}
  20089. +
  20090. +void
  20091. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  20092. +{
  20093. +}
  20094. +
  20095. +void
  20096. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  20097. +{
  20098. +}
  20099. +
  20100. +int
  20101. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  20102. +{
  20103. + return 1; // autosuspend not supported - videocore always wanted
  20104. +}
  20105. +
  20106. +int
  20107. +vchiq_platform_use_suspend_timer(void)
  20108. +{
  20109. + return 0;
  20110. +}
  20111. +void
  20112. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  20113. +{
  20114. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  20115. +}
  20116. +void
  20117. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  20118. +{
  20119. + (void)state;
  20120. +}
  20121. +/*
  20122. + * Local functions
  20123. + */
  20124. +
  20125. +static irqreturn_t
  20126. +vchiq_doorbell_irq(int irq, void *dev_id)
  20127. +{
  20128. + VCHIQ_STATE_T *state = dev_id;
  20129. + irqreturn_t ret = IRQ_NONE;
  20130. + unsigned int status;
  20131. +
  20132. + /* Read (and clear) the doorbell */
  20133. + status = readl(__io_address(ARM_0_BELL0));
  20134. +
  20135. + if (status & 0x4) { /* Was the doorbell rung? */
  20136. + remote_event_pollall(state);
  20137. + ret = IRQ_HANDLED;
  20138. + }
  20139. +
  20140. + return ret;
  20141. +}
  20142. +
  20143. +/* There is a potential problem with partial cache lines (pages?)
  20144. +** at the ends of the block when reading. If the CPU accessed anything in
  20145. +** the same line (page?) then it may have pulled old data into the cache,
  20146. +** obscuring the new data underneath. We can solve this by transferring the
  20147. +** partial cache lines separately, and allowing the ARM to copy into the
  20148. +** cached area.
  20149. +
  20150. +** N.B. This implementation plays slightly fast and loose with the Linux
  20151. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  20152. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  20153. +** from increased speed as a result.
  20154. +*/
  20155. +
  20156. +static int
  20157. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  20158. + struct task_struct *task, PAGELIST_T ** ppagelist)
  20159. +{
  20160. + PAGELIST_T *pagelist;
  20161. + struct page **pages;
  20162. + struct page *page;
  20163. + unsigned long *addrs;
  20164. + unsigned int num_pages, offset, i;
  20165. + char *addr, *base_addr, *next_addr;
  20166. + int run, addridx, actual_pages;
  20167. + unsigned long *need_release;
  20168. +
  20169. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  20170. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  20171. +
  20172. + *ppagelist = NULL;
  20173. +
  20174. + /* Allocate enough storage to hold the page pointers and the page
  20175. + ** list
  20176. + */
  20177. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  20178. + (num_pages * sizeof(unsigned long)) +
  20179. + sizeof(unsigned long) +
  20180. + (num_pages * sizeof(pages[0])),
  20181. + GFP_KERNEL);
  20182. +
  20183. + vchiq_log_trace(vchiq_arm_log_level,
  20184. + "create_pagelist - %x", (unsigned int)pagelist);
  20185. + if (!pagelist)
  20186. + return -ENOMEM;
  20187. +
  20188. + addrs = pagelist->addrs;
  20189. + need_release = (unsigned long *)(addrs + num_pages);
  20190. + pages = (struct page **)(addrs + num_pages + 1);
  20191. +
  20192. + if (is_vmalloc_addr(buf)) {
  20193. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  20194. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  20195. + }
  20196. + *need_release = 0; /* do not try and release vmalloc pages */
  20197. + } else {
  20198. + down_read(&task->mm->mmap_sem);
  20199. + actual_pages = get_user_pages(task, task->mm,
  20200. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  20201. + num_pages,
  20202. + (type == PAGELIST_READ) /*Write */ ,
  20203. + 0 /*Force */ ,
  20204. + pages,
  20205. + NULL /*vmas */);
  20206. + up_read(&task->mm->mmap_sem);
  20207. +
  20208. + if (actual_pages != num_pages) {
  20209. + vchiq_log_info(vchiq_arm_log_level,
  20210. + "create_pagelist - only %d/%d pages locked",
  20211. + actual_pages,
  20212. + num_pages);
  20213. +
  20214. + /* This is probably due to the process being killed */
  20215. + while (actual_pages > 0)
  20216. + {
  20217. + actual_pages--;
  20218. + page_cache_release(pages[actual_pages]);
  20219. + }
  20220. + kfree(pagelist);
  20221. + if (actual_pages == 0)
  20222. + actual_pages = -ENOMEM;
  20223. + return actual_pages;
  20224. + }
  20225. + *need_release = 1; /* release user pages */
  20226. + }
  20227. +
  20228. + pagelist->length = count;
  20229. + pagelist->type = type;
  20230. + pagelist->offset = offset;
  20231. +
  20232. + /* Group the pages into runs of contiguous pages */
  20233. +
  20234. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  20235. + next_addr = base_addr + PAGE_SIZE;
  20236. + addridx = 0;
  20237. + run = 0;
  20238. +
  20239. + for (i = 1; i < num_pages; i++) {
  20240. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  20241. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  20242. + next_addr += PAGE_SIZE;
  20243. + run++;
  20244. + } else {
  20245. + addrs[addridx] = (unsigned long)base_addr + run;
  20246. + addridx++;
  20247. + base_addr = addr;
  20248. + next_addr = addr + PAGE_SIZE;
  20249. + run = 0;
  20250. + }
  20251. + }
  20252. +
  20253. + addrs[addridx] = (unsigned long)base_addr + run;
  20254. + addridx++;
  20255. +
  20256. + /* Partial cache lines (fragments) require special measures */
  20257. + if ((type == PAGELIST_READ) &&
  20258. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  20259. + ((pagelist->offset + pagelist->length) &
  20260. + (CACHE_LINE_SIZE - 1)))) {
  20261. + FRAGMENTS_T *fragments;
  20262. +
  20263. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  20264. + kfree(pagelist);
  20265. + return -EINTR;
  20266. + }
  20267. +
  20268. + WARN_ON(g_free_fragments == NULL);
  20269. +
  20270. + down(&g_free_fragments_mutex);
  20271. + fragments = (FRAGMENTS_T *) g_free_fragments;
  20272. + WARN_ON(fragments == NULL);
  20273. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  20274. + up(&g_free_fragments_mutex);
  20275. + pagelist->type =
  20276. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  20277. + g_fragments_base);
  20278. + }
  20279. +
  20280. + for (page = virt_to_page(pagelist);
  20281. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  20282. + flush_dcache_page(page);
  20283. + }
  20284. +
  20285. + *ppagelist = pagelist;
  20286. +
  20287. + return 0;
  20288. +}
  20289. +
  20290. +static void
  20291. +free_pagelist(PAGELIST_T *pagelist, int actual)
  20292. +{
  20293. + unsigned long *need_release;
  20294. + struct page **pages;
  20295. + unsigned int num_pages, i;
  20296. +
  20297. + vchiq_log_trace(vchiq_arm_log_level,
  20298. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  20299. +
  20300. + num_pages =
  20301. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  20302. + PAGE_SIZE;
  20303. +
  20304. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  20305. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  20306. +
  20307. + /* Deal with any partial cache lines (fragments) */
  20308. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  20309. + FRAGMENTS_T *fragments = g_fragments_base +
  20310. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  20311. + int head_bytes, tail_bytes;
  20312. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  20313. + (CACHE_LINE_SIZE - 1);
  20314. + tail_bytes = (pagelist->offset + actual) &
  20315. + (CACHE_LINE_SIZE - 1);
  20316. +
  20317. + if ((actual >= 0) && (head_bytes != 0)) {
  20318. + if (head_bytes > actual)
  20319. + head_bytes = actual;
  20320. +
  20321. + memcpy((char *)page_address(pages[0]) +
  20322. + pagelist->offset,
  20323. + fragments->headbuf,
  20324. + head_bytes);
  20325. + }
  20326. + if ((actual >= 0) && (head_bytes < actual) &&
  20327. + (tail_bytes != 0)) {
  20328. + memcpy((char *)page_address(pages[num_pages - 1]) +
  20329. + ((pagelist->offset + actual) &
  20330. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  20331. + fragments->tailbuf, tail_bytes);
  20332. + }
  20333. +
  20334. + down(&g_free_fragments_mutex);
  20335. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  20336. + g_free_fragments = fragments;
  20337. + up(&g_free_fragments_mutex);
  20338. + up(&g_free_fragments_sema);
  20339. + }
  20340. +
  20341. + if (*need_release) {
  20342. + for (i = 0; i < num_pages; i++) {
  20343. + if (pagelist->type != PAGELIST_WRITE)
  20344. + set_page_dirty(pages[i]);
  20345. +
  20346. + page_cache_release(pages[i]);
  20347. + }
  20348. + }
  20349. +
  20350. + kfree(pagelist);
  20351. +}
  20352. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  20353. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 1970-01-01 01:00:00.000000000 +0100
  20354. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 2014-04-24 15:35:02.893551516 +0200
  20355. @@ -0,0 +1,42 @@
  20356. +/**
  20357. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  20358. + *
  20359. + * Redistribution and use in source and binary forms, with or without
  20360. + * modification, are permitted provided that the following conditions
  20361. + * are met:
  20362. + * 1. Redistributions of source code must retain the above copyright
  20363. + * notice, this list of conditions, and the following disclaimer,
  20364. + * without modification.
  20365. + * 2. Redistributions in binary form must reproduce the above copyright
  20366. + * notice, this list of conditions and the following disclaimer in the
  20367. + * documentation and/or other materials provided with the distribution.
  20368. + * 3. The names of the above-listed copyright holders may not be used
  20369. + * to endorse or promote products derived from this software without
  20370. + * specific prior written permission.
  20371. + *
  20372. + * ALTERNATIVELY, this software may be distributed under the terms of the
  20373. + * GNU General Public License ("GPL") version 2, as published by the Free
  20374. + * Software Foundation.
  20375. + *
  20376. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20377. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20378. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20379. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20380. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20381. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20382. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20383. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20384. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20385. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20386. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20387. + */
  20388. +
  20389. +#ifndef VCHIQ_2835_H
  20390. +#define VCHIQ_2835_H
  20391. +
  20392. +#include "vchiq_pagelist.h"
  20393. +
  20394. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  20395. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  20396. +
  20397. +#endif /* VCHIQ_2835_H */
  20398. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  20399. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 1970-01-01 01:00:00.000000000 +0100
  20400. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 2014-04-24 15:35:02.893551516 +0200
  20401. @@ -0,0 +1,2813 @@
  20402. +/**
  20403. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  20404. + *
  20405. + * Redistribution and use in source and binary forms, with or without
  20406. + * modification, are permitted provided that the following conditions
  20407. + * are met:
  20408. + * 1. Redistributions of source code must retain the above copyright
  20409. + * notice, this list of conditions, and the following disclaimer,
  20410. + * without modification.
  20411. + * 2. Redistributions in binary form must reproduce the above copyright
  20412. + * notice, this list of conditions and the following disclaimer in the
  20413. + * documentation and/or other materials provided with the distribution.
  20414. + * 3. The names of the above-listed copyright holders may not be used
  20415. + * to endorse or promote products derived from this software without
  20416. + * specific prior written permission.
  20417. + *
  20418. + * ALTERNATIVELY, this software may be distributed under the terms of the
  20419. + * GNU General Public License ("GPL") version 2, as published by the Free
  20420. + * Software Foundation.
  20421. + *
  20422. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20423. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20424. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20425. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20426. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20427. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20428. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20429. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20430. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20431. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20432. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20433. + */
  20434. +
  20435. +#include <linux/kernel.h>
  20436. +#include <linux/module.h>
  20437. +#include <linux/types.h>
  20438. +#include <linux/errno.h>
  20439. +#include <linux/cdev.h>
  20440. +#include <linux/fs.h>
  20441. +#include <linux/device.h>
  20442. +#include <linux/mm.h>
  20443. +#include <linux/highmem.h>
  20444. +#include <linux/pagemap.h>
  20445. +#include <linux/bug.h>
  20446. +#include <linux/semaphore.h>
  20447. +#include <linux/list.h>
  20448. +#include <linux/proc_fs.h>
  20449. +
  20450. +#include "vchiq_core.h"
  20451. +#include "vchiq_ioctl.h"
  20452. +#include "vchiq_arm.h"
  20453. +
  20454. +#define DEVICE_NAME "vchiq"
  20455. +
  20456. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  20457. +#undef MODULE_PARAM_PREFIX
  20458. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  20459. +
  20460. +#define VCHIQ_MINOR 0
  20461. +
  20462. +/* Some per-instance constants */
  20463. +#define MAX_COMPLETIONS 16
  20464. +#define MAX_SERVICES 64
  20465. +#define MAX_ELEMENTS 8
  20466. +#define MSG_QUEUE_SIZE 64
  20467. +
  20468. +#define KEEPALIVE_VER 1
  20469. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  20470. +
  20471. +/* Run time control of log level, based on KERN_XXX level. */
  20472. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  20473. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  20474. +
  20475. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  20476. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  20477. +
  20478. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  20479. +static const char *const suspend_state_names[] = {
  20480. + "VC_SUSPEND_FORCE_CANCELED",
  20481. + "VC_SUSPEND_REJECTED",
  20482. + "VC_SUSPEND_FAILED",
  20483. + "VC_SUSPEND_IDLE",
  20484. + "VC_SUSPEND_REQUESTED",
  20485. + "VC_SUSPEND_IN_PROGRESS",
  20486. + "VC_SUSPEND_SUSPENDED"
  20487. +};
  20488. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  20489. +static const char *const resume_state_names[] = {
  20490. + "VC_RESUME_FAILED",
  20491. + "VC_RESUME_IDLE",
  20492. + "VC_RESUME_REQUESTED",
  20493. + "VC_RESUME_IN_PROGRESS",
  20494. + "VC_RESUME_RESUMED"
  20495. +};
  20496. +/* The number of times we allow force suspend to timeout before actually
  20497. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  20498. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  20499. +*/
  20500. +#define FORCE_SUSPEND_FAIL_MAX 8
  20501. +
  20502. +/* The time in ms allowed for videocore to go idle when force suspend has been
  20503. + * requested */
  20504. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  20505. +
  20506. +
  20507. +static void suspend_timer_callback(unsigned long context);
  20508. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance);
  20509. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance);
  20510. +
  20511. +
  20512. +typedef struct user_service_struct {
  20513. + VCHIQ_SERVICE_T *service;
  20514. + void *userdata;
  20515. + VCHIQ_INSTANCE_T instance;
  20516. + int is_vchi;
  20517. + int dequeue_pending;
  20518. + int message_available_pos;
  20519. + int msg_insert;
  20520. + int msg_remove;
  20521. + struct semaphore insert_event;
  20522. + struct semaphore remove_event;
  20523. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  20524. +} USER_SERVICE_T;
  20525. +
  20526. +struct bulk_waiter_node {
  20527. + struct bulk_waiter bulk_waiter;
  20528. + int pid;
  20529. + struct list_head list;
  20530. +};
  20531. +
  20532. +struct vchiq_instance_struct {
  20533. + VCHIQ_STATE_T *state;
  20534. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  20535. + int completion_insert;
  20536. + int completion_remove;
  20537. + struct semaphore insert_event;
  20538. + struct semaphore remove_event;
  20539. + struct mutex completion_mutex;
  20540. +
  20541. + int connected;
  20542. + int closing;
  20543. + int pid;
  20544. + int mark;
  20545. +
  20546. + struct list_head bulk_waiter_list;
  20547. + struct mutex bulk_waiter_list_mutex;
  20548. +
  20549. + struct proc_dir_entry *proc_entry;
  20550. +};
  20551. +
  20552. +typedef struct dump_context_struct {
  20553. + char __user *buf;
  20554. + size_t actual;
  20555. + size_t space;
  20556. + loff_t offset;
  20557. +} DUMP_CONTEXT_T;
  20558. +
  20559. +static struct cdev vchiq_cdev;
  20560. +static dev_t vchiq_devid;
  20561. +static VCHIQ_STATE_T g_state;
  20562. +static struct class *vchiq_class;
  20563. +static struct device *vchiq_dev;
  20564. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  20565. +
  20566. +static const char *const ioctl_names[] = {
  20567. + "CONNECT",
  20568. + "SHUTDOWN",
  20569. + "CREATE_SERVICE",
  20570. + "REMOVE_SERVICE",
  20571. + "QUEUE_MESSAGE",
  20572. + "QUEUE_BULK_TRANSMIT",
  20573. + "QUEUE_BULK_RECEIVE",
  20574. + "AWAIT_COMPLETION",
  20575. + "DEQUEUE_MESSAGE",
  20576. + "GET_CLIENT_ID",
  20577. + "GET_CONFIG",
  20578. + "CLOSE_SERVICE",
  20579. + "USE_SERVICE",
  20580. + "RELEASE_SERVICE",
  20581. + "SET_SERVICE_OPTION",
  20582. + "DUMP_PHYS_MEM"
  20583. +};
  20584. +
  20585. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  20586. + (VCHIQ_IOC_MAX + 1));
  20587. +
  20588. +static void
  20589. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  20590. +
  20591. +/****************************************************************************
  20592. +*
  20593. +* add_completion
  20594. +*
  20595. +***************************************************************************/
  20596. +
  20597. +static VCHIQ_STATUS_T
  20598. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  20599. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  20600. + void *bulk_userdata)
  20601. +{
  20602. + VCHIQ_COMPLETION_DATA_T *completion;
  20603. + DEBUG_INITIALISE(g_state.local)
  20604. +
  20605. + while (instance->completion_insert ==
  20606. + (instance->completion_remove + MAX_COMPLETIONS)) {
  20607. + /* Out of space - wait for the client */
  20608. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20609. + vchiq_log_trace(vchiq_arm_log_level,
  20610. + "add_completion - completion queue full");
  20611. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  20612. + if (down_interruptible(&instance->remove_event) != 0) {
  20613. + vchiq_log_info(vchiq_arm_log_level,
  20614. + "service_callback interrupted");
  20615. + return VCHIQ_RETRY;
  20616. + } else if (instance->closing) {
  20617. + vchiq_log_info(vchiq_arm_log_level,
  20618. + "service_callback closing");
  20619. + return VCHIQ_ERROR;
  20620. + }
  20621. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20622. + }
  20623. +
  20624. + completion =
  20625. + &instance->completions[instance->completion_insert &
  20626. + (MAX_COMPLETIONS - 1)];
  20627. +
  20628. + completion->header = header;
  20629. + completion->reason = reason;
  20630. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  20631. + completion->service_userdata = user_service->service;
  20632. + completion->bulk_userdata = bulk_userdata;
  20633. +
  20634. + if (reason == VCHIQ_SERVICE_CLOSED)
  20635. + /* Take an extra reference, to be held until
  20636. + this CLOSED notification is delivered. */
  20637. + lock_service(user_service->service);
  20638. +
  20639. + /* A write barrier is needed here to ensure that the entire completion
  20640. + record is written out before the insert point. */
  20641. + wmb();
  20642. +
  20643. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  20644. + user_service->message_available_pos =
  20645. + instance->completion_insert;
  20646. + instance->completion_insert++;
  20647. +
  20648. + up(&instance->insert_event);
  20649. +
  20650. + return VCHIQ_SUCCESS;
  20651. +}
  20652. +
  20653. +/****************************************************************************
  20654. +*
  20655. +* service_callback
  20656. +*
  20657. +***************************************************************************/
  20658. +
  20659. +static VCHIQ_STATUS_T
  20660. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  20661. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  20662. +{
  20663. + /* How do we ensure the callback goes to the right client?
  20664. + ** The service_user data points to a USER_SERVICE_T record containing
  20665. + ** the original callback and the user state structure, which contains a
  20666. + ** circular buffer for completion records.
  20667. + */
  20668. + USER_SERVICE_T *user_service;
  20669. + VCHIQ_SERVICE_T *service;
  20670. + VCHIQ_INSTANCE_T instance;
  20671. + DEBUG_INITIALISE(g_state.local)
  20672. +
  20673. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20674. +
  20675. + service = handle_to_service(handle);
  20676. + BUG_ON(!service);
  20677. + user_service = (USER_SERVICE_T *)service->base.userdata;
  20678. + instance = user_service->instance;
  20679. +
  20680. + if (!instance || instance->closing)
  20681. + return VCHIQ_SUCCESS;
  20682. +
  20683. + vchiq_log_trace(vchiq_arm_log_level,
  20684. + "service_callback - service %lx(%d), reason %d, header %lx, "
  20685. + "instance %lx, bulk_userdata %lx",
  20686. + (unsigned long)user_service,
  20687. + service->localport,
  20688. + reason, (unsigned long)header,
  20689. + (unsigned long)instance, (unsigned long)bulk_userdata);
  20690. +
  20691. + if (header && user_service->is_vchi) {
  20692. + spin_lock(&msg_queue_spinlock);
  20693. + while (user_service->msg_insert ==
  20694. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  20695. + spin_unlock(&msg_queue_spinlock);
  20696. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20697. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  20698. + vchiq_log_trace(vchiq_arm_log_level,
  20699. + "service_callback - msg queue full");
  20700. + /* If there is no MESSAGE_AVAILABLE in the completion
  20701. + ** queue, add one
  20702. + */
  20703. + if ((user_service->message_available_pos -
  20704. + instance->completion_remove) < 0) {
  20705. + VCHIQ_STATUS_T status;
  20706. + vchiq_log_info(vchiq_arm_log_level,
  20707. + "Inserting extra MESSAGE_AVAILABLE");
  20708. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20709. + status = add_completion(instance, reason,
  20710. + NULL, user_service, bulk_userdata);
  20711. + if (status != VCHIQ_SUCCESS) {
  20712. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20713. + return status;
  20714. + }
  20715. + }
  20716. +
  20717. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20718. + if (down_interruptible(&user_service->remove_event)
  20719. + != 0) {
  20720. + vchiq_log_info(vchiq_arm_log_level,
  20721. + "service_callback interrupted");
  20722. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20723. + return VCHIQ_RETRY;
  20724. + } else if (instance->closing) {
  20725. + vchiq_log_info(vchiq_arm_log_level,
  20726. + "service_callback closing");
  20727. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20728. + return VCHIQ_ERROR;
  20729. + }
  20730. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20731. + spin_lock(&msg_queue_spinlock);
  20732. + }
  20733. +
  20734. + user_service->msg_queue[user_service->msg_insert &
  20735. + (MSG_QUEUE_SIZE - 1)] = header;
  20736. + user_service->msg_insert++;
  20737. + spin_unlock(&msg_queue_spinlock);
  20738. +
  20739. + up(&user_service->insert_event);
  20740. +
  20741. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  20742. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  20743. + ** bypass the completion queue.
  20744. + */
  20745. + if (((user_service->message_available_pos -
  20746. + instance->completion_remove) >= 0) ||
  20747. + user_service->dequeue_pending) {
  20748. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20749. + user_service->dequeue_pending = 0;
  20750. + return VCHIQ_SUCCESS;
  20751. + }
  20752. +
  20753. + header = NULL;
  20754. + }
  20755. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  20756. +
  20757. + return add_completion(instance, reason, header, user_service,
  20758. + bulk_userdata);
  20759. +}
  20760. +
  20761. +/****************************************************************************
  20762. +*
  20763. +* user_service_free
  20764. +*
  20765. +***************************************************************************/
  20766. +static void
  20767. +user_service_free(void *userdata)
  20768. +{
  20769. + kfree(userdata);
  20770. +}
  20771. +
  20772. +/****************************************************************************
  20773. +*
  20774. +* vchiq_ioctl
  20775. +*
  20776. +***************************************************************************/
  20777. +
  20778. +static long
  20779. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  20780. +{
  20781. + VCHIQ_INSTANCE_T instance = file->private_data;
  20782. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  20783. + VCHIQ_SERVICE_T *service = NULL;
  20784. + long ret = 0;
  20785. + int i, rc;
  20786. + DEBUG_INITIALISE(g_state.local)
  20787. +
  20788. + vchiq_log_trace(vchiq_arm_log_level,
  20789. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  20790. + (unsigned int)instance,
  20791. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  20792. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  20793. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  20794. +
  20795. + switch (cmd) {
  20796. + case VCHIQ_IOC_SHUTDOWN:
  20797. + if (!instance->connected)
  20798. + break;
  20799. +
  20800. + /* Remove all services */
  20801. + i = 0;
  20802. + while ((service = next_service_by_instance(instance->state,
  20803. + instance, &i)) != NULL) {
  20804. + status = vchiq_remove_service(service->handle);
  20805. + unlock_service(service);
  20806. + if (status != VCHIQ_SUCCESS)
  20807. + break;
  20808. + }
  20809. + service = NULL;
  20810. +
  20811. + if (status == VCHIQ_SUCCESS) {
  20812. + /* Wake the completion thread and ask it to exit */
  20813. + instance->closing = 1;
  20814. + up(&instance->insert_event);
  20815. + }
  20816. +
  20817. + break;
  20818. +
  20819. + case VCHIQ_IOC_CONNECT:
  20820. + if (instance->connected) {
  20821. + ret = -EINVAL;
  20822. + break;
  20823. + }
  20824. + rc = mutex_lock_interruptible(&instance->state->mutex);
  20825. + if (rc != 0) {
  20826. + vchiq_log_error(vchiq_arm_log_level,
  20827. + "vchiq: connect: could not lock mutex for "
  20828. + "state %d: %d",
  20829. + instance->state->id, rc);
  20830. + ret = -EINTR;
  20831. + break;
  20832. + }
  20833. + status = vchiq_connect_internal(instance->state, instance);
  20834. + mutex_unlock(&instance->state->mutex);
  20835. +
  20836. + if (status == VCHIQ_SUCCESS)
  20837. + instance->connected = 1;
  20838. + else
  20839. + vchiq_log_error(vchiq_arm_log_level,
  20840. + "vchiq: could not connect: %d", status);
  20841. + break;
  20842. +
  20843. + case VCHIQ_IOC_CREATE_SERVICE: {
  20844. + VCHIQ_CREATE_SERVICE_T args;
  20845. + USER_SERVICE_T *user_service = NULL;
  20846. + void *userdata;
  20847. + int srvstate;
  20848. +
  20849. + if (copy_from_user
  20850. + (&args, (const void __user *)arg,
  20851. + sizeof(args)) != 0) {
  20852. + ret = -EFAULT;
  20853. + break;
  20854. + }
  20855. +
  20856. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  20857. + if (!user_service) {
  20858. + ret = -ENOMEM;
  20859. + break;
  20860. + }
  20861. +
  20862. + if (args.is_open) {
  20863. + if (!instance->connected) {
  20864. + ret = -ENOTCONN;
  20865. + kfree(user_service);
  20866. + break;
  20867. + }
  20868. + srvstate = VCHIQ_SRVSTATE_OPENING;
  20869. + } else {
  20870. + srvstate =
  20871. + instance->connected ?
  20872. + VCHIQ_SRVSTATE_LISTENING :
  20873. + VCHIQ_SRVSTATE_HIDDEN;
  20874. + }
  20875. +
  20876. + userdata = args.params.userdata;
  20877. + args.params.callback = service_callback;
  20878. + args.params.userdata = user_service;
  20879. + service = vchiq_add_service_internal(
  20880. + instance->state,
  20881. + &args.params, srvstate,
  20882. + instance, user_service_free);
  20883. +
  20884. + if (service != NULL) {
  20885. + user_service->service = service;
  20886. + user_service->userdata = userdata;
  20887. + user_service->instance = instance;
  20888. + user_service->is_vchi = args.is_vchi;
  20889. + user_service->dequeue_pending = 0;
  20890. + user_service->message_available_pos =
  20891. + instance->completion_remove - 1;
  20892. + user_service->msg_insert = 0;
  20893. + user_service->msg_remove = 0;
  20894. + sema_init(&user_service->insert_event, 0);
  20895. + sema_init(&user_service->remove_event, 0);
  20896. +
  20897. + if (args.is_open) {
  20898. + status = vchiq_open_service_internal
  20899. + (service, instance->pid);
  20900. + if (status != VCHIQ_SUCCESS) {
  20901. + vchiq_remove_service(service->handle);
  20902. + service = NULL;
  20903. + ret = (status == VCHIQ_RETRY) ?
  20904. + -EINTR : -EIO;
  20905. + break;
  20906. + }
  20907. + }
  20908. +
  20909. + if (copy_to_user((void __user *)
  20910. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  20911. + arg)->handle),
  20912. + (const void *)&service->handle,
  20913. + sizeof(service->handle)) != 0) {
  20914. + ret = -EFAULT;
  20915. + vchiq_remove_service(service->handle);
  20916. + }
  20917. +
  20918. + service = NULL;
  20919. + } else {
  20920. + ret = -EEXIST;
  20921. + kfree(user_service);
  20922. + }
  20923. + } break;
  20924. +
  20925. + case VCHIQ_IOC_CLOSE_SERVICE: {
  20926. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20927. +
  20928. + service = find_service_for_instance(instance, handle);
  20929. + if (service != NULL)
  20930. + status = vchiq_close_service(service->handle);
  20931. + else
  20932. + ret = -EINVAL;
  20933. + } break;
  20934. +
  20935. + case VCHIQ_IOC_REMOVE_SERVICE: {
  20936. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20937. +
  20938. + service = find_service_for_instance(instance, handle);
  20939. + if (service != NULL)
  20940. + status = vchiq_remove_service(service->handle);
  20941. + else
  20942. + ret = -EINVAL;
  20943. + } break;
  20944. +
  20945. + case VCHIQ_IOC_USE_SERVICE:
  20946. + case VCHIQ_IOC_RELEASE_SERVICE: {
  20947. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20948. +
  20949. + service = find_service_for_instance(instance, handle);
  20950. + if (service != NULL) {
  20951. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  20952. + vchiq_use_service_internal(service) :
  20953. + vchiq_release_service_internal(service);
  20954. + if (status != VCHIQ_SUCCESS) {
  20955. + vchiq_log_error(vchiq_susp_log_level,
  20956. + "%s: cmd %s returned error %d for "
  20957. + "service %c%c%c%c:%03d",
  20958. + __func__,
  20959. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  20960. + "VCHIQ_IOC_USE_SERVICE" :
  20961. + "VCHIQ_IOC_RELEASE_SERVICE",
  20962. + status,
  20963. + VCHIQ_FOURCC_AS_4CHARS(
  20964. + service->base.fourcc),
  20965. + service->client_id);
  20966. + ret = -EINVAL;
  20967. + }
  20968. + } else
  20969. + ret = -EINVAL;
  20970. + } break;
  20971. +
  20972. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  20973. + VCHIQ_QUEUE_MESSAGE_T args;
  20974. + if (copy_from_user
  20975. + (&args, (const void __user *)arg,
  20976. + sizeof(args)) != 0) {
  20977. + ret = -EFAULT;
  20978. + break;
  20979. + }
  20980. +
  20981. + service = find_service_for_instance(instance, args.handle);
  20982. +
  20983. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  20984. + /* Copy elements into kernel space */
  20985. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  20986. + if (copy_from_user(elements, args.elements,
  20987. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  20988. + status = vchiq_queue_message
  20989. + (args.handle,
  20990. + elements, args.count);
  20991. + else
  20992. + ret = -EFAULT;
  20993. + } else {
  20994. + ret = -EINVAL;
  20995. + }
  20996. + } break;
  20997. +
  20998. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  20999. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  21000. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  21001. + struct bulk_waiter_node *waiter = NULL;
  21002. + VCHIQ_BULK_DIR_T dir =
  21003. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  21004. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  21005. +
  21006. + if (copy_from_user
  21007. + (&args, (const void __user *)arg,
  21008. + sizeof(args)) != 0) {
  21009. + ret = -EFAULT;
  21010. + break;
  21011. + }
  21012. +
  21013. + service = find_service_for_instance(instance, args.handle);
  21014. + if (!service) {
  21015. + ret = -EINVAL;
  21016. + break;
  21017. + }
  21018. +
  21019. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  21020. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  21021. + GFP_KERNEL);
  21022. + if (!waiter) {
  21023. + ret = -ENOMEM;
  21024. + break;
  21025. + }
  21026. + args.userdata = &waiter->bulk_waiter;
  21027. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  21028. + struct list_head *pos;
  21029. + mutex_lock(&instance->bulk_waiter_list_mutex);
  21030. + list_for_each(pos, &instance->bulk_waiter_list) {
  21031. + if (list_entry(pos, struct bulk_waiter_node,
  21032. + list)->pid == current->pid) {
  21033. + waiter = list_entry(pos,
  21034. + struct bulk_waiter_node,
  21035. + list);
  21036. + list_del(pos);
  21037. + break;
  21038. + }
  21039. +
  21040. + }
  21041. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  21042. + if (!waiter) {
  21043. + vchiq_log_error(vchiq_arm_log_level,
  21044. + "no bulk_waiter found for pid %d",
  21045. + current->pid);
  21046. + ret = -ESRCH;
  21047. + break;
  21048. + }
  21049. + vchiq_log_info(vchiq_arm_log_level,
  21050. + "found bulk_waiter %x for pid %d",
  21051. + (unsigned int)waiter, current->pid);
  21052. + args.userdata = &waiter->bulk_waiter;
  21053. + }
  21054. + status = vchiq_bulk_transfer
  21055. + (args.handle,
  21056. + VCHI_MEM_HANDLE_INVALID,
  21057. + args.data, args.size,
  21058. + args.userdata, args.mode,
  21059. + dir);
  21060. + if (!waiter)
  21061. + break;
  21062. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  21063. + !waiter->bulk_waiter.bulk) {
  21064. + if (waiter->bulk_waiter.bulk) {
  21065. + /* Cancel the signal when the transfer
  21066. + ** completes. */
  21067. + spin_lock(&bulk_waiter_spinlock);
  21068. + waiter->bulk_waiter.bulk->userdata = NULL;
  21069. + spin_unlock(&bulk_waiter_spinlock);
  21070. + }
  21071. + kfree(waiter);
  21072. + } else {
  21073. + const VCHIQ_BULK_MODE_T mode_waiting =
  21074. + VCHIQ_BULK_MODE_WAITING;
  21075. + waiter->pid = current->pid;
  21076. + mutex_lock(&instance->bulk_waiter_list_mutex);
  21077. + list_add(&waiter->list, &instance->bulk_waiter_list);
  21078. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  21079. + vchiq_log_info(vchiq_arm_log_level,
  21080. + "saved bulk_waiter %x for pid %d",
  21081. + (unsigned int)waiter, current->pid);
  21082. +
  21083. + if (copy_to_user((void __user *)
  21084. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  21085. + arg)->mode),
  21086. + (const void *)&mode_waiting,
  21087. + sizeof(mode_waiting)) != 0)
  21088. + ret = -EFAULT;
  21089. + }
  21090. + } break;
  21091. +
  21092. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  21093. + VCHIQ_AWAIT_COMPLETION_T args;
  21094. +
  21095. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21096. + if (!instance->connected) {
  21097. + ret = -ENOTCONN;
  21098. + break;
  21099. + }
  21100. +
  21101. + if (copy_from_user(&args, (const void __user *)arg,
  21102. + sizeof(args)) != 0) {
  21103. + ret = -EFAULT;
  21104. + break;
  21105. + }
  21106. +
  21107. + mutex_lock(&instance->completion_mutex);
  21108. +
  21109. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21110. + while ((instance->completion_remove ==
  21111. + instance->completion_insert)
  21112. + && !instance->closing) {
  21113. + int rc;
  21114. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21115. + mutex_unlock(&instance->completion_mutex);
  21116. + rc = down_interruptible(&instance->insert_event);
  21117. + mutex_lock(&instance->completion_mutex);
  21118. + if (rc != 0) {
  21119. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21120. + vchiq_log_info(vchiq_arm_log_level,
  21121. + "AWAIT_COMPLETION interrupted");
  21122. + ret = -EINTR;
  21123. + break;
  21124. + }
  21125. + }
  21126. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21127. +
  21128. + /* A read memory barrier is needed to stop prefetch of a stale
  21129. + ** completion record
  21130. + */
  21131. + rmb();
  21132. +
  21133. + if (ret == 0) {
  21134. + int msgbufcount = args.msgbufcount;
  21135. + for (ret = 0; ret < args.count; ret++) {
  21136. + VCHIQ_COMPLETION_DATA_T *completion;
  21137. + VCHIQ_SERVICE_T *service;
  21138. + USER_SERVICE_T *user_service;
  21139. + VCHIQ_HEADER_T *header;
  21140. + if (instance->completion_remove ==
  21141. + instance->completion_insert)
  21142. + break;
  21143. + completion = &instance->completions[
  21144. + instance->completion_remove &
  21145. + (MAX_COMPLETIONS - 1)];
  21146. +
  21147. + service = completion->service_userdata;
  21148. + user_service = service->base.userdata;
  21149. + completion->service_userdata =
  21150. + user_service->userdata;
  21151. +
  21152. + header = completion->header;
  21153. + if (header) {
  21154. + void __user *msgbuf;
  21155. + int msglen;
  21156. +
  21157. + msglen = header->size +
  21158. + sizeof(VCHIQ_HEADER_T);
  21159. + /* This must be a VCHIQ-style service */
  21160. + if (args.msgbufsize < msglen) {
  21161. + vchiq_log_error(
  21162. + vchiq_arm_log_level,
  21163. + "header %x: msgbufsize"
  21164. + " %x < msglen %x",
  21165. + (unsigned int)header,
  21166. + args.msgbufsize,
  21167. + msglen);
  21168. + WARN(1, "invalid message "
  21169. + "size\n");
  21170. + if (ret == 0)
  21171. + ret = -EMSGSIZE;
  21172. + break;
  21173. + }
  21174. + if (msgbufcount <= 0)
  21175. + /* Stall here for lack of a
  21176. + ** buffer for the message. */
  21177. + break;
  21178. + /* Get the pointer from user space */
  21179. + msgbufcount--;
  21180. + if (copy_from_user(&msgbuf,
  21181. + (const void __user *)
  21182. + &args.msgbufs[msgbufcount],
  21183. + sizeof(msgbuf)) != 0) {
  21184. + if (ret == 0)
  21185. + ret = -EFAULT;
  21186. + break;
  21187. + }
  21188. +
  21189. + /* Copy the message to user space */
  21190. + if (copy_to_user(msgbuf, header,
  21191. + msglen) != 0) {
  21192. + if (ret == 0)
  21193. + ret = -EFAULT;
  21194. + break;
  21195. + }
  21196. +
  21197. + /* Now it has been copied, the message
  21198. + ** can be released. */
  21199. + vchiq_release_message(service->handle,
  21200. + header);
  21201. +
  21202. + /* The completion must point to the
  21203. + ** msgbuf. */
  21204. + completion->header = msgbuf;
  21205. + }
  21206. +
  21207. + if (completion->reason ==
  21208. + VCHIQ_SERVICE_CLOSED)
  21209. + unlock_service(service);
  21210. +
  21211. + if (copy_to_user((void __user *)(
  21212. + (size_t)args.buf +
  21213. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  21214. + completion,
  21215. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  21216. + if (ret == 0)
  21217. + ret = -EFAULT;
  21218. + break;
  21219. + }
  21220. +
  21221. + instance->completion_remove++;
  21222. + }
  21223. +
  21224. + if (msgbufcount != args.msgbufcount) {
  21225. + if (copy_to_user((void __user *)
  21226. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  21227. + msgbufcount,
  21228. + &msgbufcount,
  21229. + sizeof(msgbufcount)) != 0) {
  21230. + ret = -EFAULT;
  21231. + }
  21232. + }
  21233. + }
  21234. +
  21235. + if (ret != 0)
  21236. + up(&instance->remove_event);
  21237. + mutex_unlock(&instance->completion_mutex);
  21238. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  21239. + } break;
  21240. +
  21241. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  21242. + VCHIQ_DEQUEUE_MESSAGE_T args;
  21243. + USER_SERVICE_T *user_service;
  21244. + VCHIQ_HEADER_T *header;
  21245. +
  21246. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21247. + if (copy_from_user
  21248. + (&args, (const void __user *)arg,
  21249. + sizeof(args)) != 0) {
  21250. + ret = -EFAULT;
  21251. + break;
  21252. + }
  21253. + service = find_service_for_instance(instance, args.handle);
  21254. + if (!service) {
  21255. + ret = -EINVAL;
  21256. + break;
  21257. + }
  21258. + user_service = (USER_SERVICE_T *)service->base.userdata;
  21259. + if (user_service->is_vchi == 0) {
  21260. + ret = -EINVAL;
  21261. + break;
  21262. + }
  21263. +
  21264. + spin_lock(&msg_queue_spinlock);
  21265. + if (user_service->msg_remove == user_service->msg_insert) {
  21266. + if (!args.blocking) {
  21267. + spin_unlock(&msg_queue_spinlock);
  21268. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21269. + ret = -EWOULDBLOCK;
  21270. + break;
  21271. + }
  21272. + user_service->dequeue_pending = 1;
  21273. + do {
  21274. + spin_unlock(&msg_queue_spinlock);
  21275. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21276. + if (down_interruptible(
  21277. + &user_service->insert_event) != 0) {
  21278. + vchiq_log_info(vchiq_arm_log_level,
  21279. + "DEQUEUE_MESSAGE interrupted");
  21280. + ret = -EINTR;
  21281. + break;
  21282. + }
  21283. + spin_lock(&msg_queue_spinlock);
  21284. + } while (user_service->msg_remove ==
  21285. + user_service->msg_insert);
  21286. +
  21287. + if (ret)
  21288. + break;
  21289. + }
  21290. +
  21291. + BUG_ON((int)(user_service->msg_insert -
  21292. + user_service->msg_remove) < 0);
  21293. +
  21294. + header = user_service->msg_queue[user_service->msg_remove &
  21295. + (MSG_QUEUE_SIZE - 1)];
  21296. + user_service->msg_remove++;
  21297. + spin_unlock(&msg_queue_spinlock);
  21298. +
  21299. + up(&user_service->remove_event);
  21300. + if (header == NULL)
  21301. + ret = -ENOTCONN;
  21302. + else if (header->size <= args.bufsize) {
  21303. + /* Copy to user space if msgbuf is not NULL */
  21304. + if ((args.buf == NULL) ||
  21305. + (copy_to_user((void __user *)args.buf,
  21306. + header->data,
  21307. + header->size) == 0)) {
  21308. + ret = header->size;
  21309. + vchiq_release_message(
  21310. + service->handle,
  21311. + header);
  21312. + } else
  21313. + ret = -EFAULT;
  21314. + } else {
  21315. + vchiq_log_error(vchiq_arm_log_level,
  21316. + "header %x: bufsize %x < size %x",
  21317. + (unsigned int)header, args.bufsize,
  21318. + header->size);
  21319. + WARN(1, "invalid size\n");
  21320. + ret = -EMSGSIZE;
  21321. + }
  21322. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  21323. + } break;
  21324. +
  21325. + case VCHIQ_IOC_GET_CLIENT_ID: {
  21326. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  21327. +
  21328. + ret = vchiq_get_client_id(handle);
  21329. + } break;
  21330. +
  21331. + case VCHIQ_IOC_GET_CONFIG: {
  21332. + VCHIQ_GET_CONFIG_T args;
  21333. + VCHIQ_CONFIG_T config;
  21334. +
  21335. + if (copy_from_user(&args, (const void __user *)arg,
  21336. + sizeof(args)) != 0) {
  21337. + ret = -EFAULT;
  21338. + break;
  21339. + }
  21340. + if (args.config_size > sizeof(config)) {
  21341. + ret = -EINVAL;
  21342. + break;
  21343. + }
  21344. + status = vchiq_get_config(instance, args.config_size, &config);
  21345. + if (status == VCHIQ_SUCCESS) {
  21346. + if (copy_to_user((void __user *)args.pconfig,
  21347. + &config, args.config_size) != 0) {
  21348. + ret = -EFAULT;
  21349. + break;
  21350. + }
  21351. + }
  21352. + } break;
  21353. +
  21354. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  21355. + VCHIQ_SET_SERVICE_OPTION_T args;
  21356. +
  21357. + if (copy_from_user(
  21358. + &args, (const void __user *)arg,
  21359. + sizeof(args)) != 0) {
  21360. + ret = -EFAULT;
  21361. + break;
  21362. + }
  21363. +
  21364. + service = find_service_for_instance(instance, args.handle);
  21365. + if (!service) {
  21366. + ret = -EINVAL;
  21367. + break;
  21368. + }
  21369. +
  21370. + status = vchiq_set_service_option(
  21371. + args.handle, args.option, args.value);
  21372. + } break;
  21373. +
  21374. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  21375. + VCHIQ_DUMP_MEM_T args;
  21376. +
  21377. + if (copy_from_user
  21378. + (&args, (const void __user *)arg,
  21379. + sizeof(args)) != 0) {
  21380. + ret = -EFAULT;
  21381. + break;
  21382. + }
  21383. + dump_phys_mem(args.virt_addr, args.num_bytes);
  21384. + } break;
  21385. +
  21386. + default:
  21387. + ret = -ENOTTY;
  21388. + break;
  21389. + }
  21390. +
  21391. + if (service)
  21392. + unlock_service(service);
  21393. +
  21394. + if (ret == 0) {
  21395. + if (status == VCHIQ_ERROR)
  21396. + ret = -EIO;
  21397. + else if (status == VCHIQ_RETRY)
  21398. + ret = -EINTR;
  21399. + }
  21400. +
  21401. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  21402. + (ret != -EWOULDBLOCK))
  21403. + vchiq_log_info(vchiq_arm_log_level,
  21404. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  21405. + (unsigned long)instance,
  21406. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  21407. + ioctl_names[_IOC_NR(cmd)] :
  21408. + "<invalid>",
  21409. + status, ret);
  21410. + else
  21411. + vchiq_log_trace(vchiq_arm_log_level,
  21412. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  21413. + (unsigned long)instance,
  21414. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  21415. + ioctl_names[_IOC_NR(cmd)] :
  21416. + "<invalid>",
  21417. + status, ret);
  21418. +
  21419. + return ret;
  21420. +}
  21421. +
  21422. +/****************************************************************************
  21423. +*
  21424. +* vchiq_open
  21425. +*
  21426. +***************************************************************************/
  21427. +
  21428. +static int
  21429. +vchiq_open(struct inode *inode, struct file *file)
  21430. +{
  21431. + int dev = iminor(inode) & 0x0f;
  21432. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  21433. + switch (dev) {
  21434. + case VCHIQ_MINOR: {
  21435. + int ret;
  21436. + VCHIQ_STATE_T *state = vchiq_get_state();
  21437. + VCHIQ_INSTANCE_T instance;
  21438. +
  21439. + if (!state) {
  21440. + vchiq_log_error(vchiq_arm_log_level,
  21441. + "vchiq has no connection to VideoCore");
  21442. + return -ENOTCONN;
  21443. + }
  21444. +
  21445. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  21446. + if (!instance)
  21447. + return -ENOMEM;
  21448. +
  21449. + instance->state = state;
  21450. + instance->pid = current->tgid;
  21451. +
  21452. + ret = vchiq_proc_add_instance(instance);
  21453. + if (ret != 0) {
  21454. + kfree(instance);
  21455. + return ret;
  21456. + }
  21457. +
  21458. + sema_init(&instance->insert_event, 0);
  21459. + sema_init(&instance->remove_event, 0);
  21460. + mutex_init(&instance->completion_mutex);
  21461. + mutex_init(&instance->bulk_waiter_list_mutex);
  21462. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  21463. +
  21464. + file->private_data = instance;
  21465. + } break;
  21466. +
  21467. + default:
  21468. + vchiq_log_error(vchiq_arm_log_level,
  21469. + "Unknown minor device: %d", dev);
  21470. + return -ENXIO;
  21471. + }
  21472. +
  21473. + return 0;
  21474. +}
  21475. +
  21476. +/****************************************************************************
  21477. +*
  21478. +* vchiq_release
  21479. +*
  21480. +***************************************************************************/
  21481. +
  21482. +static int
  21483. +vchiq_release(struct inode *inode, struct file *file)
  21484. +{
  21485. + int dev = iminor(inode) & 0x0f;
  21486. + int ret = 0;
  21487. + switch (dev) {
  21488. + case VCHIQ_MINOR: {
  21489. + VCHIQ_INSTANCE_T instance = file->private_data;
  21490. + VCHIQ_STATE_T *state = vchiq_get_state();
  21491. + VCHIQ_SERVICE_T *service;
  21492. + int i;
  21493. +
  21494. + vchiq_log_info(vchiq_arm_log_level,
  21495. + "vchiq_release: instance=%lx",
  21496. + (unsigned long)instance);
  21497. +
  21498. + if (!state) {
  21499. + ret = -EPERM;
  21500. + goto out;
  21501. + }
  21502. +
  21503. + /* Ensure videocore is awake to allow termination. */
  21504. + vchiq_use_internal(instance->state, NULL,
  21505. + USE_TYPE_VCHIQ);
  21506. +
  21507. + mutex_lock(&instance->completion_mutex);
  21508. +
  21509. + /* Wake the completion thread and ask it to exit */
  21510. + instance->closing = 1;
  21511. + up(&instance->insert_event);
  21512. +
  21513. + mutex_unlock(&instance->completion_mutex);
  21514. +
  21515. + /* Wake the slot handler if the completion queue is full. */
  21516. + up(&instance->remove_event);
  21517. +
  21518. + /* Mark all services for termination... */
  21519. + i = 0;
  21520. + while ((service = next_service_by_instance(state, instance,
  21521. + &i)) != NULL) {
  21522. + USER_SERVICE_T *user_service = service->base.userdata;
  21523. +
  21524. + /* Wake the slot handler if the msg queue is full. */
  21525. + up(&user_service->remove_event);
  21526. +
  21527. + vchiq_terminate_service_internal(service);
  21528. + unlock_service(service);
  21529. + }
  21530. +
  21531. + /* ...and wait for them to die */
  21532. + i = 0;
  21533. + while ((service = next_service_by_instance(state, instance, &i))
  21534. + != NULL) {
  21535. + USER_SERVICE_T *user_service = service->base.userdata;
  21536. +
  21537. + down(&service->remove_event);
  21538. +
  21539. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  21540. +
  21541. + spin_lock(&msg_queue_spinlock);
  21542. +
  21543. + while (user_service->msg_remove !=
  21544. + user_service->msg_insert) {
  21545. + VCHIQ_HEADER_T *header = user_service->
  21546. + msg_queue[user_service->msg_remove &
  21547. + (MSG_QUEUE_SIZE - 1)];
  21548. + user_service->msg_remove++;
  21549. + spin_unlock(&msg_queue_spinlock);
  21550. +
  21551. + if (header)
  21552. + vchiq_release_message(
  21553. + service->handle,
  21554. + header);
  21555. + spin_lock(&msg_queue_spinlock);
  21556. + }
  21557. +
  21558. + spin_unlock(&msg_queue_spinlock);
  21559. +
  21560. + unlock_service(service);
  21561. + }
  21562. +
  21563. + /* Release any closed services */
  21564. + while (instance->completion_remove !=
  21565. + instance->completion_insert) {
  21566. + VCHIQ_COMPLETION_DATA_T *completion;
  21567. + VCHIQ_SERVICE_T *service;
  21568. + completion = &instance->completions[
  21569. + instance->completion_remove &
  21570. + (MAX_COMPLETIONS - 1)];
  21571. + service = completion->service_userdata;
  21572. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  21573. + unlock_service(service);
  21574. + instance->completion_remove++;
  21575. + }
  21576. +
  21577. + /* Release the PEER service count. */
  21578. + vchiq_release_internal(instance->state, NULL);
  21579. +
  21580. + {
  21581. + struct list_head *pos, *next;
  21582. + list_for_each_safe(pos, next,
  21583. + &instance->bulk_waiter_list) {
  21584. + struct bulk_waiter_node *waiter;
  21585. + waiter = list_entry(pos,
  21586. + struct bulk_waiter_node,
  21587. + list);
  21588. + list_del(pos);
  21589. + vchiq_log_info(vchiq_arm_log_level,
  21590. + "bulk_waiter - cleaned up %x "
  21591. + "for pid %d",
  21592. + (unsigned int)waiter, waiter->pid);
  21593. + kfree(waiter);
  21594. + }
  21595. + }
  21596. +
  21597. + vchiq_proc_remove_instance(instance);
  21598. +
  21599. + kfree(instance);
  21600. + file->private_data = NULL;
  21601. + } break;
  21602. +
  21603. + default:
  21604. + vchiq_log_error(vchiq_arm_log_level,
  21605. + "Unknown minor device: %d", dev);
  21606. + ret = -ENXIO;
  21607. + }
  21608. +
  21609. +out:
  21610. + return ret;
  21611. +}
  21612. +
  21613. +/****************************************************************************
  21614. +*
  21615. +* vchiq_dump
  21616. +*
  21617. +***************************************************************************/
  21618. +
  21619. +void
  21620. +vchiq_dump(void *dump_context, const char *str, int len)
  21621. +{
  21622. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  21623. +
  21624. + if (context->actual < context->space) {
  21625. + int copy_bytes;
  21626. + if (context->offset > 0) {
  21627. + int skip_bytes = min(len, (int)context->offset);
  21628. + str += skip_bytes;
  21629. + len -= skip_bytes;
  21630. + context->offset -= skip_bytes;
  21631. + if (context->offset > 0)
  21632. + return;
  21633. + }
  21634. + copy_bytes = min(len, (int)(context->space - context->actual));
  21635. + if (copy_bytes == 0)
  21636. + return;
  21637. + if (copy_to_user(context->buf + context->actual, str,
  21638. + copy_bytes))
  21639. + context->actual = -EFAULT;
  21640. + context->actual += copy_bytes;
  21641. + len -= copy_bytes;
  21642. +
  21643. + /* If tne terminating NUL is included in the length, then it
  21644. + ** marks the end of a line and should be replaced with a
  21645. + ** carriage return. */
  21646. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  21647. + char cr = '\n';
  21648. + if (copy_to_user(context->buf + context->actual - 1,
  21649. + &cr, 1))
  21650. + context->actual = -EFAULT;
  21651. + }
  21652. + }
  21653. +}
  21654. +
  21655. +/****************************************************************************
  21656. +*
  21657. +* vchiq_dump_platform_instance_state
  21658. +*
  21659. +***************************************************************************/
  21660. +
  21661. +void
  21662. +vchiq_dump_platform_instances(void *dump_context)
  21663. +{
  21664. + VCHIQ_STATE_T *state = vchiq_get_state();
  21665. + char buf[80];
  21666. + int len;
  21667. + int i;
  21668. +
  21669. + /* There is no list of instances, so instead scan all services,
  21670. + marking those that have been dumped. */
  21671. +
  21672. + for (i = 0; i < state->unused_service; i++) {
  21673. + VCHIQ_SERVICE_T *service = state->services[i];
  21674. + VCHIQ_INSTANCE_T instance;
  21675. +
  21676. + if (service && (service->base.callback == service_callback)) {
  21677. + instance = service->instance;
  21678. + if (instance)
  21679. + instance->mark = 0;
  21680. + }
  21681. + }
  21682. +
  21683. + for (i = 0; i < state->unused_service; i++) {
  21684. + VCHIQ_SERVICE_T *service = state->services[i];
  21685. + VCHIQ_INSTANCE_T instance;
  21686. +
  21687. + if (service && (service->base.callback == service_callback)) {
  21688. + instance = service->instance;
  21689. + if (instance && !instance->mark) {
  21690. + len = snprintf(buf, sizeof(buf),
  21691. + "Instance %x: pid %d,%s completions "
  21692. + "%d/%d",
  21693. + (unsigned int)instance, instance->pid,
  21694. + instance->connected ? " connected, " :
  21695. + "",
  21696. + instance->completion_insert -
  21697. + instance->completion_remove,
  21698. + MAX_COMPLETIONS);
  21699. +
  21700. + vchiq_dump(dump_context, buf, len + 1);
  21701. +
  21702. + instance->mark = 1;
  21703. + }
  21704. + }
  21705. + }
  21706. +}
  21707. +
  21708. +/****************************************************************************
  21709. +*
  21710. +* vchiq_dump_platform_service_state
  21711. +*
  21712. +***************************************************************************/
  21713. +
  21714. +void
  21715. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  21716. +{
  21717. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  21718. + char buf[80];
  21719. + int len;
  21720. +
  21721. + len = snprintf(buf, sizeof(buf), " instance %x",
  21722. + (unsigned int)service->instance);
  21723. +
  21724. + if ((service->base.callback == service_callback) &&
  21725. + user_service->is_vchi) {
  21726. + len += snprintf(buf + len, sizeof(buf) - len,
  21727. + ", %d/%d messages",
  21728. + user_service->msg_insert - user_service->msg_remove,
  21729. + MSG_QUEUE_SIZE);
  21730. +
  21731. + if (user_service->dequeue_pending)
  21732. + len += snprintf(buf + len, sizeof(buf) - len,
  21733. + " (dequeue pending)");
  21734. + }
  21735. +
  21736. + vchiq_dump(dump_context, buf, len + 1);
  21737. +}
  21738. +
  21739. +/****************************************************************************
  21740. +*
  21741. +* dump_user_mem
  21742. +*
  21743. +***************************************************************************/
  21744. +
  21745. +static void
  21746. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  21747. +{
  21748. + int rc;
  21749. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  21750. + int num_pages;
  21751. + int offset;
  21752. + int end_offset;
  21753. + int page_idx;
  21754. + int prev_idx;
  21755. + struct page *page;
  21756. + struct page **pages;
  21757. + uint8_t *kmapped_virt_ptr;
  21758. +
  21759. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  21760. +
  21761. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  21762. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  21763. + ~0x0fuL);
  21764. +
  21765. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  21766. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  21767. +
  21768. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  21769. +
  21770. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  21771. + if (pages == NULL) {
  21772. + vchiq_log_error(vchiq_arm_log_level,
  21773. + "Unable to allocation memory for %d pages\n",
  21774. + num_pages);
  21775. + return;
  21776. + }
  21777. +
  21778. + down_read(&current->mm->mmap_sem);
  21779. + rc = get_user_pages(current, /* task */
  21780. + current->mm, /* mm */
  21781. + (unsigned long)virt_addr, /* start */
  21782. + num_pages, /* len */
  21783. + 0, /* write */
  21784. + 0, /* force */
  21785. + pages, /* pages (array of page pointers) */
  21786. + NULL); /* vmas */
  21787. + up_read(&current->mm->mmap_sem);
  21788. +
  21789. + prev_idx = -1;
  21790. + page = NULL;
  21791. +
  21792. + while (offset < end_offset) {
  21793. +
  21794. + int page_offset = offset % PAGE_SIZE;
  21795. + page_idx = offset / PAGE_SIZE;
  21796. +
  21797. + if (page_idx != prev_idx) {
  21798. +
  21799. + if (page != NULL)
  21800. + kunmap(page);
  21801. + page = pages[page_idx];
  21802. + kmapped_virt_ptr = kmap(page);
  21803. +
  21804. + prev_idx = page_idx;
  21805. + }
  21806. +
  21807. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  21808. + vchiq_log_dump_mem("ph",
  21809. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  21810. + page_offset],
  21811. + &kmapped_virt_ptr[page_offset], 16);
  21812. +
  21813. + offset += 16;
  21814. + }
  21815. + if (page != NULL)
  21816. + kunmap(page);
  21817. +
  21818. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  21819. + page_cache_release(pages[page_idx]);
  21820. +
  21821. + kfree(pages);
  21822. +}
  21823. +
  21824. +/****************************************************************************
  21825. +*
  21826. +* vchiq_read
  21827. +*
  21828. +***************************************************************************/
  21829. +
  21830. +static ssize_t
  21831. +vchiq_read(struct file *file, char __user *buf,
  21832. + size_t count, loff_t *ppos)
  21833. +{
  21834. + DUMP_CONTEXT_T context;
  21835. + context.buf = buf;
  21836. + context.actual = 0;
  21837. + context.space = count;
  21838. + context.offset = *ppos;
  21839. +
  21840. + vchiq_dump_state(&context, &g_state);
  21841. +
  21842. + *ppos += context.actual;
  21843. +
  21844. + return context.actual;
  21845. +}
  21846. +
  21847. +VCHIQ_STATE_T *
  21848. +vchiq_get_state(void)
  21849. +{
  21850. +
  21851. + if (g_state.remote == NULL)
  21852. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  21853. + else if (g_state.remote->initialised != 1)
  21854. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  21855. + __func__, g_state.remote->initialised);
  21856. +
  21857. + return ((g_state.remote != NULL) &&
  21858. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  21859. +}
  21860. +
  21861. +static const struct file_operations
  21862. +vchiq_fops = {
  21863. + .owner = THIS_MODULE,
  21864. + .unlocked_ioctl = vchiq_ioctl,
  21865. + .open = vchiq_open,
  21866. + .release = vchiq_release,
  21867. + .read = vchiq_read
  21868. +};
  21869. +
  21870. +/*
  21871. + * Autosuspend related functionality
  21872. + */
  21873. +
  21874. +int
  21875. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  21876. +{
  21877. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21878. + if (!arm_state)
  21879. + /* autosuspend not supported - always return wanted */
  21880. + return 1;
  21881. + else if (arm_state->blocked_count)
  21882. + return 1;
  21883. + else if (!arm_state->videocore_use_count)
  21884. + /* usage count zero - check for override unless we're forcing */
  21885. + if (arm_state->resume_blocked)
  21886. + return 0;
  21887. + else
  21888. + return vchiq_platform_videocore_wanted(state);
  21889. + else
  21890. + /* non-zero usage count - videocore still required */
  21891. + return 1;
  21892. +}
  21893. +
  21894. +static VCHIQ_STATUS_T
  21895. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  21896. + VCHIQ_HEADER_T *header,
  21897. + VCHIQ_SERVICE_HANDLE_T service_user,
  21898. + void *bulk_user)
  21899. +{
  21900. + vchiq_log_error(vchiq_susp_log_level,
  21901. + "%s callback reason %d", __func__, reason);
  21902. + return 0;
  21903. +}
  21904. +
  21905. +static int
  21906. +vchiq_keepalive_thread_func(void *v)
  21907. +{
  21908. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  21909. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21910. +
  21911. + VCHIQ_STATUS_T status;
  21912. + VCHIQ_INSTANCE_T instance;
  21913. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  21914. +
  21915. + VCHIQ_SERVICE_PARAMS_T params = {
  21916. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  21917. + .callback = vchiq_keepalive_vchiq_callback,
  21918. + .version = KEEPALIVE_VER,
  21919. + .version_min = KEEPALIVE_VER_MIN
  21920. + };
  21921. +
  21922. + status = vchiq_initialise(&instance);
  21923. + if (status != VCHIQ_SUCCESS) {
  21924. + vchiq_log_error(vchiq_susp_log_level,
  21925. + "%s vchiq_initialise failed %d", __func__, status);
  21926. + goto exit;
  21927. + }
  21928. +
  21929. + status = vchiq_connect(instance);
  21930. + if (status != VCHIQ_SUCCESS) {
  21931. + vchiq_log_error(vchiq_susp_log_level,
  21932. + "%s vchiq_connect failed %d", __func__, status);
  21933. + goto shutdown;
  21934. + }
  21935. +
  21936. + status = vchiq_add_service(instance, &params, &ka_handle);
  21937. + if (status != VCHIQ_SUCCESS) {
  21938. + vchiq_log_error(vchiq_susp_log_level,
  21939. + "%s vchiq_open_service failed %d", __func__, status);
  21940. + goto shutdown;
  21941. + }
  21942. +
  21943. + while (1) {
  21944. + long rc = 0, uc = 0;
  21945. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  21946. + != 0) {
  21947. + vchiq_log_error(vchiq_susp_log_level,
  21948. + "%s interrupted", __func__);
  21949. + flush_signals(current);
  21950. + continue;
  21951. + }
  21952. +
  21953. + /* read and clear counters. Do release_count then use_count to
  21954. + * prevent getting more releases than uses */
  21955. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  21956. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  21957. +
  21958. + /* Call use/release service the requisite number of times.
  21959. + * Process use before release so use counts don't go negative */
  21960. + while (uc--) {
  21961. + atomic_inc(&arm_state->ka_use_ack_count);
  21962. + status = vchiq_use_service(ka_handle);
  21963. + if (status != VCHIQ_SUCCESS) {
  21964. + vchiq_log_error(vchiq_susp_log_level,
  21965. + "%s vchiq_use_service error %d",
  21966. + __func__, status);
  21967. + }
  21968. + }
  21969. + while (rc--) {
  21970. + status = vchiq_release_service(ka_handle);
  21971. + if (status != VCHIQ_SUCCESS) {
  21972. + vchiq_log_error(vchiq_susp_log_level,
  21973. + "%s vchiq_release_service error %d",
  21974. + __func__, status);
  21975. + }
  21976. + }
  21977. + }
  21978. +
  21979. +shutdown:
  21980. + vchiq_shutdown(instance);
  21981. +exit:
  21982. + return 0;
  21983. +}
  21984. +
  21985. +
  21986. +
  21987. +VCHIQ_STATUS_T
  21988. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  21989. +{
  21990. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  21991. +
  21992. + if (arm_state) {
  21993. + rwlock_init(&arm_state->susp_res_lock);
  21994. +
  21995. + init_completion(&arm_state->ka_evt);
  21996. + atomic_set(&arm_state->ka_use_count, 0);
  21997. + atomic_set(&arm_state->ka_use_ack_count, 0);
  21998. + atomic_set(&arm_state->ka_release_count, 0);
  21999. +
  22000. + init_completion(&arm_state->vc_suspend_complete);
  22001. +
  22002. + init_completion(&arm_state->vc_resume_complete);
  22003. + /* Initialise to 'done' state. We only want to block on resume
  22004. + * completion while videocore is suspended. */
  22005. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  22006. +
  22007. + init_completion(&arm_state->resume_blocker);
  22008. + /* Initialise to 'done' state. We only want to block on this
  22009. + * completion while resume is blocked */
  22010. + complete_all(&arm_state->resume_blocker);
  22011. +
  22012. + init_completion(&arm_state->blocked_blocker);
  22013. + /* Initialise to 'done' state. We only want to block on this
  22014. + * completion while things are waiting on the resume blocker */
  22015. + complete_all(&arm_state->blocked_blocker);
  22016. +
  22017. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  22018. + arm_state->suspend_timer_running = 0;
  22019. + init_timer(&arm_state->suspend_timer);
  22020. + arm_state->suspend_timer.data = (unsigned long)(state);
  22021. + arm_state->suspend_timer.function = suspend_timer_callback;
  22022. +
  22023. + arm_state->first_connect = 0;
  22024. +
  22025. + }
  22026. + return status;
  22027. +}
  22028. +
  22029. +/*
  22030. +** Functions to modify the state variables;
  22031. +** set_suspend_state
  22032. +** set_resume_state
  22033. +**
  22034. +** There are more state variables than we might like, so ensure they remain in
  22035. +** step. Suspend and resume state are maintained separately, since most of
  22036. +** these state machines can operate independently. However, there are a few
  22037. +** states where state transitions in one state machine cause a reset to the
  22038. +** other state machine. In addition, there are some completion events which
  22039. +** need to occur on state machine reset and end-state(s), so these are also
  22040. +** dealt with in these functions.
  22041. +**
  22042. +** In all states we set the state variable according to the input, but in some
  22043. +** cases we perform additional steps outlined below;
  22044. +**
  22045. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  22046. +** The suspend completion is completed after any suspend
  22047. +** attempt. When we reset the state machine we also reset
  22048. +** the completion. This reset occurs when videocore is
  22049. +** resumed, and also if we initiate suspend after a suspend
  22050. +** failure.
  22051. +**
  22052. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  22053. +** suspend - ie from this point on we must try to suspend
  22054. +** before resuming can occur. We therefore also reset the
  22055. +** resume state machine to VC_RESUME_IDLE in this state.
  22056. +**
  22057. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  22058. +** complete_all on the suspend completion to notify
  22059. +** anything waiting for suspend to happen.
  22060. +**
  22061. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  22062. +** initiate resume, so no need to alter resume state.
  22063. +** We call complete_all on the suspend completion to notify
  22064. +** of suspend rejection.
  22065. +**
  22066. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  22067. +** suspend completion and reset the resume state machine.
  22068. +**
  22069. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  22070. +** resume completion is in it's 'done' state whenever
  22071. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  22072. +** implies that videocore is suspended.
  22073. +** Hence, any thread which needs to wait until videocore is
  22074. +** running can wait on this completion - it will only block
  22075. +** if videocore is suspended.
  22076. +**
  22077. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  22078. +** Call complete_all on the resume completion to unblock
  22079. +** any threads waiting for resume. Also reset the suspend
  22080. +** state machine to it's idle state.
  22081. +**
  22082. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  22083. +*/
  22084. +
  22085. +inline void
  22086. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  22087. + enum vc_suspend_status new_state)
  22088. +{
  22089. + /* set the state in all cases */
  22090. + arm_state->vc_suspend_state = new_state;
  22091. +
  22092. + /* state specific additional actions */
  22093. + switch (new_state) {
  22094. + case VC_SUSPEND_FORCE_CANCELED:
  22095. + complete_all(&arm_state->vc_suspend_complete);
  22096. + break;
  22097. + case VC_SUSPEND_REJECTED:
  22098. + complete_all(&arm_state->vc_suspend_complete);
  22099. + break;
  22100. + case VC_SUSPEND_FAILED:
  22101. + complete_all(&arm_state->vc_suspend_complete);
  22102. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  22103. + complete_all(&arm_state->vc_resume_complete);
  22104. + break;
  22105. + case VC_SUSPEND_IDLE:
  22106. + INIT_COMPLETION(arm_state->vc_suspend_complete);
  22107. + break;
  22108. + case VC_SUSPEND_REQUESTED:
  22109. + break;
  22110. + case VC_SUSPEND_IN_PROGRESS:
  22111. + set_resume_state(arm_state, VC_RESUME_IDLE);
  22112. + break;
  22113. + case VC_SUSPEND_SUSPENDED:
  22114. + complete_all(&arm_state->vc_suspend_complete);
  22115. + break;
  22116. + default:
  22117. + BUG();
  22118. + break;
  22119. + }
  22120. +}
  22121. +
  22122. +inline void
  22123. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  22124. + enum vc_resume_status new_state)
  22125. +{
  22126. + /* set the state in all cases */
  22127. + arm_state->vc_resume_state = new_state;
  22128. +
  22129. + /* state specific additional actions */
  22130. + switch (new_state) {
  22131. + case VC_RESUME_FAILED:
  22132. + break;
  22133. + case VC_RESUME_IDLE:
  22134. + INIT_COMPLETION(arm_state->vc_resume_complete);
  22135. + break;
  22136. + case VC_RESUME_REQUESTED:
  22137. + break;
  22138. + case VC_RESUME_IN_PROGRESS:
  22139. + break;
  22140. + case VC_RESUME_RESUMED:
  22141. + complete_all(&arm_state->vc_resume_complete);
  22142. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22143. + break;
  22144. + default:
  22145. + BUG();
  22146. + break;
  22147. + }
  22148. +}
  22149. +
  22150. +
  22151. +/* should be called with the write lock held */
  22152. +inline void
  22153. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  22154. +{
  22155. + del_timer(&arm_state->suspend_timer);
  22156. + arm_state->suspend_timer.expires = jiffies +
  22157. + msecs_to_jiffies(arm_state->
  22158. + suspend_timer_timeout);
  22159. + add_timer(&arm_state->suspend_timer);
  22160. + arm_state->suspend_timer_running = 1;
  22161. +}
  22162. +
  22163. +/* should be called with the write lock held */
  22164. +static inline void
  22165. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  22166. +{
  22167. + if (arm_state->suspend_timer_running) {
  22168. + del_timer(&arm_state->suspend_timer);
  22169. + arm_state->suspend_timer_running = 0;
  22170. + }
  22171. +}
  22172. +
  22173. +static inline int
  22174. +need_resume(VCHIQ_STATE_T *state)
  22175. +{
  22176. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22177. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  22178. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  22179. + vchiq_videocore_wanted(state);
  22180. +}
  22181. +
  22182. +static int
  22183. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  22184. +{
  22185. + int status = VCHIQ_SUCCESS;
  22186. + const unsigned long timeout_val =
  22187. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  22188. + int resume_count = 0;
  22189. +
  22190. + /* Allow any threads which were blocked by the last force suspend to
  22191. + * complete if they haven't already. Only give this one shot; if
  22192. + * blocked_count is incremented after blocked_blocker is completed
  22193. + * (which only happens when blocked_count hits 0) then those threads
  22194. + * will have to wait until next time around */
  22195. + if (arm_state->blocked_count) {
  22196. + INIT_COMPLETION(arm_state->blocked_blocker);
  22197. + write_unlock_bh(&arm_state->susp_res_lock);
  22198. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  22199. + "blocked clients", __func__);
  22200. + if (wait_for_completion_interruptible_timeout(
  22201. + &arm_state->blocked_blocker, timeout_val)
  22202. + <= 0) {
  22203. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  22204. + "previously blocked clients failed" , __func__);
  22205. + status = VCHIQ_ERROR;
  22206. + write_lock_bh(&arm_state->susp_res_lock);
  22207. + goto out;
  22208. + }
  22209. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  22210. + "clients resumed", __func__);
  22211. + write_lock_bh(&arm_state->susp_res_lock);
  22212. + }
  22213. +
  22214. + /* We need to wait for resume to complete if it's in process */
  22215. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  22216. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  22217. + if (resume_count > 1) {
  22218. + status = VCHIQ_ERROR;
  22219. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  22220. + "many times for resume" , __func__);
  22221. + goto out;
  22222. + }
  22223. + write_unlock_bh(&arm_state->susp_res_lock);
  22224. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  22225. + __func__);
  22226. + if (wait_for_completion_interruptible_timeout(
  22227. + &arm_state->vc_resume_complete, timeout_val)
  22228. + <= 0) {
  22229. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  22230. + "resume failed (%s)", __func__,
  22231. + resume_state_names[arm_state->vc_resume_state +
  22232. + VC_RESUME_NUM_OFFSET]);
  22233. + status = VCHIQ_ERROR;
  22234. + write_lock_bh(&arm_state->susp_res_lock);
  22235. + goto out;
  22236. + }
  22237. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  22238. + write_lock_bh(&arm_state->susp_res_lock);
  22239. + resume_count++;
  22240. + }
  22241. + INIT_COMPLETION(arm_state->resume_blocker);
  22242. + arm_state->resume_blocked = 1;
  22243. +
  22244. +out:
  22245. + return status;
  22246. +}
  22247. +
  22248. +static inline void
  22249. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  22250. +{
  22251. + complete_all(&arm_state->resume_blocker);
  22252. + arm_state->resume_blocked = 0;
  22253. +}
  22254. +
  22255. +/* Initiate suspend via slot handler. Should be called with the write lock
  22256. + * held */
  22257. +VCHIQ_STATUS_T
  22258. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  22259. +{
  22260. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  22261. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22262. +
  22263. + if (!arm_state)
  22264. + goto out;
  22265. +
  22266. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22267. + status = VCHIQ_SUCCESS;
  22268. +
  22269. +
  22270. + switch (arm_state->vc_suspend_state) {
  22271. + case VC_SUSPEND_REQUESTED:
  22272. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  22273. + "requested", __func__);
  22274. + break;
  22275. + case VC_SUSPEND_IN_PROGRESS:
  22276. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  22277. + "progress", __func__);
  22278. + break;
  22279. +
  22280. + default:
  22281. + /* We don't expect to be in other states, so log but continue
  22282. + * anyway */
  22283. + vchiq_log_error(vchiq_susp_log_level,
  22284. + "%s unexpected suspend state %s", __func__,
  22285. + suspend_state_names[arm_state->vc_suspend_state +
  22286. + VC_SUSPEND_NUM_OFFSET]);
  22287. + /* fall through */
  22288. + case VC_SUSPEND_REJECTED:
  22289. + case VC_SUSPEND_FAILED:
  22290. + /* Ensure any idle state actions have been run */
  22291. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22292. + /* fall through */
  22293. + case VC_SUSPEND_IDLE:
  22294. + vchiq_log_info(vchiq_susp_log_level,
  22295. + "%s: suspending", __func__);
  22296. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  22297. + /* kick the slot handler thread to initiate suspend */
  22298. + request_poll(state, NULL, 0);
  22299. + break;
  22300. + }
  22301. +
  22302. +out:
  22303. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  22304. + return status;
  22305. +}
  22306. +
  22307. +void
  22308. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  22309. +{
  22310. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22311. + int susp = 0;
  22312. +
  22313. + if (!arm_state)
  22314. + goto out;
  22315. +
  22316. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22317. +
  22318. + write_lock_bh(&arm_state->susp_res_lock);
  22319. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  22320. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  22321. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  22322. + susp = 1;
  22323. + }
  22324. + write_unlock_bh(&arm_state->susp_res_lock);
  22325. +
  22326. + if (susp)
  22327. + vchiq_platform_suspend(state);
  22328. +
  22329. +out:
  22330. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22331. + return;
  22332. +}
  22333. +
  22334. +
  22335. +static void
  22336. +output_timeout_error(VCHIQ_STATE_T *state)
  22337. +{
  22338. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22339. + char service_err[50] = "";
  22340. + int vc_use_count = arm_state->videocore_use_count;
  22341. + int active_services = state->unused_service;
  22342. + int i;
  22343. +
  22344. + if (!arm_state->videocore_use_count) {
  22345. + snprintf(service_err, 50, " Videocore usecount is 0");
  22346. + goto output_msg;
  22347. + }
  22348. + for (i = 0; i < active_services; i++) {
  22349. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  22350. + if (service_ptr && service_ptr->service_use_count &&
  22351. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  22352. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  22353. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  22354. + service_ptr->base.fourcc),
  22355. + service_ptr->client_id,
  22356. + service_ptr->service_use_count,
  22357. + service_ptr->service_use_count ==
  22358. + vc_use_count ? "" : " (+ more)");
  22359. + break;
  22360. + }
  22361. + }
  22362. +
  22363. +output_msg:
  22364. + vchiq_log_error(vchiq_susp_log_level,
  22365. + "timed out waiting for vc suspend (%d).%s",
  22366. + arm_state->autosuspend_override, service_err);
  22367. +
  22368. +}
  22369. +
  22370. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  22371. +** We don't actually force suspend, since videocore may get into a bad state
  22372. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  22373. +** determine a good point to suspend. If this doesn't happen within 100ms we
  22374. +** report failure.
  22375. +**
  22376. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  22377. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  22378. +*/
  22379. +VCHIQ_STATUS_T
  22380. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  22381. +{
  22382. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22383. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  22384. + long rc = 0;
  22385. + int repeat = -1;
  22386. +
  22387. + if (!arm_state)
  22388. + goto out;
  22389. +
  22390. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22391. +
  22392. + write_lock_bh(&arm_state->susp_res_lock);
  22393. +
  22394. + status = block_resume(arm_state);
  22395. + if (status != VCHIQ_SUCCESS)
  22396. + goto unlock;
  22397. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  22398. + /* Already suspended - just block resume and exit */
  22399. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  22400. + __func__);
  22401. + status = VCHIQ_SUCCESS;
  22402. + goto unlock;
  22403. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  22404. + /* initiate suspend immediately in the case that we're waiting
  22405. + * for the timeout */
  22406. + stop_suspend_timer(arm_state);
  22407. + if (!vchiq_videocore_wanted(state)) {
  22408. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  22409. + "idle, initiating suspend", __func__);
  22410. + status = vchiq_arm_vcsuspend(state);
  22411. + } else if (arm_state->autosuspend_override <
  22412. + FORCE_SUSPEND_FAIL_MAX) {
  22413. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  22414. + "videocore go idle", __func__);
  22415. + status = VCHIQ_SUCCESS;
  22416. + } else {
  22417. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  22418. + "many times - attempting suspend", __func__);
  22419. + status = vchiq_arm_vcsuspend(state);
  22420. + }
  22421. + } else {
  22422. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  22423. + "in progress - wait for completion", __func__);
  22424. + status = VCHIQ_SUCCESS;
  22425. + }
  22426. +
  22427. + /* Wait for suspend to happen due to system idle (not forced..) */
  22428. + if (status != VCHIQ_SUCCESS)
  22429. + goto unblock_resume;
  22430. +
  22431. + do {
  22432. + write_unlock_bh(&arm_state->susp_res_lock);
  22433. +
  22434. + rc = wait_for_completion_interruptible_timeout(
  22435. + &arm_state->vc_suspend_complete,
  22436. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  22437. +
  22438. + write_lock_bh(&arm_state->susp_res_lock);
  22439. + if (rc < 0) {
  22440. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  22441. + "interrupted waiting for suspend", __func__);
  22442. + status = VCHIQ_ERROR;
  22443. + goto unblock_resume;
  22444. + } else if (rc == 0) {
  22445. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  22446. + /* Repeat timeout once if in progress */
  22447. + if (repeat < 0) {
  22448. + repeat = 1;
  22449. + continue;
  22450. + }
  22451. + }
  22452. + arm_state->autosuspend_override++;
  22453. + output_timeout_error(state);
  22454. +
  22455. + status = VCHIQ_RETRY;
  22456. + goto unblock_resume;
  22457. + }
  22458. + } while (0 < (repeat--));
  22459. +
  22460. + /* Check and report state in case we need to abort ARM suspend */
  22461. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  22462. + status = VCHIQ_RETRY;
  22463. + vchiq_log_error(vchiq_susp_log_level,
  22464. + "%s videocore suspend failed (state %s)", __func__,
  22465. + suspend_state_names[arm_state->vc_suspend_state +
  22466. + VC_SUSPEND_NUM_OFFSET]);
  22467. + /* Reset the state only if it's still in an error state.
  22468. + * Something could have already initiated another suspend. */
  22469. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  22470. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22471. +
  22472. + goto unblock_resume;
  22473. + }
  22474. +
  22475. + /* successfully suspended - unlock and exit */
  22476. + goto unlock;
  22477. +
  22478. +unblock_resume:
  22479. + /* all error states need to unblock resume before exit */
  22480. + unblock_resume(arm_state);
  22481. +
  22482. +unlock:
  22483. + write_unlock_bh(&arm_state->susp_res_lock);
  22484. +
  22485. +out:
  22486. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  22487. + return status;
  22488. +}
  22489. +
  22490. +void
  22491. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  22492. +{
  22493. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22494. +
  22495. + if (!arm_state)
  22496. + goto out;
  22497. +
  22498. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22499. +
  22500. + write_lock_bh(&arm_state->susp_res_lock);
  22501. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  22502. + arm_state->first_connect &&
  22503. + !vchiq_videocore_wanted(state)) {
  22504. + vchiq_arm_vcsuspend(state);
  22505. + }
  22506. + write_unlock_bh(&arm_state->susp_res_lock);
  22507. +
  22508. +out:
  22509. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22510. + return;
  22511. +}
  22512. +
  22513. +
  22514. +int
  22515. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  22516. +{
  22517. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22518. + int resume = 0;
  22519. + int ret = -1;
  22520. +
  22521. + if (!arm_state)
  22522. + goto out;
  22523. +
  22524. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22525. +
  22526. + write_lock_bh(&arm_state->susp_res_lock);
  22527. + unblock_resume(arm_state);
  22528. + resume = vchiq_check_resume(state);
  22529. + write_unlock_bh(&arm_state->susp_res_lock);
  22530. +
  22531. + if (resume) {
  22532. + if (wait_for_completion_interruptible(
  22533. + &arm_state->vc_resume_complete) < 0) {
  22534. + vchiq_log_error(vchiq_susp_log_level,
  22535. + "%s interrupted", __func__);
  22536. + /* failed, cannot accurately derive suspend
  22537. + * state, so exit early. */
  22538. + goto out;
  22539. + }
  22540. + }
  22541. +
  22542. + read_lock_bh(&arm_state->susp_res_lock);
  22543. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  22544. + vchiq_log_info(vchiq_susp_log_level,
  22545. + "%s: Videocore remains suspended", __func__);
  22546. + } else {
  22547. + vchiq_log_info(vchiq_susp_log_level,
  22548. + "%s: Videocore resumed", __func__);
  22549. + ret = 0;
  22550. + }
  22551. + read_unlock_bh(&arm_state->susp_res_lock);
  22552. +out:
  22553. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22554. + return ret;
  22555. +}
  22556. +
  22557. +/* This function should be called with the write lock held */
  22558. +int
  22559. +vchiq_check_resume(VCHIQ_STATE_T *state)
  22560. +{
  22561. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22562. + int resume = 0;
  22563. +
  22564. + if (!arm_state)
  22565. + goto out;
  22566. +
  22567. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22568. +
  22569. + if (need_resume(state)) {
  22570. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  22571. + request_poll(state, NULL, 0);
  22572. + resume = 1;
  22573. + }
  22574. +
  22575. +out:
  22576. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22577. + return resume;
  22578. +}
  22579. +
  22580. +void
  22581. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  22582. +{
  22583. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22584. + int res = 0;
  22585. +
  22586. + if (!arm_state)
  22587. + goto out;
  22588. +
  22589. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22590. +
  22591. + write_lock_bh(&arm_state->susp_res_lock);
  22592. + if (arm_state->wake_address == 0) {
  22593. + vchiq_log_info(vchiq_susp_log_level,
  22594. + "%s: already awake", __func__);
  22595. + goto unlock;
  22596. + }
  22597. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  22598. + vchiq_log_info(vchiq_susp_log_level,
  22599. + "%s: already resuming", __func__);
  22600. + goto unlock;
  22601. + }
  22602. +
  22603. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  22604. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  22605. + res = 1;
  22606. + } else
  22607. + vchiq_log_trace(vchiq_susp_log_level,
  22608. + "%s: not resuming (resume state %s)", __func__,
  22609. + resume_state_names[arm_state->vc_resume_state +
  22610. + VC_RESUME_NUM_OFFSET]);
  22611. +
  22612. +unlock:
  22613. + write_unlock_bh(&arm_state->susp_res_lock);
  22614. +
  22615. + if (res)
  22616. + vchiq_platform_resume(state);
  22617. +
  22618. +out:
  22619. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  22620. + return;
  22621. +
  22622. +}
  22623. +
  22624. +
  22625. +
  22626. +VCHIQ_STATUS_T
  22627. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  22628. + enum USE_TYPE_E use_type)
  22629. +{
  22630. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22631. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  22632. + char entity[16];
  22633. + int *entity_uc;
  22634. + int local_uc, local_entity_uc;
  22635. +
  22636. + if (!arm_state)
  22637. + goto out;
  22638. +
  22639. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22640. +
  22641. + if (use_type == USE_TYPE_VCHIQ) {
  22642. + sprintf(entity, "VCHIQ: ");
  22643. + entity_uc = &arm_state->peer_use_count;
  22644. + } else if (service) {
  22645. + sprintf(entity, "%c%c%c%c:%03d",
  22646. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  22647. + service->client_id);
  22648. + entity_uc = &service->service_use_count;
  22649. + } else {
  22650. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  22651. + "ptr", __func__);
  22652. + ret = VCHIQ_ERROR;
  22653. + goto out;
  22654. + }
  22655. +
  22656. + write_lock_bh(&arm_state->susp_res_lock);
  22657. + while (arm_state->resume_blocked) {
  22658. + /* If we call 'use' while force suspend is waiting for suspend,
  22659. + * then we're about to block the thread which the force is
  22660. + * waiting to complete, so we're bound to just time out. In this
  22661. + * case, set the suspend state such that the wait will be
  22662. + * canceled, so we can complete as quickly as possible. */
  22663. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  22664. + VC_SUSPEND_IDLE) {
  22665. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  22666. + break;
  22667. + }
  22668. + /* If suspend is already in progress then we need to block */
  22669. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  22670. + /* Indicate that there are threads waiting on the resume
  22671. + * blocker. These need to be allowed to complete before
  22672. + * a _second_ call to force suspend can complete,
  22673. + * otherwise low priority threads might never actually
  22674. + * continue */
  22675. + arm_state->blocked_count++;
  22676. + write_unlock_bh(&arm_state->susp_res_lock);
  22677. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  22678. + "blocked - waiting...", __func__, entity);
  22679. + if (wait_for_completion_killable(
  22680. + &arm_state->resume_blocker) != 0) {
  22681. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  22682. + "wait for resume blocker interrupted",
  22683. + __func__, entity);
  22684. + ret = VCHIQ_ERROR;
  22685. + write_lock_bh(&arm_state->susp_res_lock);
  22686. + arm_state->blocked_count--;
  22687. + write_unlock_bh(&arm_state->susp_res_lock);
  22688. + goto out;
  22689. + }
  22690. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  22691. + "unblocked", __func__, entity);
  22692. + write_lock_bh(&arm_state->susp_res_lock);
  22693. + if (--arm_state->blocked_count == 0)
  22694. + complete_all(&arm_state->blocked_blocker);
  22695. + }
  22696. + }
  22697. +
  22698. + stop_suspend_timer(arm_state);
  22699. +
  22700. + local_uc = ++arm_state->videocore_use_count;
  22701. + local_entity_uc = ++(*entity_uc);
  22702. +
  22703. + /* If there's a pending request which hasn't yet been serviced then
  22704. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  22705. + * vc_resume_complete will block until we either resume or fail to
  22706. + * suspend */
  22707. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  22708. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  22709. +
  22710. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  22711. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  22712. + vchiq_log_info(vchiq_susp_log_level,
  22713. + "%s %s count %d, state count %d",
  22714. + __func__, entity, local_entity_uc, local_uc);
  22715. + request_poll(state, NULL, 0);
  22716. + } else
  22717. + vchiq_log_trace(vchiq_susp_log_level,
  22718. + "%s %s count %d, state count %d",
  22719. + __func__, entity, *entity_uc, local_uc);
  22720. +
  22721. +
  22722. + write_unlock_bh(&arm_state->susp_res_lock);
  22723. +
  22724. + /* Completion is in a done state when we're not suspended, so this won't
  22725. + * block for the non-suspended case. */
  22726. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  22727. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  22728. + __func__, entity);
  22729. + if (wait_for_completion_killable(
  22730. + &arm_state->vc_resume_complete) != 0) {
  22731. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  22732. + "resume interrupted", __func__, entity);
  22733. + ret = VCHIQ_ERROR;
  22734. + goto out;
  22735. + }
  22736. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  22737. + entity);
  22738. + }
  22739. +
  22740. + if (ret == VCHIQ_SUCCESS) {
  22741. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  22742. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  22743. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  22744. + /* Send the use notify to videocore */
  22745. + status = vchiq_send_remote_use_active(state);
  22746. + if (status == VCHIQ_SUCCESS)
  22747. + ack_cnt--;
  22748. + else
  22749. + atomic_add(ack_cnt,
  22750. + &arm_state->ka_use_ack_count);
  22751. + }
  22752. + }
  22753. +
  22754. +out:
  22755. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22756. + return ret;
  22757. +}
  22758. +
  22759. +VCHIQ_STATUS_T
  22760. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  22761. +{
  22762. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22763. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  22764. + char entity[16];
  22765. + int *entity_uc;
  22766. + int local_uc, local_entity_uc;
  22767. +
  22768. + if (!arm_state)
  22769. + goto out;
  22770. +
  22771. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22772. +
  22773. + if (service) {
  22774. + sprintf(entity, "%c%c%c%c:%03d",
  22775. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  22776. + service->client_id);
  22777. + entity_uc = &service->service_use_count;
  22778. + } else {
  22779. + sprintf(entity, "PEER: ");
  22780. + entity_uc = &arm_state->peer_use_count;
  22781. + }
  22782. +
  22783. + write_lock_bh(&arm_state->susp_res_lock);
  22784. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  22785. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  22786. + WARN_ON(!arm_state->videocore_use_count);
  22787. + WARN_ON(!(*entity_uc));
  22788. + ret = VCHIQ_ERROR;
  22789. + goto unlock;
  22790. + }
  22791. + local_uc = --arm_state->videocore_use_count;
  22792. + local_entity_uc = --(*entity_uc);
  22793. +
  22794. + if (!vchiq_videocore_wanted(state)) {
  22795. + if (vchiq_platform_use_suspend_timer() &&
  22796. + !arm_state->resume_blocked) {
  22797. + /* Only use the timer if we're not trying to force
  22798. + * suspend (=> resume_blocked) */
  22799. + start_suspend_timer(arm_state);
  22800. + } else {
  22801. + vchiq_log_info(vchiq_susp_log_level,
  22802. + "%s %s count %d, state count %d - suspending",
  22803. + __func__, entity, *entity_uc,
  22804. + arm_state->videocore_use_count);
  22805. + vchiq_arm_vcsuspend(state);
  22806. + }
  22807. + } else
  22808. + vchiq_log_trace(vchiq_susp_log_level,
  22809. + "%s %s count %d, state count %d",
  22810. + __func__, entity, *entity_uc,
  22811. + arm_state->videocore_use_count);
  22812. +
  22813. +unlock:
  22814. + write_unlock_bh(&arm_state->susp_res_lock);
  22815. +
  22816. +out:
  22817. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22818. + return ret;
  22819. +}
  22820. +
  22821. +void
  22822. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  22823. +{
  22824. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22825. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22826. + atomic_inc(&arm_state->ka_use_count);
  22827. + complete(&arm_state->ka_evt);
  22828. +}
  22829. +
  22830. +void
  22831. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  22832. +{
  22833. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22834. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22835. + atomic_inc(&arm_state->ka_release_count);
  22836. + complete(&arm_state->ka_evt);
  22837. +}
  22838. +
  22839. +VCHIQ_STATUS_T
  22840. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  22841. +{
  22842. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  22843. +}
  22844. +
  22845. +VCHIQ_STATUS_T
  22846. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  22847. +{
  22848. + return vchiq_release_internal(service->state, service);
  22849. +}
  22850. +
  22851. +static void suspend_timer_callback(unsigned long context)
  22852. +{
  22853. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  22854. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22855. + if (!arm_state)
  22856. + goto out;
  22857. + vchiq_log_info(vchiq_susp_log_level,
  22858. + "%s - suspend timer expired - check suspend", __func__);
  22859. + vchiq_check_suspend(state);
  22860. +out:
  22861. + return;
  22862. +}
  22863. +
  22864. +VCHIQ_STATUS_T
  22865. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  22866. +{
  22867. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22868. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22869. + if (service) {
  22870. + ret = vchiq_use_internal(service->state, service,
  22871. + USE_TYPE_SERVICE_NO_RESUME);
  22872. + unlock_service(service);
  22873. + }
  22874. + return ret;
  22875. +}
  22876. +
  22877. +VCHIQ_STATUS_T
  22878. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  22879. +{
  22880. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22881. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22882. + if (service) {
  22883. + ret = vchiq_use_internal(service->state, service,
  22884. + USE_TYPE_SERVICE);
  22885. + unlock_service(service);
  22886. + }
  22887. + return ret;
  22888. +}
  22889. +
  22890. +VCHIQ_STATUS_T
  22891. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  22892. +{
  22893. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22894. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22895. + if (service) {
  22896. + ret = vchiq_release_internal(service->state, service);
  22897. + unlock_service(service);
  22898. + }
  22899. + return ret;
  22900. +}
  22901. +
  22902. +void
  22903. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  22904. +{
  22905. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22906. + int i, j = 0;
  22907. + /* Only dump 64 services */
  22908. + static const int local_max_services = 64;
  22909. + /* If there's more than 64 services, only dump ones with
  22910. + * non-zero counts */
  22911. + int only_nonzero = 0;
  22912. + static const char *nz = "<-- preventing suspend";
  22913. +
  22914. + enum vc_suspend_status vc_suspend_state;
  22915. + enum vc_resume_status vc_resume_state;
  22916. + int peer_count;
  22917. + int vc_use_count;
  22918. + int active_services;
  22919. + struct service_data_struct {
  22920. + int fourcc;
  22921. + int clientid;
  22922. + int use_count;
  22923. + } service_data[local_max_services];
  22924. +
  22925. + if (!arm_state)
  22926. + return;
  22927. +
  22928. + read_lock_bh(&arm_state->susp_res_lock);
  22929. + vc_suspend_state = arm_state->vc_suspend_state;
  22930. + vc_resume_state = arm_state->vc_resume_state;
  22931. + peer_count = arm_state->peer_use_count;
  22932. + vc_use_count = arm_state->videocore_use_count;
  22933. + active_services = state->unused_service;
  22934. + if (active_services > local_max_services)
  22935. + only_nonzero = 1;
  22936. +
  22937. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  22938. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  22939. + if (!service_ptr)
  22940. + continue;
  22941. +
  22942. + if (only_nonzero && !service_ptr->service_use_count)
  22943. + continue;
  22944. +
  22945. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  22946. + service_data[j].fourcc = service_ptr->base.fourcc;
  22947. + service_data[j].clientid = service_ptr->client_id;
  22948. + service_data[j++].use_count = service_ptr->
  22949. + service_use_count;
  22950. + }
  22951. + }
  22952. +
  22953. + read_unlock_bh(&arm_state->susp_res_lock);
  22954. +
  22955. + vchiq_log_warning(vchiq_susp_log_level,
  22956. + "-- Videcore suspend state: %s --",
  22957. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  22958. + vchiq_log_warning(vchiq_susp_log_level,
  22959. + "-- Videcore resume state: %s --",
  22960. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  22961. +
  22962. + if (only_nonzero)
  22963. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  22964. + "services (%d). Only dumping up to first %d services "
  22965. + "with non-zero use-count", active_services,
  22966. + local_max_services);
  22967. +
  22968. + for (i = 0; i < j; i++) {
  22969. + vchiq_log_warning(vchiq_susp_log_level,
  22970. + "----- %c%c%c%c:%d service count %d %s",
  22971. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  22972. + service_data[i].clientid,
  22973. + service_data[i].use_count,
  22974. + service_data[i].use_count ? nz : "");
  22975. + }
  22976. + vchiq_log_warning(vchiq_susp_log_level,
  22977. + "----- VCHIQ use count count %d", peer_count);
  22978. + vchiq_log_warning(vchiq_susp_log_level,
  22979. + "--- Overall vchiq instance use count %d", vc_use_count);
  22980. +
  22981. + vchiq_dump_platform_use_state(state);
  22982. +}
  22983. +
  22984. +VCHIQ_STATUS_T
  22985. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  22986. +{
  22987. + VCHIQ_ARM_STATE_T *arm_state;
  22988. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22989. +
  22990. + if (!service || !service->state)
  22991. + goto out;
  22992. +
  22993. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22994. +
  22995. + arm_state = vchiq_platform_get_arm_state(service->state);
  22996. +
  22997. + read_lock_bh(&arm_state->susp_res_lock);
  22998. + if (service->service_use_count)
  22999. + ret = VCHIQ_SUCCESS;
  23000. + read_unlock_bh(&arm_state->susp_res_lock);
  23001. +
  23002. + if (ret == VCHIQ_ERROR) {
  23003. + vchiq_log_error(vchiq_susp_log_level,
  23004. + "%s ERROR - %c%c%c%c:%d service count %d, "
  23005. + "state count %d, videocore suspend state %s", __func__,
  23006. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  23007. + service->client_id, service->service_use_count,
  23008. + arm_state->videocore_use_count,
  23009. + suspend_state_names[arm_state->vc_suspend_state +
  23010. + VC_SUSPEND_NUM_OFFSET]);
  23011. + vchiq_dump_service_use_state(service->state);
  23012. + }
  23013. +out:
  23014. + return ret;
  23015. +}
  23016. +
  23017. +/* stub functions */
  23018. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  23019. +{
  23020. + (void)state;
  23021. +}
  23022. +
  23023. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  23024. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  23025. +{
  23026. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  23027. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  23028. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  23029. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  23030. + write_lock_bh(&arm_state->susp_res_lock);
  23031. + if (!arm_state->first_connect) {
  23032. + char threadname[10];
  23033. + arm_state->first_connect = 1;
  23034. + write_unlock_bh(&arm_state->susp_res_lock);
  23035. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  23036. + state->id);
  23037. + arm_state->ka_thread = kthread_create(
  23038. + &vchiq_keepalive_thread_func,
  23039. + (void *)state,
  23040. + threadname);
  23041. + if (arm_state->ka_thread == NULL) {
  23042. + vchiq_log_error(vchiq_susp_log_level,
  23043. + "vchiq: FATAL: couldn't create thread %s",
  23044. + threadname);
  23045. + } else {
  23046. + wake_up_process(arm_state->ka_thread);
  23047. + }
  23048. + } else
  23049. + write_unlock_bh(&arm_state->susp_res_lock);
  23050. + }
  23051. +}
  23052. +
  23053. +
  23054. +/****************************************************************************
  23055. +*
  23056. +* vchiq_init - called when the module is loaded.
  23057. +*
  23058. +***************************************************************************/
  23059. +
  23060. +static int __init
  23061. +vchiq_init(void)
  23062. +{
  23063. + int err;
  23064. + void *ptr_err;
  23065. +
  23066. + /* create proc entries */
  23067. + err = vchiq_proc_init();
  23068. + if (err != 0)
  23069. + goto failed_proc_init;
  23070. +
  23071. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  23072. + if (err != 0) {
  23073. + vchiq_log_error(vchiq_arm_log_level,
  23074. + "Unable to allocate device number");
  23075. + goto failed_alloc_chrdev;
  23076. + }
  23077. + cdev_init(&vchiq_cdev, &vchiq_fops);
  23078. + vchiq_cdev.owner = THIS_MODULE;
  23079. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  23080. + if (err != 0) {
  23081. + vchiq_log_error(vchiq_arm_log_level,
  23082. + "Unable to register device");
  23083. + goto failed_cdev_add;
  23084. + }
  23085. +
  23086. + /* create sysfs entries */
  23087. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  23088. + ptr_err = vchiq_class;
  23089. + if (IS_ERR(ptr_err))
  23090. + goto failed_class_create;
  23091. +
  23092. + vchiq_dev = device_create(vchiq_class, NULL,
  23093. + vchiq_devid, NULL, "vchiq");
  23094. + ptr_err = vchiq_dev;
  23095. + if (IS_ERR(ptr_err))
  23096. + goto failed_device_create;
  23097. +
  23098. + err = vchiq_platform_init(&g_state);
  23099. + if (err != 0)
  23100. + goto failed_platform_init;
  23101. +
  23102. + vchiq_log_info(vchiq_arm_log_level,
  23103. + "vchiq: initialised - version %d (min %d), device %d.%d",
  23104. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  23105. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  23106. +
  23107. + return 0;
  23108. +
  23109. +failed_platform_init:
  23110. + device_destroy(vchiq_class, vchiq_devid);
  23111. +failed_device_create:
  23112. + class_destroy(vchiq_class);
  23113. +failed_class_create:
  23114. + cdev_del(&vchiq_cdev);
  23115. + err = PTR_ERR(ptr_err);
  23116. +failed_cdev_add:
  23117. + unregister_chrdev_region(vchiq_devid, 1);
  23118. +failed_alloc_chrdev:
  23119. + vchiq_proc_deinit();
  23120. +failed_proc_init:
  23121. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  23122. + return err;
  23123. +}
  23124. +
  23125. +static int vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  23126. +{
  23127. + VCHIQ_SERVICE_T *service;
  23128. + int use_count = 0, i;
  23129. + i = 0;
  23130. + while ((service = next_service_by_instance(instance->state,
  23131. + instance, &i)) != NULL) {
  23132. + use_count += service->service_use_count;
  23133. + unlock_service(service);
  23134. + }
  23135. + return use_count;
  23136. +}
  23137. +
  23138. +/* read the per-process use-count */
  23139. +static int proc_read_use_count(char *page, char **start,
  23140. + off_t off, int count,
  23141. + int *eof, void *data)
  23142. +{
  23143. + VCHIQ_INSTANCE_T instance = data;
  23144. + int len, use_count;
  23145. +
  23146. + use_count = vchiq_instance_get_use_count(instance);
  23147. + len = snprintf(page+off, count, "%d\n", use_count);
  23148. +
  23149. + return len;
  23150. +}
  23151. +
  23152. +/* add an instance (process) to the proc entries */
  23153. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance)
  23154. +{
  23155. +#if 1
  23156. + return 0;
  23157. +#else
  23158. + char pidstr[32];
  23159. + struct proc_dir_entry *top, *use_count;
  23160. + struct proc_dir_entry *clients = vchiq_clients_top();
  23161. + int pid = instance->pid;
  23162. +
  23163. + snprintf(pidstr, sizeof(pidstr), "%d", pid);
  23164. + top = proc_mkdir(pidstr, clients);
  23165. + if (!top)
  23166. + goto fail_top;
  23167. +
  23168. + use_count = create_proc_read_entry("use_count",
  23169. + 0444, top,
  23170. + proc_read_use_count,
  23171. + instance);
  23172. + if (!use_count)
  23173. + goto fail_use_count;
  23174. +
  23175. + instance->proc_entry = top;
  23176. +
  23177. + return 0;
  23178. +
  23179. +fail_use_count:
  23180. + remove_proc_entry(top->name, clients);
  23181. +fail_top:
  23182. + return -ENOMEM;
  23183. +#endif
  23184. +}
  23185. +
  23186. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance)
  23187. +{
  23188. +#if 0
  23189. + struct proc_dir_entry *clients = vchiq_clients_top();
  23190. + remove_proc_entry("use_count", instance->proc_entry);
  23191. + remove_proc_entry(instance->proc_entry->name, clients);
  23192. +#endif
  23193. +}
  23194. +
  23195. +/****************************************************************************
  23196. +*
  23197. +* vchiq_exit - called when the module is unloaded.
  23198. +*
  23199. +***************************************************************************/
  23200. +
  23201. +static void __exit
  23202. +vchiq_exit(void)
  23203. +{
  23204. + vchiq_platform_exit(&g_state);
  23205. + device_destroy(vchiq_class, vchiq_devid);
  23206. + class_destroy(vchiq_class);
  23207. + cdev_del(&vchiq_cdev);
  23208. + unregister_chrdev_region(vchiq_devid, 1);
  23209. +}
  23210. +
  23211. +module_init(vchiq_init);
  23212. +module_exit(vchiq_exit);
  23213. +MODULE_LICENSE("GPL");
  23214. +MODULE_AUTHOR("Broadcom Corporation");
  23215. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  23216. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 1970-01-01 01:00:00.000000000 +0100
  23217. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 2014-04-24 15:35:02.893551516 +0200
  23218. @@ -0,0 +1,212 @@
  23219. +/**
  23220. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23221. + *
  23222. + * Redistribution and use in source and binary forms, with or without
  23223. + * modification, are permitted provided that the following conditions
  23224. + * are met:
  23225. + * 1. Redistributions of source code must retain the above copyright
  23226. + * notice, this list of conditions, and the following disclaimer,
  23227. + * without modification.
  23228. + * 2. Redistributions in binary form must reproduce the above copyright
  23229. + * notice, this list of conditions and the following disclaimer in the
  23230. + * documentation and/or other materials provided with the distribution.
  23231. + * 3. The names of the above-listed copyright holders may not be used
  23232. + * to endorse or promote products derived from this software without
  23233. + * specific prior written permission.
  23234. + *
  23235. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23236. + * GNU General Public License ("GPL") version 2, as published by the Free
  23237. + * Software Foundation.
  23238. + *
  23239. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23240. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23241. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23242. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23243. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23244. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23245. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23246. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23247. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23248. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23249. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23250. + */
  23251. +
  23252. +#ifndef VCHIQ_ARM_H
  23253. +#define VCHIQ_ARM_H
  23254. +
  23255. +#include <linux/mutex.h>
  23256. +#include <linux/semaphore.h>
  23257. +#include <linux/atomic.h>
  23258. +#include "vchiq_core.h"
  23259. +
  23260. +
  23261. +enum vc_suspend_status {
  23262. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  23263. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  23264. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  23265. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  23266. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  23267. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  23268. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  23269. +};
  23270. +
  23271. +enum vc_resume_status {
  23272. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  23273. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  23274. + VC_RESUME_REQUESTED, /* User has requested resume */
  23275. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  23276. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  23277. +};
  23278. +
  23279. +
  23280. +enum USE_TYPE_E {
  23281. + USE_TYPE_SERVICE,
  23282. + USE_TYPE_SERVICE_NO_RESUME,
  23283. + USE_TYPE_VCHIQ
  23284. +};
  23285. +
  23286. +
  23287. +
  23288. +typedef struct vchiq_arm_state_struct {
  23289. + /* Keepalive-related data */
  23290. + struct task_struct *ka_thread;
  23291. + struct completion ka_evt;
  23292. + atomic_t ka_use_count;
  23293. + atomic_t ka_use_ack_count;
  23294. + atomic_t ka_release_count;
  23295. +
  23296. + struct completion vc_suspend_complete;
  23297. + struct completion vc_resume_complete;
  23298. +
  23299. + rwlock_t susp_res_lock;
  23300. + enum vc_suspend_status vc_suspend_state;
  23301. + enum vc_resume_status vc_resume_state;
  23302. +
  23303. + unsigned int wake_address;
  23304. +
  23305. + struct timer_list suspend_timer;
  23306. + int suspend_timer_timeout;
  23307. + int suspend_timer_running;
  23308. +
  23309. + /* Global use count for videocore.
  23310. + ** This is equal to the sum of the use counts for all services. When
  23311. + ** this hits zero the videocore suspend procedure will be initiated.
  23312. + */
  23313. + int videocore_use_count;
  23314. +
  23315. + /* Use count to track requests from videocore peer.
  23316. + ** This use count is not associated with a service, so needs to be
  23317. + ** tracked separately with the state.
  23318. + */
  23319. + int peer_use_count;
  23320. +
  23321. + /* Flag to indicate whether resume is blocked. This happens when the
  23322. + ** ARM is suspending
  23323. + */
  23324. + struct completion resume_blocker;
  23325. + int resume_blocked;
  23326. + struct completion blocked_blocker;
  23327. + int blocked_count;
  23328. +
  23329. + int autosuspend_override;
  23330. +
  23331. + /* Flag to indicate that the first vchiq connect has made it through.
  23332. + ** This means that both sides should be fully ready, and we should
  23333. + ** be able to suspend after this point.
  23334. + */
  23335. + int first_connect;
  23336. +
  23337. + unsigned long long suspend_start_time;
  23338. + unsigned long long sleep_start_time;
  23339. + unsigned long long resume_start_time;
  23340. + unsigned long long last_wake_time;
  23341. +
  23342. +} VCHIQ_ARM_STATE_T;
  23343. +
  23344. +extern int vchiq_arm_log_level;
  23345. +extern int vchiq_susp_log_level;
  23346. +
  23347. +extern int __init
  23348. +vchiq_platform_init(VCHIQ_STATE_T *state);
  23349. +
  23350. +extern void __exit
  23351. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  23352. +
  23353. +extern VCHIQ_STATE_T *
  23354. +vchiq_get_state(void);
  23355. +
  23356. +extern VCHIQ_STATUS_T
  23357. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  23358. +
  23359. +extern VCHIQ_STATUS_T
  23360. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  23361. +
  23362. +extern int
  23363. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  23364. +
  23365. +extern VCHIQ_STATUS_T
  23366. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  23367. +
  23368. +extern VCHIQ_STATUS_T
  23369. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  23370. +
  23371. +extern int
  23372. +vchiq_check_resume(VCHIQ_STATE_T *state);
  23373. +
  23374. +extern void
  23375. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  23376. +
  23377. +extern VCHIQ_STATUS_T
  23378. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  23379. +
  23380. +extern VCHIQ_STATUS_T
  23381. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  23382. +
  23383. +extern VCHIQ_STATUS_T
  23384. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  23385. +
  23386. +extern VCHIQ_STATUS_T
  23387. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  23388. +
  23389. +extern int
  23390. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  23391. +
  23392. +extern int
  23393. +vchiq_platform_use_suspend_timer(void);
  23394. +
  23395. +extern void
  23396. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  23397. +
  23398. +extern void
  23399. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  23400. +
  23401. +extern VCHIQ_ARM_STATE_T*
  23402. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  23403. +
  23404. +extern int
  23405. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  23406. +
  23407. +extern VCHIQ_STATUS_T
  23408. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  23409. + enum USE_TYPE_E use_type);
  23410. +extern VCHIQ_STATUS_T
  23411. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  23412. +
  23413. +void
  23414. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  23415. + enum vc_suspend_status new_state);
  23416. +
  23417. +void
  23418. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  23419. + enum vc_resume_status new_state);
  23420. +
  23421. +void
  23422. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  23423. +
  23424. +extern int vchiq_proc_init(void);
  23425. +extern void vchiq_proc_deinit(void);
  23426. +extern struct proc_dir_entry *vchiq_proc_top(void);
  23427. +extern struct proc_dir_entry *vchiq_clients_top(void);
  23428. +
  23429. +
  23430. +#endif /* VCHIQ_ARM_H */
  23431. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  23432. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 1970-01-01 01:00:00.000000000 +0100
  23433. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 2014-04-24 15:35:02.893551516 +0200
  23434. @@ -0,0 +1,37 @@
  23435. +/**
  23436. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23437. + *
  23438. + * Redistribution and use in source and binary forms, with or without
  23439. + * modification, are permitted provided that the following conditions
  23440. + * are met:
  23441. + * 1. Redistributions of source code must retain the above copyright
  23442. + * notice, this list of conditions, and the following disclaimer,
  23443. + * without modification.
  23444. + * 2. Redistributions in binary form must reproduce the above copyright
  23445. + * notice, this list of conditions and the following disclaimer in the
  23446. + * documentation and/or other materials provided with the distribution.
  23447. + * 3. The names of the above-listed copyright holders may not be used
  23448. + * to endorse or promote products derived from this software without
  23449. + * specific prior written permission.
  23450. + *
  23451. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23452. + * GNU General Public License ("GPL") version 2, as published by the Free
  23453. + * Software Foundation.
  23454. + *
  23455. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23456. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23457. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23458. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23459. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23460. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23461. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23462. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23463. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23464. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23465. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23466. + */
  23467. +
  23468. +const char *vchiq_get_build_hostname(void);
  23469. +const char *vchiq_get_build_version(void);
  23470. +const char *vchiq_get_build_time(void);
  23471. +const char *vchiq_get_build_date(void);
  23472. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  23473. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 1970-01-01 01:00:00.000000000 +0100
  23474. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 2014-04-24 15:35:02.893551516 +0200
  23475. @@ -0,0 +1,60 @@
  23476. +/**
  23477. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23478. + *
  23479. + * Redistribution and use in source and binary forms, with or without
  23480. + * modification, are permitted provided that the following conditions
  23481. + * are met:
  23482. + * 1. Redistributions of source code must retain the above copyright
  23483. + * notice, this list of conditions, and the following disclaimer,
  23484. + * without modification.
  23485. + * 2. Redistributions in binary form must reproduce the above copyright
  23486. + * notice, this list of conditions and the following disclaimer in the
  23487. + * documentation and/or other materials provided with the distribution.
  23488. + * 3. The names of the above-listed copyright holders may not be used
  23489. + * to endorse or promote products derived from this software without
  23490. + * specific prior written permission.
  23491. + *
  23492. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23493. + * GNU General Public License ("GPL") version 2, as published by the Free
  23494. + * Software Foundation.
  23495. + *
  23496. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23497. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23498. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23499. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23500. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23501. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23502. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23503. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23504. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23505. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23506. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23507. + */
  23508. +
  23509. +#ifndef VCHIQ_CFG_H
  23510. +#define VCHIQ_CFG_H
  23511. +
  23512. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  23513. +/* The version of VCHIQ - change with any non-trivial change */
  23514. +#define VCHIQ_VERSION 6
  23515. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  23516. +** incompatible change */
  23517. +#define VCHIQ_VERSION_MIN 3
  23518. +
  23519. +#define VCHIQ_MAX_STATES 1
  23520. +#define VCHIQ_MAX_SERVICES 4096
  23521. +#define VCHIQ_MAX_SLOTS 128
  23522. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  23523. +
  23524. +#define VCHIQ_NUM_CURRENT_BULKS 32
  23525. +#define VCHIQ_NUM_SERVICE_BULKS 4
  23526. +
  23527. +#ifndef VCHIQ_ENABLE_DEBUG
  23528. +#define VCHIQ_ENABLE_DEBUG 1
  23529. +#endif
  23530. +
  23531. +#ifndef VCHIQ_ENABLE_STATS
  23532. +#define VCHIQ_ENABLE_STATS 1
  23533. +#endif
  23534. +
  23535. +#endif /* VCHIQ_CFG_H */
  23536. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  23537. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 1970-01-01 01:00:00.000000000 +0100
  23538. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 2014-04-24 15:35:02.893551516 +0200
  23539. @@ -0,0 +1,119 @@
  23540. +/**
  23541. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23542. + *
  23543. + * Redistribution and use in source and binary forms, with or without
  23544. + * modification, are permitted provided that the following conditions
  23545. + * are met:
  23546. + * 1. Redistributions of source code must retain the above copyright
  23547. + * notice, this list of conditions, and the following disclaimer,
  23548. + * without modification.
  23549. + * 2. Redistributions in binary form must reproduce the above copyright
  23550. + * notice, this list of conditions and the following disclaimer in the
  23551. + * documentation and/or other materials provided with the distribution.
  23552. + * 3. The names of the above-listed copyright holders may not be used
  23553. + * to endorse or promote products derived from this software without
  23554. + * specific prior written permission.
  23555. + *
  23556. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23557. + * GNU General Public License ("GPL") version 2, as published by the Free
  23558. + * Software Foundation.
  23559. + *
  23560. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23561. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23562. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23563. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23564. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23565. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23566. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23567. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23568. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23569. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23570. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23571. + */
  23572. +
  23573. +#include "vchiq_connected.h"
  23574. +#include "vchiq_core.h"
  23575. +#include <linux/module.h>
  23576. +#include <linux/mutex.h>
  23577. +
  23578. +#define MAX_CALLBACKS 10
  23579. +
  23580. +static int g_connected;
  23581. +static int g_num_deferred_callbacks;
  23582. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  23583. +static int g_once_init;
  23584. +static struct mutex g_connected_mutex;
  23585. +
  23586. +/****************************************************************************
  23587. +*
  23588. +* Function to initialize our lock.
  23589. +*
  23590. +***************************************************************************/
  23591. +
  23592. +static void connected_init(void)
  23593. +{
  23594. + if (!g_once_init) {
  23595. + mutex_init(&g_connected_mutex);
  23596. + g_once_init = 1;
  23597. + }
  23598. +}
  23599. +
  23600. +/****************************************************************************
  23601. +*
  23602. +* This function is used to defer initialization until the vchiq stack is
  23603. +* initialized. If the stack is already initialized, then the callback will
  23604. +* be made immediately, otherwise it will be deferred until
  23605. +* vchiq_call_connected_callbacks is called.
  23606. +*
  23607. +***************************************************************************/
  23608. +
  23609. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  23610. +{
  23611. + connected_init();
  23612. +
  23613. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  23614. + return;
  23615. +
  23616. + if (g_connected)
  23617. + /* We're already connected. Call the callback immediately. */
  23618. +
  23619. + callback();
  23620. + else {
  23621. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  23622. + vchiq_log_error(vchiq_core_log_level,
  23623. + "There already %d callback registered - "
  23624. + "please increase MAX_CALLBACKS",
  23625. + g_num_deferred_callbacks);
  23626. + else {
  23627. + g_deferred_callback[g_num_deferred_callbacks] =
  23628. + callback;
  23629. + g_num_deferred_callbacks++;
  23630. + }
  23631. + }
  23632. + mutex_unlock(&g_connected_mutex);
  23633. +}
  23634. +
  23635. +/****************************************************************************
  23636. +*
  23637. +* This function is called by the vchiq stack once it has been connected to
  23638. +* the videocore and clients can start to use the stack.
  23639. +*
  23640. +***************************************************************************/
  23641. +
  23642. +void vchiq_call_connected_callbacks(void)
  23643. +{
  23644. + int i;
  23645. +
  23646. + connected_init();
  23647. +
  23648. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  23649. + return;
  23650. +
  23651. + for (i = 0; i < g_num_deferred_callbacks; i++)
  23652. + g_deferred_callback[i]();
  23653. +
  23654. + g_num_deferred_callbacks = 0;
  23655. + g_connected = 1;
  23656. + mutex_unlock(&g_connected_mutex);
  23657. +}
  23658. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  23659. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  23660. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 1970-01-01 01:00:00.000000000 +0100
  23661. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 2014-04-24 15:35:02.893551516 +0200
  23662. @@ -0,0 +1,51 @@
  23663. +/**
  23664. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23665. + *
  23666. + * Redistribution and use in source and binary forms, with or without
  23667. + * modification, are permitted provided that the following conditions
  23668. + * are met:
  23669. + * 1. Redistributions of source code must retain the above copyright
  23670. + * notice, this list of conditions, and the following disclaimer,
  23671. + * without modification.
  23672. + * 2. Redistributions in binary form must reproduce the above copyright
  23673. + * notice, this list of conditions and the following disclaimer in the
  23674. + * documentation and/or other materials provided with the distribution.
  23675. + * 3. The names of the above-listed copyright holders may not be used
  23676. + * to endorse or promote products derived from this software without
  23677. + * specific prior written permission.
  23678. + *
  23679. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23680. + * GNU General Public License ("GPL") version 2, as published by the Free
  23681. + * Software Foundation.
  23682. + *
  23683. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23684. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23685. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23686. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23687. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23688. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23689. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23690. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23691. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23692. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23693. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23694. + */
  23695. +
  23696. +#ifndef VCHIQ_CONNECTED_H
  23697. +#define VCHIQ_CONNECTED_H
  23698. +
  23699. +/* ---- Include Files ----------------------------------------------------- */
  23700. +
  23701. +/* ---- Constants and Types ---------------------------------------------- */
  23702. +
  23703. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  23704. +
  23705. +/* ---- Variable Externs ------------------------------------------------- */
  23706. +
  23707. +/* ---- Function Prototypes ---------------------------------------------- */
  23708. +
  23709. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  23710. +void vchiq_call_connected_callbacks(void);
  23711. +
  23712. +#endif /* VCHIQ_CONNECTED_H */
  23713. +
  23714. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  23715. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 1970-01-01 01:00:00.000000000 +0100
  23716. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 2014-04-24 15:35:02.893551516 +0200
  23717. @@ -0,0 +1,3824 @@
  23718. +/**
  23719. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23720. + *
  23721. + * Redistribution and use in source and binary forms, with or without
  23722. + * modification, are permitted provided that the following conditions
  23723. + * are met:
  23724. + * 1. Redistributions of source code must retain the above copyright
  23725. + * notice, this list of conditions, and the following disclaimer,
  23726. + * without modification.
  23727. + * 2. Redistributions in binary form must reproduce the above copyright
  23728. + * notice, this list of conditions and the following disclaimer in the
  23729. + * documentation and/or other materials provided with the distribution.
  23730. + * 3. The names of the above-listed copyright holders may not be used
  23731. + * to endorse or promote products derived from this software without
  23732. + * specific prior written permission.
  23733. + *
  23734. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23735. + * GNU General Public License ("GPL") version 2, as published by the Free
  23736. + * Software Foundation.
  23737. + *
  23738. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23739. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23740. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23741. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23742. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23743. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23744. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23745. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23746. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23747. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23748. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23749. + */
  23750. +
  23751. +#include "vchiq_core.h"
  23752. +
  23753. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  23754. +
  23755. +#define HANDLE_STATE_SHIFT 12
  23756. +
  23757. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  23758. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  23759. +#define SLOT_INDEX_FROM_DATA(state, data) \
  23760. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  23761. + VCHIQ_SLOT_SIZE)
  23762. +#define SLOT_INDEX_FROM_INFO(state, info) \
  23763. + ((unsigned int)(info - state->slot_info))
  23764. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  23765. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  23766. +
  23767. +
  23768. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  23769. +
  23770. +
  23771. +struct vchiq_open_payload {
  23772. + int fourcc;
  23773. + int client_id;
  23774. + short version;
  23775. + short version_min;
  23776. +};
  23777. +
  23778. +struct vchiq_openack_payload {
  23779. + short version;
  23780. +};
  23781. +
  23782. +/* we require this for consistency between endpoints */
  23783. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  23784. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  23785. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  23786. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  23787. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  23788. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  23789. +
  23790. +/* Run time control of log level, based on KERN_XXX level. */
  23791. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  23792. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  23793. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  23794. +
  23795. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  23796. +
  23797. +static DEFINE_SPINLOCK(service_spinlock);
  23798. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  23799. +DEFINE_SPINLOCK(quota_spinlock);
  23800. +
  23801. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  23802. +static unsigned int handle_seq;
  23803. +
  23804. +static const char *const srvstate_names[] = {
  23805. + "FREE",
  23806. + "HIDDEN",
  23807. + "LISTENING",
  23808. + "OPENING",
  23809. + "OPEN",
  23810. + "OPENSYNC",
  23811. + "CLOSESENT",
  23812. + "CLOSERECVD",
  23813. + "CLOSEWAIT",
  23814. + "CLOSED"
  23815. +};
  23816. +
  23817. +static const char *const reason_names[] = {
  23818. + "SERVICE_OPENED",
  23819. + "SERVICE_CLOSED",
  23820. + "MESSAGE_AVAILABLE",
  23821. + "BULK_TRANSMIT_DONE",
  23822. + "BULK_RECEIVE_DONE",
  23823. + "BULK_TRANSMIT_ABORTED",
  23824. + "BULK_RECEIVE_ABORTED"
  23825. +};
  23826. +
  23827. +static const char *const conn_state_names[] = {
  23828. + "DISCONNECTED",
  23829. + "CONNECTING",
  23830. + "CONNECTED",
  23831. + "PAUSING",
  23832. + "PAUSE_SENT",
  23833. + "PAUSED",
  23834. + "RESUMING",
  23835. + "PAUSE_TIMEOUT",
  23836. + "RESUME_TIMEOUT"
  23837. +};
  23838. +
  23839. +
  23840. +static void
  23841. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  23842. +
  23843. +static const char *msg_type_str(unsigned int msg_type)
  23844. +{
  23845. + switch (msg_type) {
  23846. + case VCHIQ_MSG_PADDING: return "PADDING";
  23847. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  23848. + case VCHIQ_MSG_OPEN: return "OPEN";
  23849. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  23850. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  23851. + case VCHIQ_MSG_DATA: return "DATA";
  23852. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  23853. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  23854. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  23855. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  23856. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  23857. + case VCHIQ_MSG_RESUME: return "RESUME";
  23858. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  23859. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  23860. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  23861. + }
  23862. + return "???";
  23863. +}
  23864. +
  23865. +static inline void
  23866. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  23867. +{
  23868. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  23869. + service->state->id, service->localport,
  23870. + srvstate_names[service->srvstate],
  23871. + srvstate_names[newstate]);
  23872. + service->srvstate = newstate;
  23873. +}
  23874. +
  23875. +VCHIQ_SERVICE_T *
  23876. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  23877. +{
  23878. + VCHIQ_SERVICE_T *service;
  23879. +
  23880. + spin_lock(&service_spinlock);
  23881. + service = handle_to_service(handle);
  23882. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23883. + (service->handle == handle)) {
  23884. + BUG_ON(service->ref_count == 0);
  23885. + service->ref_count++;
  23886. + } else
  23887. + service = NULL;
  23888. + spin_unlock(&service_spinlock);
  23889. +
  23890. + if (!service)
  23891. + vchiq_log_info(vchiq_core_log_level,
  23892. + "Invalid service handle 0x%x", handle);
  23893. +
  23894. + return service;
  23895. +}
  23896. +
  23897. +VCHIQ_SERVICE_T *
  23898. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  23899. +{
  23900. + VCHIQ_SERVICE_T *service = NULL;
  23901. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  23902. + spin_lock(&service_spinlock);
  23903. + service = state->services[localport];
  23904. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  23905. + BUG_ON(service->ref_count == 0);
  23906. + service->ref_count++;
  23907. + } else
  23908. + service = NULL;
  23909. + spin_unlock(&service_spinlock);
  23910. + }
  23911. +
  23912. + if (!service)
  23913. + vchiq_log_info(vchiq_core_log_level,
  23914. + "Invalid port %d", localport);
  23915. +
  23916. + return service;
  23917. +}
  23918. +
  23919. +VCHIQ_SERVICE_T *
  23920. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  23921. + VCHIQ_SERVICE_HANDLE_T handle) {
  23922. + VCHIQ_SERVICE_T *service;
  23923. +
  23924. + spin_lock(&service_spinlock);
  23925. + service = handle_to_service(handle);
  23926. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23927. + (service->handle == handle) &&
  23928. + (service->instance == instance)) {
  23929. + BUG_ON(service->ref_count == 0);
  23930. + service->ref_count++;
  23931. + } else
  23932. + service = NULL;
  23933. + spin_unlock(&service_spinlock);
  23934. +
  23935. + if (!service)
  23936. + vchiq_log_info(vchiq_core_log_level,
  23937. + "Invalid service handle 0x%x", handle);
  23938. +
  23939. + return service;
  23940. +}
  23941. +
  23942. +VCHIQ_SERVICE_T *
  23943. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  23944. + int *pidx)
  23945. +{
  23946. + VCHIQ_SERVICE_T *service = NULL;
  23947. + int idx = *pidx;
  23948. +
  23949. + spin_lock(&service_spinlock);
  23950. + while (idx < state->unused_service) {
  23951. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  23952. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23953. + (srv->instance == instance)) {
  23954. + service = srv;
  23955. + BUG_ON(service->ref_count == 0);
  23956. + service->ref_count++;
  23957. + break;
  23958. + }
  23959. + }
  23960. + spin_unlock(&service_spinlock);
  23961. +
  23962. + *pidx = idx;
  23963. +
  23964. + return service;
  23965. +}
  23966. +
  23967. +void
  23968. +lock_service(VCHIQ_SERVICE_T *service)
  23969. +{
  23970. + spin_lock(&service_spinlock);
  23971. + BUG_ON(!service || (service->ref_count == 0));
  23972. + if (service)
  23973. + service->ref_count++;
  23974. + spin_unlock(&service_spinlock);
  23975. +}
  23976. +
  23977. +void
  23978. +unlock_service(VCHIQ_SERVICE_T *service)
  23979. +{
  23980. + VCHIQ_STATE_T *state = service->state;
  23981. + spin_lock(&service_spinlock);
  23982. + BUG_ON(!service || (service->ref_count == 0));
  23983. + if (service && service->ref_count) {
  23984. + service->ref_count--;
  23985. + if (!service->ref_count) {
  23986. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  23987. + state->services[service->localport] = NULL;
  23988. + } else
  23989. + service = NULL;
  23990. + }
  23991. + spin_unlock(&service_spinlock);
  23992. +
  23993. + if (service && service->userdata_term)
  23994. + service->userdata_term(service->base.userdata);
  23995. +
  23996. + kfree(service);
  23997. +}
  23998. +
  23999. +int
  24000. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  24001. +{
  24002. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  24003. + int id;
  24004. +
  24005. + id = service ? service->client_id : 0;
  24006. + if (service)
  24007. + unlock_service(service);
  24008. +
  24009. + return id;
  24010. +}
  24011. +
  24012. +void *
  24013. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  24014. +{
  24015. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  24016. +
  24017. + return service ? service->base.userdata : NULL;
  24018. +}
  24019. +
  24020. +int
  24021. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  24022. +{
  24023. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  24024. +
  24025. + return service ? service->base.fourcc : 0;
  24026. +}
  24027. +
  24028. +static void
  24029. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  24030. +{
  24031. + VCHIQ_STATE_T *state = service->state;
  24032. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  24033. +
  24034. + service->closing = 1;
  24035. +
  24036. + /* Synchronise with other threads. */
  24037. + mutex_lock(&state->recycle_mutex);
  24038. + mutex_unlock(&state->recycle_mutex);
  24039. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  24040. + /* If we're pausing then the slot_mutex is held until resume
  24041. + * by the slot handler. Therefore don't try to acquire this
  24042. + * mutex if we're the slot handler and in the pause sent state.
  24043. + * We don't need to in this case anyway. */
  24044. + mutex_lock(&state->slot_mutex);
  24045. + mutex_unlock(&state->slot_mutex);
  24046. + }
  24047. +
  24048. + /* Unblock any sending thread. */
  24049. + service_quota = &state->service_quotas[service->localport];
  24050. + up(&service_quota->quota_event);
  24051. +}
  24052. +
  24053. +static void
  24054. +mark_service_closing(VCHIQ_SERVICE_T *service)
  24055. +{
  24056. + mark_service_closing_internal(service, 0);
  24057. +}
  24058. +
  24059. +static inline VCHIQ_STATUS_T
  24060. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  24061. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  24062. +{
  24063. + VCHIQ_STATUS_T status;
  24064. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  24065. + service->state->id, service->localport, reason_names[reason],
  24066. + (unsigned int)header, (unsigned int)bulk_userdata);
  24067. + status = service->base.callback(reason, header, service->handle,
  24068. + bulk_userdata);
  24069. + if (status == VCHIQ_ERROR) {
  24070. + vchiq_log_warning(vchiq_core_log_level,
  24071. + "%d: ignoring ERROR from callback to service %x",
  24072. + service->state->id, service->handle);
  24073. + status = VCHIQ_SUCCESS;
  24074. + }
  24075. + return status;
  24076. +}
  24077. +
  24078. +inline void
  24079. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  24080. +{
  24081. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  24082. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  24083. + conn_state_names[oldstate],
  24084. + conn_state_names[newstate]);
  24085. + state->conn_state = newstate;
  24086. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  24087. +}
  24088. +
  24089. +static inline void
  24090. +remote_event_create(REMOTE_EVENT_T *event)
  24091. +{
  24092. + event->armed = 0;
  24093. + /* Don't clear the 'fired' flag because it may already have been set
  24094. + ** by the other side. */
  24095. + sema_init(event->event, 0);
  24096. +}
  24097. +
  24098. +static inline void
  24099. +remote_event_destroy(REMOTE_EVENT_T *event)
  24100. +{
  24101. + (void)event;
  24102. +}
  24103. +
  24104. +static inline int
  24105. +remote_event_wait(REMOTE_EVENT_T *event)
  24106. +{
  24107. + if (!event->fired) {
  24108. + event->armed = 1;
  24109. + dsb();
  24110. + if (!event->fired) {
  24111. + if (down_interruptible(event->event) != 0) {
  24112. + event->armed = 0;
  24113. + return 0;
  24114. + }
  24115. + }
  24116. + event->armed = 0;
  24117. + wmb();
  24118. + }
  24119. +
  24120. + event->fired = 0;
  24121. + return 1;
  24122. +}
  24123. +
  24124. +static inline void
  24125. +remote_event_signal_local(REMOTE_EVENT_T *event)
  24126. +{
  24127. + event->armed = 0;
  24128. + up(event->event);
  24129. +}
  24130. +
  24131. +static inline void
  24132. +remote_event_poll(REMOTE_EVENT_T *event)
  24133. +{
  24134. + if (event->fired && event->armed)
  24135. + remote_event_signal_local(event);
  24136. +}
  24137. +
  24138. +void
  24139. +remote_event_pollall(VCHIQ_STATE_T *state)
  24140. +{
  24141. + remote_event_poll(&state->local->sync_trigger);
  24142. + remote_event_poll(&state->local->sync_release);
  24143. + remote_event_poll(&state->local->trigger);
  24144. + remote_event_poll(&state->local->recycle);
  24145. +}
  24146. +
  24147. +/* Round up message sizes so that any space at the end of a slot is always big
  24148. +** enough for a header. This relies on header size being a power of two, which
  24149. +** has been verified earlier by a static assertion. */
  24150. +
  24151. +static inline unsigned int
  24152. +calc_stride(unsigned int size)
  24153. +{
  24154. + /* Allow room for the header */
  24155. + size += sizeof(VCHIQ_HEADER_T);
  24156. +
  24157. + /* Round up */
  24158. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  24159. + - 1);
  24160. +}
  24161. +
  24162. +/* Called by the slot handler thread */
  24163. +static VCHIQ_SERVICE_T *
  24164. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  24165. +{
  24166. + int i;
  24167. +
  24168. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  24169. +
  24170. + for (i = 0; i < state->unused_service; i++) {
  24171. + VCHIQ_SERVICE_T *service = state->services[i];
  24172. + if (service &&
  24173. + (service->public_fourcc == fourcc) &&
  24174. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  24175. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  24176. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  24177. + lock_service(service);
  24178. + return service;
  24179. + }
  24180. + }
  24181. +
  24182. + return NULL;
  24183. +}
  24184. +
  24185. +/* Called by the slot handler thread */
  24186. +static VCHIQ_SERVICE_T *
  24187. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  24188. +{
  24189. + int i;
  24190. + for (i = 0; i < state->unused_service; i++) {
  24191. + VCHIQ_SERVICE_T *service = state->services[i];
  24192. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  24193. + && (service->remoteport == port)) {
  24194. + lock_service(service);
  24195. + return service;
  24196. + }
  24197. + }
  24198. + return NULL;
  24199. +}
  24200. +
  24201. +inline void
  24202. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  24203. +{
  24204. + uint32_t value;
  24205. +
  24206. + if (service) {
  24207. + do {
  24208. + value = atomic_read(&service->poll_flags);
  24209. + } while (atomic_cmpxchg(&service->poll_flags, value,
  24210. + value | (1 << poll_type)) != value);
  24211. +
  24212. + do {
  24213. + value = atomic_read(&state->poll_services[
  24214. + service->localport>>5]);
  24215. + } while (atomic_cmpxchg(
  24216. + &state->poll_services[service->localport>>5],
  24217. + value, value | (1 << (service->localport & 0x1f)))
  24218. + != value);
  24219. + }
  24220. +
  24221. + state->poll_needed = 1;
  24222. + wmb();
  24223. +
  24224. + /* ... and ensure the slot handler runs. */
  24225. + remote_event_signal_local(&state->local->trigger);
  24226. +}
  24227. +
  24228. +/* Called from queue_message, by the slot handler and application threads,
  24229. +** with slot_mutex held */
  24230. +static VCHIQ_HEADER_T *
  24231. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  24232. +{
  24233. + VCHIQ_SHARED_STATE_T *local = state->local;
  24234. + int tx_pos = state->local_tx_pos;
  24235. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  24236. +
  24237. + if (space > slot_space) {
  24238. + VCHIQ_HEADER_T *header;
  24239. + /* Fill the remaining space with padding */
  24240. + WARN_ON(state->tx_data == NULL);
  24241. + header = (VCHIQ_HEADER_T *)
  24242. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  24243. + header->msgid = VCHIQ_MSGID_PADDING;
  24244. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  24245. +
  24246. + tx_pos += slot_space;
  24247. + }
  24248. +
  24249. + /* If necessary, get the next slot. */
  24250. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  24251. + int slot_index;
  24252. +
  24253. + /* If there is no free slot... */
  24254. +
  24255. + if (down_trylock(&state->slot_available_event) != 0) {
  24256. + /* ...wait for one. */
  24257. +
  24258. + VCHIQ_STATS_INC(state, slot_stalls);
  24259. +
  24260. + /* But first, flush through the last slot. */
  24261. + state->local_tx_pos = tx_pos;
  24262. + local->tx_pos = tx_pos;
  24263. + remote_event_signal(&state->remote->trigger);
  24264. +
  24265. + if (!is_blocking ||
  24266. + (down_interruptible(
  24267. + &state->slot_available_event) != 0))
  24268. + return NULL; /* No space available */
  24269. + }
  24270. +
  24271. + BUG_ON(tx_pos ==
  24272. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  24273. +
  24274. + slot_index = local->slot_queue[
  24275. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  24276. + VCHIQ_SLOT_QUEUE_MASK];
  24277. + state->tx_data =
  24278. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  24279. + }
  24280. +
  24281. + state->local_tx_pos = tx_pos + space;
  24282. +
  24283. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  24284. +}
  24285. +
  24286. +/* Called by the recycle thread. */
  24287. +static void
  24288. +process_free_queue(VCHIQ_STATE_T *state)
  24289. +{
  24290. + VCHIQ_SHARED_STATE_T *local = state->local;
  24291. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  24292. + int slot_queue_available;
  24293. +
  24294. + /* Use a read memory barrier to ensure that any state that may have
  24295. + ** been modified by another thread is not masked by stale prefetched
  24296. + ** values. */
  24297. + rmb();
  24298. +
  24299. + /* Find slots which have been freed by the other side, and return them
  24300. + ** to the available queue. */
  24301. + slot_queue_available = state->slot_queue_available;
  24302. +
  24303. + while (slot_queue_available != local->slot_queue_recycle) {
  24304. + unsigned int pos;
  24305. + int slot_index = local->slot_queue[slot_queue_available++ &
  24306. + VCHIQ_SLOT_QUEUE_MASK];
  24307. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  24308. + int data_found = 0;
  24309. +
  24310. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  24311. + state->id, slot_index, (unsigned int)data,
  24312. + local->slot_queue_recycle, slot_queue_available);
  24313. +
  24314. + /* Initialise the bitmask for services which have used this
  24315. + ** slot */
  24316. + BITSET_ZERO(service_found);
  24317. +
  24318. + pos = 0;
  24319. +
  24320. + while (pos < VCHIQ_SLOT_SIZE) {
  24321. + VCHIQ_HEADER_T *header =
  24322. + (VCHIQ_HEADER_T *)(data + pos);
  24323. + int msgid = header->msgid;
  24324. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  24325. + int port = VCHIQ_MSG_SRCPORT(msgid);
  24326. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  24327. + &state->service_quotas[port];
  24328. + int count;
  24329. + spin_lock(&quota_spinlock);
  24330. + count = service_quota->message_use_count;
  24331. + if (count > 0)
  24332. + service_quota->message_use_count =
  24333. + count - 1;
  24334. + spin_unlock(&quota_spinlock);
  24335. +
  24336. + if (count == service_quota->message_quota)
  24337. + /* Signal the service that it
  24338. + ** has dropped below its quota
  24339. + */
  24340. + up(&service_quota->quota_event);
  24341. + else if (count == 0) {
  24342. + vchiq_log_error(vchiq_core_log_level,
  24343. + "service %d "
  24344. + "message_use_count=%d "
  24345. + "(header %x, msgid %x, "
  24346. + "header->msgid %x, "
  24347. + "header->size %x)",
  24348. + port,
  24349. + service_quota->
  24350. + message_use_count,
  24351. + (unsigned int)header, msgid,
  24352. + header->msgid,
  24353. + header->size);
  24354. + WARN(1, "invalid message use count\n");
  24355. + }
  24356. + if (!BITSET_IS_SET(service_found, port)) {
  24357. + /* Set the found bit for this service */
  24358. + BITSET_SET(service_found, port);
  24359. +
  24360. + spin_lock(&quota_spinlock);
  24361. + count = service_quota->slot_use_count;
  24362. + if (count > 0)
  24363. + service_quota->slot_use_count =
  24364. + count - 1;
  24365. + spin_unlock(&quota_spinlock);
  24366. +
  24367. + if (count > 0) {
  24368. + /* Signal the service in case
  24369. + ** it has dropped below its
  24370. + ** quota */
  24371. + up(&service_quota->quota_event);
  24372. + vchiq_log_trace(
  24373. + vchiq_core_log_level,
  24374. + "%d: pfq:%d %x@%x - "
  24375. + "slot_use->%d",
  24376. + state->id, port,
  24377. + header->size,
  24378. + (unsigned int)header,
  24379. + count - 1);
  24380. + } else {
  24381. + vchiq_log_error(
  24382. + vchiq_core_log_level,
  24383. + "service %d "
  24384. + "slot_use_count"
  24385. + "=%d (header %x"
  24386. + ", msgid %x, "
  24387. + "header->msgid"
  24388. + " %x, header->"
  24389. + "size %x)",
  24390. + port, count,
  24391. + (unsigned int)header,
  24392. + msgid,
  24393. + header->msgid,
  24394. + header->size);
  24395. + WARN(1, "bad slot use count\n");
  24396. + }
  24397. + }
  24398. +
  24399. + data_found = 1;
  24400. + }
  24401. +
  24402. + pos += calc_stride(header->size);
  24403. + if (pos > VCHIQ_SLOT_SIZE) {
  24404. + vchiq_log_error(vchiq_core_log_level,
  24405. + "pfq - pos %x: header %x, msgid %x, "
  24406. + "header->msgid %x, header->size %x",
  24407. + pos, (unsigned int)header, msgid,
  24408. + header->msgid, header->size);
  24409. + WARN(1, "invalid slot position\n");
  24410. + }
  24411. + }
  24412. +
  24413. + if (data_found) {
  24414. + int count;
  24415. + spin_lock(&quota_spinlock);
  24416. + count = state->data_use_count;
  24417. + if (count > 0)
  24418. + state->data_use_count =
  24419. + count - 1;
  24420. + spin_unlock(&quota_spinlock);
  24421. + if (count == state->data_quota)
  24422. + up(&state->data_quota_event);
  24423. + }
  24424. +
  24425. + state->slot_queue_available = slot_queue_available;
  24426. + up(&state->slot_available_event);
  24427. + }
  24428. +}
  24429. +
  24430. +/* Called by the slot handler and application threads */
  24431. +static VCHIQ_STATUS_T
  24432. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  24433. + int msgid, const VCHIQ_ELEMENT_T *elements,
  24434. + int count, int size, int is_blocking)
  24435. +{
  24436. + VCHIQ_SHARED_STATE_T *local;
  24437. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  24438. + VCHIQ_HEADER_T *header;
  24439. + int type = VCHIQ_MSG_TYPE(msgid);
  24440. +
  24441. + unsigned int stride;
  24442. +
  24443. + local = state->local;
  24444. +
  24445. + stride = calc_stride(size);
  24446. +
  24447. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  24448. +
  24449. + if ((type != VCHIQ_MSG_RESUME) &&
  24450. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  24451. + return VCHIQ_RETRY;
  24452. +
  24453. + if (type == VCHIQ_MSG_DATA) {
  24454. + int tx_end_index;
  24455. +
  24456. + BUG_ON(!service);
  24457. +
  24458. + if (service->closing) {
  24459. + /* The service has been closed */
  24460. + mutex_unlock(&state->slot_mutex);
  24461. + return VCHIQ_ERROR;
  24462. + }
  24463. +
  24464. + service_quota = &state->service_quotas[service->localport];
  24465. +
  24466. + spin_lock(&quota_spinlock);
  24467. +
  24468. + /* Ensure this service doesn't use more than its quota of
  24469. + ** messages or slots */
  24470. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  24471. + state->local_tx_pos + stride - 1);
  24472. +
  24473. + /* Ensure data messages don't use more than their quota of
  24474. + ** slots */
  24475. + while ((tx_end_index != state->previous_data_index) &&
  24476. + (state->data_use_count == state->data_quota)) {
  24477. + VCHIQ_STATS_INC(state, data_stalls);
  24478. + spin_unlock(&quota_spinlock);
  24479. + mutex_unlock(&state->slot_mutex);
  24480. +
  24481. + if (down_interruptible(&state->data_quota_event)
  24482. + != 0)
  24483. + return VCHIQ_RETRY;
  24484. +
  24485. + mutex_lock(&state->slot_mutex);
  24486. + spin_lock(&quota_spinlock);
  24487. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  24488. + state->local_tx_pos + stride - 1);
  24489. + if ((tx_end_index == state->previous_data_index) ||
  24490. + (state->data_use_count < state->data_quota)) {
  24491. + /* Pass the signal on to other waiters */
  24492. + up(&state->data_quota_event);
  24493. + break;
  24494. + }
  24495. + }
  24496. +
  24497. + while ((service_quota->message_use_count ==
  24498. + service_quota->message_quota) ||
  24499. + ((tx_end_index != service_quota->previous_tx_index) &&
  24500. + (service_quota->slot_use_count ==
  24501. + service_quota->slot_quota))) {
  24502. + spin_unlock(&quota_spinlock);
  24503. + vchiq_log_trace(vchiq_core_log_level,
  24504. + "%d: qm:%d %s,%x - quota stall "
  24505. + "(msg %d, slot %d)",
  24506. + state->id, service->localport,
  24507. + msg_type_str(type), size,
  24508. + service_quota->message_use_count,
  24509. + service_quota->slot_use_count);
  24510. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  24511. + mutex_unlock(&state->slot_mutex);
  24512. + if (down_interruptible(&service_quota->quota_event)
  24513. + != 0)
  24514. + return VCHIQ_RETRY;
  24515. + if (service->closing)
  24516. + return VCHIQ_ERROR;
  24517. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  24518. + return VCHIQ_RETRY;
  24519. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  24520. + /* The service has been closed */
  24521. + mutex_unlock(&state->slot_mutex);
  24522. + return VCHIQ_ERROR;
  24523. + }
  24524. + spin_lock(&quota_spinlock);
  24525. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  24526. + state->local_tx_pos + stride - 1);
  24527. + }
  24528. +
  24529. + spin_unlock(&quota_spinlock);
  24530. + }
  24531. +
  24532. + header = reserve_space(state, stride, is_blocking);
  24533. +
  24534. + if (!header) {
  24535. + if (service)
  24536. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  24537. + mutex_unlock(&state->slot_mutex);
  24538. + return VCHIQ_RETRY;
  24539. + }
  24540. +
  24541. + if (type == VCHIQ_MSG_DATA) {
  24542. + int i, pos;
  24543. + int tx_end_index;
  24544. + int slot_use_count;
  24545. +
  24546. + vchiq_log_info(vchiq_core_log_level,
  24547. + "%d: qm %s@%x,%x (%d->%d)",
  24548. + state->id,
  24549. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24550. + (unsigned int)header, size,
  24551. + VCHIQ_MSG_SRCPORT(msgid),
  24552. + VCHIQ_MSG_DSTPORT(msgid));
  24553. +
  24554. + BUG_ON(!service);
  24555. +
  24556. + for (i = 0, pos = 0; i < (unsigned int)count;
  24557. + pos += elements[i++].size)
  24558. + if (elements[i].size) {
  24559. + if (vchiq_copy_from_user
  24560. + (header->data + pos, elements[i].data,
  24561. + (size_t) elements[i].size) !=
  24562. + VCHIQ_SUCCESS) {
  24563. + mutex_unlock(&state->slot_mutex);
  24564. + VCHIQ_SERVICE_STATS_INC(service,
  24565. + error_count);
  24566. + return VCHIQ_ERROR;
  24567. + }
  24568. + if (i == 0) {
  24569. + if (vchiq_core_msg_log_level >=
  24570. + VCHIQ_LOG_INFO)
  24571. + vchiq_log_dump_mem("Sent", 0,
  24572. + header->data + pos,
  24573. + min(64u,
  24574. + elements[0].size));
  24575. + }
  24576. + }
  24577. +
  24578. + spin_lock(&quota_spinlock);
  24579. + service_quota->message_use_count++;
  24580. +
  24581. + tx_end_index =
  24582. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  24583. +
  24584. + /* If this transmission can't fit in the last slot used by any
  24585. + ** service, the data_use_count must be increased. */
  24586. + if (tx_end_index != state->previous_data_index) {
  24587. + state->previous_data_index = tx_end_index;
  24588. + state->data_use_count++;
  24589. + }
  24590. +
  24591. + /* If this isn't the same slot last used by this service,
  24592. + ** the service's slot_use_count must be increased. */
  24593. + if (tx_end_index != service_quota->previous_tx_index) {
  24594. + service_quota->previous_tx_index = tx_end_index;
  24595. + slot_use_count = ++service_quota->slot_use_count;
  24596. + } else {
  24597. + slot_use_count = 0;
  24598. + }
  24599. +
  24600. + spin_unlock(&quota_spinlock);
  24601. +
  24602. + if (slot_use_count)
  24603. + vchiq_log_trace(vchiq_core_log_level,
  24604. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  24605. + state->id, service->localport,
  24606. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  24607. + slot_use_count, header);
  24608. +
  24609. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  24610. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  24611. + } else {
  24612. + vchiq_log_info(vchiq_core_log_level,
  24613. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  24614. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24615. + (unsigned int)header, size,
  24616. + VCHIQ_MSG_SRCPORT(msgid),
  24617. + VCHIQ_MSG_DSTPORT(msgid));
  24618. + if (size != 0) {
  24619. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  24620. + memcpy(header->data, elements[0].data,
  24621. + elements[0].size);
  24622. + }
  24623. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  24624. + }
  24625. +
  24626. + header->msgid = msgid;
  24627. + header->size = size;
  24628. +
  24629. + {
  24630. + int svc_fourcc;
  24631. +
  24632. + svc_fourcc = service
  24633. + ? service->base.fourcc
  24634. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  24635. +
  24636. + vchiq_log_info(vchiq_core_msg_log_level,
  24637. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  24638. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24639. + VCHIQ_MSG_TYPE(msgid),
  24640. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  24641. + VCHIQ_MSG_SRCPORT(msgid),
  24642. + VCHIQ_MSG_DSTPORT(msgid),
  24643. + size);
  24644. + }
  24645. +
  24646. + /* Make sure the new header is visible to the peer. */
  24647. + wmb();
  24648. +
  24649. + /* Make the new tx_pos visible to the peer. */
  24650. + local->tx_pos = state->local_tx_pos;
  24651. + wmb();
  24652. +
  24653. + if (service && (type == VCHIQ_MSG_CLOSE))
  24654. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  24655. +
  24656. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  24657. + mutex_unlock(&state->slot_mutex);
  24658. +
  24659. + remote_event_signal(&state->remote->trigger);
  24660. +
  24661. + return VCHIQ_SUCCESS;
  24662. +}
  24663. +
  24664. +/* Called by the slot handler and application threads */
  24665. +static VCHIQ_STATUS_T
  24666. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  24667. + int msgid, const VCHIQ_ELEMENT_T *elements,
  24668. + int count, int size, int is_blocking)
  24669. +{
  24670. + VCHIQ_SHARED_STATE_T *local;
  24671. + VCHIQ_HEADER_T *header;
  24672. +
  24673. + local = state->local;
  24674. +
  24675. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  24676. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  24677. + return VCHIQ_RETRY;
  24678. +
  24679. + remote_event_wait(&local->sync_release);
  24680. +
  24681. + rmb();
  24682. +
  24683. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  24684. + local->slot_sync);
  24685. +
  24686. + {
  24687. + int oldmsgid = header->msgid;
  24688. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  24689. + vchiq_log_error(vchiq_core_log_level,
  24690. + "%d: qms - msgid %x, not PADDING",
  24691. + state->id, oldmsgid);
  24692. + }
  24693. +
  24694. + if (service) {
  24695. + int i, pos;
  24696. +
  24697. + vchiq_log_info(vchiq_sync_log_level,
  24698. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  24699. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24700. + (unsigned int)header, size,
  24701. + VCHIQ_MSG_SRCPORT(msgid),
  24702. + VCHIQ_MSG_DSTPORT(msgid));
  24703. +
  24704. + for (i = 0, pos = 0; i < (unsigned int)count;
  24705. + pos += elements[i++].size)
  24706. + if (elements[i].size) {
  24707. + if (vchiq_copy_from_user
  24708. + (header->data + pos, elements[i].data,
  24709. + (size_t) elements[i].size) !=
  24710. + VCHIQ_SUCCESS) {
  24711. + mutex_unlock(&state->sync_mutex);
  24712. + VCHIQ_SERVICE_STATS_INC(service,
  24713. + error_count);
  24714. + return VCHIQ_ERROR;
  24715. + }
  24716. + if (i == 0) {
  24717. + if (vchiq_sync_log_level >=
  24718. + VCHIQ_LOG_TRACE)
  24719. + vchiq_log_dump_mem("Sent Sync",
  24720. + 0, header->data + pos,
  24721. + min(64u,
  24722. + elements[0].size));
  24723. + }
  24724. + }
  24725. +
  24726. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  24727. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  24728. + } else {
  24729. + vchiq_log_info(vchiq_sync_log_level,
  24730. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  24731. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24732. + (unsigned int)header, size,
  24733. + VCHIQ_MSG_SRCPORT(msgid),
  24734. + VCHIQ_MSG_DSTPORT(msgid));
  24735. + if (size != 0) {
  24736. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  24737. + memcpy(header->data, elements[0].data,
  24738. + elements[0].size);
  24739. + }
  24740. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  24741. + }
  24742. +
  24743. + header->size = size;
  24744. + header->msgid = msgid;
  24745. +
  24746. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  24747. + int svc_fourcc;
  24748. +
  24749. + svc_fourcc = service
  24750. + ? service->base.fourcc
  24751. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  24752. +
  24753. + vchiq_log_trace(vchiq_sync_log_level,
  24754. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  24755. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  24756. + VCHIQ_MSG_TYPE(msgid),
  24757. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  24758. + VCHIQ_MSG_SRCPORT(msgid),
  24759. + VCHIQ_MSG_DSTPORT(msgid),
  24760. + size);
  24761. + }
  24762. +
  24763. + /* Make sure the new header is visible to the peer. */
  24764. + wmb();
  24765. +
  24766. + remote_event_signal(&state->remote->sync_trigger);
  24767. +
  24768. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  24769. + mutex_unlock(&state->sync_mutex);
  24770. +
  24771. + return VCHIQ_SUCCESS;
  24772. +}
  24773. +
  24774. +static inline void
  24775. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  24776. +{
  24777. + slot->use_count++;
  24778. +}
  24779. +
  24780. +static void
  24781. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  24782. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  24783. +{
  24784. + int release_count;
  24785. +
  24786. + mutex_lock(&state->recycle_mutex);
  24787. +
  24788. + if (header) {
  24789. + int msgid = header->msgid;
  24790. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  24791. + (service && service->closing)) {
  24792. + mutex_unlock(&state->recycle_mutex);
  24793. + return;
  24794. + }
  24795. +
  24796. + /* Rewrite the message header to prevent a double
  24797. + ** release */
  24798. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  24799. + }
  24800. +
  24801. + release_count = slot_info->release_count;
  24802. + slot_info->release_count = ++release_count;
  24803. +
  24804. + if (release_count == slot_info->use_count) {
  24805. + int slot_queue_recycle;
  24806. + /* Add to the freed queue */
  24807. +
  24808. + /* A read barrier is necessary here to prevent speculative
  24809. + ** fetches of remote->slot_queue_recycle from overtaking the
  24810. + ** mutex. */
  24811. + rmb();
  24812. +
  24813. + slot_queue_recycle = state->remote->slot_queue_recycle;
  24814. + state->remote->slot_queue[slot_queue_recycle &
  24815. + VCHIQ_SLOT_QUEUE_MASK] =
  24816. + SLOT_INDEX_FROM_INFO(state, slot_info);
  24817. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  24818. + vchiq_log_info(vchiq_core_log_level,
  24819. + "%d: release_slot %d - recycle->%x",
  24820. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  24821. + state->remote->slot_queue_recycle);
  24822. +
  24823. + /* A write barrier is necessary, but remote_event_signal
  24824. + ** contains one. */
  24825. + remote_event_signal(&state->remote->recycle);
  24826. + }
  24827. +
  24828. + mutex_unlock(&state->recycle_mutex);
  24829. +}
  24830. +
  24831. +/* Called by the slot handler - don't hold the bulk mutex */
  24832. +static VCHIQ_STATUS_T
  24833. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  24834. + int retry_poll)
  24835. +{
  24836. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  24837. +
  24838. + vchiq_log_trace(vchiq_core_log_level,
  24839. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  24840. + service->state->id, service->localport,
  24841. + (queue == &service->bulk_tx) ? 't' : 'r',
  24842. + queue->process, queue->remote_notify, queue->remove);
  24843. +
  24844. + if (service->state->is_master) {
  24845. + while (queue->remote_notify != queue->process) {
  24846. + VCHIQ_BULK_T *bulk =
  24847. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  24848. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  24849. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  24850. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  24851. + service->remoteport);
  24852. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  24853. + /* Only reply to non-dummy bulk requests */
  24854. + if (bulk->remote_data) {
  24855. + status = queue_message(service->state, NULL,
  24856. + msgid, &element, 1, 4, 0);
  24857. + if (status != VCHIQ_SUCCESS)
  24858. + break;
  24859. + }
  24860. + queue->remote_notify++;
  24861. + }
  24862. + } else {
  24863. + queue->remote_notify = queue->process;
  24864. + }
  24865. +
  24866. + if (status == VCHIQ_SUCCESS) {
  24867. + while (queue->remove != queue->remote_notify) {
  24868. + VCHIQ_BULK_T *bulk =
  24869. + &queue->bulks[BULK_INDEX(queue->remove)];
  24870. +
  24871. + /* Only generate callbacks for non-dummy bulk
  24872. + ** requests, and non-terminated services */
  24873. + if (bulk->data && service->instance) {
  24874. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  24875. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  24876. + VCHIQ_SERVICE_STATS_INC(service,
  24877. + bulk_tx_count);
  24878. + VCHIQ_SERVICE_STATS_ADD(service,
  24879. + bulk_tx_bytes,
  24880. + bulk->actual);
  24881. + } else {
  24882. + VCHIQ_SERVICE_STATS_INC(service,
  24883. + bulk_rx_count);
  24884. + VCHIQ_SERVICE_STATS_ADD(service,
  24885. + bulk_rx_bytes,
  24886. + bulk->actual);
  24887. + }
  24888. + } else {
  24889. + VCHIQ_SERVICE_STATS_INC(service,
  24890. + bulk_aborted_count);
  24891. + }
  24892. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  24893. + struct bulk_waiter *waiter;
  24894. + spin_lock(&bulk_waiter_spinlock);
  24895. + waiter = bulk->userdata;
  24896. + if (waiter) {
  24897. + waiter->actual = bulk->actual;
  24898. + up(&waiter->event);
  24899. + }
  24900. + spin_unlock(&bulk_waiter_spinlock);
  24901. + } else if (bulk->mode ==
  24902. + VCHIQ_BULK_MODE_CALLBACK) {
  24903. + VCHIQ_REASON_T reason = (bulk->dir ==
  24904. + VCHIQ_BULK_TRANSMIT) ?
  24905. + ((bulk->actual ==
  24906. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  24907. + VCHIQ_BULK_TRANSMIT_ABORTED :
  24908. + VCHIQ_BULK_TRANSMIT_DONE) :
  24909. + ((bulk->actual ==
  24910. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  24911. + VCHIQ_BULK_RECEIVE_ABORTED :
  24912. + VCHIQ_BULK_RECEIVE_DONE);
  24913. + status = make_service_callback(service,
  24914. + reason, NULL, bulk->userdata);
  24915. + if (status == VCHIQ_RETRY)
  24916. + break;
  24917. + }
  24918. + }
  24919. +
  24920. + queue->remove++;
  24921. + up(&service->bulk_remove_event);
  24922. + }
  24923. + if (!retry_poll)
  24924. + status = VCHIQ_SUCCESS;
  24925. + }
  24926. +
  24927. + if (status == VCHIQ_RETRY)
  24928. + request_poll(service->state, service,
  24929. + (queue == &service->bulk_tx) ?
  24930. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  24931. +
  24932. + return status;
  24933. +}
  24934. +
  24935. +/* Called by the slot handler thread */
  24936. +static void
  24937. +poll_services(VCHIQ_STATE_T *state)
  24938. +{
  24939. + int group, i;
  24940. +
  24941. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  24942. + uint32_t flags;
  24943. + flags = atomic_xchg(&state->poll_services[group], 0);
  24944. + for (i = 0; flags; i++) {
  24945. + if (flags & (1 << i)) {
  24946. + VCHIQ_SERVICE_T *service =
  24947. + find_service_by_port(state,
  24948. + (group<<5) + i);
  24949. + uint32_t service_flags;
  24950. + flags &= ~(1 << i);
  24951. + if (!service)
  24952. + continue;
  24953. + service_flags =
  24954. + atomic_xchg(&service->poll_flags, 0);
  24955. + if (service_flags &
  24956. + (1 << VCHIQ_POLL_REMOVE)) {
  24957. + vchiq_log_info(vchiq_core_log_level,
  24958. + "%d: ps - remove %d<->%d",
  24959. + state->id, service->localport,
  24960. + service->remoteport);
  24961. +
  24962. + /* Make it look like a client, because
  24963. + it must be removed and not left in
  24964. + the LISTENING state. */
  24965. + service->public_fourcc =
  24966. + VCHIQ_FOURCC_INVALID;
  24967. +
  24968. + if (vchiq_close_service_internal(
  24969. + service, 0/*!close_recvd*/) !=
  24970. + VCHIQ_SUCCESS)
  24971. + request_poll(state, service,
  24972. + VCHIQ_POLL_REMOVE);
  24973. + } else if (service_flags &
  24974. + (1 << VCHIQ_POLL_TERMINATE)) {
  24975. + vchiq_log_info(vchiq_core_log_level,
  24976. + "%d: ps - terminate %d<->%d",
  24977. + state->id, service->localport,
  24978. + service->remoteport);
  24979. + if (vchiq_close_service_internal(
  24980. + service, 0/*!close_recvd*/) !=
  24981. + VCHIQ_SUCCESS)
  24982. + request_poll(state, service,
  24983. + VCHIQ_POLL_TERMINATE);
  24984. + }
  24985. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  24986. + notify_bulks(service,
  24987. + &service->bulk_tx,
  24988. + 1/*retry_poll*/);
  24989. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  24990. + notify_bulks(service,
  24991. + &service->bulk_rx,
  24992. + 1/*retry_poll*/);
  24993. + unlock_service(service);
  24994. + }
  24995. + }
  24996. + }
  24997. +}
  24998. +
  24999. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  25000. +static int
  25001. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  25002. +{
  25003. + VCHIQ_STATE_T *state = service->state;
  25004. + int resolved = 0;
  25005. + int rc;
  25006. +
  25007. + while ((queue->process != queue->local_insert) &&
  25008. + (queue->process != queue->remote_insert)) {
  25009. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  25010. +
  25011. + vchiq_log_trace(vchiq_core_log_level,
  25012. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  25013. + state->id, service->localport,
  25014. + (queue == &service->bulk_tx) ? 't' : 'r',
  25015. + queue->local_insert, queue->remote_insert,
  25016. + queue->process);
  25017. +
  25018. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  25019. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  25020. +
  25021. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  25022. + if (rc != 0)
  25023. + break;
  25024. +
  25025. + vchiq_transfer_bulk(bulk);
  25026. + mutex_unlock(&state->bulk_transfer_mutex);
  25027. +
  25028. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  25029. + const char *header = (queue == &service->bulk_tx) ?
  25030. + "Send Bulk to" : "Recv Bulk from";
  25031. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  25032. + vchiq_log_info(vchiq_core_msg_log_level,
  25033. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  25034. + header,
  25035. + VCHIQ_FOURCC_AS_4CHARS(
  25036. + service->base.fourcc),
  25037. + service->remoteport,
  25038. + bulk->size,
  25039. + (unsigned int)bulk->data,
  25040. + (unsigned int)bulk->remote_data);
  25041. + else
  25042. + vchiq_log_info(vchiq_core_msg_log_level,
  25043. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  25044. + " rx len:%d %x<->%x",
  25045. + header,
  25046. + VCHIQ_FOURCC_AS_4CHARS(
  25047. + service->base.fourcc),
  25048. + service->remoteport,
  25049. + bulk->size,
  25050. + bulk->remote_size,
  25051. + (unsigned int)bulk->data,
  25052. + (unsigned int)bulk->remote_data);
  25053. + }
  25054. +
  25055. + vchiq_complete_bulk(bulk);
  25056. + queue->process++;
  25057. + resolved++;
  25058. + }
  25059. + return resolved;
  25060. +}
  25061. +
  25062. +/* Called with the bulk_mutex held */
  25063. +static void
  25064. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  25065. +{
  25066. + int is_tx = (queue == &service->bulk_tx);
  25067. + vchiq_log_trace(vchiq_core_log_level,
  25068. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  25069. + service->state->id, service->localport, is_tx ? 't' : 'r',
  25070. + queue->local_insert, queue->remote_insert, queue->process);
  25071. +
  25072. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  25073. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  25074. +
  25075. + while ((queue->process != queue->local_insert) ||
  25076. + (queue->process != queue->remote_insert)) {
  25077. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  25078. +
  25079. + if (queue->process == queue->remote_insert) {
  25080. + /* fabricate a matching dummy bulk */
  25081. + bulk->remote_data = NULL;
  25082. + bulk->remote_size = 0;
  25083. + queue->remote_insert++;
  25084. + }
  25085. +
  25086. + if (queue->process != queue->local_insert) {
  25087. + vchiq_complete_bulk(bulk);
  25088. +
  25089. + vchiq_log_info(vchiq_core_msg_log_level,
  25090. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  25091. + "rx len:%d",
  25092. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  25093. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  25094. + service->remoteport,
  25095. + bulk->size,
  25096. + bulk->remote_size);
  25097. + } else {
  25098. + /* fabricate a matching dummy bulk */
  25099. + bulk->data = NULL;
  25100. + bulk->size = 0;
  25101. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  25102. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  25103. + VCHIQ_BULK_RECEIVE;
  25104. + queue->local_insert++;
  25105. + }
  25106. +
  25107. + queue->process++;
  25108. + }
  25109. +}
  25110. +
  25111. +/* Called from the slot handler thread */
  25112. +static void
  25113. +pause_bulks(VCHIQ_STATE_T *state)
  25114. +{
  25115. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  25116. + WARN_ON_ONCE(1);
  25117. + atomic_set(&pause_bulks_count, 1);
  25118. + return;
  25119. + }
  25120. +
  25121. + /* Block bulk transfers from all services */
  25122. + mutex_lock(&state->bulk_transfer_mutex);
  25123. +}
  25124. +
  25125. +/* Called from the slot handler thread */
  25126. +static void
  25127. +resume_bulks(VCHIQ_STATE_T *state)
  25128. +{
  25129. + int i;
  25130. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  25131. + WARN_ON_ONCE(1);
  25132. + atomic_set(&pause_bulks_count, 0);
  25133. + return;
  25134. + }
  25135. +
  25136. + /* Allow bulk transfers from all services */
  25137. + mutex_unlock(&state->bulk_transfer_mutex);
  25138. +
  25139. + if (state->deferred_bulks == 0)
  25140. + return;
  25141. +
  25142. + /* Deal with any bulks which had to be deferred due to being in
  25143. + * paused state. Don't try to match up to number of deferred bulks
  25144. + * in case we've had something come and close the service in the
  25145. + * interim - just process all bulk queues for all services */
  25146. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  25147. + __func__, state->deferred_bulks);
  25148. +
  25149. + for (i = 0; i < state->unused_service; i++) {
  25150. + VCHIQ_SERVICE_T *service = state->services[i];
  25151. + int resolved_rx = 0;
  25152. + int resolved_tx = 0;
  25153. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  25154. + continue;
  25155. +
  25156. + mutex_lock(&service->bulk_mutex);
  25157. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  25158. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  25159. + mutex_unlock(&service->bulk_mutex);
  25160. + if (resolved_rx)
  25161. + notify_bulks(service, &service->bulk_rx, 1);
  25162. + if (resolved_tx)
  25163. + notify_bulks(service, &service->bulk_tx, 1);
  25164. + }
  25165. + state->deferred_bulks = 0;
  25166. +}
  25167. +
  25168. +static int
  25169. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  25170. +{
  25171. + VCHIQ_SERVICE_T *service = NULL;
  25172. + int msgid, size;
  25173. + int type;
  25174. + unsigned int localport, remoteport;
  25175. +
  25176. + msgid = header->msgid;
  25177. + size = header->size;
  25178. + type = VCHIQ_MSG_TYPE(msgid);
  25179. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25180. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25181. + if (size >= sizeof(struct vchiq_open_payload)) {
  25182. + const struct vchiq_open_payload *payload =
  25183. + (struct vchiq_open_payload *)header->data;
  25184. + unsigned int fourcc;
  25185. +
  25186. + fourcc = payload->fourcc;
  25187. + vchiq_log_info(vchiq_core_log_level,
  25188. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  25189. + state->id, (unsigned int)header,
  25190. + localport,
  25191. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  25192. +
  25193. + service = get_listening_service(state, fourcc);
  25194. +
  25195. + if (service) {
  25196. + /* A matching service exists */
  25197. + short version = payload->version;
  25198. + short version_min = payload->version_min;
  25199. + if ((service->version < version_min) ||
  25200. + (version < service->version_min)) {
  25201. + /* Version mismatch */
  25202. + vchiq_loud_error_header();
  25203. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  25204. + "version mismatch - local (%d, min %d)"
  25205. + " vs. remote (%d, min %d)",
  25206. + state->id, service->localport,
  25207. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  25208. + service->version, service->version_min,
  25209. + version, version_min);
  25210. + vchiq_loud_error_footer();
  25211. + unlock_service(service);
  25212. + service = NULL;
  25213. + goto fail_open;
  25214. + }
  25215. + service->peer_version = version;
  25216. +
  25217. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  25218. + struct vchiq_openack_payload ack_payload = {
  25219. + service->version
  25220. + };
  25221. + VCHIQ_ELEMENT_T body = {
  25222. + &ack_payload,
  25223. + sizeof(ack_payload)
  25224. + };
  25225. +
  25226. + /* Acknowledge the OPEN */
  25227. + if (service->sync) {
  25228. + if (queue_message_sync(state, NULL,
  25229. + VCHIQ_MAKE_MSG(
  25230. + VCHIQ_MSG_OPENACK,
  25231. + service->localport,
  25232. + remoteport),
  25233. + &body, 1, sizeof(ack_payload),
  25234. + 0) == VCHIQ_RETRY)
  25235. + goto bail_not_ready;
  25236. + } else {
  25237. + if (queue_message(state, NULL,
  25238. + VCHIQ_MAKE_MSG(
  25239. + VCHIQ_MSG_OPENACK,
  25240. + service->localport,
  25241. + remoteport),
  25242. + &body, 1, sizeof(ack_payload),
  25243. + 0) == VCHIQ_RETRY)
  25244. + goto bail_not_ready;
  25245. + }
  25246. +
  25247. + /* The service is now open */
  25248. + vchiq_set_service_state(service,
  25249. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  25250. + : VCHIQ_SRVSTATE_OPEN);
  25251. + }
  25252. +
  25253. + service->remoteport = remoteport;
  25254. + service->client_id = ((int *)header->data)[1];
  25255. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  25256. + NULL, NULL) == VCHIQ_RETRY) {
  25257. + /* Bail out if not ready */
  25258. + service->remoteport = VCHIQ_PORT_FREE;
  25259. + goto bail_not_ready;
  25260. + }
  25261. +
  25262. + /* Success - the message has been dealt with */
  25263. + unlock_service(service);
  25264. + return 1;
  25265. + }
  25266. + }
  25267. +
  25268. +fail_open:
  25269. + /* No available service, or an invalid request - send a CLOSE */
  25270. + if (queue_message(state, NULL,
  25271. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  25272. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  25273. + goto bail_not_ready;
  25274. +
  25275. + return 1;
  25276. +
  25277. +bail_not_ready:
  25278. + if (service)
  25279. + unlock_service(service);
  25280. +
  25281. + return 0;
  25282. +}
  25283. +
  25284. +/* Called by the slot handler thread */
  25285. +static void
  25286. +parse_rx_slots(VCHIQ_STATE_T *state)
  25287. +{
  25288. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  25289. + VCHIQ_SERVICE_T *service = NULL;
  25290. + int tx_pos;
  25291. + DEBUG_INITIALISE(state->local)
  25292. +
  25293. + tx_pos = remote->tx_pos;
  25294. +
  25295. + while (state->rx_pos != tx_pos) {
  25296. + VCHIQ_HEADER_T *header;
  25297. + int msgid, size;
  25298. + int type;
  25299. + unsigned int localport, remoteport;
  25300. +
  25301. + DEBUG_TRACE(PARSE_LINE);
  25302. + if (!state->rx_data) {
  25303. + int rx_index;
  25304. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  25305. + rx_index = remote->slot_queue[
  25306. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  25307. + VCHIQ_SLOT_QUEUE_MASK];
  25308. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  25309. + rx_index);
  25310. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  25311. +
  25312. + /* Initialise use_count to one, and increment
  25313. + ** release_count at the end of the slot to avoid
  25314. + ** releasing the slot prematurely. */
  25315. + state->rx_info->use_count = 1;
  25316. + state->rx_info->release_count = 0;
  25317. + }
  25318. +
  25319. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  25320. + (state->rx_pos & VCHIQ_SLOT_MASK));
  25321. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  25322. + msgid = header->msgid;
  25323. + DEBUG_VALUE(PARSE_MSGID, msgid);
  25324. + size = header->size;
  25325. + type = VCHIQ_MSG_TYPE(msgid);
  25326. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25327. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25328. +
  25329. + if (type != VCHIQ_MSG_DATA)
  25330. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  25331. +
  25332. + switch (type) {
  25333. + case VCHIQ_MSG_OPENACK:
  25334. + case VCHIQ_MSG_CLOSE:
  25335. + case VCHIQ_MSG_DATA:
  25336. + case VCHIQ_MSG_BULK_RX:
  25337. + case VCHIQ_MSG_BULK_TX:
  25338. + case VCHIQ_MSG_BULK_RX_DONE:
  25339. + case VCHIQ_MSG_BULK_TX_DONE:
  25340. + service = find_service_by_port(state, localport);
  25341. + if ((!service || service->remoteport != remoteport) &&
  25342. + (localport == 0) &&
  25343. + (type == VCHIQ_MSG_CLOSE)) {
  25344. + /* This could be a CLOSE from a client which
  25345. + hadn't yet received the OPENACK - look for
  25346. + the connected service */
  25347. + if (service)
  25348. + unlock_service(service);
  25349. + service = get_connected_service(state,
  25350. + remoteport);
  25351. + if (service)
  25352. + vchiq_log_warning(vchiq_core_log_level,
  25353. + "%d: prs %s@%x (%d->%d) - "
  25354. + "found connected service %d",
  25355. + state->id, msg_type_str(type),
  25356. + (unsigned int)header,
  25357. + remoteport, localport,
  25358. + service->localport);
  25359. + }
  25360. +
  25361. + if (!service) {
  25362. + vchiq_log_error(vchiq_core_log_level,
  25363. + "%d: prs %s@%x (%d->%d) - "
  25364. + "invalid/closed service %d",
  25365. + state->id, msg_type_str(type),
  25366. + (unsigned int)header,
  25367. + remoteport, localport, localport);
  25368. + goto skip_message;
  25369. + }
  25370. + break;
  25371. + default:
  25372. + break;
  25373. + }
  25374. +
  25375. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  25376. + int svc_fourcc;
  25377. +
  25378. + svc_fourcc = service
  25379. + ? service->base.fourcc
  25380. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  25381. + vchiq_log_info(vchiq_core_msg_log_level,
  25382. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  25383. + "len:%d",
  25384. + msg_type_str(type), type,
  25385. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  25386. + remoteport, localport, size);
  25387. + if (size > 0)
  25388. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  25389. + min(64, size));
  25390. + }
  25391. +
  25392. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  25393. + > VCHIQ_SLOT_SIZE) {
  25394. + vchiq_log_error(vchiq_core_log_level,
  25395. + "header %x (msgid %x) - size %x too big for "
  25396. + "slot",
  25397. + (unsigned int)header, (unsigned int)msgid,
  25398. + (unsigned int)size);
  25399. + WARN(1, "oversized for slot\n");
  25400. + }
  25401. +
  25402. + switch (type) {
  25403. + case VCHIQ_MSG_OPEN:
  25404. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  25405. + if (!parse_open(state, header))
  25406. + goto bail_not_ready;
  25407. + break;
  25408. + case VCHIQ_MSG_OPENACK:
  25409. + if (size >= sizeof(struct vchiq_openack_payload)) {
  25410. + const struct vchiq_openack_payload *payload =
  25411. + (struct vchiq_openack_payload *)
  25412. + header->data;
  25413. + service->peer_version = payload->version;
  25414. + }
  25415. + vchiq_log_info(vchiq_core_log_level,
  25416. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  25417. + state->id, (unsigned int)header, size,
  25418. + remoteport, localport, service->peer_version);
  25419. + if (service->srvstate ==
  25420. + VCHIQ_SRVSTATE_OPENING) {
  25421. + service->remoteport = remoteport;
  25422. + vchiq_set_service_state(service,
  25423. + VCHIQ_SRVSTATE_OPEN);
  25424. + up(&service->remove_event);
  25425. + } else
  25426. + vchiq_log_error(vchiq_core_log_level,
  25427. + "OPENACK received in state %s",
  25428. + srvstate_names[service->srvstate]);
  25429. + break;
  25430. + case VCHIQ_MSG_CLOSE:
  25431. + WARN_ON(size != 0); /* There should be no data */
  25432. +
  25433. + vchiq_log_info(vchiq_core_log_level,
  25434. + "%d: prs CLOSE@%x (%d->%d)",
  25435. + state->id, (unsigned int)header,
  25436. + remoteport, localport);
  25437. +
  25438. + mark_service_closing_internal(service, 1);
  25439. +
  25440. + if (vchiq_close_service_internal(service,
  25441. + 1/*close_recvd*/) == VCHIQ_RETRY)
  25442. + goto bail_not_ready;
  25443. +
  25444. + vchiq_log_info(vchiq_core_log_level,
  25445. + "Close Service %c%c%c%c s:%u d:%d",
  25446. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  25447. + service->localport,
  25448. + service->remoteport);
  25449. + break;
  25450. + case VCHIQ_MSG_DATA:
  25451. + vchiq_log_trace(vchiq_core_log_level,
  25452. + "%d: prs DATA@%x,%x (%d->%d)",
  25453. + state->id, (unsigned int)header, size,
  25454. + remoteport, localport);
  25455. +
  25456. + if ((service->remoteport == remoteport)
  25457. + && (service->srvstate ==
  25458. + VCHIQ_SRVSTATE_OPEN)) {
  25459. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  25460. + claim_slot(state->rx_info);
  25461. + DEBUG_TRACE(PARSE_LINE);
  25462. + if (make_service_callback(service,
  25463. + VCHIQ_MESSAGE_AVAILABLE, header,
  25464. + NULL) == VCHIQ_RETRY) {
  25465. + DEBUG_TRACE(PARSE_LINE);
  25466. + goto bail_not_ready;
  25467. + }
  25468. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  25469. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  25470. + size);
  25471. + } else {
  25472. + VCHIQ_STATS_INC(state, error_count);
  25473. + }
  25474. + break;
  25475. + case VCHIQ_MSG_CONNECT:
  25476. + vchiq_log_info(vchiq_core_log_level,
  25477. + "%d: prs CONNECT@%x",
  25478. + state->id, (unsigned int)header);
  25479. + up(&state->connect);
  25480. + break;
  25481. + case VCHIQ_MSG_BULK_RX:
  25482. + case VCHIQ_MSG_BULK_TX: {
  25483. + VCHIQ_BULK_QUEUE_T *queue;
  25484. + WARN_ON(!state->is_master);
  25485. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  25486. + &service->bulk_tx : &service->bulk_rx;
  25487. + if ((service->remoteport == remoteport)
  25488. + && (service->srvstate ==
  25489. + VCHIQ_SRVSTATE_OPEN)) {
  25490. + VCHIQ_BULK_T *bulk;
  25491. + int resolved = 0;
  25492. +
  25493. + DEBUG_TRACE(PARSE_LINE);
  25494. + if (mutex_lock_interruptible(
  25495. + &service->bulk_mutex) != 0) {
  25496. + DEBUG_TRACE(PARSE_LINE);
  25497. + goto bail_not_ready;
  25498. + }
  25499. +
  25500. + WARN_ON(!(queue->remote_insert < queue->remove +
  25501. + VCHIQ_NUM_SERVICE_BULKS));
  25502. + bulk = &queue->bulks[
  25503. + BULK_INDEX(queue->remote_insert)];
  25504. + bulk->remote_data =
  25505. + (void *)((int *)header->data)[0];
  25506. + bulk->remote_size = ((int *)header->data)[1];
  25507. + wmb();
  25508. +
  25509. + vchiq_log_info(vchiq_core_log_level,
  25510. + "%d: prs %s@%x (%d->%d) %x@%x",
  25511. + state->id, msg_type_str(type),
  25512. + (unsigned int)header,
  25513. + remoteport, localport,
  25514. + bulk->remote_size,
  25515. + (unsigned int)bulk->remote_data);
  25516. +
  25517. + queue->remote_insert++;
  25518. +
  25519. + if (atomic_read(&pause_bulks_count)) {
  25520. + state->deferred_bulks++;
  25521. + vchiq_log_info(vchiq_core_log_level,
  25522. + "%s: deferring bulk (%d)",
  25523. + __func__,
  25524. + state->deferred_bulks);
  25525. + if (state->conn_state !=
  25526. + VCHIQ_CONNSTATE_PAUSE_SENT)
  25527. + vchiq_log_error(
  25528. + vchiq_core_log_level,
  25529. + "%s: bulks paused in "
  25530. + "unexpected state %s",
  25531. + __func__,
  25532. + conn_state_names[
  25533. + state->conn_state]);
  25534. + } else if (state->conn_state ==
  25535. + VCHIQ_CONNSTATE_CONNECTED) {
  25536. + DEBUG_TRACE(PARSE_LINE);
  25537. + resolved = resolve_bulks(service,
  25538. + queue);
  25539. + }
  25540. +
  25541. + mutex_unlock(&service->bulk_mutex);
  25542. + if (resolved)
  25543. + notify_bulks(service, queue,
  25544. + 1/*retry_poll*/);
  25545. + }
  25546. + } break;
  25547. + case VCHIQ_MSG_BULK_RX_DONE:
  25548. + case VCHIQ_MSG_BULK_TX_DONE:
  25549. + WARN_ON(state->is_master);
  25550. + if ((service->remoteport == remoteport)
  25551. + && (service->srvstate !=
  25552. + VCHIQ_SRVSTATE_FREE)) {
  25553. + VCHIQ_BULK_QUEUE_T *queue;
  25554. + VCHIQ_BULK_T *bulk;
  25555. +
  25556. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  25557. + &service->bulk_rx : &service->bulk_tx;
  25558. +
  25559. + DEBUG_TRACE(PARSE_LINE);
  25560. + if (mutex_lock_interruptible(
  25561. + &service->bulk_mutex) != 0) {
  25562. + DEBUG_TRACE(PARSE_LINE);
  25563. + goto bail_not_ready;
  25564. + }
  25565. + if ((int)(queue->remote_insert -
  25566. + queue->local_insert) >= 0) {
  25567. + vchiq_log_error(vchiq_core_log_level,
  25568. + "%d: prs %s@%x (%d->%d) "
  25569. + "unexpected (ri=%d,li=%d)",
  25570. + state->id, msg_type_str(type),
  25571. + (unsigned int)header,
  25572. + remoteport, localport,
  25573. + queue->remote_insert,
  25574. + queue->local_insert);
  25575. + mutex_unlock(&service->bulk_mutex);
  25576. + break;
  25577. + }
  25578. +
  25579. + BUG_ON(queue->process == queue->local_insert);
  25580. + BUG_ON(queue->process != queue->remote_insert);
  25581. +
  25582. + bulk = &queue->bulks[
  25583. + BULK_INDEX(queue->remote_insert)];
  25584. + bulk->actual = *(int *)header->data;
  25585. + queue->remote_insert++;
  25586. +
  25587. + vchiq_log_info(vchiq_core_log_level,
  25588. + "%d: prs %s@%x (%d->%d) %x@%x",
  25589. + state->id, msg_type_str(type),
  25590. + (unsigned int)header,
  25591. + remoteport, localport,
  25592. + bulk->actual, (unsigned int)bulk->data);
  25593. +
  25594. + vchiq_log_trace(vchiq_core_log_level,
  25595. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  25596. + state->id, localport,
  25597. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  25598. + 'r' : 't',
  25599. + queue->local_insert,
  25600. + queue->remote_insert, queue->process);
  25601. +
  25602. + DEBUG_TRACE(PARSE_LINE);
  25603. + WARN_ON(queue->process == queue->local_insert);
  25604. + vchiq_complete_bulk(bulk);
  25605. + queue->process++;
  25606. + mutex_unlock(&service->bulk_mutex);
  25607. + DEBUG_TRACE(PARSE_LINE);
  25608. + notify_bulks(service, queue, 1/*retry_poll*/);
  25609. + DEBUG_TRACE(PARSE_LINE);
  25610. + }
  25611. + break;
  25612. + case VCHIQ_MSG_PADDING:
  25613. + vchiq_log_trace(vchiq_core_log_level,
  25614. + "%d: prs PADDING@%x,%x",
  25615. + state->id, (unsigned int)header, size);
  25616. + break;
  25617. + case VCHIQ_MSG_PAUSE:
  25618. + /* If initiated, signal the application thread */
  25619. + vchiq_log_trace(vchiq_core_log_level,
  25620. + "%d: prs PAUSE@%x,%x",
  25621. + state->id, (unsigned int)header, size);
  25622. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  25623. + vchiq_log_error(vchiq_core_log_level,
  25624. + "%d: PAUSE received in state PAUSED",
  25625. + state->id);
  25626. + break;
  25627. + }
  25628. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  25629. + /* Send a PAUSE in response */
  25630. + if (queue_message(state, NULL,
  25631. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  25632. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  25633. + goto bail_not_ready;
  25634. + if (state->is_master)
  25635. + pause_bulks(state);
  25636. + }
  25637. + /* At this point slot_mutex is held */
  25638. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  25639. + vchiq_platform_paused(state);
  25640. + break;
  25641. + case VCHIQ_MSG_RESUME:
  25642. + vchiq_log_trace(vchiq_core_log_level,
  25643. + "%d: prs RESUME@%x,%x",
  25644. + state->id, (unsigned int)header, size);
  25645. + /* Release the slot mutex */
  25646. + mutex_unlock(&state->slot_mutex);
  25647. + if (state->is_master)
  25648. + resume_bulks(state);
  25649. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  25650. + vchiq_platform_resumed(state);
  25651. + break;
  25652. +
  25653. + case VCHIQ_MSG_REMOTE_USE:
  25654. + vchiq_on_remote_use(state);
  25655. + break;
  25656. + case VCHIQ_MSG_REMOTE_RELEASE:
  25657. + vchiq_on_remote_release(state);
  25658. + break;
  25659. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  25660. + vchiq_on_remote_use_active(state);
  25661. + break;
  25662. +
  25663. + default:
  25664. + vchiq_log_error(vchiq_core_log_level,
  25665. + "%d: prs invalid msgid %x@%x,%x",
  25666. + state->id, msgid, (unsigned int)header, size);
  25667. + WARN(1, "invalid message\n");
  25668. + break;
  25669. + }
  25670. +
  25671. +skip_message:
  25672. + if (service) {
  25673. + unlock_service(service);
  25674. + service = NULL;
  25675. + }
  25676. +
  25677. + state->rx_pos += calc_stride(size);
  25678. +
  25679. + DEBUG_TRACE(PARSE_LINE);
  25680. + /* Perform some housekeeping when the end of the slot is
  25681. + ** reached. */
  25682. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  25683. + /* Remove the extra reference count. */
  25684. + release_slot(state, state->rx_info, NULL, NULL);
  25685. + state->rx_data = NULL;
  25686. + }
  25687. + }
  25688. +
  25689. +bail_not_ready:
  25690. + if (service)
  25691. + unlock_service(service);
  25692. +}
  25693. +
  25694. +/* Called by the slot handler thread */
  25695. +static int
  25696. +slot_handler_func(void *v)
  25697. +{
  25698. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25699. + VCHIQ_SHARED_STATE_T *local = state->local;
  25700. + DEBUG_INITIALISE(local)
  25701. +
  25702. + while (1) {
  25703. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  25704. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  25705. + remote_event_wait(&local->trigger);
  25706. +
  25707. + rmb();
  25708. +
  25709. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  25710. + if (state->poll_needed) {
  25711. + /* Check if we need to suspend - may change our
  25712. + * conn_state */
  25713. + vchiq_platform_check_suspend(state);
  25714. +
  25715. + state->poll_needed = 0;
  25716. +
  25717. + /* Handle service polling and other rare conditions here
  25718. + ** out of the mainline code */
  25719. + switch (state->conn_state) {
  25720. + case VCHIQ_CONNSTATE_CONNECTED:
  25721. + /* Poll the services as requested */
  25722. + poll_services(state);
  25723. + break;
  25724. +
  25725. + case VCHIQ_CONNSTATE_PAUSING:
  25726. + if (state->is_master)
  25727. + pause_bulks(state);
  25728. + if (queue_message(state, NULL,
  25729. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  25730. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  25731. + vchiq_set_conn_state(state,
  25732. + VCHIQ_CONNSTATE_PAUSE_SENT);
  25733. + } else {
  25734. + if (state->is_master)
  25735. + resume_bulks(state);
  25736. + /* Retry later */
  25737. + state->poll_needed = 1;
  25738. + }
  25739. + break;
  25740. +
  25741. + case VCHIQ_CONNSTATE_PAUSED:
  25742. + vchiq_platform_resume(state);
  25743. + break;
  25744. +
  25745. + case VCHIQ_CONNSTATE_RESUMING:
  25746. + if (queue_message(state, NULL,
  25747. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  25748. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  25749. + if (state->is_master)
  25750. + resume_bulks(state);
  25751. + vchiq_set_conn_state(state,
  25752. + VCHIQ_CONNSTATE_CONNECTED);
  25753. + vchiq_platform_resumed(state);
  25754. + } else {
  25755. + /* This should really be impossible,
  25756. + ** since the PAUSE should have flushed
  25757. + ** through outstanding messages. */
  25758. + vchiq_log_error(vchiq_core_log_level,
  25759. + "Failed to send RESUME "
  25760. + "message");
  25761. + BUG();
  25762. + }
  25763. + break;
  25764. +
  25765. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  25766. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  25767. + vchiq_platform_handle_timeout(state);
  25768. + break;
  25769. + default:
  25770. + break;
  25771. + }
  25772. +
  25773. +
  25774. + }
  25775. +
  25776. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  25777. + parse_rx_slots(state);
  25778. + }
  25779. + return 0;
  25780. +}
  25781. +
  25782. +
  25783. +/* Called by the recycle thread */
  25784. +static int
  25785. +recycle_func(void *v)
  25786. +{
  25787. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25788. + VCHIQ_SHARED_STATE_T *local = state->local;
  25789. +
  25790. + while (1) {
  25791. + remote_event_wait(&local->recycle);
  25792. +
  25793. + process_free_queue(state);
  25794. + }
  25795. + return 0;
  25796. +}
  25797. +
  25798. +
  25799. +/* Called by the sync thread */
  25800. +static int
  25801. +sync_func(void *v)
  25802. +{
  25803. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25804. + VCHIQ_SHARED_STATE_T *local = state->local;
  25805. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  25806. + state->remote->slot_sync);
  25807. +
  25808. + while (1) {
  25809. + VCHIQ_SERVICE_T *service;
  25810. + int msgid, size;
  25811. + int type;
  25812. + unsigned int localport, remoteport;
  25813. +
  25814. + remote_event_wait(&local->sync_trigger);
  25815. +
  25816. + rmb();
  25817. +
  25818. + msgid = header->msgid;
  25819. + size = header->size;
  25820. + type = VCHIQ_MSG_TYPE(msgid);
  25821. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25822. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25823. +
  25824. + service = find_service_by_port(state, localport);
  25825. +
  25826. + if (!service) {
  25827. + vchiq_log_error(vchiq_sync_log_level,
  25828. + "%d: sf %s@%x (%d->%d) - "
  25829. + "invalid/closed service %d",
  25830. + state->id, msg_type_str(type),
  25831. + (unsigned int)header,
  25832. + remoteport, localport, localport);
  25833. + release_message_sync(state, header);
  25834. + continue;
  25835. + }
  25836. +
  25837. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  25838. + int svc_fourcc;
  25839. +
  25840. + svc_fourcc = service
  25841. + ? service->base.fourcc
  25842. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  25843. + vchiq_log_trace(vchiq_sync_log_level,
  25844. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  25845. + msg_type_str(type),
  25846. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  25847. + remoteport, localport, size);
  25848. + if (size > 0)
  25849. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  25850. + min(64, size));
  25851. + }
  25852. +
  25853. + switch (type) {
  25854. + case VCHIQ_MSG_OPENACK:
  25855. + if (size >= sizeof(struct vchiq_openack_payload)) {
  25856. + const struct vchiq_openack_payload *payload =
  25857. + (struct vchiq_openack_payload *)
  25858. + header->data;
  25859. + service->peer_version = payload->version;
  25860. + }
  25861. + vchiq_log_info(vchiq_sync_log_level,
  25862. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  25863. + state->id, (unsigned int)header, size,
  25864. + remoteport, localport, service->peer_version);
  25865. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  25866. + service->remoteport = remoteport;
  25867. + vchiq_set_service_state(service,
  25868. + VCHIQ_SRVSTATE_OPENSYNC);
  25869. + up(&service->remove_event);
  25870. + }
  25871. + release_message_sync(state, header);
  25872. + break;
  25873. +
  25874. + case VCHIQ_MSG_DATA:
  25875. + vchiq_log_trace(vchiq_sync_log_level,
  25876. + "%d: sf DATA@%x,%x (%d->%d)",
  25877. + state->id, (unsigned int)header, size,
  25878. + remoteport, localport);
  25879. +
  25880. + if ((service->remoteport == remoteport) &&
  25881. + (service->srvstate ==
  25882. + VCHIQ_SRVSTATE_OPENSYNC)) {
  25883. + if (make_service_callback(service,
  25884. + VCHIQ_MESSAGE_AVAILABLE, header,
  25885. + NULL) == VCHIQ_RETRY)
  25886. + vchiq_log_error(vchiq_sync_log_level,
  25887. + "synchronous callback to "
  25888. + "service %d returns "
  25889. + "VCHIQ_RETRY",
  25890. + localport);
  25891. + }
  25892. + break;
  25893. +
  25894. + default:
  25895. + vchiq_log_error(vchiq_sync_log_level,
  25896. + "%d: sf unexpected msgid %x@%x,%x",
  25897. + state->id, msgid, (unsigned int)header, size);
  25898. + release_message_sync(state, header);
  25899. + break;
  25900. + }
  25901. +
  25902. + unlock_service(service);
  25903. + }
  25904. +
  25905. + return 0;
  25906. +}
  25907. +
  25908. +
  25909. +static void
  25910. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  25911. +{
  25912. + queue->local_insert = 0;
  25913. + queue->remote_insert = 0;
  25914. + queue->process = 0;
  25915. + queue->remote_notify = 0;
  25916. + queue->remove = 0;
  25917. +}
  25918. +
  25919. +
  25920. +inline const char *
  25921. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  25922. +{
  25923. + return conn_state_names[conn_state];
  25924. +}
  25925. +
  25926. +
  25927. +VCHIQ_SLOT_ZERO_T *
  25928. +vchiq_init_slots(void *mem_base, int mem_size)
  25929. +{
  25930. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  25931. + VCHIQ_SLOT_ZERO_T *slot_zero =
  25932. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  25933. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  25934. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  25935. +
  25936. + /* Ensure there is enough memory to run an absolutely minimum system */
  25937. + num_slots -= first_data_slot;
  25938. +
  25939. + if (num_slots < 4) {
  25940. + vchiq_log_error(vchiq_core_log_level,
  25941. + "vchiq_init_slots - insufficient memory %x bytes",
  25942. + mem_size);
  25943. + return NULL;
  25944. + }
  25945. +
  25946. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  25947. +
  25948. + slot_zero->magic = VCHIQ_MAGIC;
  25949. + slot_zero->version = VCHIQ_VERSION;
  25950. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  25951. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  25952. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  25953. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  25954. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  25955. +
  25956. + slot_zero->master.slot_sync = first_data_slot;
  25957. + slot_zero->master.slot_first = first_data_slot + 1;
  25958. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  25959. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  25960. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  25961. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  25962. +
  25963. + return slot_zero;
  25964. +}
  25965. +
  25966. +VCHIQ_STATUS_T
  25967. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  25968. + int is_master)
  25969. +{
  25970. + VCHIQ_SHARED_STATE_T *local;
  25971. + VCHIQ_SHARED_STATE_T *remote;
  25972. + VCHIQ_STATUS_T status;
  25973. + char threadname[10];
  25974. + static int id;
  25975. + int i;
  25976. +
  25977. + vchiq_log_warning(vchiq_core_log_level,
  25978. + "%s: slot_zero = 0x%08lx, is_master = %d",
  25979. + __func__, (unsigned long)slot_zero, is_master);
  25980. +
  25981. + /* Check the input configuration */
  25982. +
  25983. + if (slot_zero->magic != VCHIQ_MAGIC) {
  25984. + vchiq_loud_error_header();
  25985. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  25986. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  25987. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  25988. + vchiq_loud_error_footer();
  25989. + return VCHIQ_ERROR;
  25990. + }
  25991. +
  25992. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  25993. + vchiq_loud_error_header();
  25994. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  25995. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  25996. + "(minimum %d)",
  25997. + (unsigned int)slot_zero, slot_zero->version,
  25998. + VCHIQ_VERSION_MIN);
  25999. + vchiq_loud_error("Restart with a newer VideoCore image.");
  26000. + vchiq_loud_error_footer();
  26001. + return VCHIQ_ERROR;
  26002. + }
  26003. +
  26004. + if (VCHIQ_VERSION < slot_zero->version_min) {
  26005. + vchiq_loud_error_header();
  26006. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  26007. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  26008. + "minimum %d)",
  26009. + (unsigned int)slot_zero, VCHIQ_VERSION,
  26010. + slot_zero->version_min);
  26011. + vchiq_loud_error("Restart with a newer kernel.");
  26012. + vchiq_loud_error_footer();
  26013. + return VCHIQ_ERROR;
  26014. + }
  26015. +
  26016. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  26017. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  26018. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  26019. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  26020. + vchiq_loud_error_header();
  26021. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  26022. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  26023. + "(expected %x)",
  26024. + (unsigned int)slot_zero,
  26025. + slot_zero->slot_zero_size,
  26026. + sizeof(VCHIQ_SLOT_ZERO_T));
  26027. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  26028. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  26029. + "(expected %d",
  26030. + (unsigned int)slot_zero, slot_zero->slot_size,
  26031. + VCHIQ_SLOT_SIZE);
  26032. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  26033. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  26034. + "(expected %d)",
  26035. + (unsigned int)slot_zero, slot_zero->max_slots,
  26036. + VCHIQ_MAX_SLOTS);
  26037. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  26038. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  26039. + "(expected %d)",
  26040. + (unsigned int)slot_zero,
  26041. + slot_zero->max_slots_per_side,
  26042. + VCHIQ_MAX_SLOTS_PER_SIDE);
  26043. + vchiq_loud_error_footer();
  26044. + return VCHIQ_ERROR;
  26045. + }
  26046. +
  26047. + if (is_master) {
  26048. + local = &slot_zero->master;
  26049. + remote = &slot_zero->slave;
  26050. + } else {
  26051. + local = &slot_zero->slave;
  26052. + remote = &slot_zero->master;
  26053. + }
  26054. +
  26055. + if (local->initialised) {
  26056. + vchiq_loud_error_header();
  26057. + if (remote->initialised)
  26058. + vchiq_loud_error("local state has already been "
  26059. + "initialised");
  26060. + else
  26061. + vchiq_loud_error("master/slave mismatch - two %ss",
  26062. + is_master ? "master" : "slave");
  26063. + vchiq_loud_error_footer();
  26064. + return VCHIQ_ERROR;
  26065. + }
  26066. +
  26067. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  26068. +
  26069. + state->id = id++;
  26070. + state->is_master = is_master;
  26071. +
  26072. + /*
  26073. + initialize shared state pointers
  26074. + */
  26075. +
  26076. + state->local = local;
  26077. + state->remote = remote;
  26078. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  26079. +
  26080. + /*
  26081. + initialize events and mutexes
  26082. + */
  26083. +
  26084. + sema_init(&state->connect, 0);
  26085. + mutex_init(&state->mutex);
  26086. + sema_init(&state->trigger_event, 0);
  26087. + sema_init(&state->recycle_event, 0);
  26088. + sema_init(&state->sync_trigger_event, 0);
  26089. + sema_init(&state->sync_release_event, 0);
  26090. +
  26091. + mutex_init(&state->slot_mutex);
  26092. + mutex_init(&state->recycle_mutex);
  26093. + mutex_init(&state->sync_mutex);
  26094. + mutex_init(&state->bulk_transfer_mutex);
  26095. +
  26096. + sema_init(&state->slot_available_event, 0);
  26097. + sema_init(&state->slot_remove_event, 0);
  26098. + sema_init(&state->data_quota_event, 0);
  26099. +
  26100. + state->slot_queue_available = 0;
  26101. +
  26102. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  26103. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  26104. + &state->service_quotas[i];
  26105. + sema_init(&service_quota->quota_event, 0);
  26106. + }
  26107. +
  26108. + for (i = local->slot_first; i <= local->slot_last; i++) {
  26109. + local->slot_queue[state->slot_queue_available++] = i;
  26110. + up(&state->slot_available_event);
  26111. + }
  26112. +
  26113. + state->default_slot_quota = state->slot_queue_available/2;
  26114. + state->default_message_quota =
  26115. + min((unsigned short)(state->default_slot_quota * 256),
  26116. + (unsigned short)~0);
  26117. +
  26118. + state->previous_data_index = -1;
  26119. + state->data_use_count = 0;
  26120. + state->data_quota = state->slot_queue_available - 1;
  26121. +
  26122. + local->trigger.event = &state->trigger_event;
  26123. + remote_event_create(&local->trigger);
  26124. + local->tx_pos = 0;
  26125. +
  26126. + local->recycle.event = &state->recycle_event;
  26127. + remote_event_create(&local->recycle);
  26128. + local->slot_queue_recycle = state->slot_queue_available;
  26129. +
  26130. + local->sync_trigger.event = &state->sync_trigger_event;
  26131. + remote_event_create(&local->sync_trigger);
  26132. +
  26133. + local->sync_release.event = &state->sync_release_event;
  26134. + remote_event_create(&local->sync_release);
  26135. +
  26136. + /* At start-of-day, the slot is empty and available */
  26137. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  26138. + = VCHIQ_MSGID_PADDING;
  26139. + remote_event_signal_local(&local->sync_release);
  26140. +
  26141. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  26142. +
  26143. + status = vchiq_platform_init_state(state);
  26144. +
  26145. + /*
  26146. + bring up slot handler thread
  26147. + */
  26148. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  26149. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  26150. + (void *)state,
  26151. + threadname);
  26152. +
  26153. + if (state->slot_handler_thread == NULL) {
  26154. + vchiq_loud_error_header();
  26155. + vchiq_loud_error("couldn't create thread %s", threadname);
  26156. + vchiq_loud_error_footer();
  26157. + return VCHIQ_ERROR;
  26158. + }
  26159. + set_user_nice(state->slot_handler_thread, -19);
  26160. + wake_up_process(state->slot_handler_thread);
  26161. +
  26162. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  26163. + state->recycle_thread = kthread_create(&recycle_func,
  26164. + (void *)state,
  26165. + threadname);
  26166. + if (state->recycle_thread == NULL) {
  26167. + vchiq_loud_error_header();
  26168. + vchiq_loud_error("couldn't create thread %s", threadname);
  26169. + vchiq_loud_error_footer();
  26170. + return VCHIQ_ERROR;
  26171. + }
  26172. + set_user_nice(state->recycle_thread, -19);
  26173. + wake_up_process(state->recycle_thread);
  26174. +
  26175. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  26176. + state->sync_thread = kthread_create(&sync_func,
  26177. + (void *)state,
  26178. + threadname);
  26179. + if (state->sync_thread == NULL) {
  26180. + vchiq_loud_error_header();
  26181. + vchiq_loud_error("couldn't create thread %s", threadname);
  26182. + vchiq_loud_error_footer();
  26183. + return VCHIQ_ERROR;
  26184. + }
  26185. + set_user_nice(state->sync_thread, -20);
  26186. + wake_up_process(state->sync_thread);
  26187. +
  26188. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  26189. + vchiq_states[state->id] = state;
  26190. +
  26191. + /* Indicate readiness to the other side */
  26192. + local->initialised = 1;
  26193. +
  26194. + return status;
  26195. +}
  26196. +
  26197. +/* Called from application thread when a client or server service is created. */
  26198. +VCHIQ_SERVICE_T *
  26199. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  26200. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  26201. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  26202. +{
  26203. + VCHIQ_SERVICE_T *service;
  26204. +
  26205. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  26206. + if (service) {
  26207. + service->base.fourcc = params->fourcc;
  26208. + service->base.callback = params->callback;
  26209. + service->base.userdata = params->userdata;
  26210. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  26211. + service->ref_count = 1;
  26212. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  26213. + service->userdata_term = userdata_term;
  26214. + service->localport = VCHIQ_PORT_FREE;
  26215. + service->remoteport = VCHIQ_PORT_FREE;
  26216. +
  26217. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  26218. + VCHIQ_FOURCC_INVALID : params->fourcc;
  26219. + service->client_id = 0;
  26220. + service->auto_close = 1;
  26221. + service->sync = 0;
  26222. + service->closing = 0;
  26223. + atomic_set(&service->poll_flags, 0);
  26224. + service->version = params->version;
  26225. + service->version_min = params->version_min;
  26226. + service->state = state;
  26227. + service->instance = instance;
  26228. + service->service_use_count = 0;
  26229. + init_bulk_queue(&service->bulk_tx);
  26230. + init_bulk_queue(&service->bulk_rx);
  26231. + sema_init(&service->remove_event, 0);
  26232. + sema_init(&service->bulk_remove_event, 0);
  26233. + mutex_init(&service->bulk_mutex);
  26234. + memset(&service->stats, 0, sizeof(service->stats));
  26235. + } else {
  26236. + vchiq_log_error(vchiq_core_log_level,
  26237. + "Out of memory");
  26238. + }
  26239. +
  26240. + if (service) {
  26241. + VCHIQ_SERVICE_T **pservice = NULL;
  26242. + int i;
  26243. +
  26244. + /* Although it is perfectly possible to use service_spinlock
  26245. + ** to protect the creation of services, it is overkill as it
  26246. + ** disables interrupts while the array is searched.
  26247. + ** The only danger is of another thread trying to create a
  26248. + ** service - service deletion is safe.
  26249. + ** Therefore it is preferable to use state->mutex which,
  26250. + ** although slower to claim, doesn't block interrupts while
  26251. + ** it is held.
  26252. + */
  26253. +
  26254. + mutex_lock(&state->mutex);
  26255. +
  26256. + /* Prepare to use a previously unused service */
  26257. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  26258. + pservice = &state->services[state->unused_service];
  26259. +
  26260. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  26261. + for (i = 0; i < state->unused_service; i++) {
  26262. + VCHIQ_SERVICE_T *srv = state->services[i];
  26263. + if (!srv) {
  26264. + pservice = &state->services[i];
  26265. + break;
  26266. + }
  26267. + }
  26268. + } else {
  26269. + for (i = (state->unused_service - 1); i >= 0; i--) {
  26270. + VCHIQ_SERVICE_T *srv = state->services[i];
  26271. + if (!srv)
  26272. + pservice = &state->services[i];
  26273. + else if ((srv->public_fourcc == params->fourcc)
  26274. + && ((srv->instance != instance) ||
  26275. + (srv->base.callback !=
  26276. + params->callback))) {
  26277. + /* There is another server using this
  26278. + ** fourcc which doesn't match. */
  26279. + pservice = NULL;
  26280. + break;
  26281. + }
  26282. + }
  26283. + }
  26284. +
  26285. + if (pservice) {
  26286. + service->localport = (pservice - state->services);
  26287. + if (!handle_seq)
  26288. + handle_seq = VCHIQ_MAX_STATES *
  26289. + VCHIQ_MAX_SERVICES;
  26290. + service->handle = handle_seq |
  26291. + (state->id * VCHIQ_MAX_SERVICES) |
  26292. + service->localport;
  26293. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  26294. + *pservice = service;
  26295. + if (pservice == &state->services[state->unused_service])
  26296. + state->unused_service++;
  26297. + }
  26298. +
  26299. + mutex_unlock(&state->mutex);
  26300. +
  26301. + if (!pservice) {
  26302. + kfree(service);
  26303. + service = NULL;
  26304. + }
  26305. + }
  26306. +
  26307. + if (service) {
  26308. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  26309. + &state->service_quotas[service->localport];
  26310. + service_quota->slot_quota = state->default_slot_quota;
  26311. + service_quota->message_quota = state->default_message_quota;
  26312. + if (service_quota->slot_use_count == 0)
  26313. + service_quota->previous_tx_index =
  26314. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  26315. + - 1;
  26316. +
  26317. + /* Bring this service online */
  26318. + vchiq_set_service_state(service, srvstate);
  26319. +
  26320. + vchiq_log_info(vchiq_core_msg_log_level,
  26321. + "%s Service %c%c%c%c SrcPort:%d",
  26322. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  26323. + ? "Open" : "Add",
  26324. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  26325. + service->localport);
  26326. + }
  26327. +
  26328. + /* Don't unlock the service - leave it with a ref_count of 1. */
  26329. +
  26330. + return service;
  26331. +}
  26332. +
  26333. +VCHIQ_STATUS_T
  26334. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  26335. +{
  26336. + struct vchiq_open_payload payload = {
  26337. + service->base.fourcc,
  26338. + client_id,
  26339. + service->version,
  26340. + service->version_min
  26341. + };
  26342. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  26343. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26344. +
  26345. + service->client_id = client_id;
  26346. + vchiq_use_service_internal(service);
  26347. + status = queue_message(service->state, NULL,
  26348. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  26349. + &body, 1, sizeof(payload), 1);
  26350. + if (status == VCHIQ_SUCCESS) {
  26351. + if (down_interruptible(&service->remove_event) != 0) {
  26352. + status = VCHIQ_RETRY;
  26353. + vchiq_release_service_internal(service);
  26354. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  26355. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  26356. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  26357. + vchiq_log_error(vchiq_core_log_level,
  26358. + "%d: osi - srvstate = %s (ref %d)",
  26359. + service->state->id,
  26360. + srvstate_names[service->srvstate],
  26361. + service->ref_count);
  26362. + status = VCHIQ_ERROR;
  26363. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  26364. + vchiq_release_service_internal(service);
  26365. + }
  26366. + }
  26367. + return status;
  26368. +}
  26369. +
  26370. +static void
  26371. +release_service_messages(VCHIQ_SERVICE_T *service)
  26372. +{
  26373. + VCHIQ_STATE_T *state = service->state;
  26374. + int slot_last = state->remote->slot_last;
  26375. + int i;
  26376. +
  26377. + /* Release any claimed messages */
  26378. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  26379. + VCHIQ_SLOT_INFO_T *slot_info =
  26380. + SLOT_INFO_FROM_INDEX(state, i);
  26381. + if (slot_info->release_count != slot_info->use_count) {
  26382. + char *data =
  26383. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  26384. + unsigned int pos, end;
  26385. +
  26386. + end = VCHIQ_SLOT_SIZE;
  26387. + if (data == state->rx_data)
  26388. + /* This buffer is still being read from - stop
  26389. + ** at the current read position */
  26390. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  26391. +
  26392. + pos = 0;
  26393. +
  26394. + while (pos < end) {
  26395. + VCHIQ_HEADER_T *header =
  26396. + (VCHIQ_HEADER_T *)(data + pos);
  26397. + int msgid = header->msgid;
  26398. + int port = VCHIQ_MSG_DSTPORT(msgid);
  26399. + if ((port == service->localport) &&
  26400. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  26401. + vchiq_log_info(vchiq_core_log_level,
  26402. + " fsi - hdr %x",
  26403. + (unsigned int)header);
  26404. + release_slot(state, slot_info, header,
  26405. + NULL);
  26406. + }
  26407. + pos += calc_stride(header->size);
  26408. + if (pos > VCHIQ_SLOT_SIZE) {
  26409. + vchiq_log_error(vchiq_core_log_level,
  26410. + "fsi - pos %x: header %x, "
  26411. + "msgid %x, header->msgid %x, "
  26412. + "header->size %x",
  26413. + pos, (unsigned int)header,
  26414. + msgid, header->msgid,
  26415. + header->size);
  26416. + WARN(1, "invalid slot position\n");
  26417. + }
  26418. + }
  26419. + }
  26420. + }
  26421. +}
  26422. +
  26423. +static int
  26424. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  26425. +{
  26426. + VCHIQ_STATUS_T status;
  26427. +
  26428. + /* Abort any outstanding bulk transfers */
  26429. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  26430. + return 0;
  26431. + abort_outstanding_bulks(service, &service->bulk_tx);
  26432. + abort_outstanding_bulks(service, &service->bulk_rx);
  26433. + mutex_unlock(&service->bulk_mutex);
  26434. +
  26435. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  26436. + if (status == VCHIQ_SUCCESS)
  26437. + status = notify_bulks(service, &service->bulk_rx,
  26438. + 0/*!retry_poll*/);
  26439. + return (status == VCHIQ_SUCCESS);
  26440. +}
  26441. +
  26442. +static VCHIQ_STATUS_T
  26443. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  26444. +{
  26445. + VCHIQ_STATUS_T status;
  26446. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  26447. + int newstate;
  26448. +
  26449. + switch (service->srvstate) {
  26450. + case VCHIQ_SRVSTATE_OPEN:
  26451. + case VCHIQ_SRVSTATE_CLOSESENT:
  26452. + case VCHIQ_SRVSTATE_CLOSERECVD:
  26453. + if (is_server) {
  26454. + if (service->auto_close) {
  26455. + service->client_id = 0;
  26456. + service->remoteport = VCHIQ_PORT_FREE;
  26457. + newstate = VCHIQ_SRVSTATE_LISTENING;
  26458. + } else
  26459. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  26460. + } else
  26461. + newstate = VCHIQ_SRVSTATE_CLOSED;
  26462. + vchiq_set_service_state(service, newstate);
  26463. + break;
  26464. + case VCHIQ_SRVSTATE_LISTENING:
  26465. + break;
  26466. + default:
  26467. + vchiq_log_error(vchiq_core_log_level,
  26468. + "close_service_complete(%x) called in state %s",
  26469. + service->handle, srvstate_names[service->srvstate]);
  26470. + WARN(1, "close_service_complete in unexpected state\n");
  26471. + return VCHIQ_ERROR;
  26472. + }
  26473. +
  26474. + status = make_service_callback(service,
  26475. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  26476. +
  26477. + if (status != VCHIQ_RETRY) {
  26478. + int uc = service->service_use_count;
  26479. + int i;
  26480. + /* Complete the close process */
  26481. + for (i = 0; i < uc; i++)
  26482. + /* cater for cases where close is forced and the
  26483. + ** client may not close all it's handles */
  26484. + vchiq_release_service_internal(service);
  26485. +
  26486. + service->client_id = 0;
  26487. + service->remoteport = VCHIQ_PORT_FREE;
  26488. +
  26489. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  26490. + vchiq_free_service_internal(service);
  26491. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  26492. + if (is_server)
  26493. + service->closing = 0;
  26494. +
  26495. + up(&service->remove_event);
  26496. + }
  26497. + } else
  26498. + vchiq_set_service_state(service, failstate);
  26499. +
  26500. + return status;
  26501. +}
  26502. +
  26503. +/* Called by the slot handler */
  26504. +VCHIQ_STATUS_T
  26505. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  26506. +{
  26507. + VCHIQ_STATE_T *state = service->state;
  26508. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26509. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  26510. +
  26511. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  26512. + service->state->id, service->localport, close_recvd,
  26513. + srvstate_names[service->srvstate]);
  26514. +
  26515. + switch (service->srvstate) {
  26516. + case VCHIQ_SRVSTATE_CLOSED:
  26517. + case VCHIQ_SRVSTATE_HIDDEN:
  26518. + case VCHIQ_SRVSTATE_LISTENING:
  26519. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  26520. + if (close_recvd)
  26521. + vchiq_log_error(vchiq_core_log_level,
  26522. + "vchiq_close_service_internal(1) called "
  26523. + "in state %s",
  26524. + srvstate_names[service->srvstate]);
  26525. + else if (is_server) {
  26526. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  26527. + status = VCHIQ_ERROR;
  26528. + } else {
  26529. + service->client_id = 0;
  26530. + service->remoteport = VCHIQ_PORT_FREE;
  26531. + if (service->srvstate ==
  26532. + VCHIQ_SRVSTATE_CLOSEWAIT)
  26533. + vchiq_set_service_state(service,
  26534. + VCHIQ_SRVSTATE_LISTENING);
  26535. + }
  26536. + up(&service->remove_event);
  26537. + } else
  26538. + vchiq_free_service_internal(service);
  26539. + break;
  26540. + case VCHIQ_SRVSTATE_OPENING:
  26541. + if (close_recvd) {
  26542. + /* The open was rejected - tell the user */
  26543. + vchiq_set_service_state(service,
  26544. + VCHIQ_SRVSTATE_CLOSEWAIT);
  26545. + up(&service->remove_event);
  26546. + } else {
  26547. + /* Shutdown mid-open - let the other side know */
  26548. + status = queue_message(state, service,
  26549. + VCHIQ_MAKE_MSG
  26550. + (VCHIQ_MSG_CLOSE,
  26551. + service->localport,
  26552. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  26553. + NULL, 0, 0, 0);
  26554. + }
  26555. + break;
  26556. +
  26557. + case VCHIQ_SRVSTATE_OPENSYNC:
  26558. + mutex_lock(&state->sync_mutex);
  26559. + /* Drop through */
  26560. +
  26561. + case VCHIQ_SRVSTATE_OPEN:
  26562. + if (state->is_master || close_recvd) {
  26563. + if (!do_abort_bulks(service))
  26564. + status = VCHIQ_RETRY;
  26565. + }
  26566. +
  26567. + release_service_messages(service);
  26568. +
  26569. + if (status == VCHIQ_SUCCESS)
  26570. + status = queue_message(state, service,
  26571. + VCHIQ_MAKE_MSG
  26572. + (VCHIQ_MSG_CLOSE,
  26573. + service->localport,
  26574. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  26575. + NULL, 0, 0, 0);
  26576. +
  26577. + if (status == VCHIQ_SUCCESS) {
  26578. + if (!close_recvd)
  26579. + break;
  26580. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  26581. + mutex_unlock(&state->sync_mutex);
  26582. + break;
  26583. + } else
  26584. + break;
  26585. +
  26586. + status = close_service_complete(service,
  26587. + VCHIQ_SRVSTATE_CLOSERECVD);
  26588. + break;
  26589. +
  26590. + case VCHIQ_SRVSTATE_CLOSESENT:
  26591. + if (!close_recvd)
  26592. + /* This happens when a process is killed mid-close */
  26593. + break;
  26594. +
  26595. + if (!state->is_master) {
  26596. + if (!do_abort_bulks(service)) {
  26597. + status = VCHIQ_RETRY;
  26598. + break;
  26599. + }
  26600. + }
  26601. +
  26602. + if (status == VCHIQ_SUCCESS)
  26603. + status = close_service_complete(service,
  26604. + VCHIQ_SRVSTATE_CLOSERECVD);
  26605. + break;
  26606. +
  26607. + case VCHIQ_SRVSTATE_CLOSERECVD:
  26608. + if (!close_recvd && is_server)
  26609. + /* Force into LISTENING mode */
  26610. + vchiq_set_service_state(service,
  26611. + VCHIQ_SRVSTATE_LISTENING);
  26612. + status = close_service_complete(service,
  26613. + VCHIQ_SRVSTATE_CLOSERECVD);
  26614. + break;
  26615. +
  26616. + default:
  26617. + vchiq_log_error(vchiq_core_log_level,
  26618. + "vchiq_close_service_internal(%d) called in state %s",
  26619. + close_recvd, srvstate_names[service->srvstate]);
  26620. + break;
  26621. + }
  26622. +
  26623. + return status;
  26624. +}
  26625. +
  26626. +/* Called from the application process upon process death */
  26627. +void
  26628. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  26629. +{
  26630. + VCHIQ_STATE_T *state = service->state;
  26631. +
  26632. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  26633. + state->id, service->localport, service->remoteport);
  26634. +
  26635. + mark_service_closing(service);
  26636. +
  26637. + /* Mark the service for removal by the slot handler */
  26638. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  26639. +}
  26640. +
  26641. +/* Called from the slot handler */
  26642. +void
  26643. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  26644. +{
  26645. + VCHIQ_STATE_T *state = service->state;
  26646. +
  26647. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  26648. + state->id, service->localport);
  26649. +
  26650. + switch (service->srvstate) {
  26651. + case VCHIQ_SRVSTATE_OPENING:
  26652. + case VCHIQ_SRVSTATE_CLOSED:
  26653. + case VCHIQ_SRVSTATE_HIDDEN:
  26654. + case VCHIQ_SRVSTATE_LISTENING:
  26655. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  26656. + break;
  26657. + default:
  26658. + vchiq_log_error(vchiq_core_log_level,
  26659. + "%d: fsi - (%d) in state %s",
  26660. + state->id, service->localport,
  26661. + srvstate_names[service->srvstate]);
  26662. + return;
  26663. + }
  26664. +
  26665. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  26666. +
  26667. + up(&service->remove_event);
  26668. +
  26669. + /* Release the initial lock */
  26670. + unlock_service(service);
  26671. +}
  26672. +
  26673. +VCHIQ_STATUS_T
  26674. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  26675. +{
  26676. + VCHIQ_SERVICE_T *service;
  26677. + int i;
  26678. +
  26679. + /* Find all services registered to this client and enable them. */
  26680. + i = 0;
  26681. + while ((service = next_service_by_instance(state, instance,
  26682. + &i)) != NULL) {
  26683. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  26684. + vchiq_set_service_state(service,
  26685. + VCHIQ_SRVSTATE_LISTENING);
  26686. + unlock_service(service);
  26687. + }
  26688. +
  26689. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  26690. + if (queue_message(state, NULL,
  26691. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  26692. + 0, 1) == VCHIQ_RETRY)
  26693. + return VCHIQ_RETRY;
  26694. +
  26695. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  26696. + }
  26697. +
  26698. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  26699. + if (down_interruptible(&state->connect) != 0)
  26700. + return VCHIQ_RETRY;
  26701. +
  26702. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  26703. + up(&state->connect);
  26704. + }
  26705. +
  26706. + return VCHIQ_SUCCESS;
  26707. +}
  26708. +
  26709. +VCHIQ_STATUS_T
  26710. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  26711. +{
  26712. + VCHIQ_SERVICE_T *service;
  26713. + int i;
  26714. +
  26715. + /* Find all services registered to this client and enable them. */
  26716. + i = 0;
  26717. + while ((service = next_service_by_instance(state, instance,
  26718. + &i)) != NULL) {
  26719. + (void)vchiq_remove_service(service->handle);
  26720. + unlock_service(service);
  26721. + }
  26722. +
  26723. + return VCHIQ_SUCCESS;
  26724. +}
  26725. +
  26726. +VCHIQ_STATUS_T
  26727. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  26728. +{
  26729. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26730. +
  26731. + switch (state->conn_state) {
  26732. + case VCHIQ_CONNSTATE_CONNECTED:
  26733. + /* Request a pause */
  26734. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  26735. + request_poll(state, NULL, 0);
  26736. + break;
  26737. + default:
  26738. + vchiq_log_error(vchiq_core_log_level,
  26739. + "vchiq_pause_internal in state %s\n",
  26740. + conn_state_names[state->conn_state]);
  26741. + status = VCHIQ_ERROR;
  26742. + VCHIQ_STATS_INC(state, error_count);
  26743. + break;
  26744. + }
  26745. +
  26746. + return status;
  26747. +}
  26748. +
  26749. +VCHIQ_STATUS_T
  26750. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  26751. +{
  26752. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26753. +
  26754. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  26755. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  26756. + request_poll(state, NULL, 0);
  26757. + } else {
  26758. + status = VCHIQ_ERROR;
  26759. + VCHIQ_STATS_INC(state, error_count);
  26760. + }
  26761. +
  26762. + return status;
  26763. +}
  26764. +
  26765. +VCHIQ_STATUS_T
  26766. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  26767. +{
  26768. + /* Unregister the service */
  26769. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26770. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26771. +
  26772. + if (!service)
  26773. + return VCHIQ_ERROR;
  26774. +
  26775. + vchiq_log_info(vchiq_core_log_level,
  26776. + "%d: close_service:%d",
  26777. + service->state->id, service->localport);
  26778. +
  26779. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26780. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  26781. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  26782. + unlock_service(service);
  26783. + return VCHIQ_ERROR;
  26784. + }
  26785. +
  26786. + mark_service_closing(service);
  26787. +
  26788. + if (current == service->state->slot_handler_thread) {
  26789. + status = vchiq_close_service_internal(service,
  26790. + 0/*!close_recvd*/);
  26791. + BUG_ON(status == VCHIQ_RETRY);
  26792. + } else {
  26793. + /* Mark the service for termination by the slot handler */
  26794. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  26795. + }
  26796. +
  26797. + while (1) {
  26798. + if (down_interruptible(&service->remove_event) != 0) {
  26799. + status = VCHIQ_RETRY;
  26800. + break;
  26801. + }
  26802. +
  26803. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26804. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  26805. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  26806. + break;
  26807. +
  26808. + vchiq_log_warning(vchiq_core_log_level,
  26809. + "%d: close_service:%d - waiting in state %s",
  26810. + service->state->id, service->localport,
  26811. + srvstate_names[service->srvstate]);
  26812. + }
  26813. +
  26814. + if ((status == VCHIQ_SUCCESS) &&
  26815. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  26816. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  26817. + status = VCHIQ_ERROR;
  26818. +
  26819. + unlock_service(service);
  26820. +
  26821. + return status;
  26822. +}
  26823. +
  26824. +VCHIQ_STATUS_T
  26825. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  26826. +{
  26827. + /* Unregister the service */
  26828. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26829. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26830. +
  26831. + if (!service)
  26832. + return VCHIQ_ERROR;
  26833. +
  26834. + vchiq_log_info(vchiq_core_log_level,
  26835. + "%d: remove_service:%d",
  26836. + service->state->id, service->localport);
  26837. +
  26838. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  26839. + unlock_service(service);
  26840. + return VCHIQ_ERROR;
  26841. + }
  26842. +
  26843. + mark_service_closing(service);
  26844. +
  26845. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  26846. + (current == service->state->slot_handler_thread)) {
  26847. + /* Make it look like a client, because it must be removed and
  26848. + not left in the LISTENING state. */
  26849. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  26850. +
  26851. + status = vchiq_close_service_internal(service,
  26852. + 0/*!close_recvd*/);
  26853. + BUG_ON(status == VCHIQ_RETRY);
  26854. + } else {
  26855. + /* Mark the service for removal by the slot handler */
  26856. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  26857. + }
  26858. + while (1) {
  26859. + if (down_interruptible(&service->remove_event) != 0) {
  26860. + status = VCHIQ_RETRY;
  26861. + break;
  26862. + }
  26863. +
  26864. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26865. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  26866. + break;
  26867. +
  26868. + vchiq_log_warning(vchiq_core_log_level,
  26869. + "%d: remove_service:%d - waiting in state %s",
  26870. + service->state->id, service->localport,
  26871. + srvstate_names[service->srvstate]);
  26872. + }
  26873. +
  26874. + if ((status == VCHIQ_SUCCESS) &&
  26875. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  26876. + status = VCHIQ_ERROR;
  26877. +
  26878. + unlock_service(service);
  26879. +
  26880. + return status;
  26881. +}
  26882. +
  26883. +
  26884. +/* This function may be called by kernel threads or user threads.
  26885. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  26886. + * received and the call should be retried after being returned to user
  26887. + * context.
  26888. + * When called in blocking mode, the userdata field points to a bulk_waiter
  26889. + * structure.
  26890. + */
  26891. +VCHIQ_STATUS_T
  26892. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  26893. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  26894. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  26895. +{
  26896. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26897. + VCHIQ_BULK_QUEUE_T *queue;
  26898. + VCHIQ_BULK_T *bulk;
  26899. + VCHIQ_STATE_T *state;
  26900. + struct bulk_waiter *bulk_waiter = NULL;
  26901. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  26902. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  26903. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  26904. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26905. +
  26906. + if (!service ||
  26907. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  26908. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  26909. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  26910. + goto error_exit;
  26911. +
  26912. + switch (mode) {
  26913. + case VCHIQ_BULK_MODE_NOCALLBACK:
  26914. + case VCHIQ_BULK_MODE_CALLBACK:
  26915. + break;
  26916. + case VCHIQ_BULK_MODE_BLOCKING:
  26917. + bulk_waiter = (struct bulk_waiter *)userdata;
  26918. + sema_init(&bulk_waiter->event, 0);
  26919. + bulk_waiter->actual = 0;
  26920. + bulk_waiter->bulk = NULL;
  26921. + break;
  26922. + case VCHIQ_BULK_MODE_WAITING:
  26923. + bulk_waiter = (struct bulk_waiter *)userdata;
  26924. + bulk = bulk_waiter->bulk;
  26925. + goto waiting;
  26926. + default:
  26927. + goto error_exit;
  26928. + }
  26929. +
  26930. + state = service->state;
  26931. +
  26932. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  26933. + &service->bulk_tx : &service->bulk_rx;
  26934. +
  26935. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  26936. + status = VCHIQ_RETRY;
  26937. + goto error_exit;
  26938. + }
  26939. +
  26940. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  26941. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  26942. + do {
  26943. + mutex_unlock(&service->bulk_mutex);
  26944. + if (down_interruptible(&service->bulk_remove_event)
  26945. + != 0) {
  26946. + status = VCHIQ_RETRY;
  26947. + goto error_exit;
  26948. + }
  26949. + if (mutex_lock_interruptible(&service->bulk_mutex)
  26950. + != 0) {
  26951. + status = VCHIQ_RETRY;
  26952. + goto error_exit;
  26953. + }
  26954. + } while (queue->local_insert == queue->remove +
  26955. + VCHIQ_NUM_SERVICE_BULKS);
  26956. + }
  26957. +
  26958. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  26959. +
  26960. + bulk->mode = mode;
  26961. + bulk->dir = dir;
  26962. + bulk->userdata = userdata;
  26963. + bulk->size = size;
  26964. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  26965. +
  26966. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  26967. + VCHIQ_SUCCESS)
  26968. + goto unlock_error_exit;
  26969. +
  26970. + wmb();
  26971. +
  26972. + vchiq_log_info(vchiq_core_log_level,
  26973. + "%d: bt (%d->%d) %cx %x@%x %x",
  26974. + state->id,
  26975. + service->localport, service->remoteport, dir_char,
  26976. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  26977. +
  26978. + if (state->is_master) {
  26979. + queue->local_insert++;
  26980. + if (resolve_bulks(service, queue))
  26981. + request_poll(state, service,
  26982. + (dir == VCHIQ_BULK_TRANSMIT) ?
  26983. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  26984. + } else {
  26985. + int payload[2] = { (int)bulk->data, bulk->size };
  26986. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  26987. +
  26988. + status = queue_message(state, NULL,
  26989. + VCHIQ_MAKE_MSG(dir_msgtype,
  26990. + service->localport, service->remoteport),
  26991. + &element, 1, sizeof(payload), 1);
  26992. + if (status != VCHIQ_SUCCESS) {
  26993. + vchiq_complete_bulk(bulk);
  26994. + goto unlock_error_exit;
  26995. + }
  26996. + queue->local_insert++;
  26997. + }
  26998. +
  26999. + mutex_unlock(&service->bulk_mutex);
  27000. +
  27001. + vchiq_log_trace(vchiq_core_log_level,
  27002. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  27003. + state->id,
  27004. + service->localport, dir_char,
  27005. + queue->local_insert, queue->remote_insert, queue->process);
  27006. +
  27007. +waiting:
  27008. + unlock_service(service);
  27009. +
  27010. + status = VCHIQ_SUCCESS;
  27011. +
  27012. + if (bulk_waiter) {
  27013. + bulk_waiter->bulk = bulk;
  27014. + if (down_interruptible(&bulk_waiter->event) != 0)
  27015. + status = VCHIQ_RETRY;
  27016. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  27017. + status = VCHIQ_ERROR;
  27018. + }
  27019. +
  27020. + return status;
  27021. +
  27022. +unlock_error_exit:
  27023. + mutex_unlock(&service->bulk_mutex);
  27024. +
  27025. +error_exit:
  27026. + if (service)
  27027. + unlock_service(service);
  27028. + return status;
  27029. +}
  27030. +
  27031. +VCHIQ_STATUS_T
  27032. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  27033. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  27034. +{
  27035. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27036. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  27037. +
  27038. + unsigned int size = 0;
  27039. + unsigned int i;
  27040. +
  27041. + if (!service ||
  27042. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  27043. + goto error_exit;
  27044. +
  27045. + for (i = 0; i < (unsigned int)count; i++) {
  27046. + if (elements[i].size) {
  27047. + if (elements[i].data == NULL) {
  27048. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  27049. + goto error_exit;
  27050. + }
  27051. + size += elements[i].size;
  27052. + }
  27053. + }
  27054. +
  27055. + if (size > VCHIQ_MAX_MSG_SIZE) {
  27056. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  27057. + goto error_exit;
  27058. + }
  27059. +
  27060. + switch (service->srvstate) {
  27061. + case VCHIQ_SRVSTATE_OPEN:
  27062. + status = queue_message(service->state, service,
  27063. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  27064. + service->localport,
  27065. + service->remoteport),
  27066. + elements, count, size, 1);
  27067. + break;
  27068. + case VCHIQ_SRVSTATE_OPENSYNC:
  27069. + status = queue_message_sync(service->state, service,
  27070. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  27071. + service->localport,
  27072. + service->remoteport),
  27073. + elements, count, size, 1);
  27074. + break;
  27075. + default:
  27076. + status = VCHIQ_ERROR;
  27077. + break;
  27078. + }
  27079. +
  27080. +error_exit:
  27081. + if (service)
  27082. + unlock_service(service);
  27083. +
  27084. + return status;
  27085. +}
  27086. +
  27087. +void
  27088. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  27089. +{
  27090. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27091. + VCHIQ_SHARED_STATE_T *remote;
  27092. + VCHIQ_STATE_T *state;
  27093. + int slot_index;
  27094. +
  27095. + if (!service)
  27096. + return;
  27097. +
  27098. + state = service->state;
  27099. + remote = state->remote;
  27100. +
  27101. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  27102. +
  27103. + if ((slot_index >= remote->slot_first) &&
  27104. + (slot_index <= remote->slot_last)) {
  27105. + int msgid = header->msgid;
  27106. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  27107. + VCHIQ_SLOT_INFO_T *slot_info =
  27108. + SLOT_INFO_FROM_INDEX(state, slot_index);
  27109. +
  27110. + release_slot(state, slot_info, header, service);
  27111. + }
  27112. + } else if (slot_index == remote->slot_sync)
  27113. + release_message_sync(state, header);
  27114. +
  27115. + unlock_service(service);
  27116. +}
  27117. +
  27118. +static void
  27119. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  27120. +{
  27121. + header->msgid = VCHIQ_MSGID_PADDING;
  27122. + wmb();
  27123. + remote_event_signal(&state->remote->sync_release);
  27124. +}
  27125. +
  27126. +VCHIQ_STATUS_T
  27127. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  27128. +{
  27129. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  27130. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27131. +
  27132. + if (!service ||
  27133. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  27134. + !peer_version)
  27135. + goto exit;
  27136. + *peer_version = service->peer_version;
  27137. + status = VCHIQ_SUCCESS;
  27138. +
  27139. +exit:
  27140. + if (service)
  27141. + unlock_service(service);
  27142. + return status;
  27143. +}
  27144. +
  27145. +VCHIQ_STATUS_T
  27146. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  27147. + int config_size, VCHIQ_CONFIG_T *pconfig)
  27148. +{
  27149. + VCHIQ_CONFIG_T config;
  27150. +
  27151. + (void)instance;
  27152. +
  27153. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  27154. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  27155. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  27156. + config.max_services = VCHIQ_MAX_SERVICES;
  27157. + config.version = VCHIQ_VERSION;
  27158. + config.version_min = VCHIQ_VERSION_MIN;
  27159. +
  27160. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  27161. + return VCHIQ_ERROR;
  27162. +
  27163. + memcpy(pconfig, &config,
  27164. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  27165. +
  27166. + return VCHIQ_SUCCESS;
  27167. +}
  27168. +
  27169. +VCHIQ_STATUS_T
  27170. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  27171. + VCHIQ_SERVICE_OPTION_T option, int value)
  27172. +{
  27173. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27174. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  27175. +
  27176. + if (service) {
  27177. + switch (option) {
  27178. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  27179. + service->auto_close = value;
  27180. + status = VCHIQ_SUCCESS;
  27181. + break;
  27182. +
  27183. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  27184. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  27185. + &service->state->service_quotas[
  27186. + service->localport];
  27187. + if (value == 0)
  27188. + value = service->state->default_slot_quota;
  27189. + if ((value >= service_quota->slot_use_count) &&
  27190. + (value < (unsigned short)~0)) {
  27191. + service_quota->slot_quota = value;
  27192. + if ((value >= service_quota->slot_use_count) &&
  27193. + (service_quota->message_quota >=
  27194. + service_quota->message_use_count)) {
  27195. + /* Signal the service that it may have
  27196. + ** dropped below its quota */
  27197. + up(&service_quota->quota_event);
  27198. + }
  27199. + status = VCHIQ_SUCCESS;
  27200. + }
  27201. + } break;
  27202. +
  27203. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  27204. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  27205. + &service->state->service_quotas[
  27206. + service->localport];
  27207. + if (value == 0)
  27208. + value = service->state->default_message_quota;
  27209. + if ((value >= service_quota->message_use_count) &&
  27210. + (value < (unsigned short)~0)) {
  27211. + service_quota->message_quota = value;
  27212. + if ((value >=
  27213. + service_quota->message_use_count) &&
  27214. + (service_quota->slot_quota >=
  27215. + service_quota->slot_use_count))
  27216. + /* Signal the service that it may have
  27217. + ** dropped below its quota */
  27218. + up(&service_quota->quota_event);
  27219. + status = VCHIQ_SUCCESS;
  27220. + }
  27221. + } break;
  27222. +
  27223. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  27224. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  27225. + (service->srvstate ==
  27226. + VCHIQ_SRVSTATE_LISTENING)) {
  27227. + service->sync = value;
  27228. + status = VCHIQ_SUCCESS;
  27229. + }
  27230. + break;
  27231. +
  27232. + default:
  27233. + break;
  27234. + }
  27235. + unlock_service(service);
  27236. + }
  27237. +
  27238. + return status;
  27239. +}
  27240. +
  27241. +void
  27242. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  27243. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  27244. +{
  27245. + static const char *const debug_names[] = {
  27246. + "<entries>",
  27247. + "SLOT_HANDLER_COUNT",
  27248. + "SLOT_HANDLER_LINE",
  27249. + "PARSE_LINE",
  27250. + "PARSE_HEADER",
  27251. + "PARSE_MSGID",
  27252. + "AWAIT_COMPLETION_LINE",
  27253. + "DEQUEUE_MESSAGE_LINE",
  27254. + "SERVICE_CALLBACK_LINE",
  27255. + "MSG_QUEUE_FULL_COUNT",
  27256. + "COMPLETION_QUEUE_FULL_COUNT"
  27257. + };
  27258. + int i;
  27259. +
  27260. + char buf[80];
  27261. + int len;
  27262. + len = snprintf(buf, sizeof(buf),
  27263. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  27264. + label, shared->slot_first, shared->slot_last,
  27265. + shared->tx_pos, shared->slot_queue_recycle);
  27266. + vchiq_dump(dump_context, buf, len + 1);
  27267. +
  27268. + len = snprintf(buf, sizeof(buf),
  27269. + " Slots claimed:");
  27270. + vchiq_dump(dump_context, buf, len + 1);
  27271. +
  27272. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  27273. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  27274. + if (slot_info.use_count != slot_info.release_count) {
  27275. + len = snprintf(buf, sizeof(buf),
  27276. + " %d: %d/%d", i, slot_info.use_count,
  27277. + slot_info.release_count);
  27278. + vchiq_dump(dump_context, buf, len + 1);
  27279. + }
  27280. + }
  27281. +
  27282. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  27283. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  27284. + debug_names[i], shared->debug[i], shared->debug[i]);
  27285. + vchiq_dump(dump_context, buf, len + 1);
  27286. + }
  27287. +}
  27288. +
  27289. +void
  27290. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  27291. +{
  27292. + char buf[80];
  27293. + int len;
  27294. + int i;
  27295. +
  27296. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  27297. + conn_state_names[state->conn_state]);
  27298. + vchiq_dump(dump_context, buf, len + 1);
  27299. +
  27300. + len = snprintf(buf, sizeof(buf),
  27301. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  27302. + state->local->tx_pos,
  27303. + (uint32_t)state->tx_data +
  27304. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  27305. + state->rx_pos,
  27306. + (uint32_t)state->rx_data +
  27307. + (state->rx_pos & VCHIQ_SLOT_MASK));
  27308. + vchiq_dump(dump_context, buf, len + 1);
  27309. +
  27310. + len = snprintf(buf, sizeof(buf),
  27311. + " Version: %d (min %d)",
  27312. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  27313. + vchiq_dump(dump_context, buf, len + 1);
  27314. +
  27315. + if (VCHIQ_ENABLE_STATS) {
  27316. + len = snprintf(buf, sizeof(buf),
  27317. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  27318. + "error_count=%d",
  27319. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  27320. + state->stats.error_count);
  27321. + vchiq_dump(dump_context, buf, len + 1);
  27322. + }
  27323. +
  27324. + len = snprintf(buf, sizeof(buf),
  27325. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  27326. + "(%d data)",
  27327. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  27328. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  27329. + state->data_quota - state->data_use_count,
  27330. + state->local->slot_queue_recycle - state->slot_queue_available,
  27331. + state->stats.slot_stalls, state->stats.data_stalls);
  27332. + vchiq_dump(dump_context, buf, len + 1);
  27333. +
  27334. + vchiq_dump_platform_state(dump_context);
  27335. +
  27336. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  27337. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  27338. +
  27339. + vchiq_dump_platform_instances(dump_context);
  27340. +
  27341. + for (i = 0; i < state->unused_service; i++) {
  27342. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  27343. +
  27344. + if (service) {
  27345. + vchiq_dump_service_state(dump_context, service);
  27346. + unlock_service(service);
  27347. + }
  27348. + }
  27349. +}
  27350. +
  27351. +void
  27352. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  27353. +{
  27354. + char buf[80];
  27355. + int len;
  27356. +
  27357. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  27358. + service->localport, srvstate_names[service->srvstate],
  27359. + service->ref_count - 1); /*Don't include the lock just taken*/
  27360. +
  27361. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  27362. + char remoteport[30];
  27363. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  27364. + &service->state->service_quotas[service->localport];
  27365. + int fourcc = service->base.fourcc;
  27366. + int tx_pending, rx_pending;
  27367. + if (service->remoteport != VCHIQ_PORT_FREE) {
  27368. + int len2 = snprintf(remoteport, sizeof(remoteport),
  27369. + "%d", service->remoteport);
  27370. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  27371. + snprintf(remoteport + len2,
  27372. + sizeof(remoteport) - len2,
  27373. + " (client %x)", service->client_id);
  27374. + } else
  27375. + strcpy(remoteport, "n/a");
  27376. +
  27377. + len += snprintf(buf + len, sizeof(buf) - len,
  27378. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  27379. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  27380. + remoteport,
  27381. + service_quota->message_use_count,
  27382. + service_quota->message_quota,
  27383. + service_quota->slot_use_count,
  27384. + service_quota->slot_quota);
  27385. +
  27386. + vchiq_dump(dump_context, buf, len + 1);
  27387. +
  27388. + tx_pending = service->bulk_tx.local_insert -
  27389. + service->bulk_tx.remote_insert;
  27390. +
  27391. + rx_pending = service->bulk_rx.local_insert -
  27392. + service->bulk_rx.remote_insert;
  27393. +
  27394. + len = snprintf(buf, sizeof(buf),
  27395. + " Bulk: tx_pending=%d (size %d),"
  27396. + " rx_pending=%d (size %d)",
  27397. + tx_pending,
  27398. + tx_pending ? service->bulk_tx.bulks[
  27399. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  27400. + rx_pending,
  27401. + rx_pending ? service->bulk_rx.bulks[
  27402. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  27403. +
  27404. + if (VCHIQ_ENABLE_STATS) {
  27405. + vchiq_dump(dump_context, buf, len + 1);
  27406. +
  27407. + len = snprintf(buf, sizeof(buf),
  27408. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  27409. + "rx_count=%d, rx_bytes=%llu",
  27410. + service->stats.ctrl_tx_count,
  27411. + service->stats.ctrl_tx_bytes,
  27412. + service->stats.ctrl_rx_count,
  27413. + service->stats.ctrl_rx_bytes);
  27414. + vchiq_dump(dump_context, buf, len + 1);
  27415. +
  27416. + len = snprintf(buf, sizeof(buf),
  27417. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  27418. + "rx_count=%d, rx_bytes=%llu",
  27419. + service->stats.bulk_tx_count,
  27420. + service->stats.bulk_tx_bytes,
  27421. + service->stats.bulk_rx_count,
  27422. + service->stats.bulk_rx_bytes);
  27423. + vchiq_dump(dump_context, buf, len + 1);
  27424. +
  27425. + len = snprintf(buf, sizeof(buf),
  27426. + " %d quota stalls, %d slot stalls, "
  27427. + "%d bulk stalls, %d aborted, %d errors",
  27428. + service->stats.quota_stalls,
  27429. + service->stats.slot_stalls,
  27430. + service->stats.bulk_stalls,
  27431. + service->stats.bulk_aborted_count,
  27432. + service->stats.error_count);
  27433. + }
  27434. + }
  27435. +
  27436. + vchiq_dump(dump_context, buf, len + 1);
  27437. +
  27438. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  27439. + vchiq_dump_platform_service_state(dump_context, service);
  27440. +}
  27441. +
  27442. +
  27443. +void
  27444. +vchiq_loud_error_header(void)
  27445. +{
  27446. + vchiq_log_error(vchiq_core_log_level,
  27447. + "============================================================"
  27448. + "================");
  27449. + vchiq_log_error(vchiq_core_log_level,
  27450. + "============================================================"
  27451. + "================");
  27452. + vchiq_log_error(vchiq_core_log_level, "=====");
  27453. +}
  27454. +
  27455. +void
  27456. +vchiq_loud_error_footer(void)
  27457. +{
  27458. + vchiq_log_error(vchiq_core_log_level, "=====");
  27459. + vchiq_log_error(vchiq_core_log_level,
  27460. + "============================================================"
  27461. + "================");
  27462. + vchiq_log_error(vchiq_core_log_level,
  27463. + "============================================================"
  27464. + "================");
  27465. +}
  27466. +
  27467. +
  27468. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  27469. +{
  27470. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  27471. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  27472. + status = queue_message(state, NULL,
  27473. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  27474. + NULL, 0, 0, 0);
  27475. + return status;
  27476. +}
  27477. +
  27478. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  27479. +{
  27480. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  27481. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  27482. + status = queue_message(state, NULL,
  27483. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  27484. + NULL, 0, 0, 0);
  27485. + return status;
  27486. +}
  27487. +
  27488. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  27489. +{
  27490. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  27491. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  27492. + status = queue_message(state, NULL,
  27493. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  27494. + NULL, 0, 0, 0);
  27495. + return status;
  27496. +}
  27497. +
  27498. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  27499. + size_t numBytes)
  27500. +{
  27501. + const uint8_t *mem = (const uint8_t *)voidMem;
  27502. + size_t offset;
  27503. + char lineBuf[100];
  27504. + char *s;
  27505. +
  27506. + while (numBytes > 0) {
  27507. + s = lineBuf;
  27508. +
  27509. + for (offset = 0; offset < 16; offset++) {
  27510. + if (offset < numBytes)
  27511. + s += snprintf(s, 4, "%02x ", mem[offset]);
  27512. + else
  27513. + s += snprintf(s, 4, " ");
  27514. + }
  27515. +
  27516. + for (offset = 0; offset < 16; offset++) {
  27517. + if (offset < numBytes) {
  27518. + uint8_t ch = mem[offset];
  27519. +
  27520. + if ((ch < ' ') || (ch > '~'))
  27521. + ch = '.';
  27522. + *s++ = (char)ch;
  27523. + }
  27524. + }
  27525. + *s++ = '\0';
  27526. +
  27527. + if ((label != NULL) && (*label != '\0'))
  27528. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  27529. + "%s: %08x: %s", label, addr, lineBuf);
  27530. + else
  27531. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  27532. + "%08x: %s", addr, lineBuf);
  27533. +
  27534. + addr += 16;
  27535. + mem += 16;
  27536. + if (numBytes > 16)
  27537. + numBytes -= 16;
  27538. + else
  27539. + numBytes = 0;
  27540. + }
  27541. +}
  27542. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  27543. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 1970-01-01 01:00:00.000000000 +0100
  27544. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 2014-04-24 15:35:02.893551516 +0200
  27545. @@ -0,0 +1,706 @@
  27546. +/**
  27547. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27548. + *
  27549. + * Redistribution and use in source and binary forms, with or without
  27550. + * modification, are permitted provided that the following conditions
  27551. + * are met:
  27552. + * 1. Redistributions of source code must retain the above copyright
  27553. + * notice, this list of conditions, and the following disclaimer,
  27554. + * without modification.
  27555. + * 2. Redistributions in binary form must reproduce the above copyright
  27556. + * notice, this list of conditions and the following disclaimer in the
  27557. + * documentation and/or other materials provided with the distribution.
  27558. + * 3. The names of the above-listed copyright holders may not be used
  27559. + * to endorse or promote products derived from this software without
  27560. + * specific prior written permission.
  27561. + *
  27562. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27563. + * GNU General Public License ("GPL") version 2, as published by the Free
  27564. + * Software Foundation.
  27565. + *
  27566. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27567. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27568. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27569. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27570. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27571. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27572. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27573. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27574. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27575. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27576. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27577. + */
  27578. +
  27579. +#ifndef VCHIQ_CORE_H
  27580. +#define VCHIQ_CORE_H
  27581. +
  27582. +#include <linux/mutex.h>
  27583. +#include <linux/semaphore.h>
  27584. +#include <linux/kthread.h>
  27585. +
  27586. +#include "vchiq_cfg.h"
  27587. +
  27588. +#include "vchiq.h"
  27589. +
  27590. +/* Run time control of log level, based on KERN_XXX level. */
  27591. +#define VCHIQ_LOG_DEFAULT 4
  27592. +#define VCHIQ_LOG_ERROR 3
  27593. +#define VCHIQ_LOG_WARNING 4
  27594. +#define VCHIQ_LOG_INFO 6
  27595. +#define VCHIQ_LOG_TRACE 7
  27596. +
  27597. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  27598. +
  27599. +#ifndef vchiq_log_error
  27600. +#define vchiq_log_error(cat, fmt, ...) \
  27601. + do { if (cat >= VCHIQ_LOG_ERROR) \
  27602. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27603. +#endif
  27604. +#ifndef vchiq_log_warning
  27605. +#define vchiq_log_warning(cat, fmt, ...) \
  27606. + do { if (cat >= VCHIQ_LOG_WARNING) \
  27607. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27608. +#endif
  27609. +#ifndef vchiq_log_info
  27610. +#define vchiq_log_info(cat, fmt, ...) \
  27611. + do { if (cat >= VCHIQ_LOG_INFO) \
  27612. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27613. +#endif
  27614. +#ifndef vchiq_log_trace
  27615. +#define vchiq_log_trace(cat, fmt, ...) \
  27616. + do { if (cat >= VCHIQ_LOG_TRACE) \
  27617. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  27618. +#endif
  27619. +
  27620. +#define vchiq_loud_error(...) \
  27621. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  27622. +
  27623. +#ifndef vchiq_static_assert
  27624. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  27625. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  27626. +#endif
  27627. +
  27628. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  27629. +
  27630. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  27631. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  27632. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  27633. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  27634. +
  27635. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  27636. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  27637. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  27638. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  27639. +
  27640. +#define VCHIQ_MSG_PADDING 0 /* - */
  27641. +#define VCHIQ_MSG_CONNECT 1 /* - */
  27642. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  27643. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  27644. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  27645. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  27646. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  27647. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  27648. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  27649. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  27650. +#define VCHIQ_MSG_PAUSE 10 /* - */
  27651. +#define VCHIQ_MSG_RESUME 11 /* - */
  27652. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  27653. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  27654. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  27655. +
  27656. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  27657. +#define VCHIQ_PORT_FREE 0x1000
  27658. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  27659. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  27660. + ((type<<24) | (srcport<<12) | (dstport<<0))
  27661. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  27662. +#define VCHIQ_MSG_SRCPORT(msgid) \
  27663. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  27664. +#define VCHIQ_MSG_DSTPORT(msgid) \
  27665. + ((unsigned short)msgid & 0xfff)
  27666. +
  27667. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  27668. + ((fourcc) >> 24) & 0xff, \
  27669. + ((fourcc) >> 16) & 0xff, \
  27670. + ((fourcc) >> 8) & 0xff, \
  27671. + (fourcc) & 0xff
  27672. +
  27673. +/* Ensure the fields are wide enough */
  27674. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  27675. + == 0);
  27676. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  27677. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  27678. + (unsigned int)VCHIQ_PORT_FREE);
  27679. +
  27680. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  27681. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  27682. +
  27683. +#define VCHIQ_FOURCC_INVALID 0x00000000
  27684. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  27685. +
  27686. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  27687. +
  27688. +typedef uint32_t BITSET_T;
  27689. +
  27690. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  27691. +
  27692. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  27693. +#define BITSET_WORD(b) (b >> 5)
  27694. +#define BITSET_BIT(b) (1 << (b & 31))
  27695. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  27696. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  27697. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  27698. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  27699. +
  27700. +#if VCHIQ_ENABLE_STATS
  27701. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  27702. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  27703. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  27704. + (service->stats. stat += addend)
  27705. +#else
  27706. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  27707. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  27708. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  27709. +#endif
  27710. +
  27711. +enum {
  27712. + DEBUG_ENTRIES,
  27713. +#if VCHIQ_ENABLE_DEBUG
  27714. + DEBUG_SLOT_HANDLER_COUNT,
  27715. + DEBUG_SLOT_HANDLER_LINE,
  27716. + DEBUG_PARSE_LINE,
  27717. + DEBUG_PARSE_HEADER,
  27718. + DEBUG_PARSE_MSGID,
  27719. + DEBUG_AWAIT_COMPLETION_LINE,
  27720. + DEBUG_DEQUEUE_MESSAGE_LINE,
  27721. + DEBUG_SERVICE_CALLBACK_LINE,
  27722. + DEBUG_MSG_QUEUE_FULL_COUNT,
  27723. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  27724. +#endif
  27725. + DEBUG_MAX
  27726. +};
  27727. +
  27728. +#if VCHIQ_ENABLE_DEBUG
  27729. +
  27730. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  27731. +#define DEBUG_TRACE(d) \
  27732. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  27733. +#define DEBUG_VALUE(d, v) \
  27734. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  27735. +#define DEBUG_COUNT(d) \
  27736. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  27737. +
  27738. +#else /* VCHIQ_ENABLE_DEBUG */
  27739. +
  27740. +#define DEBUG_INITIALISE(local)
  27741. +#define DEBUG_TRACE(d)
  27742. +#define DEBUG_VALUE(d, v)
  27743. +#define DEBUG_COUNT(d)
  27744. +
  27745. +#endif /* VCHIQ_ENABLE_DEBUG */
  27746. +
  27747. +typedef enum {
  27748. + VCHIQ_CONNSTATE_DISCONNECTED,
  27749. + VCHIQ_CONNSTATE_CONNECTING,
  27750. + VCHIQ_CONNSTATE_CONNECTED,
  27751. + VCHIQ_CONNSTATE_PAUSING,
  27752. + VCHIQ_CONNSTATE_PAUSE_SENT,
  27753. + VCHIQ_CONNSTATE_PAUSED,
  27754. + VCHIQ_CONNSTATE_RESUMING,
  27755. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  27756. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  27757. +} VCHIQ_CONNSTATE_T;
  27758. +
  27759. +enum {
  27760. + VCHIQ_SRVSTATE_FREE,
  27761. + VCHIQ_SRVSTATE_HIDDEN,
  27762. + VCHIQ_SRVSTATE_LISTENING,
  27763. + VCHIQ_SRVSTATE_OPENING,
  27764. + VCHIQ_SRVSTATE_OPEN,
  27765. + VCHIQ_SRVSTATE_OPENSYNC,
  27766. + VCHIQ_SRVSTATE_CLOSESENT,
  27767. + VCHIQ_SRVSTATE_CLOSERECVD,
  27768. + VCHIQ_SRVSTATE_CLOSEWAIT,
  27769. + VCHIQ_SRVSTATE_CLOSED
  27770. +};
  27771. +
  27772. +enum {
  27773. + VCHIQ_POLL_TERMINATE,
  27774. + VCHIQ_POLL_REMOVE,
  27775. + VCHIQ_POLL_TXNOTIFY,
  27776. + VCHIQ_POLL_RXNOTIFY,
  27777. + VCHIQ_POLL_COUNT
  27778. +};
  27779. +
  27780. +typedef enum {
  27781. + VCHIQ_BULK_TRANSMIT,
  27782. + VCHIQ_BULK_RECEIVE
  27783. +} VCHIQ_BULK_DIR_T;
  27784. +
  27785. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  27786. +
  27787. +typedef struct vchiq_bulk_struct {
  27788. + short mode;
  27789. + short dir;
  27790. + void *userdata;
  27791. + VCHI_MEM_HANDLE_T handle;
  27792. + void *data;
  27793. + int size;
  27794. + void *remote_data;
  27795. + int remote_size;
  27796. + int actual;
  27797. +} VCHIQ_BULK_T;
  27798. +
  27799. +typedef struct vchiq_bulk_queue_struct {
  27800. + int local_insert; /* Where to insert the next local bulk */
  27801. + int remote_insert; /* Where to insert the next remote bulk (master) */
  27802. + int process; /* Bulk to transfer next */
  27803. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  27804. + int remove; /* Bulk to notify the local client of, and remove,
  27805. + ** next */
  27806. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  27807. +} VCHIQ_BULK_QUEUE_T;
  27808. +
  27809. +typedef struct remote_event_struct {
  27810. + int armed;
  27811. + int fired;
  27812. + struct semaphore *event;
  27813. +} REMOTE_EVENT_T;
  27814. +
  27815. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  27816. +
  27817. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  27818. +
  27819. +typedef struct vchiq_slot_struct {
  27820. + char data[VCHIQ_SLOT_SIZE];
  27821. +} VCHIQ_SLOT_T;
  27822. +
  27823. +typedef struct vchiq_slot_info_struct {
  27824. + /* Use two counters rather than one to avoid the need for a mutex. */
  27825. + short use_count;
  27826. + short release_count;
  27827. +} VCHIQ_SLOT_INFO_T;
  27828. +
  27829. +typedef struct vchiq_service_struct {
  27830. + VCHIQ_SERVICE_BASE_T base;
  27831. + VCHIQ_SERVICE_HANDLE_T handle;
  27832. + unsigned int ref_count;
  27833. + int srvstate;
  27834. + VCHIQ_USERDATA_TERM_T userdata_term;
  27835. + unsigned int localport;
  27836. + unsigned int remoteport;
  27837. + int public_fourcc;
  27838. + int client_id;
  27839. + char auto_close;
  27840. + char sync;
  27841. + char closing;
  27842. + atomic_t poll_flags;
  27843. + short version;
  27844. + short version_min;
  27845. + short peer_version;
  27846. +
  27847. + VCHIQ_STATE_T *state;
  27848. + VCHIQ_INSTANCE_T instance;
  27849. +
  27850. + int service_use_count;
  27851. +
  27852. + VCHIQ_BULK_QUEUE_T bulk_tx;
  27853. + VCHIQ_BULK_QUEUE_T bulk_rx;
  27854. +
  27855. + struct semaphore remove_event;
  27856. + struct semaphore bulk_remove_event;
  27857. + struct mutex bulk_mutex;
  27858. +
  27859. + struct service_stats_struct {
  27860. + int quota_stalls;
  27861. + int slot_stalls;
  27862. + int bulk_stalls;
  27863. + int error_count;
  27864. + int ctrl_tx_count;
  27865. + int ctrl_rx_count;
  27866. + int bulk_tx_count;
  27867. + int bulk_rx_count;
  27868. + int bulk_aborted_count;
  27869. + uint64_t ctrl_tx_bytes;
  27870. + uint64_t ctrl_rx_bytes;
  27871. + uint64_t bulk_tx_bytes;
  27872. + uint64_t bulk_rx_bytes;
  27873. + } stats;
  27874. +} VCHIQ_SERVICE_T;
  27875. +
  27876. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  27877. + statically allocated, since for accounting reasons a service's slot
  27878. + usage is carried over between users of the same port number.
  27879. + */
  27880. +typedef struct vchiq_service_quota_struct {
  27881. + unsigned short slot_quota;
  27882. + unsigned short slot_use_count;
  27883. + unsigned short message_quota;
  27884. + unsigned short message_use_count;
  27885. + struct semaphore quota_event;
  27886. + int previous_tx_index;
  27887. +} VCHIQ_SERVICE_QUOTA_T;
  27888. +
  27889. +typedef struct vchiq_shared_state_struct {
  27890. +
  27891. + /* A non-zero value here indicates that the content is valid. */
  27892. + int initialised;
  27893. +
  27894. + /* The first and last (inclusive) slots allocated to the owner. */
  27895. + int slot_first;
  27896. + int slot_last;
  27897. +
  27898. + /* The slot allocated to synchronous messages from the owner. */
  27899. + int slot_sync;
  27900. +
  27901. + /* Signalling this event indicates that owner's slot handler thread
  27902. + ** should run. */
  27903. + REMOTE_EVENT_T trigger;
  27904. +
  27905. + /* Indicates the byte position within the stream where the next message
  27906. + ** will be written. The least significant bits are an index into the
  27907. + ** slot. The next bits are the index of the slot in slot_queue. */
  27908. + int tx_pos;
  27909. +
  27910. + /* This event should be signalled when a slot is recycled. */
  27911. + REMOTE_EVENT_T recycle;
  27912. +
  27913. + /* The slot_queue index where the next recycled slot will be written. */
  27914. + int slot_queue_recycle;
  27915. +
  27916. + /* This event should be signalled when a synchronous message is sent. */
  27917. + REMOTE_EVENT_T sync_trigger;
  27918. +
  27919. + /* This event should be signalled when a synchronous message has been
  27920. + ** released. */
  27921. + REMOTE_EVENT_T sync_release;
  27922. +
  27923. + /* A circular buffer of slot indexes. */
  27924. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  27925. +
  27926. + /* Debugging state */
  27927. + int debug[DEBUG_MAX];
  27928. +} VCHIQ_SHARED_STATE_T;
  27929. +
  27930. +typedef struct vchiq_slot_zero_struct {
  27931. + int magic;
  27932. + short version;
  27933. + short version_min;
  27934. + int slot_zero_size;
  27935. + int slot_size;
  27936. + int max_slots;
  27937. + int max_slots_per_side;
  27938. + int platform_data[2];
  27939. + VCHIQ_SHARED_STATE_T master;
  27940. + VCHIQ_SHARED_STATE_T slave;
  27941. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  27942. +} VCHIQ_SLOT_ZERO_T;
  27943. +
  27944. +struct vchiq_state_struct {
  27945. + int id;
  27946. + int initialised;
  27947. + VCHIQ_CONNSTATE_T conn_state;
  27948. + int is_master;
  27949. +
  27950. + VCHIQ_SHARED_STATE_T *local;
  27951. + VCHIQ_SHARED_STATE_T *remote;
  27952. + VCHIQ_SLOT_T *slot_data;
  27953. +
  27954. + unsigned short default_slot_quota;
  27955. + unsigned short default_message_quota;
  27956. +
  27957. + /* Event indicating connect message received */
  27958. + struct semaphore connect;
  27959. +
  27960. + /* Mutex protecting services */
  27961. + struct mutex mutex;
  27962. + VCHIQ_INSTANCE_T *instance;
  27963. +
  27964. + /* Processes incoming messages */
  27965. + struct task_struct *slot_handler_thread;
  27966. +
  27967. + /* Processes recycled slots */
  27968. + struct task_struct *recycle_thread;
  27969. +
  27970. + /* Processes synchronous messages */
  27971. + struct task_struct *sync_thread;
  27972. +
  27973. + /* Local implementation of the trigger remote event */
  27974. + struct semaphore trigger_event;
  27975. +
  27976. + /* Local implementation of the recycle remote event */
  27977. + struct semaphore recycle_event;
  27978. +
  27979. + /* Local implementation of the sync trigger remote event */
  27980. + struct semaphore sync_trigger_event;
  27981. +
  27982. + /* Local implementation of the sync release remote event */
  27983. + struct semaphore sync_release_event;
  27984. +
  27985. + char *tx_data;
  27986. + char *rx_data;
  27987. + VCHIQ_SLOT_INFO_T *rx_info;
  27988. +
  27989. + struct mutex slot_mutex;
  27990. +
  27991. + struct mutex recycle_mutex;
  27992. +
  27993. + struct mutex sync_mutex;
  27994. +
  27995. + struct mutex bulk_transfer_mutex;
  27996. +
  27997. + /* Indicates the byte position within the stream from where the next
  27998. + ** message will be read. The least significant bits are an index into
  27999. + ** the slot.The next bits are the index of the slot in
  28000. + ** remote->slot_queue. */
  28001. + int rx_pos;
  28002. +
  28003. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  28004. + from remote->tx_pos. */
  28005. + int local_tx_pos;
  28006. +
  28007. + /* The slot_queue index of the slot to become available next. */
  28008. + int slot_queue_available;
  28009. +
  28010. + /* A flag to indicate if any poll has been requested */
  28011. + int poll_needed;
  28012. +
  28013. + /* Ths index of the previous slot used for data messages. */
  28014. + int previous_data_index;
  28015. +
  28016. + /* The number of slots occupied by data messages. */
  28017. + unsigned short data_use_count;
  28018. +
  28019. + /* The maximum number of slots to be occupied by data messages. */
  28020. + unsigned short data_quota;
  28021. +
  28022. + /* An array of bit sets indicating which services must be polled. */
  28023. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  28024. +
  28025. + /* The number of the first unused service */
  28026. + int unused_service;
  28027. +
  28028. + /* Signalled when a free slot becomes available. */
  28029. + struct semaphore slot_available_event;
  28030. +
  28031. + struct semaphore slot_remove_event;
  28032. +
  28033. + /* Signalled when a free data slot becomes available. */
  28034. + struct semaphore data_quota_event;
  28035. +
  28036. + /* Incremented when there are bulk transfers which cannot be processed
  28037. + * whilst paused and must be processed on resume */
  28038. + int deferred_bulks;
  28039. +
  28040. + struct state_stats_struct {
  28041. + int slot_stalls;
  28042. + int data_stalls;
  28043. + int ctrl_tx_count;
  28044. + int ctrl_rx_count;
  28045. + int error_count;
  28046. + } stats;
  28047. +
  28048. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  28049. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  28050. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  28051. +
  28052. + VCHIQ_PLATFORM_STATE_T platform_state;
  28053. +};
  28054. +
  28055. +struct bulk_waiter {
  28056. + VCHIQ_BULK_T *bulk;
  28057. + struct semaphore event;
  28058. + int actual;
  28059. +};
  28060. +
  28061. +extern spinlock_t bulk_waiter_spinlock;
  28062. +
  28063. +extern int vchiq_core_log_level;
  28064. +extern int vchiq_core_msg_log_level;
  28065. +extern int vchiq_sync_log_level;
  28066. +
  28067. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  28068. +
  28069. +extern const char *
  28070. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  28071. +
  28072. +extern VCHIQ_SLOT_ZERO_T *
  28073. +vchiq_init_slots(void *mem_base, int mem_size);
  28074. +
  28075. +extern VCHIQ_STATUS_T
  28076. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  28077. + int is_master);
  28078. +
  28079. +extern VCHIQ_STATUS_T
  28080. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  28081. +
  28082. +extern VCHIQ_SERVICE_T *
  28083. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  28084. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  28085. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  28086. +
  28087. +extern VCHIQ_STATUS_T
  28088. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  28089. +
  28090. +extern VCHIQ_STATUS_T
  28091. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  28092. +
  28093. +extern void
  28094. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  28095. +
  28096. +extern void
  28097. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  28098. +
  28099. +extern VCHIQ_STATUS_T
  28100. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  28101. +
  28102. +extern VCHIQ_STATUS_T
  28103. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  28104. +
  28105. +extern VCHIQ_STATUS_T
  28106. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  28107. +
  28108. +extern void
  28109. +remote_event_pollall(VCHIQ_STATE_T *state);
  28110. +
  28111. +extern VCHIQ_STATUS_T
  28112. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  28113. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  28114. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  28115. +
  28116. +extern void
  28117. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  28118. +
  28119. +extern void
  28120. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  28121. +
  28122. +extern void
  28123. +vchiq_loud_error_header(void);
  28124. +
  28125. +extern void
  28126. +vchiq_loud_error_footer(void);
  28127. +
  28128. +extern void
  28129. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  28130. +
  28131. +static inline VCHIQ_SERVICE_T *
  28132. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  28133. +{
  28134. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  28135. + (VCHIQ_MAX_STATES - 1)];
  28136. + if (!state)
  28137. + return NULL;
  28138. +
  28139. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  28140. +}
  28141. +
  28142. +extern VCHIQ_SERVICE_T *
  28143. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  28144. +
  28145. +extern VCHIQ_SERVICE_T *
  28146. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  28147. +
  28148. +extern VCHIQ_SERVICE_T *
  28149. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  28150. + VCHIQ_SERVICE_HANDLE_T handle);
  28151. +
  28152. +extern VCHIQ_SERVICE_T *
  28153. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  28154. + int *pidx);
  28155. +
  28156. +extern void
  28157. +lock_service(VCHIQ_SERVICE_T *service);
  28158. +
  28159. +extern void
  28160. +unlock_service(VCHIQ_SERVICE_T *service);
  28161. +
  28162. +/* The following functions are called from vchiq_core, and external
  28163. +** implementations must be provided. */
  28164. +
  28165. +extern VCHIQ_STATUS_T
  28166. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  28167. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  28168. +
  28169. +extern void
  28170. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  28171. +
  28172. +extern void
  28173. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  28174. +
  28175. +extern VCHIQ_STATUS_T
  28176. +vchiq_copy_from_user(void *dst, const void *src, int size);
  28177. +
  28178. +extern void
  28179. +remote_event_signal(REMOTE_EVENT_T *event);
  28180. +
  28181. +void
  28182. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  28183. +
  28184. +extern void
  28185. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  28186. +
  28187. +extern VCHIQ_STATUS_T
  28188. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  28189. +
  28190. +extern void
  28191. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  28192. +
  28193. +extern void
  28194. +vchiq_dump(void *dump_context, const char *str, int len);
  28195. +
  28196. +extern void
  28197. +vchiq_dump_platform_state(void *dump_context);
  28198. +
  28199. +extern void
  28200. +vchiq_dump_platform_instances(void *dump_context);
  28201. +
  28202. +extern void
  28203. +vchiq_dump_platform_service_state(void *dump_context,
  28204. + VCHIQ_SERVICE_T *service);
  28205. +
  28206. +extern VCHIQ_STATUS_T
  28207. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  28208. +
  28209. +extern VCHIQ_STATUS_T
  28210. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  28211. +
  28212. +extern void
  28213. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  28214. +
  28215. +extern void
  28216. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  28217. +
  28218. +extern VCHIQ_STATUS_T
  28219. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  28220. +
  28221. +extern VCHIQ_STATUS_T
  28222. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  28223. +
  28224. +extern void
  28225. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  28226. +
  28227. +extern VCHIQ_STATUS_T
  28228. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  28229. +
  28230. +extern VCHIQ_STATUS_T
  28231. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  28232. +
  28233. +extern VCHIQ_STATUS_T
  28234. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  28235. +
  28236. +extern void
  28237. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  28238. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  28239. +
  28240. +extern void
  28241. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  28242. +
  28243. +extern void
  28244. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  28245. +
  28246. +
  28247. +extern void
  28248. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  28249. + size_t numBytes);
  28250. +
  28251. +#endif
  28252. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  28253. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 1970-01-01 01:00:00.000000000 +0100
  28254. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 2014-04-24 15:35:02.893551516 +0200
  28255. @@ -0,0 +1,89 @@
  28256. +#!/usr/bin/perl -w
  28257. +
  28258. +use strict;
  28259. +
  28260. +#
  28261. +# Generate a version from available information
  28262. +#
  28263. +
  28264. +my $prefix = shift @ARGV;
  28265. +my $root = shift @ARGV;
  28266. +
  28267. +
  28268. +if ( not defined $root ) {
  28269. + die "usage: $0 prefix root-dir\n";
  28270. +}
  28271. +
  28272. +if ( ! -d $root ) {
  28273. + die "root directory $root not found\n";
  28274. +}
  28275. +
  28276. +my $version = "unknown";
  28277. +my $tainted = "";
  28278. +
  28279. +if ( -d "$root/.git" ) {
  28280. + # attempt to work out git version. only do so
  28281. + # on a linux build host, as cygwin builds are
  28282. + # already slow enough
  28283. +
  28284. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  28285. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  28286. + $version = "no git version";
  28287. + }
  28288. + else {
  28289. + $version = <F>;
  28290. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  28291. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  28292. + }
  28293. +
  28294. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  28295. + $tainted = <G>;
  28296. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  28297. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  28298. + if (length $tainted) {
  28299. + $version = join ' ', $version, "(tainted)";
  28300. + }
  28301. + else {
  28302. + $version = join ' ', $version, "(clean)";
  28303. + }
  28304. + }
  28305. + }
  28306. +}
  28307. +
  28308. +my $hostname = `hostname`;
  28309. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  28310. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  28311. +
  28312. +
  28313. +print STDERR "Version $version\n";
  28314. +print <<EOF;
  28315. +#include "${prefix}_build_info.h"
  28316. +#include <linux/broadcom/vc_debug_sym.h>
  28317. +
  28318. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  28319. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  28320. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  28321. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  28322. +
  28323. +const char *vchiq_get_build_hostname( void )
  28324. +{
  28325. + return vchiq_build_hostname;
  28326. +}
  28327. +
  28328. +const char *vchiq_get_build_version( void )
  28329. +{
  28330. + return vchiq_build_version;
  28331. +}
  28332. +
  28333. +const char *vchiq_get_build_date( void )
  28334. +{
  28335. + return vchiq_build_date;
  28336. +}
  28337. +
  28338. +const char *vchiq_get_build_time( void )
  28339. +{
  28340. + return vchiq_build_time;
  28341. +}
  28342. +EOF
  28343. +
  28344. +
  28345. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  28346. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 1970-01-01 01:00:00.000000000 +0100
  28347. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 2014-04-24 15:35:02.893551516 +0200
  28348. @@ -0,0 +1,41 @@
  28349. +/**
  28350. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28351. + *
  28352. + * Redistribution and use in source and binary forms, with or without
  28353. + * modification, are permitted provided that the following conditions
  28354. + * are met:
  28355. + * 1. Redistributions of source code must retain the above copyright
  28356. + * notice, this list of conditions, and the following disclaimer,
  28357. + * without modification.
  28358. + * 2. Redistributions in binary form must reproduce the above copyright
  28359. + * notice, this list of conditions and the following disclaimer in the
  28360. + * documentation and/or other materials provided with the distribution.
  28361. + * 3. The names of the above-listed copyright holders may not be used
  28362. + * to endorse or promote products derived from this software without
  28363. + * specific prior written permission.
  28364. + *
  28365. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28366. + * GNU General Public License ("GPL") version 2, as published by the Free
  28367. + * Software Foundation.
  28368. + *
  28369. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28370. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28371. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28372. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28373. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28374. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28375. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28376. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28377. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28378. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28379. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28380. + */
  28381. +
  28382. +#ifndef VCHIQ_VCHIQ_H
  28383. +#define VCHIQ_VCHIQ_H
  28384. +
  28385. +#include "vchiq_if.h"
  28386. +#include "vchiq_util.h"
  28387. +
  28388. +#endif
  28389. +
  28390. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  28391. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 1970-01-01 01:00:00.000000000 +0100
  28392. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 2014-04-24 15:35:02.893551516 +0200
  28393. @@ -0,0 +1,188 @@
  28394. +/**
  28395. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28396. + *
  28397. + * Redistribution and use in source and binary forms, with or without
  28398. + * modification, are permitted provided that the following conditions
  28399. + * are met:
  28400. + * 1. Redistributions of source code must retain the above copyright
  28401. + * notice, this list of conditions, and the following disclaimer,
  28402. + * without modification.
  28403. + * 2. Redistributions in binary form must reproduce the above copyright
  28404. + * notice, this list of conditions and the following disclaimer in the
  28405. + * documentation and/or other materials provided with the distribution.
  28406. + * 3. The names of the above-listed copyright holders may not be used
  28407. + * to endorse or promote products derived from this software without
  28408. + * specific prior written permission.
  28409. + *
  28410. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28411. + * GNU General Public License ("GPL") version 2, as published by the Free
  28412. + * Software Foundation.
  28413. + *
  28414. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28415. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28416. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28417. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28418. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28419. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28420. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28421. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28422. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28423. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28424. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28425. + */
  28426. +
  28427. +#ifndef VCHIQ_IF_H
  28428. +#define VCHIQ_IF_H
  28429. +
  28430. +#include "interface/vchi/vchi_mh.h"
  28431. +
  28432. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  28433. +
  28434. +#define VCHIQ_SLOT_SIZE 4096
  28435. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  28436. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  28437. +
  28438. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  28439. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  28440. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  28441. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  28442. +
  28443. +typedef enum {
  28444. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  28445. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  28446. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  28447. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  28448. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  28449. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  28450. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  28451. +} VCHIQ_REASON_T;
  28452. +
  28453. +typedef enum {
  28454. + VCHIQ_ERROR = -1,
  28455. + VCHIQ_SUCCESS = 0,
  28456. + VCHIQ_RETRY = 1
  28457. +} VCHIQ_STATUS_T;
  28458. +
  28459. +typedef enum {
  28460. + VCHIQ_BULK_MODE_CALLBACK,
  28461. + VCHIQ_BULK_MODE_BLOCKING,
  28462. + VCHIQ_BULK_MODE_NOCALLBACK,
  28463. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  28464. +} VCHIQ_BULK_MODE_T;
  28465. +
  28466. +typedef enum {
  28467. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  28468. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  28469. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  28470. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS
  28471. +} VCHIQ_SERVICE_OPTION_T;
  28472. +
  28473. +typedef struct vchiq_header_struct {
  28474. + /* The message identifier - opaque to applications. */
  28475. + int msgid;
  28476. +
  28477. + /* Size of message data. */
  28478. + unsigned int size;
  28479. +
  28480. + char data[0]; /* message */
  28481. +} VCHIQ_HEADER_T;
  28482. +
  28483. +typedef struct {
  28484. + const void *data;
  28485. + unsigned int size;
  28486. +} VCHIQ_ELEMENT_T;
  28487. +
  28488. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  28489. +
  28490. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  28491. + VCHIQ_SERVICE_HANDLE_T, void *);
  28492. +
  28493. +typedef struct vchiq_service_base_struct {
  28494. + int fourcc;
  28495. + VCHIQ_CALLBACK_T callback;
  28496. + void *userdata;
  28497. +} VCHIQ_SERVICE_BASE_T;
  28498. +
  28499. +typedef struct vchiq_service_params_struct {
  28500. + int fourcc;
  28501. + VCHIQ_CALLBACK_T callback;
  28502. + void *userdata;
  28503. + short version; /* Increment for non-trivial changes */
  28504. + short version_min; /* Update for incompatible changes */
  28505. +} VCHIQ_SERVICE_PARAMS_T;
  28506. +
  28507. +typedef struct vchiq_config_struct {
  28508. + unsigned int max_msg_size;
  28509. + unsigned int bulk_threshold; /* The message size above which it
  28510. + is better to use a bulk transfer
  28511. + (<= max_msg_size) */
  28512. + unsigned int max_outstanding_bulks;
  28513. + unsigned int max_services;
  28514. + short version; /* The version of VCHIQ */
  28515. + short version_min; /* The minimum compatible version of VCHIQ */
  28516. +} VCHIQ_CONFIG_T;
  28517. +
  28518. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  28519. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  28520. +
  28521. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  28522. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  28523. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  28524. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  28525. + const VCHIQ_SERVICE_PARAMS_T *params,
  28526. + VCHIQ_SERVICE_HANDLE_T *pservice);
  28527. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  28528. + const VCHIQ_SERVICE_PARAMS_T *params,
  28529. + VCHIQ_SERVICE_HANDLE_T *pservice);
  28530. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  28531. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  28532. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  28533. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  28534. + VCHIQ_SERVICE_HANDLE_T service);
  28535. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  28536. +
  28537. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  28538. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  28539. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  28540. + VCHIQ_HEADER_T *header);
  28541. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  28542. + const void *data, unsigned int size, void *userdata);
  28543. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  28544. + void *data, unsigned int size, void *userdata);
  28545. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  28546. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  28547. + const void *offset, unsigned int size, void *userdata);
  28548. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  28549. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  28550. + void *offset, unsigned int size, void *userdata);
  28551. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  28552. + const void *data, unsigned int size, void *userdata,
  28553. + VCHIQ_BULK_MODE_T mode);
  28554. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  28555. + void *data, unsigned int size, void *userdata,
  28556. + VCHIQ_BULK_MODE_T mode);
  28557. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  28558. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  28559. + void *userdata, VCHIQ_BULK_MODE_T mode);
  28560. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  28561. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  28562. + void *userdata, VCHIQ_BULK_MODE_T mode);
  28563. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  28564. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  28565. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  28566. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  28567. + int config_size, VCHIQ_CONFIG_T *pconfig);
  28568. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  28569. + VCHIQ_SERVICE_OPTION_T option, int value);
  28570. +
  28571. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  28572. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  28573. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  28574. +
  28575. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  28576. + void *ptr, size_t num_bytes);
  28577. +
  28578. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  28579. + short *peer_version);
  28580. +
  28581. +#endif /* VCHIQ_IF_H */
  28582. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  28583. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 1970-01-01 01:00:00.000000000 +0100
  28584. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 2014-04-24 15:35:02.893551516 +0200
  28585. @@ -0,0 +1,129 @@
  28586. +/**
  28587. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28588. + *
  28589. + * Redistribution and use in source and binary forms, with or without
  28590. + * modification, are permitted provided that the following conditions
  28591. + * are met:
  28592. + * 1. Redistributions of source code must retain the above copyright
  28593. + * notice, this list of conditions, and the following disclaimer,
  28594. + * without modification.
  28595. + * 2. Redistributions in binary form must reproduce the above copyright
  28596. + * notice, this list of conditions and the following disclaimer in the
  28597. + * documentation and/or other materials provided with the distribution.
  28598. + * 3. The names of the above-listed copyright holders may not be used
  28599. + * to endorse or promote products derived from this software without
  28600. + * specific prior written permission.
  28601. + *
  28602. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28603. + * GNU General Public License ("GPL") version 2, as published by the Free
  28604. + * Software Foundation.
  28605. + *
  28606. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28607. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28608. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28609. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28610. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28611. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28612. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28613. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28614. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28615. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28616. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28617. + */
  28618. +
  28619. +#ifndef VCHIQ_IOCTLS_H
  28620. +#define VCHIQ_IOCTLS_H
  28621. +
  28622. +#include <linux/ioctl.h>
  28623. +#include "vchiq_if.h"
  28624. +
  28625. +#define VCHIQ_IOC_MAGIC 0xc4
  28626. +#define VCHIQ_INVALID_HANDLE (~0)
  28627. +
  28628. +typedef struct {
  28629. + VCHIQ_SERVICE_PARAMS_T params;
  28630. + int is_open;
  28631. + int is_vchi;
  28632. + unsigned int handle; /* OUT */
  28633. +} VCHIQ_CREATE_SERVICE_T;
  28634. +
  28635. +typedef struct {
  28636. + unsigned int handle;
  28637. + unsigned int count;
  28638. + const VCHIQ_ELEMENT_T *elements;
  28639. +} VCHIQ_QUEUE_MESSAGE_T;
  28640. +
  28641. +typedef struct {
  28642. + unsigned int handle;
  28643. + void *data;
  28644. + unsigned int size;
  28645. + void *userdata;
  28646. + VCHIQ_BULK_MODE_T mode;
  28647. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  28648. +
  28649. +typedef struct {
  28650. + VCHIQ_REASON_T reason;
  28651. + VCHIQ_HEADER_T *header;
  28652. + void *service_userdata;
  28653. + void *bulk_userdata;
  28654. +} VCHIQ_COMPLETION_DATA_T;
  28655. +
  28656. +typedef struct {
  28657. + unsigned int count;
  28658. + VCHIQ_COMPLETION_DATA_T *buf;
  28659. + unsigned int msgbufsize;
  28660. + unsigned int msgbufcount; /* IN/OUT */
  28661. + void **msgbufs;
  28662. +} VCHIQ_AWAIT_COMPLETION_T;
  28663. +
  28664. +typedef struct {
  28665. + unsigned int handle;
  28666. + int blocking;
  28667. + unsigned int bufsize;
  28668. + void *buf;
  28669. +} VCHIQ_DEQUEUE_MESSAGE_T;
  28670. +
  28671. +typedef struct {
  28672. + unsigned int config_size;
  28673. + VCHIQ_CONFIG_T *pconfig;
  28674. +} VCHIQ_GET_CONFIG_T;
  28675. +
  28676. +typedef struct {
  28677. + unsigned int handle;
  28678. + VCHIQ_SERVICE_OPTION_T option;
  28679. + int value;
  28680. +} VCHIQ_SET_SERVICE_OPTION_T;
  28681. +
  28682. +typedef struct {
  28683. + void *virt_addr;
  28684. + size_t num_bytes;
  28685. +} VCHIQ_DUMP_MEM_T;
  28686. +
  28687. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  28688. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  28689. +#define VCHIQ_IOC_CREATE_SERVICE \
  28690. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  28691. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  28692. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  28693. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  28694. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  28695. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  28696. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  28697. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  28698. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  28699. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  28700. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  28701. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  28702. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  28703. +#define VCHIQ_IOC_GET_CONFIG \
  28704. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  28705. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  28706. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  28707. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  28708. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  28709. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  28710. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  28711. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  28712. +#define VCHIQ_IOC_MAX 15
  28713. +
  28714. +#endif
  28715. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  28716. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 1970-01-01 01:00:00.000000000 +0100
  28717. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 2014-04-24 15:35:02.893551516 +0200
  28718. @@ -0,0 +1,456 @@
  28719. +/**
  28720. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28721. + *
  28722. + * Redistribution and use in source and binary forms, with or without
  28723. + * modification, are permitted provided that the following conditions
  28724. + * are met:
  28725. + * 1. Redistributions of source code must retain the above copyright
  28726. + * notice, this list of conditions, and the following disclaimer,
  28727. + * without modification.
  28728. + * 2. Redistributions in binary form must reproduce the above copyright
  28729. + * notice, this list of conditions and the following disclaimer in the
  28730. + * documentation and/or other materials provided with the distribution.
  28731. + * 3. The names of the above-listed copyright holders may not be used
  28732. + * to endorse or promote products derived from this software without
  28733. + * specific prior written permission.
  28734. + *
  28735. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28736. + * GNU General Public License ("GPL") version 2, as published by the Free
  28737. + * Software Foundation.
  28738. + *
  28739. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28740. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28741. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28742. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28743. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28744. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28745. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28746. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28747. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28748. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28749. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28750. + */
  28751. +
  28752. +/* ---- Include Files ---------------------------------------------------- */
  28753. +
  28754. +#include <linux/kernel.h>
  28755. +#include <linux/module.h>
  28756. +#include <linux/mutex.h>
  28757. +
  28758. +#include "vchiq_core.h"
  28759. +#include "vchiq_arm.h"
  28760. +
  28761. +/* ---- Public Variables ------------------------------------------------- */
  28762. +
  28763. +/* ---- Private Constants and Types -------------------------------------- */
  28764. +
  28765. +struct bulk_waiter_node {
  28766. + struct bulk_waiter bulk_waiter;
  28767. + int pid;
  28768. + struct list_head list;
  28769. +};
  28770. +
  28771. +struct vchiq_instance_struct {
  28772. + VCHIQ_STATE_T *state;
  28773. +
  28774. + int connected;
  28775. +
  28776. + struct list_head bulk_waiter_list;
  28777. + struct mutex bulk_waiter_list_mutex;
  28778. +};
  28779. +
  28780. +static VCHIQ_STATUS_T
  28781. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28782. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  28783. +
  28784. +/****************************************************************************
  28785. +*
  28786. +* vchiq_initialise
  28787. +*
  28788. +***************************************************************************/
  28789. +#define VCHIQ_INIT_RETRIES 10
  28790. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  28791. +{
  28792. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28793. + VCHIQ_STATE_T *state;
  28794. + VCHIQ_INSTANCE_T instance = NULL;
  28795. + int i;
  28796. +
  28797. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  28798. +
  28799. + /* VideoCore may not be ready due to boot up timing.
  28800. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  28801. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  28802. + state = vchiq_get_state();
  28803. + if (state)
  28804. + break;
  28805. + udelay(500);
  28806. + }
  28807. + if (i==VCHIQ_INIT_RETRIES) {
  28808. + vchiq_log_error(vchiq_core_log_level,
  28809. + "%s: videocore not initialized\n", __func__);
  28810. + goto failed;
  28811. + } else if (i>0) {
  28812. + vchiq_log_warning(vchiq_core_log_level,
  28813. + "%s: videocore initialized after %d retries\n", __func__, i);
  28814. + }
  28815. +
  28816. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  28817. + if (!instance) {
  28818. + vchiq_log_error(vchiq_core_log_level,
  28819. + "%s: error allocating vchiq instance\n", __func__);
  28820. + goto failed;
  28821. + }
  28822. +
  28823. + instance->connected = 0;
  28824. + instance->state = state;
  28825. + mutex_init(&instance->bulk_waiter_list_mutex);
  28826. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  28827. +
  28828. + *instanceOut = instance;
  28829. +
  28830. + status = VCHIQ_SUCCESS;
  28831. +
  28832. +failed:
  28833. + vchiq_log_trace(vchiq_core_log_level,
  28834. + "%s(%p): returning %d", __func__, instance, status);
  28835. +
  28836. + return status;
  28837. +}
  28838. +EXPORT_SYMBOL(vchiq_initialise);
  28839. +
  28840. +/****************************************************************************
  28841. +*
  28842. +* vchiq_shutdown
  28843. +*
  28844. +***************************************************************************/
  28845. +
  28846. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  28847. +{
  28848. + VCHIQ_STATUS_T status;
  28849. + VCHIQ_STATE_T *state = instance->state;
  28850. +
  28851. + vchiq_log_trace(vchiq_core_log_level,
  28852. + "%s(%p) called", __func__, instance);
  28853. +
  28854. + if (mutex_lock_interruptible(&state->mutex) != 0)
  28855. + return VCHIQ_RETRY;
  28856. +
  28857. + /* Remove all services */
  28858. + status = vchiq_shutdown_internal(state, instance);
  28859. +
  28860. + mutex_unlock(&state->mutex);
  28861. +
  28862. + vchiq_log_trace(vchiq_core_log_level,
  28863. + "%s(%p): returning %d", __func__, instance, status);
  28864. +
  28865. + if (status == VCHIQ_SUCCESS) {
  28866. + struct list_head *pos, *next;
  28867. + list_for_each_safe(pos, next,
  28868. + &instance->bulk_waiter_list) {
  28869. + struct bulk_waiter_node *waiter;
  28870. + waiter = list_entry(pos,
  28871. + struct bulk_waiter_node,
  28872. + list);
  28873. + list_del(pos);
  28874. + vchiq_log_info(vchiq_arm_log_level,
  28875. + "bulk_waiter - cleaned up %x "
  28876. + "for pid %d",
  28877. + (unsigned int)waiter, waiter->pid);
  28878. + kfree(waiter);
  28879. + }
  28880. + kfree(instance);
  28881. + }
  28882. +
  28883. + return status;
  28884. +}
  28885. +EXPORT_SYMBOL(vchiq_shutdown);
  28886. +
  28887. +/****************************************************************************
  28888. +*
  28889. +* vchiq_is_connected
  28890. +*
  28891. +***************************************************************************/
  28892. +
  28893. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  28894. +{
  28895. + return instance->connected;
  28896. +}
  28897. +
  28898. +/****************************************************************************
  28899. +*
  28900. +* vchiq_connect
  28901. +*
  28902. +***************************************************************************/
  28903. +
  28904. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  28905. +{
  28906. + VCHIQ_STATUS_T status;
  28907. + VCHIQ_STATE_T *state = instance->state;
  28908. +
  28909. + vchiq_log_trace(vchiq_core_log_level,
  28910. + "%s(%p) called", __func__, instance);
  28911. +
  28912. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  28913. + vchiq_log_trace(vchiq_core_log_level,
  28914. + "%s: call to mutex_lock failed", __func__);
  28915. + status = VCHIQ_RETRY;
  28916. + goto failed;
  28917. + }
  28918. + status = vchiq_connect_internal(state, instance);
  28919. +
  28920. + if (status == VCHIQ_SUCCESS)
  28921. + instance->connected = 1;
  28922. +
  28923. + mutex_unlock(&state->mutex);
  28924. +
  28925. +failed:
  28926. + vchiq_log_trace(vchiq_core_log_level,
  28927. + "%s(%p): returning %d", __func__, instance, status);
  28928. +
  28929. + return status;
  28930. +}
  28931. +EXPORT_SYMBOL(vchiq_connect);
  28932. +
  28933. +/****************************************************************************
  28934. +*
  28935. +* vchiq_add_service
  28936. +*
  28937. +***************************************************************************/
  28938. +
  28939. +VCHIQ_STATUS_T vchiq_add_service(
  28940. + VCHIQ_INSTANCE_T instance,
  28941. + const VCHIQ_SERVICE_PARAMS_T *params,
  28942. + VCHIQ_SERVICE_HANDLE_T *phandle)
  28943. +{
  28944. + VCHIQ_STATUS_T status;
  28945. + VCHIQ_STATE_T *state = instance->state;
  28946. + VCHIQ_SERVICE_T *service = NULL;
  28947. + int srvstate;
  28948. +
  28949. + vchiq_log_trace(vchiq_core_log_level,
  28950. + "%s(%p) called", __func__, instance);
  28951. +
  28952. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  28953. +
  28954. + srvstate = vchiq_is_connected(instance)
  28955. + ? VCHIQ_SRVSTATE_LISTENING
  28956. + : VCHIQ_SRVSTATE_HIDDEN;
  28957. +
  28958. + service = vchiq_add_service_internal(
  28959. + state,
  28960. + params,
  28961. + srvstate,
  28962. + instance,
  28963. + NULL);
  28964. +
  28965. + if (service) {
  28966. + *phandle = service->handle;
  28967. + status = VCHIQ_SUCCESS;
  28968. + } else
  28969. + status = VCHIQ_ERROR;
  28970. +
  28971. + vchiq_log_trace(vchiq_core_log_level,
  28972. + "%s(%p): returning %d", __func__, instance, status);
  28973. +
  28974. + return status;
  28975. +}
  28976. +EXPORT_SYMBOL(vchiq_add_service);
  28977. +
  28978. +/****************************************************************************
  28979. +*
  28980. +* vchiq_open_service
  28981. +*
  28982. +***************************************************************************/
  28983. +
  28984. +VCHIQ_STATUS_T vchiq_open_service(
  28985. + VCHIQ_INSTANCE_T instance,
  28986. + const VCHIQ_SERVICE_PARAMS_T *params,
  28987. + VCHIQ_SERVICE_HANDLE_T *phandle)
  28988. +{
  28989. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28990. + VCHIQ_STATE_T *state = instance->state;
  28991. + VCHIQ_SERVICE_T *service = NULL;
  28992. +
  28993. + vchiq_log_trace(vchiq_core_log_level,
  28994. + "%s(%p) called", __func__, instance);
  28995. +
  28996. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  28997. +
  28998. + if (!vchiq_is_connected(instance))
  28999. + goto failed;
  29000. +
  29001. + service = vchiq_add_service_internal(state,
  29002. + params,
  29003. + VCHIQ_SRVSTATE_OPENING,
  29004. + instance,
  29005. + NULL);
  29006. +
  29007. + if (service) {
  29008. + status = vchiq_open_service_internal(service, current->pid);
  29009. + if (status == VCHIQ_SUCCESS)
  29010. + *phandle = service->handle;
  29011. + else
  29012. + vchiq_remove_service(service->handle);
  29013. + }
  29014. +
  29015. +failed:
  29016. + vchiq_log_trace(vchiq_core_log_level,
  29017. + "%s(%p): returning %d", __func__, instance, status);
  29018. +
  29019. + return status;
  29020. +}
  29021. +EXPORT_SYMBOL(vchiq_open_service);
  29022. +
  29023. +VCHIQ_STATUS_T
  29024. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  29025. + const void *data, unsigned int size, void *userdata)
  29026. +{
  29027. + return vchiq_bulk_transfer(handle,
  29028. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  29029. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  29030. +}
  29031. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  29032. +
  29033. +VCHIQ_STATUS_T
  29034. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  29035. + unsigned int size, void *userdata)
  29036. +{
  29037. + return vchiq_bulk_transfer(handle,
  29038. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  29039. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  29040. +}
  29041. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  29042. +
  29043. +VCHIQ_STATUS_T
  29044. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  29045. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  29046. +{
  29047. + VCHIQ_STATUS_T status;
  29048. +
  29049. + switch (mode) {
  29050. + case VCHIQ_BULK_MODE_NOCALLBACK:
  29051. + case VCHIQ_BULK_MODE_CALLBACK:
  29052. + status = vchiq_bulk_transfer(handle,
  29053. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  29054. + mode, VCHIQ_BULK_TRANSMIT);
  29055. + break;
  29056. + case VCHIQ_BULK_MODE_BLOCKING:
  29057. + status = vchiq_blocking_bulk_transfer(handle,
  29058. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  29059. + break;
  29060. + default:
  29061. + return VCHIQ_ERROR;
  29062. + }
  29063. +
  29064. + return status;
  29065. +}
  29066. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  29067. +
  29068. +VCHIQ_STATUS_T
  29069. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  29070. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  29071. +{
  29072. + VCHIQ_STATUS_T status;
  29073. +
  29074. + switch (mode) {
  29075. + case VCHIQ_BULK_MODE_NOCALLBACK:
  29076. + case VCHIQ_BULK_MODE_CALLBACK:
  29077. + status = vchiq_bulk_transfer(handle,
  29078. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  29079. + mode, VCHIQ_BULK_RECEIVE);
  29080. + break;
  29081. + case VCHIQ_BULK_MODE_BLOCKING:
  29082. + status = vchiq_blocking_bulk_transfer(handle,
  29083. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  29084. + break;
  29085. + default:
  29086. + return VCHIQ_ERROR;
  29087. + }
  29088. +
  29089. + return status;
  29090. +}
  29091. +EXPORT_SYMBOL(vchiq_bulk_receive);
  29092. +
  29093. +static VCHIQ_STATUS_T
  29094. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  29095. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  29096. +{
  29097. + VCHIQ_INSTANCE_T instance;
  29098. + VCHIQ_SERVICE_T *service;
  29099. + VCHIQ_STATUS_T status;
  29100. + struct bulk_waiter_node *waiter = NULL;
  29101. + struct list_head *pos;
  29102. +
  29103. + service = find_service_by_handle(handle);
  29104. + if (!service)
  29105. + return VCHIQ_ERROR;
  29106. +
  29107. + instance = service->instance;
  29108. +
  29109. + unlock_service(service);
  29110. +
  29111. + mutex_lock(&instance->bulk_waiter_list_mutex);
  29112. + list_for_each(pos, &instance->bulk_waiter_list) {
  29113. + if (list_entry(pos, struct bulk_waiter_node,
  29114. + list)->pid == current->pid) {
  29115. + waiter = list_entry(pos,
  29116. + struct bulk_waiter_node,
  29117. + list);
  29118. + list_del(pos);
  29119. + break;
  29120. + }
  29121. + }
  29122. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  29123. +
  29124. + if (waiter) {
  29125. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  29126. + if (bulk) {
  29127. + /* This thread has an outstanding bulk transfer. */
  29128. + if ((bulk->data != data) ||
  29129. + (bulk->size != size)) {
  29130. + /* This is not a retry of the previous one.
  29131. + ** Cancel the signal when the transfer
  29132. + ** completes. */
  29133. + spin_lock(&bulk_waiter_spinlock);
  29134. + bulk->userdata = NULL;
  29135. + spin_unlock(&bulk_waiter_spinlock);
  29136. + }
  29137. + }
  29138. + }
  29139. +
  29140. + if (!waiter) {
  29141. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  29142. + if (!waiter) {
  29143. + vchiq_log_error(vchiq_core_log_level,
  29144. + "%s - out of memory", __func__);
  29145. + return VCHIQ_ERROR;
  29146. + }
  29147. + }
  29148. +
  29149. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  29150. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  29151. + dir);
  29152. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  29153. + !waiter->bulk_waiter.bulk) {
  29154. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  29155. + if (bulk) {
  29156. + /* Cancel the signal when the transfer
  29157. + ** completes. */
  29158. + spin_lock(&bulk_waiter_spinlock);
  29159. + bulk->userdata = NULL;
  29160. + spin_unlock(&bulk_waiter_spinlock);
  29161. + }
  29162. + kfree(waiter);
  29163. + } else {
  29164. + waiter->pid = current->pid;
  29165. + mutex_lock(&instance->bulk_waiter_list_mutex);
  29166. + list_add(&waiter->list, &instance->bulk_waiter_list);
  29167. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  29168. + vchiq_log_info(vchiq_arm_log_level,
  29169. + "saved bulk_waiter %x for pid %d",
  29170. + (unsigned int)waiter, current->pid);
  29171. + }
  29172. +
  29173. + return status;
  29174. +}
  29175. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  29176. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 1970-01-01 01:00:00.000000000 +0100
  29177. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 2014-04-24 15:35:02.893551516 +0200
  29178. @@ -0,0 +1,71 @@
  29179. +/**
  29180. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29181. + *
  29182. + * Redistribution and use in source and binary forms, with or without
  29183. + * modification, are permitted provided that the following conditions
  29184. + * are met:
  29185. + * 1. Redistributions of source code must retain the above copyright
  29186. + * notice, this list of conditions, and the following disclaimer,
  29187. + * without modification.
  29188. + * 2. Redistributions in binary form must reproduce the above copyright
  29189. + * notice, this list of conditions and the following disclaimer in the
  29190. + * documentation and/or other materials provided with the distribution.
  29191. + * 3. The names of the above-listed copyright holders may not be used
  29192. + * to endorse or promote products derived from this software without
  29193. + * specific prior written permission.
  29194. + *
  29195. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29196. + * GNU General Public License ("GPL") version 2, as published by the Free
  29197. + * Software Foundation.
  29198. + *
  29199. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29200. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29201. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29202. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29203. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29204. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29205. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29206. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29207. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29208. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29209. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29210. + */
  29211. +
  29212. +#ifndef VCHIQ_MEMDRV_H
  29213. +#define VCHIQ_MEMDRV_H
  29214. +
  29215. +/* ---- Include Files ----------------------------------------------------- */
  29216. +
  29217. +#include <linux/kernel.h>
  29218. +#include "vchiq_if.h"
  29219. +
  29220. +/* ---- Constants and Types ---------------------------------------------- */
  29221. +
  29222. +typedef struct {
  29223. + void *armSharedMemVirt;
  29224. + dma_addr_t armSharedMemPhys;
  29225. + size_t armSharedMemSize;
  29226. +
  29227. + void *vcSharedMemVirt;
  29228. + dma_addr_t vcSharedMemPhys;
  29229. + size_t vcSharedMemSize;
  29230. +} VCHIQ_SHARED_MEM_INFO_T;
  29231. +
  29232. +/* ---- Variable Externs ------------------------------------------------- */
  29233. +
  29234. +/* ---- Function Prototypes ---------------------------------------------- */
  29235. +
  29236. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  29237. +
  29238. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  29239. +
  29240. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  29241. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  29242. +
  29243. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  29244. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  29245. +
  29246. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  29247. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  29248. +
  29249. +#endif
  29250. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  29251. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 1970-01-01 01:00:00.000000000 +0100
  29252. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 2014-04-24 15:35:02.893551516 +0200
  29253. @@ -0,0 +1,58 @@
  29254. +/**
  29255. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29256. + *
  29257. + * Redistribution and use in source and binary forms, with or without
  29258. + * modification, are permitted provided that the following conditions
  29259. + * are met:
  29260. + * 1. Redistributions of source code must retain the above copyright
  29261. + * notice, this list of conditions, and the following disclaimer,
  29262. + * without modification.
  29263. + * 2. Redistributions in binary form must reproduce the above copyright
  29264. + * notice, this list of conditions and the following disclaimer in the
  29265. + * documentation and/or other materials provided with the distribution.
  29266. + * 3. The names of the above-listed copyright holders may not be used
  29267. + * to endorse or promote products derived from this software without
  29268. + * specific prior written permission.
  29269. + *
  29270. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29271. + * GNU General Public License ("GPL") version 2, as published by the Free
  29272. + * Software Foundation.
  29273. + *
  29274. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29275. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29276. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29277. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29278. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29279. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29280. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29281. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29282. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29283. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29284. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29285. + */
  29286. +
  29287. +#ifndef VCHIQ_PAGELIST_H
  29288. +#define VCHIQ_PAGELIST_H
  29289. +
  29290. +#ifndef PAGE_SIZE
  29291. +#define PAGE_SIZE 4096
  29292. +#endif
  29293. +#define CACHE_LINE_SIZE 32
  29294. +#define PAGELIST_WRITE 0
  29295. +#define PAGELIST_READ 1
  29296. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  29297. +
  29298. +typedef struct pagelist_struct {
  29299. + unsigned long length;
  29300. + unsigned short type;
  29301. + unsigned short offset;
  29302. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  29303. + pages at consecutive addresses. */
  29304. +} PAGELIST_T;
  29305. +
  29306. +typedef struct fragments_struct {
  29307. + char headbuf[CACHE_LINE_SIZE];
  29308. + char tailbuf[CACHE_LINE_SIZE];
  29309. +} FRAGMENTS_T;
  29310. +
  29311. +#endif /* VCHIQ_PAGELIST_H */
  29312. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
  29313. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 1970-01-01 01:00:00.000000000 +0100
  29314. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 2014-04-24 15:35:02.893551516 +0200
  29315. @@ -0,0 +1,254 @@
  29316. +/**
  29317. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29318. + *
  29319. + * Redistribution and use in source and binary forms, with or without
  29320. + * modification, are permitted provided that the following conditions
  29321. + * are met:
  29322. + * 1. Redistributions of source code must retain the above copyright
  29323. + * notice, this list of conditions, and the following disclaimer,
  29324. + * without modification.
  29325. + * 2. Redistributions in binary form must reproduce the above copyright
  29326. + * notice, this list of conditions and the following disclaimer in the
  29327. + * documentation and/or other materials provided with the distribution.
  29328. + * 3. The names of the above-listed copyright holders may not be used
  29329. + * to endorse or promote products derived from this software without
  29330. + * specific prior written permission.
  29331. + *
  29332. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29333. + * GNU General Public License ("GPL") version 2, as published by the Free
  29334. + * Software Foundation.
  29335. + *
  29336. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29337. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29338. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29339. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29340. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29341. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29342. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29343. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29344. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29345. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29346. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29347. + */
  29348. +
  29349. +
  29350. +#include <linux/proc_fs.h>
  29351. +#include "vchiq_core.h"
  29352. +#include "vchiq_arm.h"
  29353. +
  29354. +#if 1
  29355. +
  29356. +int vchiq_proc_init(void)
  29357. +{
  29358. + return 0;
  29359. +}
  29360. +
  29361. +void vchiq_proc_deinit(void)
  29362. +{
  29363. +}
  29364. +
  29365. +#else
  29366. +
  29367. +struct vchiq_proc_info {
  29368. + /* Global 'vc' proc entry used by all instances */
  29369. + struct proc_dir_entry *vc_cfg_dir;
  29370. +
  29371. + /* one entry per client process */
  29372. + struct proc_dir_entry *clients;
  29373. +
  29374. + /* log categories */
  29375. + struct proc_dir_entry *log_categories;
  29376. +};
  29377. +
  29378. +static struct vchiq_proc_info proc_info;
  29379. +
  29380. +struct proc_dir_entry *vchiq_proc_top(void)
  29381. +{
  29382. + BUG_ON(proc_info.vc_cfg_dir == NULL);
  29383. + return proc_info.vc_cfg_dir;
  29384. +}
  29385. +
  29386. +/****************************************************************************
  29387. +*
  29388. +* log category entries
  29389. +*
  29390. +***************************************************************************/
  29391. +#define PROC_WRITE_BUF_SIZE 256
  29392. +
  29393. +#define VCHIQ_LOG_ERROR_STR "error"
  29394. +#define VCHIQ_LOG_WARNING_STR "warning"
  29395. +#define VCHIQ_LOG_INFO_STR "info"
  29396. +#define VCHIQ_LOG_TRACE_STR "trace"
  29397. +
  29398. +static int log_cfg_read(char *buffer,
  29399. + char **start,
  29400. + off_t off,
  29401. + int count,
  29402. + int *eof,
  29403. + void *data)
  29404. +{
  29405. + int len = 0;
  29406. + char *log_value = NULL;
  29407. +
  29408. + switch (*((int *)data)) {
  29409. + case VCHIQ_LOG_ERROR:
  29410. + log_value = VCHIQ_LOG_ERROR_STR;
  29411. + break;
  29412. + case VCHIQ_LOG_WARNING:
  29413. + log_value = VCHIQ_LOG_WARNING_STR;
  29414. + break;
  29415. + case VCHIQ_LOG_INFO:
  29416. + log_value = VCHIQ_LOG_INFO_STR;
  29417. + break;
  29418. + case VCHIQ_LOG_TRACE:
  29419. + log_value = VCHIQ_LOG_TRACE_STR;
  29420. + break;
  29421. + default:
  29422. + break;
  29423. + }
  29424. +
  29425. + len += sprintf(buffer + len,
  29426. + "%s\n",
  29427. + log_value ? log_value : "(null)");
  29428. +
  29429. + return len;
  29430. +}
  29431. +
  29432. +
  29433. +static int log_cfg_write(struct file *file,
  29434. + const char __user *buffer,
  29435. + unsigned long count,
  29436. + void *data)
  29437. +{
  29438. + int *log_module = data;
  29439. + char kbuf[PROC_WRITE_BUF_SIZE + 1];
  29440. +
  29441. + (void)file;
  29442. +
  29443. + memset(kbuf, 0, PROC_WRITE_BUF_SIZE + 1);
  29444. + if (count >= PROC_WRITE_BUF_SIZE)
  29445. + count = PROC_WRITE_BUF_SIZE;
  29446. +
  29447. + if (copy_from_user(kbuf,
  29448. + buffer,
  29449. + count) != 0)
  29450. + return -EFAULT;
  29451. + kbuf[count - 1] = 0;
  29452. +
  29453. + if (strncmp("error", kbuf, strlen("error")) == 0)
  29454. + *log_module = VCHIQ_LOG_ERROR;
  29455. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  29456. + *log_module = VCHIQ_LOG_WARNING;
  29457. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  29458. + *log_module = VCHIQ_LOG_INFO;
  29459. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  29460. + *log_module = VCHIQ_LOG_TRACE;
  29461. + else
  29462. + *log_module = VCHIQ_LOG_DEFAULT;
  29463. +
  29464. + return count;
  29465. +}
  29466. +
  29467. +/* Log category proc entries */
  29468. +struct vchiq_proc_log_entry {
  29469. + const char *name;
  29470. + int *plevel;
  29471. + struct proc_dir_entry *dir;
  29472. +};
  29473. +
  29474. +static struct vchiq_proc_log_entry vchiq_proc_log_entries[] = {
  29475. + { "core", &vchiq_core_log_level },
  29476. + { "msg", &vchiq_core_msg_log_level },
  29477. + { "sync", &vchiq_sync_log_level },
  29478. + { "susp", &vchiq_susp_log_level },
  29479. + { "arm", &vchiq_arm_log_level },
  29480. +};
  29481. +static int n_log_entries =
  29482. + sizeof(vchiq_proc_log_entries)/sizeof(vchiq_proc_log_entries[0]);
  29483. +
  29484. +/* create an entry under /proc/vc/log for each log category */
  29485. +static int vchiq_proc_create_log_entries(struct proc_dir_entry *top)
  29486. +{
  29487. + struct proc_dir_entry *dir;
  29488. + size_t i;
  29489. + int ret = 0;
  29490. + dir = proc_mkdir("log", proc_info.vc_cfg_dir);
  29491. + if (!dir)
  29492. + return -ENOMEM;
  29493. + proc_info.log_categories = dir;
  29494. +
  29495. + for (i = 0; i < n_log_entries; i++) {
  29496. + dir = create_proc_entry(vchiq_proc_log_entries[i].name,
  29497. + 0644,
  29498. + proc_info.log_categories);
  29499. + if (!dir) {
  29500. + ret = -ENOMEM;
  29501. + break;
  29502. + }
  29503. +
  29504. + dir->read_proc = &log_cfg_read;
  29505. + dir->write_proc = &log_cfg_write;
  29506. + dir->data = (void *)vchiq_proc_log_entries[i].plevel;
  29507. +
  29508. + vchiq_proc_log_entries[i].dir = dir;
  29509. + }
  29510. + return ret;
  29511. +}
  29512. +
  29513. +
  29514. +int vchiq_proc_init(void)
  29515. +{
  29516. + BUG_ON(proc_info.vc_cfg_dir != NULL);
  29517. +
  29518. + proc_info.vc_cfg_dir = proc_mkdir("vc", NULL);
  29519. + if (proc_info.vc_cfg_dir == NULL)
  29520. + goto fail;
  29521. +
  29522. + proc_info.clients = proc_mkdir("clients",
  29523. + proc_info.vc_cfg_dir);
  29524. + if (!proc_info.clients)
  29525. + goto fail;
  29526. +
  29527. + if (vchiq_proc_create_log_entries(proc_info.vc_cfg_dir) != 0)
  29528. + goto fail;
  29529. +
  29530. + return 0;
  29531. +
  29532. +fail:
  29533. + vchiq_proc_deinit();
  29534. + vchiq_log_error(vchiq_arm_log_level,
  29535. + "%s: failed to create proc directory",
  29536. + __func__);
  29537. +
  29538. + return -ENOMEM;
  29539. +}
  29540. +
  29541. +/* remove all the proc entries */
  29542. +void vchiq_proc_deinit(void)
  29543. +{
  29544. + /* log category entries */
  29545. + if (proc_info.log_categories) {
  29546. + size_t i;
  29547. + for (i = 0; i < n_log_entries; i++)
  29548. + if (vchiq_proc_log_entries[i].dir)
  29549. + remove_proc_entry(
  29550. + vchiq_proc_log_entries[i].name,
  29551. + proc_info.log_categories);
  29552. +
  29553. + remove_proc_entry(proc_info.log_categories->name,
  29554. + proc_info.vc_cfg_dir);
  29555. + }
  29556. + if (proc_info.clients)
  29557. + remove_proc_entry(proc_info.clients->name,
  29558. + proc_info.vc_cfg_dir);
  29559. + if (proc_info.vc_cfg_dir)
  29560. + remove_proc_entry(proc_info.vc_cfg_dir->name, NULL);
  29561. +}
  29562. +
  29563. +struct proc_dir_entry *vchiq_clients_top(void)
  29564. +{
  29565. + return proc_info.clients;
  29566. +}
  29567. +
  29568. +#endif
  29569. +
  29570. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  29571. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 1970-01-01 01:00:00.000000000 +0100
  29572. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 2014-04-24 15:35:02.893551516 +0200
  29573. @@ -0,0 +1,828 @@
  29574. +/**
  29575. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29576. + *
  29577. + * Redistribution and use in source and binary forms, with or without
  29578. + * modification, are permitted provided that the following conditions
  29579. + * are met:
  29580. + * 1. Redistributions of source code must retain the above copyright
  29581. + * notice, this list of conditions, and the following disclaimer,
  29582. + * without modification.
  29583. + * 2. Redistributions in binary form must reproduce the above copyright
  29584. + * notice, this list of conditions and the following disclaimer in the
  29585. + * documentation and/or other materials provided with the distribution.
  29586. + * 3. The names of the above-listed copyright holders may not be used
  29587. + * to endorse or promote products derived from this software without
  29588. + * specific prior written permission.
  29589. + *
  29590. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29591. + * GNU General Public License ("GPL") version 2, as published by the Free
  29592. + * Software Foundation.
  29593. + *
  29594. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29595. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29596. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29597. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29598. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29599. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29600. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29601. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29602. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29603. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29604. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29605. + */
  29606. +#include <linux/module.h>
  29607. +#include <linux/types.h>
  29608. +
  29609. +#include "interface/vchi/vchi.h"
  29610. +#include "vchiq.h"
  29611. +#include "vchiq_core.h"
  29612. +
  29613. +#include "vchiq_util.h"
  29614. +
  29615. +#include <stddef.h>
  29616. +
  29617. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  29618. +
  29619. +typedef struct {
  29620. + VCHIQ_SERVICE_HANDLE_T handle;
  29621. +
  29622. + VCHIU_QUEUE_T queue;
  29623. +
  29624. + VCHI_CALLBACK_T callback;
  29625. + void *callback_param;
  29626. +} SHIM_SERVICE_T;
  29627. +
  29628. +/* ----------------------------------------------------------------------
  29629. + * return pointer to the mphi message driver function table
  29630. + * -------------------------------------------------------------------- */
  29631. +const VCHI_MESSAGE_DRIVER_T *
  29632. +vchi_mphi_message_driver_func_table(void)
  29633. +{
  29634. + return NULL;
  29635. +}
  29636. +
  29637. +/* ----------------------------------------------------------------------
  29638. + * return a pointer to the 'single' connection driver fops
  29639. + * -------------------------------------------------------------------- */
  29640. +const VCHI_CONNECTION_API_T *
  29641. +single_get_func_table(void)
  29642. +{
  29643. + return NULL;
  29644. +}
  29645. +
  29646. +VCHI_CONNECTION_T *vchi_create_connection(
  29647. + const VCHI_CONNECTION_API_T *function_table,
  29648. + const VCHI_MESSAGE_DRIVER_T *low_level)
  29649. +{
  29650. + (void)function_table;
  29651. + (void)low_level;
  29652. + return NULL;
  29653. +}
  29654. +
  29655. +/***********************************************************
  29656. + * Name: vchi_msg_peek
  29657. + *
  29658. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  29659. + * void **data,
  29660. + * uint32_t *msg_size,
  29661. +
  29662. +
  29663. + * VCHI_FLAGS_T flags
  29664. + *
  29665. + * Description: Routine to return a pointer to the current message (to allow in
  29666. + * place processing). The message can be removed using
  29667. + * vchi_msg_remove when you're finished
  29668. + *
  29669. + * Returns: int32_t - success == 0
  29670. + *
  29671. + ***********************************************************/
  29672. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  29673. + void **data,
  29674. + uint32_t *msg_size,
  29675. + VCHI_FLAGS_T flags)
  29676. +{
  29677. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29678. + VCHIQ_HEADER_T *header;
  29679. +
  29680. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29681. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29682. +
  29683. + if (flags == VCHI_FLAGS_NONE)
  29684. + if (vchiu_queue_is_empty(&service->queue))
  29685. + return -1;
  29686. +
  29687. + header = vchiu_queue_peek(&service->queue);
  29688. +
  29689. + *data = header->data;
  29690. + *msg_size = header->size;
  29691. +
  29692. + return 0;
  29693. +}
  29694. +EXPORT_SYMBOL(vchi_msg_peek);
  29695. +
  29696. +/***********************************************************
  29697. + * Name: vchi_msg_remove
  29698. + *
  29699. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  29700. + *
  29701. + * Description: Routine to remove a message (after it has been read with
  29702. + * vchi_msg_peek)
  29703. + *
  29704. + * Returns: int32_t - success == 0
  29705. + *
  29706. + ***********************************************************/
  29707. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  29708. +{
  29709. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29710. + VCHIQ_HEADER_T *header;
  29711. +
  29712. + header = vchiu_queue_pop(&service->queue);
  29713. +
  29714. + vchiq_release_message(service->handle, header);
  29715. +
  29716. + return 0;
  29717. +}
  29718. +EXPORT_SYMBOL(vchi_msg_remove);
  29719. +
  29720. +/***********************************************************
  29721. + * Name: vchi_msg_queue
  29722. + *
  29723. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29724. + * const void *data,
  29725. + * uint32_t data_size,
  29726. + * VCHI_FLAGS_T flags,
  29727. + * void *msg_handle,
  29728. + *
  29729. + * Description: Thin wrapper to queue a message onto a connection
  29730. + *
  29731. + * Returns: int32_t - success == 0
  29732. + *
  29733. + ***********************************************************/
  29734. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  29735. + const void *data,
  29736. + uint32_t data_size,
  29737. + VCHI_FLAGS_T flags,
  29738. + void *msg_handle)
  29739. +{
  29740. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29741. + VCHIQ_ELEMENT_T element = {data, data_size};
  29742. + VCHIQ_STATUS_T status;
  29743. +
  29744. + (void)msg_handle;
  29745. +
  29746. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  29747. +
  29748. + status = vchiq_queue_message(service->handle, &element, 1);
  29749. +
  29750. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  29751. + ** implement a retry mechanism since this function is supposed
  29752. + ** to block until queued
  29753. + */
  29754. + while (status == VCHIQ_RETRY) {
  29755. + msleep(1);
  29756. + status = vchiq_queue_message(service->handle, &element, 1);
  29757. + }
  29758. +
  29759. + return vchiq_status_to_vchi(status);
  29760. +}
  29761. +EXPORT_SYMBOL(vchi_msg_queue);
  29762. +
  29763. +/***********************************************************
  29764. + * Name: vchi_bulk_queue_receive
  29765. + *
  29766. + * Arguments: VCHI_BULK_HANDLE_T handle,
  29767. + * void *data_dst,
  29768. + * const uint32_t data_size,
  29769. + * VCHI_FLAGS_T flags
  29770. + * void *bulk_handle
  29771. + *
  29772. + * Description: Routine to setup a rcv buffer
  29773. + *
  29774. + * Returns: int32_t - success == 0
  29775. + *
  29776. + ***********************************************************/
  29777. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  29778. + void *data_dst,
  29779. + uint32_t data_size,
  29780. + VCHI_FLAGS_T flags,
  29781. + void *bulk_handle)
  29782. +{
  29783. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29784. + VCHIQ_BULK_MODE_T mode;
  29785. + VCHIQ_STATUS_T status;
  29786. +
  29787. + switch ((int)flags) {
  29788. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  29789. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29790. + WARN_ON(!service->callback);
  29791. + mode = VCHIQ_BULK_MODE_CALLBACK;
  29792. + break;
  29793. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  29794. + mode = VCHIQ_BULK_MODE_BLOCKING;
  29795. + break;
  29796. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29797. + case VCHI_FLAGS_NONE:
  29798. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  29799. + break;
  29800. + default:
  29801. + WARN(1, "unsupported message\n");
  29802. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  29803. + }
  29804. +
  29805. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  29806. + bulk_handle, mode);
  29807. +
  29808. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  29809. + ** implement a retry mechanism since this function is supposed
  29810. + ** to block until queued
  29811. + */
  29812. + while (status == VCHIQ_RETRY) {
  29813. + msleep(1);
  29814. + status = vchiq_bulk_receive(service->handle, data_dst,
  29815. + data_size, bulk_handle, mode);
  29816. + }
  29817. +
  29818. + return vchiq_status_to_vchi(status);
  29819. +}
  29820. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  29821. +
  29822. +/***********************************************************
  29823. + * Name: vchi_bulk_queue_transmit
  29824. + *
  29825. + * Arguments: VCHI_BULK_HANDLE_T handle,
  29826. + * const void *data_src,
  29827. + * uint32_t data_size,
  29828. + * VCHI_FLAGS_T flags,
  29829. + * void *bulk_handle
  29830. + *
  29831. + * Description: Routine to transmit some data
  29832. + *
  29833. + * Returns: int32_t - success == 0
  29834. + *
  29835. + ***********************************************************/
  29836. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  29837. + const void *data_src,
  29838. + uint32_t data_size,
  29839. + VCHI_FLAGS_T flags,
  29840. + void *bulk_handle)
  29841. +{
  29842. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29843. + VCHIQ_BULK_MODE_T mode;
  29844. + VCHIQ_STATUS_T status;
  29845. +
  29846. + switch ((int)flags) {
  29847. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  29848. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29849. + WARN_ON(!service->callback);
  29850. + mode = VCHIQ_BULK_MODE_CALLBACK;
  29851. + break;
  29852. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  29853. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  29854. + mode = VCHIQ_BULK_MODE_BLOCKING;
  29855. + break;
  29856. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29857. + case VCHI_FLAGS_NONE:
  29858. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  29859. + break;
  29860. + default:
  29861. + WARN(1, "unsupported message\n");
  29862. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  29863. + }
  29864. +
  29865. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  29866. + bulk_handle, mode);
  29867. +
  29868. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  29869. + ** implement a retry mechanism since this function is supposed
  29870. + ** to block until queued
  29871. + */
  29872. + while (status == VCHIQ_RETRY) {
  29873. + msleep(1);
  29874. + status = vchiq_bulk_transmit(service->handle, data_src,
  29875. + data_size, bulk_handle, mode);
  29876. + }
  29877. +
  29878. + return vchiq_status_to_vchi(status);
  29879. +}
  29880. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  29881. +
  29882. +/***********************************************************
  29883. + * Name: vchi_msg_dequeue
  29884. + *
  29885. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29886. + * void *data,
  29887. + * uint32_t max_data_size_to_read,
  29888. + * uint32_t *actual_msg_size
  29889. + * VCHI_FLAGS_T flags
  29890. + *
  29891. + * Description: Routine to dequeue a message into the supplied buffer
  29892. + *
  29893. + * Returns: int32_t - success == 0
  29894. + *
  29895. + ***********************************************************/
  29896. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  29897. + void *data,
  29898. + uint32_t max_data_size_to_read,
  29899. + uint32_t *actual_msg_size,
  29900. + VCHI_FLAGS_T flags)
  29901. +{
  29902. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29903. + VCHIQ_HEADER_T *header;
  29904. +
  29905. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29906. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29907. +
  29908. + if (flags == VCHI_FLAGS_NONE)
  29909. + if (vchiu_queue_is_empty(&service->queue))
  29910. + return -1;
  29911. +
  29912. + header = vchiu_queue_pop(&service->queue);
  29913. +
  29914. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  29915. + header->size : max_data_size_to_read);
  29916. +
  29917. + *actual_msg_size = header->size;
  29918. +
  29919. + vchiq_release_message(service->handle, header);
  29920. +
  29921. + return 0;
  29922. +}
  29923. +EXPORT_SYMBOL(vchi_msg_dequeue);
  29924. +
  29925. +/***********************************************************
  29926. + * Name: vchi_msg_queuev
  29927. + *
  29928. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29929. + * VCHI_MSG_VECTOR_T *vector,
  29930. + * uint32_t count,
  29931. + * VCHI_FLAGS_T flags,
  29932. + * void *msg_handle
  29933. + *
  29934. + * Description: Thin wrapper to queue a message onto a connection
  29935. + *
  29936. + * Returns: int32_t - success == 0
  29937. + *
  29938. + ***********************************************************/
  29939. +
  29940. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  29941. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  29942. + offsetof(VCHIQ_ELEMENT_T, data));
  29943. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  29944. + offsetof(VCHIQ_ELEMENT_T, size));
  29945. +
  29946. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  29947. + VCHI_MSG_VECTOR_T *vector,
  29948. + uint32_t count,
  29949. + VCHI_FLAGS_T flags,
  29950. + void *msg_handle)
  29951. +{
  29952. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29953. +
  29954. + (void)msg_handle;
  29955. +
  29956. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  29957. +
  29958. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  29959. + (const VCHIQ_ELEMENT_T *)vector, count));
  29960. +}
  29961. +EXPORT_SYMBOL(vchi_msg_queuev);
  29962. +
  29963. +/***********************************************************
  29964. + * Name: vchi_held_msg_release
  29965. + *
  29966. + * Arguments: VCHI_HELD_MSG_T *message
  29967. + *
  29968. + * Description: Routine to release a held message (after it has been read with
  29969. + * vchi_msg_hold)
  29970. + *
  29971. + * Returns: int32_t - success == 0
  29972. + *
  29973. + ***********************************************************/
  29974. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  29975. +{
  29976. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  29977. + (VCHIQ_HEADER_T *)message->message);
  29978. +
  29979. + return 0;
  29980. +}
  29981. +EXPORT_SYMBOL(vchi_held_msg_release);
  29982. +
  29983. +/***********************************************************
  29984. + * Name: vchi_msg_hold
  29985. + *
  29986. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29987. + * void **data,
  29988. + * uint32_t *msg_size,
  29989. + * VCHI_FLAGS_T flags,
  29990. + * VCHI_HELD_MSG_T *message_handle
  29991. + *
  29992. + * Description: Routine to return a pointer to the current message (to allow
  29993. + * in place processing). The message is dequeued - don't forget
  29994. + * to release the message using vchi_held_msg_release when you're
  29995. + * finished.
  29996. + *
  29997. + * Returns: int32_t - success == 0
  29998. + *
  29999. + ***********************************************************/
  30000. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  30001. + void **data,
  30002. + uint32_t *msg_size,
  30003. + VCHI_FLAGS_T flags,
  30004. + VCHI_HELD_MSG_T *message_handle)
  30005. +{
  30006. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30007. + VCHIQ_HEADER_T *header;
  30008. +
  30009. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  30010. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  30011. +
  30012. + if (flags == VCHI_FLAGS_NONE)
  30013. + if (vchiu_queue_is_empty(&service->queue))
  30014. + return -1;
  30015. +
  30016. + header = vchiu_queue_pop(&service->queue);
  30017. +
  30018. + *data = header->data;
  30019. + *msg_size = header->size;
  30020. +
  30021. + message_handle->service =
  30022. + (struct opaque_vchi_service_t *)service->handle;
  30023. + message_handle->message = header;
  30024. +
  30025. + return 0;
  30026. +}
  30027. +EXPORT_SYMBOL(vchi_msg_hold);
  30028. +
  30029. +/***********************************************************
  30030. + * Name: vchi_initialise
  30031. + *
  30032. + * Arguments: VCHI_INSTANCE_T *instance_handle
  30033. + * VCHI_CONNECTION_T **connections
  30034. + * const uint32_t num_connections
  30035. + *
  30036. + * Description: Initialises the hardware but does not transmit anything
  30037. + * When run as a Host App this will be called twice hence the need
  30038. + * to malloc the state information
  30039. + *
  30040. + * Returns: 0 if successful, failure otherwise
  30041. + *
  30042. + ***********************************************************/
  30043. +
  30044. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  30045. +{
  30046. + VCHIQ_INSTANCE_T instance;
  30047. + VCHIQ_STATUS_T status;
  30048. +
  30049. + status = vchiq_initialise(&instance);
  30050. +
  30051. + *instance_handle = (VCHI_INSTANCE_T)instance;
  30052. +
  30053. + return vchiq_status_to_vchi(status);
  30054. +}
  30055. +EXPORT_SYMBOL(vchi_initialise);
  30056. +
  30057. +/***********************************************************
  30058. + * Name: vchi_connect
  30059. + *
  30060. + * Arguments: VCHI_CONNECTION_T **connections
  30061. + * const uint32_t num_connections
  30062. + * VCHI_INSTANCE_T instance_handle)
  30063. + *
  30064. + * Description: Starts the command service on each connection,
  30065. + * causing INIT messages to be pinged back and forth
  30066. + *
  30067. + * Returns: 0 if successful, failure otherwise
  30068. + *
  30069. + ***********************************************************/
  30070. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  30071. + const uint32_t num_connections,
  30072. + VCHI_INSTANCE_T instance_handle)
  30073. +{
  30074. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  30075. +
  30076. + (void)connections;
  30077. + (void)num_connections;
  30078. +
  30079. + return vchiq_connect(instance);
  30080. +}
  30081. +EXPORT_SYMBOL(vchi_connect);
  30082. +
  30083. +
  30084. +/***********************************************************
  30085. + * Name: vchi_disconnect
  30086. + *
  30087. + * Arguments: VCHI_INSTANCE_T instance_handle
  30088. + *
  30089. + * Description: Stops the command service on each connection,
  30090. + * causing DE-INIT messages to be pinged back and forth
  30091. + *
  30092. + * Returns: 0 if successful, failure otherwise
  30093. + *
  30094. + ***********************************************************/
  30095. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  30096. +{
  30097. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  30098. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  30099. +}
  30100. +EXPORT_SYMBOL(vchi_disconnect);
  30101. +
  30102. +
  30103. +/***********************************************************
  30104. + * Name: vchi_service_open
  30105. + * Name: vchi_service_create
  30106. + *
  30107. + * Arguments: VCHI_INSTANCE_T *instance_handle
  30108. + * SERVICE_CREATION_T *setup,
  30109. + * VCHI_SERVICE_HANDLE_T *handle
  30110. + *
  30111. + * Description: Routine to open a service
  30112. + *
  30113. + * Returns: int32_t - success == 0
  30114. + *
  30115. + ***********************************************************/
  30116. +
  30117. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  30118. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  30119. +{
  30120. + SHIM_SERVICE_T *service =
  30121. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  30122. +
  30123. + if (!service->callback)
  30124. + goto release;
  30125. +
  30126. + switch (reason) {
  30127. + case VCHIQ_MESSAGE_AVAILABLE:
  30128. + vchiu_queue_push(&service->queue, header);
  30129. +
  30130. + service->callback(service->callback_param,
  30131. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  30132. +
  30133. + goto done;
  30134. + break;
  30135. +
  30136. + case VCHIQ_BULK_TRANSMIT_DONE:
  30137. + service->callback(service->callback_param,
  30138. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  30139. + break;
  30140. +
  30141. + case VCHIQ_BULK_RECEIVE_DONE:
  30142. + service->callback(service->callback_param,
  30143. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  30144. + break;
  30145. +
  30146. + case VCHIQ_SERVICE_CLOSED:
  30147. + service->callback(service->callback_param,
  30148. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  30149. + break;
  30150. +
  30151. + case VCHIQ_SERVICE_OPENED:
  30152. + /* No equivalent VCHI reason */
  30153. + break;
  30154. +
  30155. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  30156. + service->callback(service->callback_param,
  30157. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  30158. + bulk_user);
  30159. + break;
  30160. +
  30161. + case VCHIQ_BULK_RECEIVE_ABORTED:
  30162. + service->callback(service->callback_param,
  30163. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  30164. + bulk_user);
  30165. + break;
  30166. +
  30167. + default:
  30168. + WARN(1, "not supported\n");
  30169. + break;
  30170. + }
  30171. +
  30172. +release:
  30173. + vchiq_release_message(service->handle, header);
  30174. +done:
  30175. + return VCHIQ_SUCCESS;
  30176. +}
  30177. +
  30178. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  30179. + SERVICE_CREATION_T *setup)
  30180. +{
  30181. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  30182. +
  30183. + (void)instance;
  30184. +
  30185. + if (service) {
  30186. + if (vchiu_queue_init(&service->queue, 64)) {
  30187. + service->callback = setup->callback;
  30188. + service->callback_param = setup->callback_param;
  30189. + } else {
  30190. + kfree(service);
  30191. + service = NULL;
  30192. + }
  30193. + }
  30194. +
  30195. + return service;
  30196. +}
  30197. +
  30198. +static void service_free(SHIM_SERVICE_T *service)
  30199. +{
  30200. + if (service) {
  30201. + vchiu_queue_delete(&service->queue);
  30202. + kfree(service);
  30203. + }
  30204. +}
  30205. +
  30206. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  30207. + SERVICE_CREATION_T *setup,
  30208. + VCHI_SERVICE_HANDLE_T *handle)
  30209. +{
  30210. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  30211. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  30212. + if (service) {
  30213. + VCHIQ_SERVICE_PARAMS_T params;
  30214. + VCHIQ_STATUS_T status;
  30215. +
  30216. + memset(&params, 0, sizeof(params));
  30217. + params.fourcc = setup->service_id;
  30218. + params.callback = shim_callback;
  30219. + params.userdata = service;
  30220. + params.version = setup->version.version;
  30221. + params.version_min = setup->version.version_min;
  30222. +
  30223. + status = vchiq_open_service(instance, &params,
  30224. + &service->handle);
  30225. + if (status != VCHIQ_SUCCESS) {
  30226. + service_free(service);
  30227. + service = NULL;
  30228. + }
  30229. + }
  30230. +
  30231. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  30232. +
  30233. + return (service != NULL) ? 0 : -1;
  30234. +}
  30235. +EXPORT_SYMBOL(vchi_service_open);
  30236. +
  30237. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  30238. + SERVICE_CREATION_T *setup,
  30239. + VCHI_SERVICE_HANDLE_T *handle)
  30240. +{
  30241. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  30242. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  30243. + if (service) {
  30244. + VCHIQ_SERVICE_PARAMS_T params;
  30245. + VCHIQ_STATUS_T status;
  30246. +
  30247. + memset(&params, 0, sizeof(params));
  30248. + params.fourcc = setup->service_id;
  30249. + params.callback = shim_callback;
  30250. + params.userdata = service;
  30251. + params.version = setup->version.version;
  30252. + params.version_min = setup->version.version_min;
  30253. + status = vchiq_add_service(instance, &params, &service->handle);
  30254. +
  30255. + if (status != VCHIQ_SUCCESS) {
  30256. + service_free(service);
  30257. + service = NULL;
  30258. + }
  30259. + }
  30260. +
  30261. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  30262. +
  30263. + return (service != NULL) ? 0 : -1;
  30264. +}
  30265. +EXPORT_SYMBOL(vchi_service_create);
  30266. +
  30267. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  30268. +{
  30269. + int32_t ret = -1;
  30270. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30271. + if (service) {
  30272. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  30273. + if (status == VCHIQ_SUCCESS) {
  30274. + service_free(service);
  30275. + service = NULL;
  30276. + }
  30277. +
  30278. + ret = vchiq_status_to_vchi(status);
  30279. + }
  30280. + return ret;
  30281. +}
  30282. +EXPORT_SYMBOL(vchi_service_close);
  30283. +
  30284. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  30285. +{
  30286. + int32_t ret = -1;
  30287. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30288. + if (service) {
  30289. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  30290. + if (status == VCHIQ_SUCCESS) {
  30291. + service_free(service);
  30292. + service = NULL;
  30293. + }
  30294. +
  30295. + ret = vchiq_status_to_vchi(status);
  30296. + }
  30297. + return ret;
  30298. +}
  30299. +EXPORT_SYMBOL(vchi_service_destroy);
  30300. +
  30301. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  30302. +{
  30303. + int32_t ret = -1;
  30304. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30305. + if(service)
  30306. + {
  30307. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  30308. + ret = vchiq_status_to_vchi( status );
  30309. + }
  30310. + return ret;
  30311. +}
  30312. +EXPORT_SYMBOL(vchi_get_peer_version);
  30313. +
  30314. +/* ----------------------------------------------------------------------
  30315. + * read a uint32_t from buffer.
  30316. + * network format is defined to be little endian
  30317. + * -------------------------------------------------------------------- */
  30318. +uint32_t
  30319. +vchi_readbuf_uint32(const void *_ptr)
  30320. +{
  30321. + const unsigned char *ptr = _ptr;
  30322. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  30323. +}
  30324. +
  30325. +/* ----------------------------------------------------------------------
  30326. + * write a uint32_t to buffer.
  30327. + * network format is defined to be little endian
  30328. + * -------------------------------------------------------------------- */
  30329. +void
  30330. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  30331. +{
  30332. + unsigned char *ptr = _ptr;
  30333. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  30334. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  30335. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  30336. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  30337. +}
  30338. +
  30339. +/* ----------------------------------------------------------------------
  30340. + * read a uint16_t from buffer.
  30341. + * network format is defined to be little endian
  30342. + * -------------------------------------------------------------------- */
  30343. +uint16_t
  30344. +vchi_readbuf_uint16(const void *_ptr)
  30345. +{
  30346. + const unsigned char *ptr = _ptr;
  30347. + return ptr[0] | (ptr[1] << 8);
  30348. +}
  30349. +
  30350. +/* ----------------------------------------------------------------------
  30351. + * write a uint16_t into the buffer.
  30352. + * network format is defined to be little endian
  30353. + * -------------------------------------------------------------------- */
  30354. +void
  30355. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  30356. +{
  30357. + unsigned char *ptr = _ptr;
  30358. + ptr[0] = (value >> 0) & 0xFF;
  30359. + ptr[1] = (value >> 8) & 0xFF;
  30360. +}
  30361. +
  30362. +/***********************************************************
  30363. + * Name: vchi_service_use
  30364. + *
  30365. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  30366. + *
  30367. + * Description: Routine to increment refcount on a service
  30368. + *
  30369. + * Returns: void
  30370. + *
  30371. + ***********************************************************/
  30372. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  30373. +{
  30374. + int32_t ret = -1;
  30375. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30376. + if (service)
  30377. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  30378. + return ret;
  30379. +}
  30380. +EXPORT_SYMBOL(vchi_service_use);
  30381. +
  30382. +/***********************************************************
  30383. + * Name: vchi_service_release
  30384. + *
  30385. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  30386. + *
  30387. + * Description: Routine to decrement refcount on a service
  30388. + *
  30389. + * Returns: void
  30390. + *
  30391. + ***********************************************************/
  30392. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  30393. +{
  30394. + int32_t ret = -1;
  30395. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  30396. + if (service)
  30397. + ret = vchiq_status_to_vchi(
  30398. + vchiq_release_service(service->handle));
  30399. + return ret;
  30400. +}
  30401. +EXPORT_SYMBOL(vchi_service_release);
  30402. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  30403. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 1970-01-01 01:00:00.000000000 +0100
  30404. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 2014-04-24 15:35:02.893551516 +0200
  30405. @@ -0,0 +1,151 @@
  30406. +/**
  30407. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  30408. + *
  30409. + * Redistribution and use in source and binary forms, with or without
  30410. + * modification, are permitted provided that the following conditions
  30411. + * are met:
  30412. + * 1. Redistributions of source code must retain the above copyright
  30413. + * notice, this list of conditions, and the following disclaimer,
  30414. + * without modification.
  30415. + * 2. Redistributions in binary form must reproduce the above copyright
  30416. + * notice, this list of conditions and the following disclaimer in the
  30417. + * documentation and/or other materials provided with the distribution.
  30418. + * 3. The names of the above-listed copyright holders may not be used
  30419. + * to endorse or promote products derived from this software without
  30420. + * specific prior written permission.
  30421. + *
  30422. + * ALTERNATIVELY, this software may be distributed under the terms of the
  30423. + * GNU General Public License ("GPL") version 2, as published by the Free
  30424. + * Software Foundation.
  30425. + *
  30426. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30427. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30428. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30429. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30430. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30431. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30432. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30433. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30434. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30435. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30436. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30437. + */
  30438. +
  30439. +#include "vchiq_util.h"
  30440. +
  30441. +static inline int is_pow2(int i)
  30442. +{
  30443. + return i && !(i & (i - 1));
  30444. +}
  30445. +
  30446. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  30447. +{
  30448. + WARN_ON(!is_pow2(size));
  30449. +
  30450. + queue->size = size;
  30451. + queue->read = 0;
  30452. + queue->write = 0;
  30453. +
  30454. + sema_init(&queue->pop, 0);
  30455. + sema_init(&queue->push, 0);
  30456. +
  30457. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  30458. + if (queue->storage == NULL) {
  30459. + vchiu_queue_delete(queue);
  30460. + return 0;
  30461. + }
  30462. + return 1;
  30463. +}
  30464. +
  30465. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  30466. +{
  30467. + if (queue->storage != NULL)
  30468. + kfree(queue->storage);
  30469. +}
  30470. +
  30471. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  30472. +{
  30473. + return queue->read == queue->write;
  30474. +}
  30475. +
  30476. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  30477. +{
  30478. + return queue->write == queue->read + queue->size;
  30479. +}
  30480. +
  30481. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  30482. +{
  30483. + while (queue->write == queue->read + queue->size) {
  30484. + if (down_interruptible(&queue->pop) != 0) {
  30485. + flush_signals(current);
  30486. + }
  30487. + }
  30488. +
  30489. + /*
  30490. + * Write to queue->storage must be visible after read from
  30491. + * queue->read
  30492. + */
  30493. + smp_mb();
  30494. +
  30495. + queue->storage[queue->write & (queue->size - 1)] = header;
  30496. +
  30497. + /*
  30498. + * Write to queue->storage must be visible before write to
  30499. + * queue->write
  30500. + */
  30501. + smp_wmb();
  30502. +
  30503. + queue->write++;
  30504. +
  30505. + up(&queue->push);
  30506. +}
  30507. +
  30508. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  30509. +{
  30510. + while (queue->write == queue->read) {
  30511. + if (down_interruptible(&queue->push) != 0) {
  30512. + flush_signals(current);
  30513. + }
  30514. + }
  30515. +
  30516. + up(&queue->push); // We haven't removed anything from the queue.
  30517. +
  30518. + /*
  30519. + * Read from queue->storage must be visible after read from
  30520. + * queue->write
  30521. + */
  30522. + smp_rmb();
  30523. +
  30524. + return queue->storage[queue->read & (queue->size - 1)];
  30525. +}
  30526. +
  30527. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  30528. +{
  30529. + VCHIQ_HEADER_T *header;
  30530. +
  30531. + while (queue->write == queue->read) {
  30532. + if (down_interruptible(&queue->push) != 0) {
  30533. + flush_signals(current);
  30534. + }
  30535. + }
  30536. +
  30537. + /*
  30538. + * Read from queue->storage must be visible after read from
  30539. + * queue->write
  30540. + */
  30541. + smp_rmb();
  30542. +
  30543. + header = queue->storage[queue->read & (queue->size - 1)];
  30544. +
  30545. + /*
  30546. + * Read from queue->storage must be visible before write to
  30547. + * queue->read
  30548. + */
  30549. + smp_mb();
  30550. +
  30551. + queue->read++;
  30552. +
  30553. + up(&queue->pop);
  30554. +
  30555. + return header;
  30556. +}
  30557. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  30558. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 1970-01-01 01:00:00.000000000 +0100
  30559. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 2014-04-24 15:35:02.893551516 +0200
  30560. @@ -0,0 +1,82 @@
  30561. +/**
  30562. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  30563. + *
  30564. + * Redistribution and use in source and binary forms, with or without
  30565. + * modification, are permitted provided that the following conditions
  30566. + * are met:
  30567. + * 1. Redistributions of source code must retain the above copyright
  30568. + * notice, this list of conditions, and the following disclaimer,
  30569. + * without modification.
  30570. + * 2. Redistributions in binary form must reproduce the above copyright
  30571. + * notice, this list of conditions and the following disclaimer in the
  30572. + * documentation and/or other materials provided with the distribution.
  30573. + * 3. The names of the above-listed copyright holders may not be used
  30574. + * to endorse or promote products derived from this software without
  30575. + * specific prior written permission.
  30576. + *
  30577. + * ALTERNATIVELY, this software may be distributed under the terms of the
  30578. + * GNU General Public License ("GPL") version 2, as published by the Free
  30579. + * Software Foundation.
  30580. + *
  30581. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30582. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30583. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30584. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30585. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30586. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30587. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30588. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30589. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30590. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30591. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30592. + */
  30593. +
  30594. +#ifndef VCHIQ_UTIL_H
  30595. +#define VCHIQ_UTIL_H
  30596. +
  30597. +#include <linux/types.h>
  30598. +#include <linux/semaphore.h>
  30599. +#include <linux/mutex.h>
  30600. +#include <linux/bitops.h>
  30601. +#include <linux/kthread.h>
  30602. +#include <linux/wait.h>
  30603. +#include <linux/vmalloc.h>
  30604. +#include <linux/jiffies.h>
  30605. +#include <linux/delay.h>
  30606. +#include <linux/string.h>
  30607. +#include <linux/types.h>
  30608. +#include <linux/interrupt.h>
  30609. +#include <linux/random.h>
  30610. +#include <linux/sched.h>
  30611. +#include <linux/ctype.h>
  30612. +#include <linux/uaccess.h>
  30613. +#include <linux/time.h> /* for time_t */
  30614. +#include <linux/slab.h>
  30615. +#include <linux/vmalloc.h>
  30616. +
  30617. +#include "vchiq_if.h"
  30618. +
  30619. +typedef struct {
  30620. + int size;
  30621. + int read;
  30622. + int write;
  30623. +
  30624. + struct semaphore pop;
  30625. + struct semaphore push;
  30626. +
  30627. + VCHIQ_HEADER_T **storage;
  30628. +} VCHIU_QUEUE_T;
  30629. +
  30630. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  30631. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  30632. +
  30633. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  30634. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  30635. +
  30636. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  30637. +
  30638. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  30639. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  30640. +
  30641. +#endif
  30642. +
  30643. diff -Nur linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  30644. --- linux-3.10.37/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 1970-01-01 01:00:00.000000000 +0100
  30645. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 2014-04-24 15:35:02.893551516 +0200
  30646. @@ -0,0 +1,59 @@
  30647. +/**
  30648. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  30649. + *
  30650. + * Redistribution and use in source and binary forms, with or without
  30651. + * modification, are permitted provided that the following conditions
  30652. + * are met:
  30653. + * 1. Redistributions of source code must retain the above copyright
  30654. + * notice, this list of conditions, and the following disclaimer,
  30655. + * without modification.
  30656. + * 2. Redistributions in binary form must reproduce the above copyright
  30657. + * notice, this list of conditions and the following disclaimer in the
  30658. + * documentation and/or other materials provided with the distribution.
  30659. + * 3. The names of the above-listed copyright holders may not be used
  30660. + * to endorse or promote products derived from this software without
  30661. + * specific prior written permission.
  30662. + *
  30663. + * ALTERNATIVELY, this software may be distributed under the terms of the
  30664. + * GNU General Public License ("GPL") version 2, as published by the Free
  30665. + * Software Foundation.
  30666. + *
  30667. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30668. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30669. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30670. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30671. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30672. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30673. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30674. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30675. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30676. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30677. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30678. + */
  30679. +#include "vchiq_build_info.h"
  30680. +#include <linux/broadcom/vc_debug_sym.h>
  30681. +
  30682. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  30683. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  30684. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  30685. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  30686. +
  30687. +const char *vchiq_get_build_hostname( void )
  30688. +{
  30689. + return vchiq_build_hostname;
  30690. +}
  30691. +
  30692. +const char *vchiq_get_build_version( void )
  30693. +{
  30694. + return vchiq_build_version;
  30695. +}
  30696. +
  30697. +const char *vchiq_get_build_date( void )
  30698. +{
  30699. + return vchiq_build_date;
  30700. +}
  30701. +
  30702. +const char *vchiq_get_build_time( void )
  30703. +{
  30704. + return vchiq_build_time;
  30705. +}
  30706. diff -Nur linux-3.10.37/drivers/misc/vc04_services/Kconfig linux-rpi/drivers/misc/vc04_services/Kconfig
  30707. --- linux-3.10.37/drivers/misc/vc04_services/Kconfig 1970-01-01 01:00:00.000000000 +0100
  30708. +++ linux-rpi/drivers/misc/vc04_services/Kconfig 2014-04-24 15:35:02.889551471 +0200
  30709. @@ -0,0 +1,10 @@
  30710. +config BCM2708_VCHIQ
  30711. + tristate "Videocore VCHIQ"
  30712. + depends on MACH_BCM2708
  30713. + default y
  30714. + help
  30715. + Kernel to VideoCore communication interface for the
  30716. + BCM2708 family of products.
  30717. + Defaults to Y when the Broadcom Videocore services
  30718. + are included in the build, N otherwise.
  30719. +
  30720. diff -Nur linux-3.10.37/drivers/misc/vc04_services/Makefile linux-rpi/drivers/misc/vc04_services/Makefile
  30721. --- linux-3.10.37/drivers/misc/vc04_services/Makefile 1970-01-01 01:00:00.000000000 +0100
  30722. +++ linux-rpi/drivers/misc/vc04_services/Makefile 2014-04-24 15:35:02.889551471 +0200
  30723. @@ -0,0 +1,18 @@
  30724. +ifeq ($(CONFIG_MACH_BCM2708),y)
  30725. +
  30726. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  30727. +
  30728. +vchiq-objs := \
  30729. + interface/vchiq_arm/vchiq_core.o \
  30730. + interface/vchiq_arm/vchiq_arm.o \
  30731. + interface/vchiq_arm/vchiq_kern_lib.o \
  30732. + interface/vchiq_arm/vchiq_2835_arm.o \
  30733. + interface/vchiq_arm/vchiq_proc.o \
  30734. + interface/vchiq_arm/vchiq_shim.o \
  30735. + interface/vchiq_arm/vchiq_util.o \
  30736. + interface/vchiq_arm/vchiq_connected.o \
  30737. +
  30738. +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  30739. +
  30740. +endif
  30741. +
  30742. diff -Nur linux-3.10.37/drivers/mmc/card/block.c linux-rpi/drivers/mmc/card/block.c
  30743. --- linux-3.10.37/drivers/mmc/card/block.c 2014-04-14 15:42:31.000000000 +0200
  30744. +++ linux-rpi/drivers/mmc/card/block.c 2014-04-24 15:35:02.897551561 +0200
  30745. @@ -1333,7 +1333,7 @@
  30746. brq->data.blocks = 1;
  30747. }
  30748. - if (brq->data.blocks > 1 || do_rel_wr) {
  30749. + if (brq->data.blocks > 1 || do_rel_wr || card->host->caps2 & MMC_CAP2_FORCE_MULTIBLOCK) {
  30750. /* SPI multiblock writes terminate using a special
  30751. * token, not a STOP_TRANSMISSION request.
  30752. */
  30753. diff -Nur linux-3.10.37/drivers/mmc/core/mmc.c linux-rpi/drivers/mmc/core/mmc.c
  30754. --- linux-3.10.37/drivers/mmc/core/mmc.c 2014-04-14 15:42:31.000000000 +0200
  30755. +++ linux-rpi/drivers/mmc/core/mmc.c 2014-04-24 15:35:02.901551605 +0200
  30756. @@ -293,7 +293,7 @@
  30757. }
  30758. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  30759. - if (card->ext_csd.rev > 6) {
  30760. + if (card->ext_csd.rev > 7) {
  30761. pr_err("%s: unrecognised EXT_CSD revision %d\n",
  30762. mmc_hostname(card->host), card->ext_csd.rev);
  30763. err = -EINVAL;
  30764. diff -Nur linux-3.10.37/drivers/mmc/core/sd.c linux-rpi/drivers/mmc/core/sd.c
  30765. --- linux-3.10.37/drivers/mmc/core/sd.c 2014-04-14 15:42:31.000000000 +0200
  30766. +++ linux-rpi/drivers/mmc/core/sd.c 2014-04-24 15:35:02.901551605 +0200
  30767. @@ -13,6 +13,8 @@
  30768. #include <linux/err.h>
  30769. #include <linux/slab.h>
  30770. #include <linux/stat.h>
  30771. +#include <linux/jiffies.h>
  30772. +#include <linux/nmi.h>
  30773. #include <linux/mmc/host.h>
  30774. #include <linux/mmc/card.h>
  30775. @@ -58,6 +60,15 @@
  30776. __res & __mask; \
  30777. })
  30778. +// timeout for tries
  30779. +static const unsigned long retry_timeout_ms= 10*1000;
  30780. +
  30781. +// try at least 10 times, even if timeout is reached
  30782. +static const int retry_min_tries= 10;
  30783. +
  30784. +// delay between tries
  30785. +static const unsigned long retry_delay_ms= 10;
  30786. +
  30787. /*
  30788. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  30789. */
  30790. @@ -210,12 +221,62 @@
  30791. }
  30792. /*
  30793. - * Fetch and process SD Status register.
  30794. + * Fetch and process SD Configuration Register.
  30795. + */
  30796. +static int mmc_read_scr(struct mmc_card *card)
  30797. +{
  30798. + unsigned long timeout_at;
  30799. + int err, tries;
  30800. +
  30801. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  30802. + tries= 0;
  30803. +
  30804. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  30805. + {
  30806. + unsigned long delay_at;
  30807. + tries++;
  30808. +
  30809. + err = mmc_app_send_scr(card, card->raw_scr);
  30810. + if( !err )
  30811. + break; // success!!!
  30812. +
  30813. + touch_nmi_watchdog(); // we are still alive!
  30814. +
  30815. + // delay
  30816. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  30817. + while( time_before( jiffies, delay_at ) )
  30818. + {
  30819. + mdelay( 1 );
  30820. + touch_nmi_watchdog(); // we are still alive!
  30821. + }
  30822. + }
  30823. +
  30824. + if( err)
  30825. + {
  30826. + pr_err("%s: failed to read SD Configuration register (SCR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
  30827. + return err;
  30828. + }
  30829. +
  30830. + if( tries > 1 )
  30831. + {
  30832. + pr_info("%s: could read SD Configuration register (SCR) at the %dth attempt\n", mmc_hostname(card->host), tries );
  30833. + }
  30834. +
  30835. + err = mmc_decode_scr(card);
  30836. + if (err)
  30837. + return err;
  30838. +
  30839. + return err;
  30840. +}
  30841. +
  30842. +/*
  30843. + * Fetch and process SD Status Register.
  30844. */
  30845. static int mmc_read_ssr(struct mmc_card *card)
  30846. {
  30847. + unsigned long timeout_at;
  30848. unsigned int au, es, et, eo;
  30849. - int err, i;
  30850. + int err, i, tries;
  30851. u32 *ssr;
  30852. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  30853. @@ -227,15 +288,41 @@
  30854. ssr = kmalloc(64, GFP_KERNEL);
  30855. if (!ssr)
  30856. return -ENOMEM;
  30857. -
  30858. - err = mmc_app_sd_status(card, ssr);
  30859. - if (err) {
  30860. - pr_warning("%s: problem reading SD Status "
  30861. - "register.\n", mmc_hostname(card->host));
  30862. - err = 0;
  30863. +
  30864. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  30865. + tries= 0;
  30866. +
  30867. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  30868. + {
  30869. + unsigned long delay_at;
  30870. + tries++;
  30871. +
  30872. + err= mmc_app_sd_status(card, ssr);
  30873. + if( !err )
  30874. + break; // sucess!!!
  30875. +
  30876. + touch_nmi_watchdog(); // we are still alive!
  30877. +
  30878. + // delay
  30879. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  30880. + while( time_before( jiffies, delay_at ) )
  30881. + {
  30882. + mdelay( 1 );
  30883. + touch_nmi_watchdog(); // we are still alive!
  30884. + }
  30885. + }
  30886. +
  30887. + if( err)
  30888. + {
  30889. + pr_err("%s: failed to read SD Status register (SSR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
  30890. goto out;
  30891. }
  30892. + if( tries > 1 )
  30893. + {
  30894. + pr_info("%s: read SD Status register (SSR) after %d attempts\n", mmc_hostname(card->host), tries );
  30895. + }
  30896. +
  30897. for (i = 0; i < 16; i++)
  30898. ssr[i] = be32_to_cpu(ssr[i]);
  30899. @@ -808,15 +895,11 @@
  30900. if (!reinit) {
  30901. /*
  30902. - * Fetch SCR from card.
  30903. + * Fetch and decode SD Configuration register.
  30904. */
  30905. - err = mmc_app_send_scr(card, card->raw_scr);
  30906. - if (err)
  30907. - return err;
  30908. -
  30909. - err = mmc_decode_scr(card);
  30910. - if (err)
  30911. - return err;
  30912. + err = mmc_read_scr(card);
  30913. + if( err )
  30914. + return err;
  30915. /*
  30916. * Fetch and process SD Status register.
  30917. diff -Nur linux-3.10.37/drivers/mmc/host/Kconfig linux-rpi/drivers/mmc/host/Kconfig
  30918. --- linux-3.10.37/drivers/mmc/host/Kconfig 2014-04-14 15:42:31.000000000 +0200
  30919. +++ linux-rpi/drivers/mmc/host/Kconfig 2014-04-24 15:35:02.901551605 +0200
  30920. @@ -249,6 +249,27 @@
  30921. YMMV.
  30922. +config MMC_SDHCI_BCM2708
  30923. + tristate "SDHCI support on BCM2708"
  30924. + depends on MMC_SDHCI && MACH_BCM2708
  30925. + select MMC_SDHCI_IO_ACCESSORS
  30926. + help
  30927. + This selects the Secure Digital Host Controller Interface (SDHCI)
  30928. + often referrered to as the eMMC block.
  30929. +
  30930. + If you have a controller with this interface, say Y or M here.
  30931. +
  30932. + If unsure, say N.
  30933. +
  30934. +config MMC_SDHCI_BCM2708_DMA
  30935. + bool "DMA support on BCM2708 Arasan controller"
  30936. + depends on MMC_SDHCI_BCM2708
  30937. + help
  30938. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  30939. + based chips.
  30940. +
  30941. + If unsure, say N.
  30942. +
  30943. config MMC_SDHCI_BCM2835
  30944. tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
  30945. depends on ARCH_BCM2835
  30946. diff -Nur linux-3.10.37/drivers/mmc/host/Makefile linux-rpi/drivers/mmc/host/Makefile
  30947. --- linux-3.10.37/drivers/mmc/host/Makefile 2014-04-14 15:42:31.000000000 +0200
  30948. +++ linux-rpi/drivers/mmc/host/Makefile 2014-04-24 15:35:02.901551605 +0200
  30949. @@ -15,6 +15,7 @@
  30950. obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
  30951. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  30952. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  30953. +obj-$(CONFIG_MMC_SDHCI_BCM2708) += sdhci-bcm2708.o
  30954. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  30955. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  30956. obj-$(CONFIG_MMC_OMAP) += omap.o
  30957. diff -Nur linux-3.10.37/drivers/mmc/host/sdhci-bcm2708.c linux-rpi/drivers/mmc/host/sdhci-bcm2708.c
  30958. --- linux-3.10.37/drivers/mmc/host/sdhci-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  30959. +++ linux-rpi/drivers/mmc/host/sdhci-bcm2708.c 2014-04-24 15:35:02.909551694 +0200
  30960. @@ -0,0 +1,1410 @@
  30961. +/*
  30962. + * sdhci-bcm2708.c Support for SDHCI device on BCM2708
  30963. + * Copyright (c) 2010 Broadcom
  30964. + *
  30965. + * This program is free software; you can redistribute it and/or modify
  30966. + * it under the terms of the GNU General Public License version 2 as
  30967. + * published by the Free Software Foundation.
  30968. + *
  30969. + * This program is distributed in the hope that it will be useful,
  30970. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30971. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30972. + * GNU General Public License for more details.
  30973. + *
  30974. + * You should have received a copy of the GNU General Public License
  30975. + * along with this program; if not, write to the Free Software
  30976. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30977. + */
  30978. +
  30979. +/* Supports:
  30980. + * SDHCI platform device - Arasan SD controller in BCM2708
  30981. + *
  30982. + * Inspired by sdhci-pci.c, by Pierre Ossman
  30983. + */
  30984. +
  30985. +#include <linux/delay.h>
  30986. +#include <linux/highmem.h>
  30987. +#include <linux/platform_device.h>
  30988. +#include <linux/module.h>
  30989. +#include <linux/mmc/mmc.h>
  30990. +#include <linux/mmc/host.h>
  30991. +#include <linux/mmc/sd.h>
  30992. +
  30993. +#include <linux/io.h>
  30994. +#include <linux/dma-mapping.h>
  30995. +#include <mach/dma.h>
  30996. +
  30997. +#include "sdhci.h"
  30998. +
  30999. +/*****************************************************************************\
  31000. + * *
  31001. + * Configuration *
  31002. + * *
  31003. +\*****************************************************************************/
  31004. +
  31005. +#define DRIVER_NAME "bcm2708_sdhci"
  31006. +
  31007. +/* for the time being insist on DMA mode - PIO seems not to work */
  31008. +#ifndef CONFIG_MMC_SDHCI_BCM2708_DMA
  31009. +#warning Non-DMA (PIO) version of this driver currently unavailable
  31010. +#endif
  31011. +#undef CONFIG_MMC_SDHCI_BCM2708_DMA
  31012. +#define CONFIG_MMC_SDHCI_BCM2708_DMA y
  31013. +
  31014. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31015. +/* #define CHECK_DMA_USE */
  31016. +#endif
  31017. +//#define LOG_REGISTERS
  31018. +
  31019. +#define USE_SCHED_TIME
  31020. +#define USE_SPACED_WRITES_2CLK 1 /* space consecutive register writes */
  31021. +#define USE_SOFTWARE_TIMEOUTS 1 /* not hardware timeouts */
  31022. +#define SOFTWARE_ERASE_TIMEOUT_SEC 30
  31023. +
  31024. +#define SDHCI_BCM_DMA_CHAN 4 /* this default is normally overriden */
  31025. +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
  31026. +/* We are worried that SD card DMA use may be blocking the AXI bus for others */
  31027. +
  31028. +/*! TODO: obtain these from the physical address */
  31029. +#define DMA_SDHCI_BASE 0x7e300000 /* EMMC register block on Videocore */
  31030. +#define DMA_SDHCI_BUFFER (DMA_SDHCI_BASE + SDHCI_BUFFER)
  31031. +
  31032. +#define BCM2708_SDHCI_SLEEP_TIMEOUT 1000 /* msecs */
  31033. +
  31034. +/* Mhz clock that the EMMC core is running at. Should match the platform clockman settings */
  31035. +#define BCM2708_EMMC_CLOCK_FREQ 50000000
  31036. +
  31037. +#define REG_EXRDFIFO_EN 0x80
  31038. +#define REG_EXRDFIFO_CFG 0x84
  31039. +
  31040. +int cycle_delay=2;
  31041. +
  31042. +/*****************************************************************************\
  31043. + * *
  31044. + * Debug *
  31045. + * *
  31046. +\*****************************************************************************/
  31047. +
  31048. +
  31049. +
  31050. +#define DBG(f, x...) \
  31051. + pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  31052. +// printk(KERN_INFO DRIVER_NAME " [%s()]: " f, __func__,## x)//GRAYG
  31053. +
  31054. +
  31055. +/*****************************************************************************\
  31056. + * *
  31057. + * High Precision Time *
  31058. + * *
  31059. +\*****************************************************************************/
  31060. +
  31061. +#ifdef USE_SCHED_TIME
  31062. +
  31063. +#include <mach/frc.h>
  31064. +
  31065. +typedef unsigned long hptime_t;
  31066. +
  31067. +#define FMT_HPT "lu"
  31068. +
  31069. +static inline hptime_t hptime(void)
  31070. +{
  31071. + return frc_clock_ticks32();
  31072. +}
  31073. +
  31074. +#define HPTIME_CLK_NS 1000ul
  31075. +
  31076. +#else
  31077. +
  31078. +typedef unsigned long hptime_t;
  31079. +
  31080. +#define FMT_HPT "lu"
  31081. +
  31082. +static inline hptime_t hptime(void)
  31083. +{
  31084. + return jiffies;
  31085. +}
  31086. +
  31087. +#define HPTIME_CLK_NS (1000000000ul/HZ)
  31088. +
  31089. +#endif
  31090. +
  31091. +static inline unsigned long int since_ns(hptime_t t)
  31092. +{
  31093. + return (unsigned long)((hptime() - t) * HPTIME_CLK_NS);
  31094. +}
  31095. +
  31096. +static bool allow_highspeed = 1;
  31097. +static int emmc_clock_freq = BCM2708_EMMC_CLOCK_FREQ;
  31098. +static bool sync_after_dma = 1;
  31099. +static bool missing_status = 1;
  31100. +static bool spurious_crc_acmd51 = 0;
  31101. +bool enable_llm = 1;
  31102. +bool extra_messages = 0;
  31103. +
  31104. +#if 0
  31105. +static void hptime_test(void)
  31106. +{
  31107. + hptime_t now;
  31108. + hptime_t later;
  31109. +
  31110. + now = hptime();
  31111. + msleep(10);
  31112. + later = hptime();
  31113. +
  31114. + printk(KERN_INFO DRIVER_NAME": 10ms = %"FMT_HPT" clks "
  31115. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  31116. + later-now, now, later,
  31117. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  31118. +
  31119. + now = hptime();
  31120. + msleep(1000);
  31121. + later = hptime();
  31122. +
  31123. + printk(KERN_INFO DRIVER_NAME": 1s = %"FMT_HPT" clks "
  31124. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  31125. + later-now, now, later,
  31126. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  31127. +}
  31128. +#endif
  31129. +
  31130. +/*****************************************************************************\
  31131. + * *
  31132. + * SDHCI core callbacks *
  31133. + * *
  31134. +\*****************************************************************************/
  31135. +
  31136. +
  31137. +#ifdef CHECK_DMA_USE
  31138. +/*#define CHECK_DMA_REG_USE*/
  31139. +#endif
  31140. +
  31141. +#ifdef CHECK_DMA_REG_USE
  31142. +/* we don't expect anything to be using these registers during a
  31143. + DMA (except the IRQ status) - so check */
  31144. +static void check_dma_reg_use(struct sdhci_host *host, int reg);
  31145. +#else
  31146. +#define check_dma_reg_use(host, reg)
  31147. +#endif
  31148. +
  31149. +
  31150. +static inline u32 sdhci_bcm2708_raw_readl(struct sdhci_host *host, int reg)
  31151. +{
  31152. + return readl(host->ioaddr + reg);
  31153. +}
  31154. +
  31155. +u32 sdhci_bcm2708_readl(struct sdhci_host *host, int reg)
  31156. +{
  31157. + u32 l = sdhci_bcm2708_raw_readl(host, reg);
  31158. +
  31159. +#ifdef LOG_REGISTERS
  31160. + printk(KERN_ERR "%s: readl from 0x%02x, value 0x%08x\n",
  31161. + mmc_hostname(host->mmc), reg, l);
  31162. +#endif
  31163. + check_dma_reg_use(host, reg);
  31164. +
  31165. + return l;
  31166. +}
  31167. +
  31168. +u16 sdhci_bcm2708_readw(struct sdhci_host *host, int reg)
  31169. +{
  31170. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  31171. + u32 w = l >> (reg << 3 & 0x18) & 0xffff;
  31172. +
  31173. +#ifdef LOG_REGISTERS
  31174. + printk(KERN_ERR "%s: readw from 0x%02x, value 0x%04x\n",
  31175. + mmc_hostname(host->mmc), reg, w);
  31176. +#endif
  31177. + check_dma_reg_use(host, reg);
  31178. +
  31179. + return (u16)w;
  31180. +}
  31181. +
  31182. +u8 sdhci_bcm2708_readb(struct sdhci_host *host, int reg)
  31183. +{
  31184. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  31185. + u32 b = l >> (reg << 3 & 0x18) & 0xff;
  31186. +
  31187. +#ifdef LOG_REGISTERS
  31188. + printk(KERN_ERR "%s: readb from 0x%02x, value 0x%02x\n",
  31189. + mmc_hostname(host->mmc), reg, b);
  31190. +#endif
  31191. + check_dma_reg_use(host, reg);
  31192. +
  31193. + return (u8)b;
  31194. +}
  31195. +
  31196. +
  31197. +static void sdhci_bcm2708_raw_writel(struct sdhci_host *host, u32 val, int reg)
  31198. +{
  31199. + u32 ier;
  31200. +
  31201. +#if USE_SPACED_WRITES_2CLK
  31202. + static bool timeout_disabled = false;
  31203. + unsigned int ns_2clk = 0;
  31204. +
  31205. + /* The Arasan has a bugette whereby it may lose the content of
  31206. + * successive writes to registers that are within two SD-card clock
  31207. + * cycles of each other (a clock domain crossing problem).
  31208. + * It seems, however, that the data register does not have this problem.
  31209. + * (Which is just as well - otherwise we'd have to nobble the DMA engine
  31210. + * too)
  31211. + */
  31212. + if (reg != SDHCI_BUFFER && host->clock != 0) {
  31213. + /* host->clock is the clock freq in Hz */
  31214. + static hptime_t last_write_hpt;
  31215. + hptime_t now = hptime();
  31216. + ns_2clk = cycle_delay*1000000/(host->clock/1000);
  31217. +
  31218. + if (now == last_write_hpt || now == last_write_hpt+1) {
  31219. + /* we can't guarantee any significant time has
  31220. + * passed - we'll have to wait anyway ! */
  31221. + ndelay(ns_2clk);
  31222. + } else
  31223. + {
  31224. + /* we must have waited at least this many ns: */
  31225. + unsigned int ns_wait = HPTIME_CLK_NS *
  31226. + (now - last_write_hpt - 1);
  31227. + if (ns_wait < ns_2clk)
  31228. + ndelay(ns_2clk - ns_wait);
  31229. + }
  31230. + last_write_hpt = now;
  31231. + }
  31232. +#if USE_SOFTWARE_TIMEOUTS
  31233. + /* The Arasan is clocked for timeouts using the SD clock which is too
  31234. + * fast for ERASE commands and causes issues. So we disable timeouts
  31235. + * for ERASE */
  31236. + if (host->cmd != NULL && host->cmd->opcode == MMC_ERASE &&
  31237. + reg == (SDHCI_COMMAND & ~3)) {
  31238. + mod_timer(&host->timer,
  31239. + jiffies + SOFTWARE_ERASE_TIMEOUT_SEC * HZ);
  31240. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31241. + ier &= ~SDHCI_INT_DATA_TIMEOUT;
  31242. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31243. + timeout_disabled = true;
  31244. + ndelay(ns_2clk);
  31245. + } else if (timeout_disabled) {
  31246. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31247. + ier |= SDHCI_INT_DATA_TIMEOUT;
  31248. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  31249. + timeout_disabled = false;
  31250. + ndelay(ns_2clk);
  31251. + }
  31252. +#endif
  31253. + writel(val, host->ioaddr + reg);
  31254. +#else
  31255. + void __iomem * regaddr = host->ioaddr + reg;
  31256. +
  31257. + writel(val, regaddr);
  31258. +
  31259. + if (reg != SDHCI_BUFFER && reg != SDHCI_INT_STATUS && host->clock != 0)
  31260. + {
  31261. + int timeout = 100000;
  31262. + while (val != readl(regaddr) && --timeout > 0)
  31263. + continue;
  31264. +
  31265. + if (timeout <= 0)
  31266. + printk(KERN_ERR "%s: writing 0x%X to reg 0x%X "
  31267. + "always gives 0x%X\n",
  31268. + mmc_hostname(host->mmc),
  31269. + val, reg, readl(regaddr));
  31270. + BUG_ON(timeout <= 0);
  31271. + }
  31272. +#endif
  31273. +}
  31274. +
  31275. +
  31276. +void sdhci_bcm2708_writel(struct sdhci_host *host, u32 val, int reg)
  31277. +{
  31278. +#ifdef LOG_REGISTERS
  31279. + printk(KERN_ERR "%s: writel to 0x%02x, value 0x%08x\n",
  31280. + mmc_hostname(host->mmc), reg, val);
  31281. +#endif
  31282. + check_dma_reg_use(host, reg);
  31283. +
  31284. + sdhci_bcm2708_raw_writel(host, val, reg);
  31285. +}
  31286. +
  31287. +void sdhci_bcm2708_writew(struct sdhci_host *host, u16 val, int reg)
  31288. +{
  31289. + static u32 shadow = 0;
  31290. +
  31291. + u32 p = reg == SDHCI_COMMAND ? shadow :
  31292. + sdhci_bcm2708_raw_readl(host, reg & ~3);
  31293. + u32 s = reg << 3 & 0x18;
  31294. + u32 l = val << s;
  31295. + u32 m = 0xffff << s;
  31296. +
  31297. +#ifdef LOG_REGISTERS
  31298. + printk(KERN_ERR "%s: writew to 0x%02x, value 0x%04x\n",
  31299. + mmc_hostname(host->mmc), reg, val);
  31300. +#endif
  31301. +
  31302. + if (reg == SDHCI_TRANSFER_MODE)
  31303. + shadow = (p & ~m) | l;
  31304. + else {
  31305. + check_dma_reg_use(host, reg);
  31306. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  31307. + }
  31308. +}
  31309. +
  31310. +void sdhci_bcm2708_writeb(struct sdhci_host *host, u8 val, int reg)
  31311. +{
  31312. + u32 p = sdhci_bcm2708_raw_readl(host, reg & ~3);
  31313. + u32 s = reg << 3 & 0x18;
  31314. + u32 l = val << s;
  31315. + u32 m = 0xff << s;
  31316. +
  31317. +#ifdef LOG_REGISTERS
  31318. + printk(KERN_ERR "%s: writeb to 0x%02x, value 0x%02x\n",
  31319. + mmc_hostname(host->mmc), reg, val);
  31320. +#endif
  31321. +
  31322. + check_dma_reg_use(host, reg);
  31323. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  31324. +}
  31325. +
  31326. +static unsigned int sdhci_bcm2708_get_max_clock(struct sdhci_host *host)
  31327. +{
  31328. + return emmc_clock_freq;
  31329. +}
  31330. +
  31331. +/*****************************************************************************\
  31332. + * *
  31333. + * DMA Operation *
  31334. + * *
  31335. +\*****************************************************************************/
  31336. +
  31337. +struct sdhci_bcm2708_priv {
  31338. + int dma_chan;
  31339. + int dma_irq;
  31340. + void __iomem *dma_chan_base;
  31341. + struct bcm2708_dma_cb *cb_base; /* DMA control blocks */
  31342. + dma_addr_t cb_handle;
  31343. + /* tracking scatter gather progress */
  31344. + unsigned sg_ix; /* scatter gather list index */
  31345. + unsigned sg_done; /* bytes in current sg_ix done */
  31346. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31347. + unsigned char dma_wanted; /* DMA transfer requested */
  31348. + unsigned char dma_waits; /* wait states in DMAs */
  31349. +#ifdef CHECK_DMA_USE
  31350. + unsigned char dmas_pending; /* no of unfinished DMAs */
  31351. + hptime_t when_started;
  31352. + hptime_t when_reset;
  31353. + hptime_t when_stopped;
  31354. +#endif
  31355. +#endif
  31356. + /* signalling the end of a transfer */
  31357. + void (*complete)(struct sdhci_host *);
  31358. +};
  31359. +
  31360. +#define SDHCI_HOST_PRIV(host) \
  31361. + (struct sdhci_bcm2708_priv *)((struct sdhci_host *)(host)+1)
  31362. +
  31363. +
  31364. +
  31365. +#ifdef CHECK_DMA_REG_USE
  31366. +static void check_dma_reg_use(struct sdhci_host *host, int reg)
  31367. +{
  31368. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31369. + if (host_priv->dma_wanted && reg != SDHCI_INT_STATUS) {
  31370. + printk(KERN_INFO"%s: accessing register 0x%x during DMA\n",
  31371. + mmc_hostname(host->mmc), reg);
  31372. + }
  31373. +}
  31374. +#endif
  31375. +
  31376. +
  31377. +
  31378. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31379. +
  31380. +static void sdhci_clear_set_irqgen(struct sdhci_host *host, u32 clear, u32 set)
  31381. +{
  31382. + u32 ier;
  31383. +
  31384. + ier = sdhci_bcm2708_raw_readl(host, SDHCI_SIGNAL_ENABLE);
  31385. + ier &= ~clear;
  31386. + ier |= set;
  31387. + /* change which requests generate IRQs - makes no difference to
  31388. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  31389. + sdhci_bcm2708_raw_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  31390. +}
  31391. +
  31392. +static void sdhci_signal_irqs(struct sdhci_host *host, u32 irqs)
  31393. +{
  31394. + sdhci_clear_set_irqgen(host, 0, irqs);
  31395. +}
  31396. +
  31397. +static void sdhci_unsignal_irqs(struct sdhci_host *host, u32 irqs)
  31398. +{
  31399. + sdhci_clear_set_irqgen(host, irqs, 0);
  31400. +}
  31401. +
  31402. +
  31403. +
  31404. +static void schci_bcm2708_cb_read(struct sdhci_bcm2708_priv *host,
  31405. + int ix,
  31406. + dma_addr_t dma_addr, unsigned len,
  31407. + int /*bool*/ is_last)
  31408. +{
  31409. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  31410. + unsigned char dmawaits = host->dma_waits;
  31411. +
  31412. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  31413. + BCM2708_DMA_WAITS(dmawaits) |
  31414. + BCM2708_DMA_S_DREQ |
  31415. + BCM2708_DMA_D_WIDTH |
  31416. + BCM2708_DMA_D_INC;
  31417. + cb->src = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  31418. + cb->dst = dma_addr;
  31419. + cb->length = len;
  31420. + cb->stride = 0;
  31421. +
  31422. + if (is_last) {
  31423. + cb->info |= BCM2708_DMA_INT_EN |
  31424. + BCM2708_DMA_WAIT_RESP;
  31425. + cb->next = 0;
  31426. + } else
  31427. + cb->next = host->cb_handle +
  31428. + (ix+1)*sizeof(struct bcm2708_dma_cb);
  31429. +
  31430. + cb->pad[0] = 0;
  31431. + cb->pad[1] = 0;
  31432. +}
  31433. +
  31434. +static void schci_bcm2708_cb_write(struct sdhci_bcm2708_priv *host,
  31435. + int ix,
  31436. + dma_addr_t dma_addr, unsigned len,
  31437. + int /*bool*/ is_last)
  31438. +{
  31439. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  31440. + unsigned char dmawaits = host->dma_waits;
  31441. +
  31442. + /* We can make arbitrarily large writes as long as we specify DREQ to
  31443. + pace the delivery of bytes to the Arasan hardware */
  31444. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  31445. + BCM2708_DMA_WAITS(dmawaits) |
  31446. + BCM2708_DMA_D_DREQ |
  31447. + BCM2708_DMA_S_WIDTH |
  31448. + BCM2708_DMA_S_INC;
  31449. + cb->src = dma_addr;
  31450. + cb->dst = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  31451. + cb->length = len;
  31452. + cb->stride = 0;
  31453. +
  31454. + if (is_last) {
  31455. + cb->info |= BCM2708_DMA_INT_EN |
  31456. + BCM2708_DMA_WAIT_RESP;
  31457. + cb->next = 0;
  31458. + } else
  31459. + cb->next = host->cb_handle +
  31460. + (ix+1)*sizeof(struct bcm2708_dma_cb);
  31461. +
  31462. + cb->pad[0] = 0;
  31463. + cb->pad[1] = 0;
  31464. +}
  31465. +
  31466. +
  31467. +static void schci_bcm2708_dma_go(struct sdhci_host *host)
  31468. +{
  31469. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31470. + void __iomem *dma_chan_base = host_priv->dma_chan_base;
  31471. +
  31472. + BUG_ON(host_priv->dma_wanted);
  31473. +#ifdef CHECK_DMA_USE
  31474. + if (host_priv->dma_wanted)
  31475. + printk(KERN_ERR "%s: DMA already in progress - "
  31476. + "now %"FMT_HPT", last started %lu "
  31477. + "reset %lu stopped %lu\n",
  31478. + mmc_hostname(host->mmc),
  31479. + hptime(), since_ns(host_priv->when_started),
  31480. + since_ns(host_priv->when_reset),
  31481. + since_ns(host_priv->when_stopped));
  31482. + else if (host_priv->dmas_pending > 0)
  31483. + printk(KERN_INFO "%s: note - new DMA when %d reset DMAs "
  31484. + "already in progress - "
  31485. + "now %"FMT_HPT", started %lu reset %lu stopped %lu\n",
  31486. + mmc_hostname(host->mmc),
  31487. + host_priv->dmas_pending,
  31488. + hptime(), since_ns(host_priv->when_started),
  31489. + since_ns(host_priv->when_reset),
  31490. + since_ns(host_priv->when_stopped));
  31491. + host_priv->dmas_pending += 1;
  31492. + host_priv->when_started = hptime();
  31493. +#endif
  31494. + host_priv->dma_wanted = 1;
  31495. + DBG("PDMA go - base %p handle %08X\n", dma_chan_base,
  31496. + host_priv->cb_handle);
  31497. + bcm_dma_start(dma_chan_base, host_priv->cb_handle);
  31498. +}
  31499. +
  31500. +
  31501. +static void
  31502. +sdhci_platdma_read(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  31503. +{
  31504. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31505. +
  31506. + DBG("PDMA to read %d bytes\n", len);
  31507. + host_priv->sg_done += len;
  31508. + schci_bcm2708_cb_read(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  31509. + schci_bcm2708_dma_go(host);
  31510. +}
  31511. +
  31512. +
  31513. +static void
  31514. +sdhci_platdma_write(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  31515. +{
  31516. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31517. +
  31518. + DBG("PDMA to write %d bytes\n", len);
  31519. + //BUG_ON(0 != (len & 0x1ff));
  31520. +
  31521. + host_priv->sg_done += len;
  31522. + schci_bcm2708_cb_write(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  31523. + schci_bcm2708_dma_go(host);
  31524. +}
  31525. +
  31526. +/*! space is avaiable to receive into or data is available to write
  31527. + Platform DMA exported function
  31528. +*/
  31529. +void
  31530. +sdhci_bcm2708_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  31531. + void(*completion_callback)(struct sdhci_host *host))
  31532. +{
  31533. + struct mmc_data *data = host->data;
  31534. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31535. + int sg_ix;
  31536. + size_t bytes;
  31537. + dma_addr_t addr;
  31538. +
  31539. + BUG_ON(NULL == data);
  31540. + BUG_ON(0 == data->blksz);
  31541. +
  31542. + host_priv->complete = completion_callback;
  31543. +
  31544. + sg_ix = host_priv->sg_ix;
  31545. + BUG_ON(sg_ix >= data->sg_len);
  31546. +
  31547. + /* we can DMA blocks larger than blksz - it may hang the DMA
  31548. + channel but we are its only user */
  31549. + bytes = sg_dma_len(&data->sg[sg_ix]) - host_priv->sg_done;
  31550. + addr = sg_dma_address(&data->sg[sg_ix]) + host_priv->sg_done;
  31551. +
  31552. + if (bytes > 0) {
  31553. + /* We're going to poll for read/write available state until
  31554. + we finish this DMA
  31555. + */
  31556. +
  31557. + if (data->flags & MMC_DATA_READ) {
  31558. + if (*ref_intmask & SDHCI_INT_DATA_AVAIL) {
  31559. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31560. + SDHCI_INT_SPACE_AVAIL);
  31561. + sdhci_platdma_read(host, addr, bytes);
  31562. + }
  31563. + } else {
  31564. + if (*ref_intmask & SDHCI_INT_SPACE_AVAIL) {
  31565. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31566. + SDHCI_INT_SPACE_AVAIL);
  31567. + sdhci_platdma_write(host, addr, bytes);
  31568. + }
  31569. + }
  31570. + }
  31571. + /* else:
  31572. + we have run out of bytes that need transferring (e.g. we may be in
  31573. + the middle of the last DMA transfer), or
  31574. + it is also possible that we've been called when another IRQ is
  31575. + signalled, even though we've turned off signalling of our own IRQ */
  31576. +
  31577. + *ref_intmask &= ~SDHCI_INT_DATA_END;
  31578. + /* don't let the main sdhci driver act on this .. we'll deal with it
  31579. + when we respond to the DMA - if one is currently in progress */
  31580. +}
  31581. +
  31582. +/* is it possible to DMA the given mmc_data structure?
  31583. + Platform DMA exported function
  31584. +*/
  31585. +int /*bool*/
  31586. +sdhci_bcm2708_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  31587. +{
  31588. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31589. + int ok = bcm_sg_suitable_for_dma(data->sg, data->sg_len);
  31590. +
  31591. + if (!ok)
  31592. + DBG("Reverting to PIO - bad cache alignment\n");
  31593. +
  31594. + else {
  31595. + host_priv->sg_ix = 0; /* first SG index */
  31596. + host_priv->sg_done = 0; /* no bytes done */
  31597. + }
  31598. +
  31599. + return ok;
  31600. +}
  31601. +
  31602. +#include <mach/arm_control.h> //GRAYG
  31603. +/*! the current SD transacton has been abandonned
  31604. + We need to tidy up if we were in the middle of a DMA
  31605. + Platform DMA exported function
  31606. +*/
  31607. +void
  31608. +sdhci_bcm2708_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  31609. +{
  31610. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31611. +// unsigned long flags;
  31612. +
  31613. + BUG_ON(NULL == host);
  31614. +
  31615. +// spin_lock_irqsave(&host->lock, flags);
  31616. +
  31617. + if (host_priv->dma_wanted) {
  31618. + if (NULL == data) {
  31619. + printk(KERN_ERR "%s: ongoing DMA reset - no data!\n",
  31620. + mmc_hostname(host->mmc));
  31621. + BUG_ON(NULL == data);
  31622. + } else {
  31623. + struct scatterlist *sg;
  31624. + int sg_len;
  31625. + int sg_todo;
  31626. + int rc;
  31627. + unsigned long cs;
  31628. +
  31629. + sg = data->sg;
  31630. + sg_len = data->sg_len;
  31631. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  31632. +
  31633. + cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  31634. +
  31635. + if (!(BCM2708_DMA_ACTIVE & cs))
  31636. + {
  31637. + if (extra_messages)
  31638. + printk(KERN_INFO "%s: missed completion of "
  31639. + "cmd %d DMA (%d/%d [%d]/[%d]) - "
  31640. + "ignoring it\n",
  31641. + mmc_hostname(host->mmc),
  31642. + host->last_cmdop,
  31643. + host_priv->sg_done, sg_todo,
  31644. + host_priv->sg_ix+1, sg_len);
  31645. + }
  31646. + else
  31647. + printk(KERN_INFO "%s: resetting ongoing cmd %d"
  31648. + "DMA before %d/%d [%d]/[%d] complete\n",
  31649. + mmc_hostname(host->mmc),
  31650. + host->last_cmdop,
  31651. + host_priv->sg_done, sg_todo,
  31652. + host_priv->sg_ix+1, sg_len);
  31653. +#ifdef CHECK_DMA_USE
  31654. + printk(KERN_INFO "%s: now %"FMT_HPT" started %lu "
  31655. + "last reset %lu last stopped %lu\n",
  31656. + mmc_hostname(host->mmc),
  31657. + hptime(), since_ns(host_priv->when_started),
  31658. + since_ns(host_priv->when_reset),
  31659. + since_ns(host_priv->when_stopped));
  31660. + { unsigned long info, debug;
  31661. + void __iomem *base;
  31662. + unsigned long pend0, pend1, pend2;
  31663. +
  31664. + base = host_priv->dma_chan_base;
  31665. + cs = readl(base + BCM2708_DMA_CS);
  31666. + info = readl(base + BCM2708_DMA_INFO);
  31667. + debug = readl(base + BCM2708_DMA_DEBUG);
  31668. + printk(KERN_INFO "%s: DMA%d CS=%08lX TI=%08lX "
  31669. + "DEBUG=%08lX\n",
  31670. + mmc_hostname(host->mmc),
  31671. + host_priv->dma_chan,
  31672. + cs, info, debug);
  31673. + pend0 = readl(__io_address(ARM_IRQ_PEND0));
  31674. + pend1 = readl(__io_address(ARM_IRQ_PEND1));
  31675. + pend2 = readl(__io_address(ARM_IRQ_PEND2));
  31676. +
  31677. + printk(KERN_INFO "%s: PEND0=%08lX "
  31678. + "PEND1=%08lX PEND2=%08lX\n",
  31679. + mmc_hostname(host->mmc),
  31680. + pend0, pend1, pend2);
  31681. +
  31682. + //gintsts = readl(__io_address(GINTSTS));
  31683. + //gintmsk = readl(__io_address(GINTMSK));
  31684. + //printk(KERN_INFO "%s: USB GINTSTS=%08lX"
  31685. + // "GINTMSK=%08lX\n",
  31686. + // mmc_hostname(host->mmc), gintsts, gintmsk);
  31687. + }
  31688. +#endif
  31689. + rc = bcm_dma_abort(host_priv->dma_chan_base);
  31690. + BUG_ON(rc != 0);
  31691. + }
  31692. + host_priv->dma_wanted = 0;
  31693. +#ifdef CHECK_DMA_USE
  31694. + host_priv->when_reset = hptime();
  31695. +#endif
  31696. + }
  31697. +
  31698. +// spin_unlock_irqrestore(&host->lock, flags);
  31699. +}
  31700. +
  31701. +
  31702. +static void sdhci_bcm2708_dma_complete_irq(struct sdhci_host *host,
  31703. + u32 dma_cs)
  31704. +{
  31705. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31706. + struct mmc_data *data;
  31707. + struct scatterlist *sg;
  31708. + int sg_len;
  31709. + int sg_ix;
  31710. + int sg_todo;
  31711. +// unsigned long flags;
  31712. +
  31713. + BUG_ON(NULL == host);
  31714. +
  31715. +// spin_lock_irqsave(&host->lock, flags);
  31716. + data = host->data;
  31717. +
  31718. +#ifdef CHECK_DMA_USE
  31719. + if (host_priv->dmas_pending <= 0)
  31720. + DBG("on completion no DMA in progress - "
  31721. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  31722. + hptime(), since_ns(host_priv->when_started),
  31723. + since_ns(host_priv->when_reset),
  31724. + since_ns(host_priv->when_stopped));
  31725. + else if (host_priv->dmas_pending > 1)
  31726. + DBG("still %d DMA in progress after completion - "
  31727. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  31728. + host_priv->dmas_pending - 1,
  31729. + hptime(), since_ns(host_priv->when_started),
  31730. + since_ns(host_priv->when_reset),
  31731. + since_ns(host_priv->when_stopped));
  31732. + BUG_ON(host_priv->dmas_pending <= 0);
  31733. + host_priv->dmas_pending -= 1;
  31734. + host_priv->when_stopped = hptime();
  31735. +#endif
  31736. + host_priv->dma_wanted = 0;
  31737. +
  31738. + if (NULL == data) {
  31739. + DBG("PDMA unused completion - status 0x%X\n", dma_cs);
  31740. +// spin_unlock_irqrestore(&host->lock, flags);
  31741. + return;
  31742. + }
  31743. + sg = data->sg;
  31744. + sg_len = data->sg_len;
  31745. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  31746. +
  31747. + DBG("PDMA complete %d/%d [%d]/[%d]..\n",
  31748. + host_priv->sg_done, sg_todo,
  31749. + host_priv->sg_ix+1, sg_len);
  31750. +
  31751. + BUG_ON(host_priv->sg_done > sg_todo);
  31752. +
  31753. + if (host_priv->sg_done >= sg_todo) {
  31754. + host_priv->sg_ix++;
  31755. + host_priv->sg_done = 0;
  31756. + }
  31757. +
  31758. + sg_ix = host_priv->sg_ix;
  31759. + if (sg_ix < sg_len) {
  31760. + u32 irq_mask;
  31761. + /* Set off next DMA if we've got the capacity */
  31762. +
  31763. + if (data->flags & MMC_DATA_READ)
  31764. + irq_mask = SDHCI_INT_DATA_AVAIL;
  31765. + else
  31766. + irq_mask = SDHCI_INT_SPACE_AVAIL;
  31767. +
  31768. + /* We have to use the interrupt status register on the BCM2708
  31769. + rather than the SDHCI_PRESENT_STATE register because latency
  31770. + in the glue logic means that the information retrieved from
  31771. + the latter is not always up-to-date w.r.t the DMA engine -
  31772. + it may not indicate that a read or a write is ready yet */
  31773. + if (sdhci_bcm2708_raw_readl(host, SDHCI_INT_STATUS) &
  31774. + irq_mask) {
  31775. + size_t bytes = sg_dma_len(&sg[sg_ix]) -
  31776. + host_priv->sg_done;
  31777. + dma_addr_t addr = sg_dma_address(&data->sg[sg_ix]) +
  31778. + host_priv->sg_done;
  31779. +
  31780. + /* acknowledge interrupt */
  31781. + sdhci_bcm2708_raw_writel(host, irq_mask,
  31782. + SDHCI_INT_STATUS);
  31783. +
  31784. + BUG_ON(0 == bytes);
  31785. +
  31786. + if (data->flags & MMC_DATA_READ)
  31787. + sdhci_platdma_read(host, addr, bytes);
  31788. + else
  31789. + sdhci_platdma_write(host, addr, bytes);
  31790. + } else {
  31791. + DBG("PDMA - wait avail\n");
  31792. + /* may generate an IRQ if already present */
  31793. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31794. + SDHCI_INT_SPACE_AVAIL);
  31795. + }
  31796. + } else {
  31797. + if (sync_after_dma) {
  31798. + /* On the Arasan controller the stop command (which will be
  31799. + scheduled after this completes) does not seem to work
  31800. + properly if we allow it to be issued when we are
  31801. + transferring data to/from the SD card.
  31802. + We get CRC and DEND errors unless we wait for
  31803. + the SD controller to finish reading/writing to the card. */
  31804. + u32 state_mask;
  31805. + int timeout=3*1000*1000;
  31806. +
  31807. + DBG("PDMA over - sync card\n");
  31808. + if (data->flags & MMC_DATA_READ)
  31809. + state_mask = SDHCI_DOING_READ;
  31810. + else
  31811. + state_mask = SDHCI_DOING_WRITE;
  31812. +
  31813. + while (0 != (sdhci_bcm2708_raw_readl(host, SDHCI_PRESENT_STATE)
  31814. + & state_mask) && --timeout > 0)
  31815. + {
  31816. + udelay(1);
  31817. + continue;
  31818. + }
  31819. + if (timeout <= 0)
  31820. + printk(KERN_ERR"%s: final %s to SD card still "
  31821. + "running\n",
  31822. + mmc_hostname(host->mmc),
  31823. + data->flags & MMC_DATA_READ? "read": "write");
  31824. + }
  31825. + if (host_priv->complete) {
  31826. + (*host_priv->complete)(host);
  31827. + DBG("PDMA %s complete\n",
  31828. + data->flags & MMC_DATA_READ?"read":"write");
  31829. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31830. + SDHCI_INT_SPACE_AVAIL);
  31831. + }
  31832. + }
  31833. +// spin_unlock_irqrestore(&host->lock, flags);
  31834. +}
  31835. +
  31836. +static irqreturn_t sdhci_bcm2708_dma_irq(int irq, void *dev_id)
  31837. +{
  31838. + irqreturn_t result = IRQ_NONE;
  31839. + struct sdhci_host *host = dev_id;
  31840. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31841. + u32 dma_cs; /* control and status register */
  31842. +
  31843. + BUG_ON(NULL == dev_id);
  31844. + BUG_ON(NULL == host_priv->dma_chan_base);
  31845. +
  31846. + sdhci_spin_lock(host);
  31847. +
  31848. + dma_cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  31849. +
  31850. + if (dma_cs & BCM2708_DMA_ERR) {
  31851. + unsigned long debug;
  31852. + debug = readl(host_priv->dma_chan_base +
  31853. + BCM2708_DMA_DEBUG);
  31854. + printk(KERN_ERR "%s: DMA error - CS %lX DEBUG %lX\n",
  31855. + mmc_hostname(host->mmc), (unsigned long)dma_cs,
  31856. + (unsigned long)debug);
  31857. + /* reset error */
  31858. + writel(debug, host_priv->dma_chan_base +
  31859. + BCM2708_DMA_DEBUG);
  31860. + }
  31861. + if (dma_cs & BCM2708_DMA_INT) {
  31862. + /* acknowledge interrupt */
  31863. + writel(BCM2708_DMA_INT,
  31864. + host_priv->dma_chan_base + BCM2708_DMA_CS);
  31865. +
  31866. + dsb(); /* ARM data synchronization (push) operation */
  31867. +
  31868. + if (!host_priv->dma_wanted) {
  31869. + /* ignore this interrupt - it was reset */
  31870. + if (extra_messages)
  31871. + printk(KERN_INFO "%s: DMA IRQ %X ignored - "
  31872. + "results were reset\n",
  31873. + mmc_hostname(host->mmc), dma_cs);
  31874. +#ifdef CHECK_DMA_USE
  31875. + printk(KERN_INFO "%s: now %"FMT_HPT
  31876. + " started %lu reset %lu stopped %lu\n",
  31877. + mmc_hostname(host->mmc), hptime(),
  31878. + since_ns(host_priv->when_started),
  31879. + since_ns(host_priv->when_reset),
  31880. + since_ns(host_priv->when_stopped));
  31881. + host_priv->dmas_pending--;
  31882. +#endif
  31883. + } else
  31884. + sdhci_bcm2708_dma_complete_irq(host, dma_cs);
  31885. +
  31886. + result = IRQ_HANDLED;
  31887. + }
  31888. + sdhci_spin_unlock(host);
  31889. +
  31890. + return result;
  31891. +}
  31892. +#endif /* CONFIG_MMC_SDHCI_BCM2708_DMA */
  31893. +
  31894. +
  31895. +/***************************************************************************** \
  31896. + * *
  31897. + * Device Attributes *
  31898. + * *
  31899. +\*****************************************************************************/
  31900. +
  31901. +
  31902. +/**
  31903. + * Show the DMA-using status
  31904. + */
  31905. +static ssize_t attr_dma_show(struct device *_dev,
  31906. + struct device_attribute *attr, char *buf)
  31907. +{
  31908. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31909. +
  31910. + if (host) {
  31911. + int use_dma = (host->flags & SDHCI_USE_PLATDMA? 1:0);
  31912. + return sprintf(buf, "%d\n", use_dma);
  31913. + } else
  31914. + return -EINVAL;
  31915. +}
  31916. +
  31917. +/**
  31918. + * Set the DMA-using status
  31919. + */
  31920. +static ssize_t attr_dma_store(struct device *_dev,
  31921. + struct device_attribute *attr,
  31922. + const char *buf, size_t count)
  31923. +{
  31924. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31925. +
  31926. + if (host) {
  31927. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31928. + int on = simple_strtol(buf, NULL, 0);
  31929. + if (on) {
  31930. + host->flags |= SDHCI_USE_PLATDMA;
  31931. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  31932. + printk(KERN_INFO "%s: DMA enabled\n",
  31933. + mmc_hostname(host->mmc));
  31934. + } else {
  31935. + host->flags &= ~(SDHCI_USE_PLATDMA | SDHCI_REQ_USE_DMA);
  31936. + sdhci_bcm2708_writel(host, 0, REG_EXRDFIFO_EN);
  31937. + printk(KERN_INFO "%s: DMA disabled\n",
  31938. + mmc_hostname(host->mmc));
  31939. + }
  31940. +#endif
  31941. + return count;
  31942. + } else
  31943. + return -EINVAL;
  31944. +}
  31945. +
  31946. +static DEVICE_ATTR(use_dma, S_IRUGO | S_IWUGO, attr_dma_show, attr_dma_store);
  31947. +
  31948. +
  31949. +/**
  31950. + * Show the DMA wait states used
  31951. + */
  31952. +static ssize_t attr_dmawait_show(struct device *_dev,
  31953. + struct device_attribute *attr, char *buf)
  31954. +{
  31955. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31956. +
  31957. + if (host) {
  31958. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31959. + int dmawait = host_priv->dma_waits;
  31960. + return sprintf(buf, "%d\n", dmawait);
  31961. + } else
  31962. + return -EINVAL;
  31963. +}
  31964. +
  31965. +/**
  31966. + * Set the DMA wait state used
  31967. + */
  31968. +static ssize_t attr_dmawait_store(struct device *_dev,
  31969. + struct device_attribute *attr,
  31970. + const char *buf, size_t count)
  31971. +{
  31972. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31973. +
  31974. + if (host) {
  31975. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31976. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31977. + int dma_waits = simple_strtol(buf, NULL, 0);
  31978. + if (dma_waits >= 0 && dma_waits < 32)
  31979. + host_priv->dma_waits = dma_waits;
  31980. + else
  31981. + printk(KERN_ERR "%s: illegal dma_waits value - %d",
  31982. + mmc_hostname(host->mmc), dma_waits);
  31983. +#endif
  31984. + return count;
  31985. + } else
  31986. + return -EINVAL;
  31987. +}
  31988. +
  31989. +static DEVICE_ATTR(dma_wait, S_IRUGO | S_IWUGO,
  31990. + attr_dmawait_show, attr_dmawait_store);
  31991. +
  31992. +
  31993. +/**
  31994. + * Show the DMA-using status
  31995. + */
  31996. +static ssize_t attr_status_show(struct device *_dev,
  31997. + struct device_attribute *attr, char *buf)
  31998. +{
  31999. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  32000. +
  32001. + if (host) {
  32002. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  32003. + return sprintf(buf,
  32004. + "present: yes\n"
  32005. + "power: %s\n"
  32006. + "clock: %u Hz\n"
  32007. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32008. + "dma: %s (%d waits)\n",
  32009. +#else
  32010. + "dma: unconfigured\n",
  32011. +#endif
  32012. + "always on",
  32013. + host->clock
  32014. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32015. + , (host->flags & SDHCI_USE_PLATDMA)? "on": "off"
  32016. + , host_priv->dma_waits
  32017. +#endif
  32018. + );
  32019. + } else
  32020. + return -EINVAL;
  32021. +}
  32022. +
  32023. +static DEVICE_ATTR(status, S_IRUGO, attr_status_show, NULL);
  32024. +
  32025. +/***************************************************************************** \
  32026. + * *
  32027. + * Power Management *
  32028. + * *
  32029. +\*****************************************************************************/
  32030. +
  32031. +
  32032. +#ifdef CONFIG_PM
  32033. +static int sdhci_bcm2708_suspend(struct platform_device *dev, pm_message_t state)
  32034. +{
  32035. + struct sdhci_host *host = (struct sdhci_host *)
  32036. + platform_get_drvdata(dev);
  32037. + int ret = 0;
  32038. +
  32039. + if (host->mmc) {
  32040. + ret = mmc_suspend_host(host->mmc);
  32041. + }
  32042. +
  32043. + return ret;
  32044. +}
  32045. +
  32046. +static int sdhci_bcm2708_resume(struct platform_device *dev)
  32047. +{
  32048. + struct sdhci_host *host = (struct sdhci_host *)
  32049. + platform_get_drvdata(dev);
  32050. + int ret = 0;
  32051. +
  32052. + if (host->mmc) {
  32053. + ret = mmc_resume_host(host->mmc);
  32054. + }
  32055. +
  32056. + return ret;
  32057. +}
  32058. +#endif
  32059. +
  32060. +
  32061. +/*****************************************************************************\
  32062. + * *
  32063. + * Device quirk functions. Implemented as local ops because the flags *
  32064. + * field is out of space with newer kernels. This implementation can be *
  32065. + * back ported to older kernels as well. *
  32066. +\****************************************************************************/
  32067. +static unsigned int sdhci_bcm2708_quirk_extra_ints(struct sdhci_host *host)
  32068. +{
  32069. + return 1;
  32070. +}
  32071. +
  32072. +static unsigned int sdhci_bcm2708_quirk_spurious_crc_acmd51(struct sdhci_host *host)
  32073. +{
  32074. + return 1;
  32075. +}
  32076. +
  32077. +static unsigned int sdhci_bcm2708_missing_status(struct sdhci_host *host)
  32078. +{
  32079. + return 1;
  32080. +}
  32081. +
  32082. +/***************************************************************************** \
  32083. + * *
  32084. + * Device ops *
  32085. + * *
  32086. +\*****************************************************************************/
  32087. +
  32088. +static struct sdhci_ops sdhci_bcm2708_ops = {
  32089. +#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
  32090. + .read_l = sdhci_bcm2708_readl,
  32091. + .read_w = sdhci_bcm2708_readw,
  32092. + .read_b = sdhci_bcm2708_readb,
  32093. + .write_l = sdhci_bcm2708_writel,
  32094. + .write_w = sdhci_bcm2708_writew,
  32095. + .write_b = sdhci_bcm2708_writeb,
  32096. +#else
  32097. +#error The BCM2708 SDHCI driver needs CONFIG_MMC_SDHCI_IO_ACCESSORS to be set
  32098. +#endif
  32099. + .get_max_clock = sdhci_bcm2708_get_max_clock,
  32100. +
  32101. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32102. + // Platform DMA operations
  32103. + .pdma_able = sdhci_bcm2708_platdma_dmaable,
  32104. + .pdma_avail = sdhci_bcm2708_platdma_avail,
  32105. + .pdma_reset = sdhci_bcm2708_platdma_reset,
  32106. +#endif
  32107. + .extra_ints = sdhci_bcm2708_quirk_extra_ints,
  32108. +};
  32109. +
  32110. +/*****************************************************************************\
  32111. + * *
  32112. + * Device probing/removal *
  32113. + * *
  32114. +\*****************************************************************************/
  32115. +
  32116. +static int sdhci_bcm2708_probe(struct platform_device *pdev)
  32117. +{
  32118. + struct sdhci_host *host;
  32119. + struct resource *iomem;
  32120. + struct sdhci_bcm2708_priv *host_priv;
  32121. + int ret;
  32122. +
  32123. + BUG_ON(pdev == NULL);
  32124. +
  32125. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  32126. + if (!iomem) {
  32127. + ret = -ENOMEM;
  32128. + goto err;
  32129. + }
  32130. +
  32131. + if (resource_size(iomem) != 0x100)
  32132. + dev_err(&pdev->dev, "Invalid iomem size. You may "
  32133. + "experience problems.\n");
  32134. +
  32135. + if (pdev->dev.parent)
  32136. + host = sdhci_alloc_host(pdev->dev.parent,
  32137. + sizeof(struct sdhci_bcm2708_priv));
  32138. + else
  32139. + host = sdhci_alloc_host(&pdev->dev,
  32140. + sizeof(struct sdhci_bcm2708_priv));
  32141. +
  32142. + if (IS_ERR(host)) {
  32143. + ret = PTR_ERR(host);
  32144. + goto err;
  32145. + }
  32146. + if (missing_status) {
  32147. + sdhci_bcm2708_ops.missing_status = sdhci_bcm2708_missing_status;
  32148. + }
  32149. +
  32150. + if( spurious_crc_acmd51 ) {
  32151. + sdhci_bcm2708_ops.spurious_crc_acmd51 = sdhci_bcm2708_quirk_spurious_crc_acmd51;
  32152. + }
  32153. +
  32154. +
  32155. + printk("sdhci: %s low-latency mode\n",enable_llm?"Enable":"Disable");
  32156. +
  32157. + host->hw_name = "BCM2708_Arasan";
  32158. + host->ops = &sdhci_bcm2708_ops;
  32159. + host->irq = platform_get_irq(pdev, 0);
  32160. + host->second_irq = 0;
  32161. +
  32162. + host->quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  32163. + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  32164. + SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  32165. + SDHCI_QUIRK_MISSING_CAPS |
  32166. + SDHCI_QUIRK_NO_HISPD_BIT |
  32167. + (sync_after_dma ? 0:SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12);
  32168. +
  32169. +
  32170. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32171. + host->flags = SDHCI_USE_PLATDMA;
  32172. +#endif
  32173. +
  32174. + if (!request_mem_region(iomem->start, resource_size(iomem),
  32175. + mmc_hostname(host->mmc))) {
  32176. + dev_err(&pdev->dev, "cannot request region\n");
  32177. + ret = -EBUSY;
  32178. + goto err_request;
  32179. + }
  32180. +
  32181. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  32182. + if (!host->ioaddr) {
  32183. + dev_err(&pdev->dev, "failed to remap registers\n");
  32184. + ret = -ENOMEM;
  32185. + goto err_remap;
  32186. + }
  32187. +
  32188. + host_priv = SDHCI_HOST_PRIV(host);
  32189. +
  32190. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32191. + host_priv->dma_wanted = 0;
  32192. +#ifdef CHECK_DMA_USE
  32193. + host_priv->dmas_pending = 0;
  32194. + host_priv->when_started = 0;
  32195. + host_priv->when_reset = 0;
  32196. + host_priv->when_stopped = 0;
  32197. +#endif
  32198. + host_priv->sg_ix = 0;
  32199. + host_priv->sg_done = 0;
  32200. + host_priv->complete = NULL;
  32201. + host_priv->dma_waits = SDHCI_BCM_DMA_WAITS;
  32202. +
  32203. + host_priv->cb_base = dma_alloc_writecombine(&pdev->dev, SZ_4K,
  32204. + &host_priv->cb_handle,
  32205. + GFP_KERNEL);
  32206. + if (!host_priv->cb_base) {
  32207. + dev_err(&pdev->dev, "cannot allocate DMA CBs\n");
  32208. + ret = -ENOMEM;
  32209. + goto err_alloc_cb;
  32210. + }
  32211. +
  32212. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  32213. + &host_priv->dma_chan_base,
  32214. + &host_priv->dma_irq);
  32215. + if (ret < 0) {
  32216. + dev_err(&pdev->dev, "couldn't allocate a DMA channel\n");
  32217. + goto err_add_dma;
  32218. + }
  32219. + host_priv->dma_chan = ret;
  32220. +
  32221. + ret = request_irq(host_priv->dma_irq, sdhci_bcm2708_dma_irq,0,//IRQF_SHARED,
  32222. + DRIVER_NAME " (dma)", host);
  32223. + if (ret) {
  32224. + dev_err(&pdev->dev, "cannot set DMA IRQ\n");
  32225. + goto err_add_dma_irq;
  32226. + }
  32227. + host->second_irq = host_priv->dma_irq;
  32228. + DBG("DMA CBs %p handle %08X DMA%d %p DMA IRQ %d\n",
  32229. + host_priv->cb_base, (unsigned)host_priv->cb_handle,
  32230. + host_priv->dma_chan, host_priv->dma_chan_base,
  32231. + host_priv->dma_irq);
  32232. +
  32233. + // we support 3.3V
  32234. + host->caps |= SDHCI_CAN_VDD_330;
  32235. + if (allow_highspeed)
  32236. + host->mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  32237. +
  32238. + /* single block writes cause data loss with some SD cards! */
  32239. + host->mmc->caps2 |= MMC_CAP2_FORCE_MULTIBLOCK;
  32240. +#endif
  32241. +
  32242. + ret = sdhci_add_host(host);
  32243. + if (ret)
  32244. + goto err_add_host;
  32245. +
  32246. + platform_set_drvdata(pdev, host);
  32247. + ret = device_create_file(&pdev->dev, &dev_attr_use_dma);
  32248. + ret = device_create_file(&pdev->dev, &dev_attr_dma_wait);
  32249. + ret = device_create_file(&pdev->dev, &dev_attr_status);
  32250. +
  32251. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32252. + /* enable extension fifo for paced DMA transfers */
  32253. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  32254. + sdhci_bcm2708_writel(host, 4, REG_EXRDFIFO_CFG);
  32255. +#endif
  32256. +
  32257. + printk(KERN_INFO "%s: BCM2708 SDHC host at 0x%08llx DMA %d IRQ %d\n",
  32258. + mmc_hostname(host->mmc), (unsigned long long)iomem->start,
  32259. + host_priv->dma_chan, host_priv->dma_irq);
  32260. +
  32261. + return 0;
  32262. +
  32263. +err_add_host:
  32264. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32265. + free_irq(host_priv->dma_irq, host);
  32266. +err_add_dma_irq:
  32267. + bcm_dma_chan_free(host_priv->dma_chan);
  32268. +err_add_dma:
  32269. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  32270. + host_priv->cb_handle);
  32271. +err_alloc_cb:
  32272. +#endif
  32273. + iounmap(host->ioaddr);
  32274. +err_remap:
  32275. + release_mem_region(iomem->start, resource_size(iomem));
  32276. +err_request:
  32277. + sdhci_free_host(host);
  32278. +err:
  32279. + dev_err(&pdev->dev, "probe failed, err %d\n", ret);
  32280. + return ret;
  32281. +}
  32282. +
  32283. +static int sdhci_bcm2708_remove(struct platform_device *pdev)
  32284. +{
  32285. + struct sdhci_host *host = platform_get_drvdata(pdev);
  32286. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  32287. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  32288. + int dead;
  32289. + u32 scratch;
  32290. +
  32291. + dead = 0;
  32292. + scratch = sdhci_bcm2708_readl(host, SDHCI_INT_STATUS);
  32293. + if (scratch == (u32)-1)
  32294. + dead = 1;
  32295. +
  32296. + device_remove_file(&pdev->dev, &dev_attr_status);
  32297. + device_remove_file(&pdev->dev, &dev_attr_dma_wait);
  32298. + device_remove_file(&pdev->dev, &dev_attr_use_dma);
  32299. +
  32300. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  32301. + free_irq(host_priv->dma_irq, host);
  32302. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  32303. + host_priv->cb_handle);
  32304. +#endif
  32305. + sdhci_remove_host(host, dead);
  32306. + iounmap(host->ioaddr);
  32307. + release_mem_region(iomem->start, resource_size(iomem));
  32308. + sdhci_free_host(host);
  32309. + platform_set_drvdata(pdev, NULL);
  32310. +
  32311. + return 0;
  32312. +}
  32313. +
  32314. +static struct platform_driver sdhci_bcm2708_driver = {
  32315. + .driver = {
  32316. + .name = DRIVER_NAME,
  32317. + .owner = THIS_MODULE,
  32318. + },
  32319. + .probe = sdhci_bcm2708_probe,
  32320. + .remove = sdhci_bcm2708_remove,
  32321. +
  32322. +#ifdef CONFIG_PM
  32323. + .suspend = sdhci_bcm2708_suspend,
  32324. + .resume = sdhci_bcm2708_resume,
  32325. +#endif
  32326. +
  32327. +};
  32328. +
  32329. +/*****************************************************************************\
  32330. + * *
  32331. + * Driver init/exit *
  32332. + * *
  32333. +\*****************************************************************************/
  32334. +
  32335. +static int __init sdhci_drv_init(void)
  32336. +{
  32337. + return platform_driver_register(&sdhci_bcm2708_driver);
  32338. +}
  32339. +
  32340. +static void __exit sdhci_drv_exit(void)
  32341. +{
  32342. + platform_driver_unregister(&sdhci_bcm2708_driver);
  32343. +}
  32344. +
  32345. +module_init(sdhci_drv_init);
  32346. +module_exit(sdhci_drv_exit);
  32347. +
  32348. +module_param(allow_highspeed, bool, 0444);
  32349. +module_param(emmc_clock_freq, int, 0444);
  32350. +module_param(sync_after_dma, bool, 0444);
  32351. +module_param(missing_status, bool, 0444);
  32352. +module_param(spurious_crc_acmd51, bool, 0444);
  32353. +module_param(enable_llm, bool, 0444);
  32354. +module_param(cycle_delay, int, 0444);
  32355. +module_param(extra_messages, bool, 0444);
  32356. +
  32357. +MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  32358. +MODULE_AUTHOR("Broadcom <info@broadcom.com>");
  32359. +MODULE_LICENSE("GPL v2");
  32360. +MODULE_ALIAS("platform:"DRIVER_NAME);
  32361. +
  32362. +MODULE_PARM_DESC(allow_highspeed, "Allow high speed transfers modes");
  32363. +MODULE_PARM_DESC(emmc_clock_freq, "Specify the speed of emmc clock");
  32364. +MODULE_PARM_DESC(sync_after_dma, "Block in driver until dma complete");
  32365. +MODULE_PARM_DESC(missing_status, "Use the missing status quirk");
  32366. +MODULE_PARM_DESC(spurious_crc_acmd51, "Use the spurious crc quirk for reading SCR (ACMD51)");
  32367. +MODULE_PARM_DESC(enable_llm, "Enable low-latency mode");
  32368. +MODULE_PARM_DESC(extra_messages, "Enable more sdcard warning messages");
  32369. +
  32370. +
  32371. diff -Nur linux-3.10.37/drivers/mmc/host/sdhci.c linux-rpi/drivers/mmc/host/sdhci.c
  32372. --- linux-3.10.37/drivers/mmc/host/sdhci.c 2014-04-14 15:42:31.000000000 +0200
  32373. +++ linux-rpi/drivers/mmc/host/sdhci.c 2014-04-24 15:35:02.909551694 +0200
  32374. @@ -28,6 +28,7 @@
  32375. #include <linux/mmc/mmc.h>
  32376. #include <linux/mmc/host.h>
  32377. #include <linux/mmc/card.h>
  32378. +#include <linux/mmc/sd.h>
  32379. #include <linux/mmc/slot-gpio.h>
  32380. #include "sdhci.h"
  32381. @@ -123,6 +124,99 @@
  32382. * Low level functions *
  32383. * *
  32384. \*****************************************************************************/
  32385. +extern bool enable_llm;
  32386. +static int sdhci_locked=0;
  32387. +void sdhci_spin_lock(struct sdhci_host *host)
  32388. +{
  32389. + spin_lock(&host->lock);
  32390. +#ifdef CONFIG_PREEMPT
  32391. + if(enable_llm)
  32392. + {
  32393. + disable_irq_nosync(host->irq);
  32394. + if(host->second_irq)
  32395. + disable_irq_nosync(host->second_irq);
  32396. + local_irq_enable();
  32397. + }
  32398. +#endif
  32399. +}
  32400. +
  32401. +void sdhci_spin_unlock(struct sdhci_host *host)
  32402. +{
  32403. +#ifdef CONFIG_PREEMPT
  32404. + if(enable_llm)
  32405. + {
  32406. + local_irq_disable();
  32407. + if(host->second_irq)
  32408. + enable_irq(host->second_irq);
  32409. + enable_irq(host->irq);
  32410. + }
  32411. +#endif
  32412. + spin_unlock(&host->lock);
  32413. +}
  32414. +
  32415. +void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags)
  32416. +{
  32417. +#ifdef CONFIG_PREEMPT
  32418. + if(enable_llm)
  32419. + {
  32420. + while(sdhci_locked)
  32421. + {
  32422. + preempt_schedule();
  32423. + }
  32424. + spin_lock_irqsave(&host->lock,*flags);
  32425. + disable_irq(host->irq);
  32426. + if(host->second_irq)
  32427. + disable_irq(host->second_irq);
  32428. + local_irq_enable();
  32429. + }
  32430. + else
  32431. +#endif
  32432. + spin_lock_irqsave(&host->lock,*flags);
  32433. +}
  32434. +
  32435. +void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags)
  32436. +{
  32437. +#ifdef CONFIG_PREEMPT
  32438. + if(enable_llm)
  32439. + {
  32440. + local_irq_disable();
  32441. + if(host->second_irq)
  32442. + enable_irq(host->second_irq);
  32443. + enable_irq(host->irq);
  32444. + }
  32445. +#endif
  32446. + spin_unlock_irqrestore(&host->lock,flags);
  32447. +}
  32448. +
  32449. +static void sdhci_spin_enable_schedule(struct sdhci_host *host)
  32450. +{
  32451. +#ifdef CONFIG_PREEMPT
  32452. + if(enable_llm)
  32453. + {
  32454. + sdhci_locked = 1;
  32455. + preempt_enable();
  32456. + }
  32457. +#endif
  32458. +}
  32459. +
  32460. +static void sdhci_spin_disable_schedule(struct sdhci_host *host)
  32461. +{
  32462. +#ifdef CONFIG_PREEMPT
  32463. + if(enable_llm)
  32464. + {
  32465. + preempt_disable();
  32466. + sdhci_locked = 0;
  32467. + }
  32468. +#endif
  32469. +}
  32470. +
  32471. +
  32472. +#undef spin_lock_irqsave
  32473. +#define spin_lock_irqsave(host_lock, flags) sdhci_spin_lock_irqsave(container_of(host_lock, struct sdhci_host, lock), &flags)
  32474. +#define spin_unlock_irqrestore(host_lock, flags) sdhci_spin_unlock_irqrestore(container_of(host_lock, struct sdhci_host, lock), flags)
  32475. +
  32476. +#define spin_lock(host_lock) sdhci_spin_lock(container_of(host_lock, struct sdhci_host, lock))
  32477. +#define spin_unlock(host_lock) sdhci_spin_unlock(container_of(host_lock, struct sdhci_host, lock))
  32478. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  32479. {
  32480. @@ -315,7 +409,7 @@
  32481. u32 uninitialized_var(scratch);
  32482. u8 *buf;
  32483. - DBG("PIO reading\n");
  32484. + DBG("PIO reading %db\n", host->data->blksz);
  32485. blksize = host->data->blksz;
  32486. chunk = 0;
  32487. @@ -360,7 +454,7 @@
  32488. u32 scratch;
  32489. u8 *buf;
  32490. - DBG("PIO writing\n");
  32491. + DBG("PIO writing %db\n", host->data->blksz);
  32492. blksize = host->data->blksz;
  32493. chunk = 0;
  32494. @@ -399,19 +493,28 @@
  32495. local_irq_restore(flags);
  32496. }
  32497. -static void sdhci_transfer_pio(struct sdhci_host *host)
  32498. +static void sdhci_transfer_pio(struct sdhci_host *host, u32 intstate)
  32499. {
  32500. u32 mask;
  32501. + u32 state = 0;
  32502. + u32 intmask;
  32503. + int available;
  32504. BUG_ON(!host->data);
  32505. if (host->blocks == 0)
  32506. return;
  32507. - if (host->data->flags & MMC_DATA_READ)
  32508. + if (host->data->flags & MMC_DATA_READ) {
  32509. mask = SDHCI_DATA_AVAILABLE;
  32510. - else
  32511. + intmask = SDHCI_INT_DATA_AVAIL;
  32512. + } else {
  32513. mask = SDHCI_SPACE_AVAILABLE;
  32514. + intmask = SDHCI_INT_SPACE_AVAIL;
  32515. + }
  32516. +
  32517. + /* initially we can see whether we can procede using intstate */
  32518. + available = (intstate & intmask);
  32519. /*
  32520. * Some controllers (JMicron JMB38x) mess up the buffer bits
  32521. @@ -422,7 +525,7 @@
  32522. (host->data->blocks == 1))
  32523. mask = ~0;
  32524. - while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  32525. + while (available) {
  32526. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  32527. udelay(100);
  32528. @@ -434,9 +537,12 @@
  32529. host->blocks--;
  32530. if (host->blocks == 0)
  32531. break;
  32532. + state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  32533. + available = state & mask;
  32534. + break;
  32535. }
  32536. - DBG("PIO transfer complete.\n");
  32537. + DBG("PIO transfer complete - %d blocks left.\n", host->blocks);
  32538. }
  32539. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  32540. @@ -709,7 +815,9 @@
  32541. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  32542. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  32543. - if (host->flags & SDHCI_REQ_USE_DMA)
  32544. + /* platform DMA will begin on receipt of PIO irqs */
  32545. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  32546. + !(host->flags & SDHCI_USE_PLATDMA))
  32547. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  32548. else
  32549. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  32550. @@ -741,44 +849,25 @@
  32551. host->data_early = 0;
  32552. host->data->bytes_xfered = 0;
  32553. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  32554. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA | SDHCI_USE_PLATDMA))
  32555. host->flags |= SDHCI_REQ_USE_DMA;
  32556. /*
  32557. * FIXME: This doesn't account for merging when mapping the
  32558. * scatterlist.
  32559. */
  32560. - if (host->flags & SDHCI_REQ_USE_DMA) {
  32561. - int broken, i;
  32562. - struct scatterlist *sg;
  32563. -
  32564. - broken = 0;
  32565. - if (host->flags & SDHCI_USE_ADMA) {
  32566. - if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  32567. - broken = 1;
  32568. - } else {
  32569. - if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  32570. - broken = 1;
  32571. - }
  32572. -
  32573. - if (unlikely(broken)) {
  32574. - for_each_sg(data->sg, sg, data->sg_len, i) {
  32575. - if (sg->length & 0x3) {
  32576. - DBG("Reverting to PIO because of "
  32577. - "transfer size (%d)\n",
  32578. - sg->length);
  32579. - host->flags &= ~SDHCI_REQ_USE_DMA;
  32580. - break;
  32581. - }
  32582. - }
  32583. - }
  32584. - }
  32585. /*
  32586. * The assumption here being that alignment is the same after
  32587. * translation to device address space.
  32588. */
  32589. - if (host->flags & SDHCI_REQ_USE_DMA) {
  32590. + if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) ==
  32591. + (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) {
  32592. +
  32593. + if (! sdhci_platdma_dmaable(host, data))
  32594. + host->flags &= ~SDHCI_REQ_USE_DMA;
  32595. +
  32596. + } else if (host->flags & SDHCI_REQ_USE_DMA) {
  32597. int broken, i;
  32598. struct scatterlist *sg;
  32599. @@ -837,7 +926,8 @@
  32600. */
  32601. WARN_ON(1);
  32602. host->flags &= ~SDHCI_REQ_USE_DMA;
  32603. - } else {
  32604. + } else
  32605. + if (!(host->flags & SDHCI_USE_PLATDMA)) {
  32606. WARN_ON(sg_cnt != 1);
  32607. sdhci_writel(host, sg_dma_address(data->sg),
  32608. SDHCI_DMA_ADDRESS);
  32609. @@ -853,11 +943,13 @@
  32610. if (host->version >= SDHCI_SPEC_200) {
  32611. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  32612. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  32613. + if (! (host->flags & SDHCI_USE_PLATDMA)) {
  32614. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  32615. (host->flags & SDHCI_USE_ADMA))
  32616. ctrl |= SDHCI_CTRL_ADMA32;
  32617. else
  32618. ctrl |= SDHCI_CTRL_SDMA;
  32619. + }
  32620. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  32621. }
  32622. @@ -909,7 +1001,8 @@
  32623. if (data->flags & MMC_DATA_READ)
  32624. mode |= SDHCI_TRNS_READ;
  32625. - if (host->flags & SDHCI_REQ_USE_DMA)
  32626. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  32627. + !(host->flags & SDHCI_USE_PLATDMA))
  32628. mode |= SDHCI_TRNS_DMA;
  32629. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  32630. @@ -925,13 +1018,16 @@
  32631. host->data = NULL;
  32632. if (host->flags & SDHCI_REQ_USE_DMA) {
  32633. - if (host->flags & SDHCI_USE_ADMA)
  32634. - sdhci_adma_table_post(host, data);
  32635. - else {
  32636. + /* we may have to abandon an ongoing platform DMA */
  32637. + if (host->flags & SDHCI_USE_PLATDMA)
  32638. + sdhci_platdma_reset(host, data);
  32639. +
  32640. + if (host->flags & (SDHCI_USE_PLATDMA | SDHCI_USE_SDMA)) {
  32641. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  32642. data->sg_len, (data->flags & MMC_DATA_READ) ?
  32643. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  32644. - }
  32645. + } else if (host->flags & SDHCI_USE_ADMA)
  32646. + sdhci_adma_table_post(host, data);
  32647. }
  32648. /*
  32649. @@ -984,6 +1080,12 @@
  32650. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  32651. mask |= SDHCI_DATA_INHIBIT;
  32652. + if(host->ops->missing_status && (cmd->opcode == MMC_SEND_STATUS)) {
  32653. + timeout = 5000; // Really obscenely large delay to send the status, due to bug in controller
  32654. + // which might cause the STATUS command to get stuck when a data operation is in flow
  32655. + mask |= SDHCI_DATA_INHIBIT;
  32656. + }
  32657. +
  32658. /* We shouldn't wait for data inihibit for stop commands, even
  32659. though they might use busy signaling */
  32660. if (host->mrq->data && (cmd == host->mrq->data->stop))
  32661. @@ -999,12 +1101,20 @@
  32662. return;
  32663. }
  32664. timeout--;
  32665. + sdhci_spin_enable_schedule(host);
  32666. mdelay(1);
  32667. + sdhci_spin_disable_schedule(host);
  32668. }
  32669. + DBG("send cmd %d - wait 0x%X irq 0x%x\n", cmd->opcode, mask,
  32670. + sdhci_readl(host, SDHCI_INT_STATUS));
  32671. mod_timer(&host->timer, jiffies + 10 * HZ);
  32672. host->cmd = cmd;
  32673. + if (host->last_cmdop == MMC_APP_CMD)
  32674. + host->last_cmdop = -cmd->opcode;
  32675. + else
  32676. + host->last_cmdop = cmd->opcode;
  32677. sdhci_prepare_data(host, cmd);
  32678. @@ -1220,7 +1330,9 @@
  32679. return;
  32680. }
  32681. timeout--;
  32682. + sdhci_spin_enable_schedule(host);
  32683. mdelay(1);
  32684. + sdhci_spin_disable_schedule(host);
  32685. }
  32686. clk |= SDHCI_CLOCK_CARD_EN;
  32687. @@ -2164,7 +2276,7 @@
  32688. if (host->mrq) {
  32689. pr_err("%s: Timeout waiting for hardware "
  32690. - "interrupt.\n", mmc_hostname(host->mmc));
  32691. + "interrupt - cmd%d.\n", mmc_hostname(host->mmc), host->last_cmdop);
  32692. sdhci_dumpregs(host);
  32693. if (host->data) {
  32694. @@ -2209,10 +2321,13 @@
  32695. BUG_ON(intmask == 0);
  32696. if (!host->cmd) {
  32697. + if (!(host->ops->extra_ints)) {
  32698. pr_err("%s: Got command interrupt 0x%08x even "
  32699. "though no command operation was in progress.\n",
  32700. mmc_hostname(host->mmc), (unsigned)intmask);
  32701. sdhci_dumpregs(host);
  32702. + } else
  32703. + DBG("cmd irq 0x%08x cmd complete\n", (unsigned)intmask);
  32704. return;
  32705. }
  32706. @@ -2282,6 +2397,19 @@
  32707. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  32708. #endif
  32709. +static void sdhci_data_end(struct sdhci_host *host)
  32710. +{
  32711. + if (host->cmd) {
  32712. + /*
  32713. + * Data managed to finish before the
  32714. + * command completed. Make sure we do
  32715. + * things in the proper order.
  32716. + */
  32717. + host->data_early = 1;
  32718. + } else
  32719. + sdhci_finish_data(host);
  32720. +}
  32721. +
  32722. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  32723. {
  32724. u32 command;
  32725. @@ -2311,23 +2439,39 @@
  32726. }
  32727. }
  32728. + if (!(host->ops->extra_ints)) {
  32729. pr_err("%s: Got data interrupt 0x%08x even "
  32730. "though no data operation was in progress.\n",
  32731. mmc_hostname(host->mmc), (unsigned)intmask);
  32732. sdhci_dumpregs(host);
  32733. + } else
  32734. + DBG("data irq 0x%08x but no data\n", (unsigned)intmask);
  32735. return;
  32736. }
  32737. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  32738. host->data->error = -ETIMEDOUT;
  32739. - else if (intmask & SDHCI_INT_DATA_END_BIT)
  32740. + else if (intmask & SDHCI_INT_DATA_END_BIT) {
  32741. + DBG("end error in cmd %d\n", host->last_cmdop);
  32742. + if (host->ops->spurious_crc_acmd51 &&
  32743. + host->last_cmdop == -SD_APP_SEND_SCR) {
  32744. + DBG("ignoring spurious data_end_bit error\n");
  32745. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  32746. + } else
  32747. host->data->error = -EILSEQ;
  32748. - else if ((intmask & SDHCI_INT_DATA_CRC) &&
  32749. + } else if ((intmask & SDHCI_INT_DATA_CRC) &&
  32750. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  32751. - != MMC_BUS_TEST_R)
  32752. + != MMC_BUS_TEST_R) {
  32753. + DBG("crc error in cmd %d\n", host->last_cmdop);
  32754. + if (host->ops->spurious_crc_acmd51 &&
  32755. + host->last_cmdop == -SD_APP_SEND_SCR) {
  32756. + DBG("ignoring spurious data_crc_bit error\n");
  32757. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  32758. + } else {
  32759. host->data->error = -EILSEQ;
  32760. - else if (intmask & SDHCI_INT_ADMA_ERROR) {
  32761. + }
  32762. + } else if (intmask & SDHCI_INT_ADMA_ERROR) {
  32763. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  32764. sdhci_show_adma_error(host);
  32765. host->data->error = -EIO;
  32766. @@ -2335,11 +2479,18 @@
  32767. host->ops->adma_workaround(host, intmask);
  32768. }
  32769. - if (host->data->error)
  32770. + if (host->data->error) {
  32771. + DBG("finish request early on error %d\n", host->data->error);
  32772. sdhci_finish_data(host);
  32773. - else {
  32774. - if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  32775. - sdhci_transfer_pio(host);
  32776. + } else {
  32777. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
  32778. + if (host->flags & SDHCI_REQ_USE_DMA) {
  32779. + /* possible only in PLATDMA mode */
  32780. + sdhci_platdma_avail(host, &intmask,
  32781. + &sdhci_data_end);
  32782. + } else
  32783. + sdhci_transfer_pio(host, intmask);
  32784. + }
  32785. /*
  32786. * We currently don't do anything fancy with DMA
  32787. @@ -2368,18 +2519,8 @@
  32788. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  32789. }
  32790. - if (intmask & SDHCI_INT_DATA_END) {
  32791. - if (host->cmd) {
  32792. - /*
  32793. - * Data managed to finish before the
  32794. - * command completed. Make sure we do
  32795. - * things in the proper order.
  32796. - */
  32797. - host->data_early = 1;
  32798. - } else {
  32799. - sdhci_finish_data(host);
  32800. - }
  32801. - }
  32802. + if (intmask & SDHCI_INT_DATA_END)
  32803. + sdhci_data_end(host);
  32804. }
  32805. }
  32806. @@ -2435,6 +2576,22 @@
  32807. tasklet_schedule(&host->card_tasklet);
  32808. }
  32809. + if (intmask & SDHCI_INT_ERROR_MASK & ~SDHCI_INT_ERROR)
  32810. + DBG("controller reports error 0x%x -"
  32811. + "%s%s%s%s%s%s%s%s%s%s",
  32812. + intmask,
  32813. + intmask & SDHCI_INT_TIMEOUT? " timeout": "",
  32814. + intmask & SDHCI_INT_CRC ? " crc": "",
  32815. + intmask & SDHCI_INT_END_BIT? " endbit": "",
  32816. + intmask & SDHCI_INT_INDEX? " index": "",
  32817. + intmask & SDHCI_INT_DATA_TIMEOUT? " data_timeout": "",
  32818. + intmask & SDHCI_INT_DATA_CRC? " data_crc": "",
  32819. + intmask & SDHCI_INT_DATA_END_BIT? " data_endbit": "",
  32820. + intmask & SDHCI_INT_BUS_POWER? " buspower": "",
  32821. + intmask & SDHCI_INT_ACMD12ERR? " acmd12": "",
  32822. + intmask & SDHCI_INT_ADMA_ERROR? " adma": ""
  32823. + );
  32824. +
  32825. if (intmask & SDHCI_INT_CMD_MASK) {
  32826. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  32827. SDHCI_INT_STATUS);
  32828. @@ -2449,7 +2606,13 @@
  32829. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  32830. - intmask &= ~SDHCI_INT_ERROR;
  32831. + if (intmask & SDHCI_INT_ERROR_MASK) {
  32832. + /* collect any uncovered errors */
  32833. + sdhci_writel(host, intmask & SDHCI_INT_ERROR_MASK,
  32834. + SDHCI_INT_STATUS);
  32835. + }
  32836. +
  32837. + intmask &= ~SDHCI_INT_ERROR_MASK;
  32838. if (intmask & SDHCI_INT_BUS_POWER) {
  32839. pr_err("%s: Card is consuming too much power!\n",
  32840. @@ -2569,7 +2732,8 @@
  32841. {
  32842. int ret;
  32843. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  32844. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32845. + SDHCI_USE_PLATDMA)) {
  32846. if (host->ops->enable_dma)
  32847. host->ops->enable_dma(host);
  32848. }
  32849. @@ -2785,14 +2949,16 @@
  32850. host->flags &= ~SDHCI_USE_ADMA;
  32851. }
  32852. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  32853. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32854. + SDHCI_USE_PLATDMA)) {
  32855. if (host->ops->enable_dma) {
  32856. if (host->ops->enable_dma(host)) {
  32857. pr_warning("%s: No suitable DMA "
  32858. "available. Falling back to PIO.\n",
  32859. mmc_hostname(mmc));
  32860. host->flags &=
  32861. - ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  32862. + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32863. + SDHCI_USE_PLATDMA);
  32864. }
  32865. }
  32866. }
  32867. @@ -3174,7 +3340,7 @@
  32868. host->tuning_timer.function = sdhci_tuning_timer;
  32869. }
  32870. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  32871. + ret = request_irq(host->irq, sdhci_irq, 0,//IRQF_SHARED,
  32872. mmc_hostname(mmc), host);
  32873. if (ret) {
  32874. pr_err("%s: Failed to request IRQ %d: %d\n",
  32875. @@ -3210,6 +3376,7 @@
  32876. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  32877. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  32878. + (host->flags & SDHCI_USE_PLATDMA) ? "platform's DMA" :
  32879. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  32880. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  32881. diff -Nur linux-3.10.37/drivers/mmc/host/sdhci.h linux-rpi/drivers/mmc/host/sdhci.h
  32882. --- linux-3.10.37/drivers/mmc/host/sdhci.h 2014-04-14 15:42:31.000000000 +0200
  32883. +++ linux-rpi/drivers/mmc/host/sdhci.h 2014-04-24 15:35:02.909551694 +0200
  32884. @@ -289,6 +289,18 @@
  32885. void (*platform_reset_enter)(struct sdhci_host *host, u8 mask);
  32886. void (*platform_reset_exit)(struct sdhci_host *host, u8 mask);
  32887. int (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
  32888. +
  32889. + int (*pdma_able)(struct sdhci_host *host,
  32890. + struct mmc_data *data);
  32891. + void (*pdma_avail)(struct sdhci_host *host,
  32892. + unsigned int *ref_intmask,
  32893. + void(*complete)(struct sdhci_host *));
  32894. + void (*pdma_reset)(struct sdhci_host *host,
  32895. + struct mmc_data *data);
  32896. + unsigned int (*extra_ints)(struct sdhci_host *host);
  32897. + unsigned int (*spurious_crc_acmd51)(struct sdhci_host *host);
  32898. + unsigned int (*missing_status)(struct sdhci_host *host);
  32899. +
  32900. void (*hw_reset)(struct sdhci_host *host);
  32901. void (*platform_suspend)(struct sdhci_host *host);
  32902. void (*platform_resume)(struct sdhci_host *host);
  32903. @@ -399,9 +411,38 @@
  32904. extern void sdhci_enable_irq_wakeups(struct sdhci_host *host);
  32905. #endif
  32906. +static inline int /*bool*/
  32907. +sdhci_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  32908. +{
  32909. + if (host->ops->pdma_able)
  32910. + return host->ops->pdma_able(host, data);
  32911. + else
  32912. + return 1;
  32913. +}
  32914. +static inline void
  32915. +sdhci_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  32916. + void(*completion_callback)(struct sdhci_host *))
  32917. +{
  32918. + if (host->ops->pdma_avail)
  32919. + host->ops->pdma_avail(host, ref_intmask, completion_callback);
  32920. +}
  32921. +
  32922. +static inline void
  32923. +sdhci_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  32924. +{
  32925. + if (host->ops->pdma_reset)
  32926. + host->ops->pdma_reset(host, data);
  32927. +}
  32928. +
  32929. #ifdef CONFIG_PM_RUNTIME
  32930. extern int sdhci_runtime_suspend_host(struct sdhci_host *host);
  32931. extern int sdhci_runtime_resume_host(struct sdhci_host *host);
  32932. #endif
  32933. +extern void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags);
  32934. +extern void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags);
  32935. +extern void sdhci_spin_lock(struct sdhci_host *host);
  32936. +extern void sdhci_spin_unlock(struct sdhci_host *host);
  32937. +
  32938. +
  32939. #endif /* __SDHCI_HW_H */
  32940. diff -Nur linux-3.10.37/drivers/net/usb/smsc95xx.c linux-rpi/drivers/net/usb/smsc95xx.c
  32941. --- linux-3.10.37/drivers/net/usb/smsc95xx.c 2014-04-14 15:42:31.000000000 +0200
  32942. +++ linux-rpi/drivers/net/usb/smsc95xx.c 2014-04-24 15:35:03.209555036 +0200
  32943. @@ -61,6 +61,7 @@
  32944. #define SUSPEND_SUSPEND3 (0x08)
  32945. #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
  32946. SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
  32947. +#define MAC_ADDR_LEN (6)
  32948. struct smsc95xx_priv {
  32949. u32 mac_cr;
  32950. @@ -76,6 +77,10 @@
  32951. module_param(turbo_mode, bool, 0644);
  32952. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  32953. +static char *macaddr = ":";
  32954. +module_param(macaddr, charp, 0);
  32955. +MODULE_PARM_DESC(macaddr, "MAC address");
  32956. +
  32957. static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
  32958. u32 *data, int in_pm)
  32959. {
  32960. @@ -765,8 +770,59 @@
  32961. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  32962. }
  32963. +/* Check the macaddr module parameter for a MAC address */
  32964. +static int smsc95xx_is_macaddr_param(struct usbnet *dev, u8 *dev_mac)
  32965. +{
  32966. + int i, j, got_num, num;
  32967. + u8 mtbl[MAC_ADDR_LEN];
  32968. +
  32969. + if (macaddr[0] == ':')
  32970. + return 0;
  32971. +
  32972. + i = 0;
  32973. + j = 0;
  32974. + num = 0;
  32975. + got_num = 0;
  32976. + while (j < MAC_ADDR_LEN) {
  32977. + if (macaddr[i] && macaddr[i] != ':') {
  32978. + got_num++;
  32979. + if ('0' <= macaddr[i] && macaddr[i] <= '9')
  32980. + num = num * 16 + macaddr[i] - '0';
  32981. + else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
  32982. + num = num * 16 + 10 + macaddr[i] - 'A';
  32983. + else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
  32984. + num = num * 16 + 10 + macaddr[i] - 'a';
  32985. + else
  32986. + break;
  32987. + i++;
  32988. + } else if (got_num == 2) {
  32989. + mtbl[j++] = (u8) num;
  32990. + num = 0;
  32991. + got_num = 0;
  32992. + i++;
  32993. + } else {
  32994. + break;
  32995. + }
  32996. + }
  32997. +
  32998. + if (j == MAC_ADDR_LEN) {
  32999. + netif_dbg(dev, ifup, dev->net, "Overriding MAC address with: "
  33000. + "%02x:%02x:%02x:%02x:%02x:%02x\n", mtbl[0], mtbl[1], mtbl[2],
  33001. + mtbl[3], mtbl[4], mtbl[5]);
  33002. + for (i = 0; i < MAC_ADDR_LEN; i++)
  33003. + dev_mac[i] = mtbl[i];
  33004. + return 1;
  33005. + } else {
  33006. + return 0;
  33007. + }
  33008. +}
  33009. +
  33010. static void smsc95xx_init_mac_address(struct usbnet *dev)
  33011. {
  33012. + /* Check module parameters */
  33013. + if (smsc95xx_is_macaddr_param(dev, dev->net->dev_addr))
  33014. + return;
  33015. +
  33016. /* try reading mac address from EEPROM */
  33017. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  33018. dev->net->dev_addr) == 0) {
  33019. diff -Nur linux-3.10.37/drivers/net/wireless/ath/ath9k/hif_usb.c linux-rpi/drivers/net/wireless/ath/ath9k/hif_usb.c
  33020. --- linux-3.10.37/drivers/net/wireless/ath/ath9k/hif_usb.c 2014-04-14 15:42:31.000000000 +0200
  33021. +++ linux-rpi/drivers/net/wireless/ath/ath9k/hif_usb.c 2014-04-24 15:35:03.253555526 +0200
  33022. @@ -37,9 +37,11 @@
  33023. { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
  33024. { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
  33025. { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
  33026. + { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
  33027. { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
  33028. { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
  33029. { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
  33030. + { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
  33031. { USB_DEVICE(0x0cf3, 0x7015),
  33032. .driver_info = AR9287_USB }, /* Atheros */
  33033. diff -Nur linux-3.10.37/drivers/net/wireless/rt2x00/rt2800.h linux-rpi/drivers/net/wireless/rt2x00/rt2800.h
  33034. --- linux-3.10.37/drivers/net/wireless/rt2x00/rt2800.h 2014-04-14 15:42:31.000000000 +0200
  33035. +++ linux-rpi/drivers/net/wireless/rt2x00/rt2800.h 2014-04-24 15:35:03.369556819 +0200
  33036. @@ -70,6 +70,7 @@
  33037. #define RF3322 0x000c
  33038. #define RF3053 0x000d
  33039. #define RF5592 0x000f
  33040. +#define RF3070 0x3070
  33041. #define RF3290 0x3290
  33042. #define RF5360 0x5360
  33043. #define RF5370 0x5370
  33044. diff -Nur linux-3.10.37/drivers/net/wireless/rt2x00/rt2800lib.c linux-rpi/drivers/net/wireless/rt2x00/rt2800lib.c
  33045. --- linux-3.10.37/drivers/net/wireless/rt2x00/rt2800lib.c 2014-04-14 15:42:31.000000000 +0200
  33046. +++ linux-rpi/drivers/net/wireless/rt2x00/rt2800lib.c 2014-04-24 15:35:03.373556863 +0200
  33047. @@ -2599,6 +2599,7 @@
  33048. break;
  33049. case RF5360:
  33050. case RF5370:
  33051. + case RF3070:
  33052. case RF5372:
  33053. case RF5390:
  33054. case RF5392:
  33055. @@ -2615,6 +2616,7 @@
  33056. rt2x00_rf(rt2x00dev, RF3322) ||
  33057. rt2x00_rf(rt2x00dev, RF5360) ||
  33058. rt2x00_rf(rt2x00dev, RF5370) ||
  33059. + rt2x00_rf(rt2x00dev, RF3070) ||
  33060. rt2x00_rf(rt2x00dev, RF5372) ||
  33061. rt2x00_rf(rt2x00dev, RF5390) ||
  33062. rt2x00_rf(rt2x00dev, RF5392)) {
  33063. @@ -3206,6 +3208,7 @@
  33064. case RF3290:
  33065. case RF5360:
  33066. case RF5370:
  33067. + case RF3070:
  33068. case RF5372:
  33069. case RF5390:
  33070. case RF5392:
  33071. @@ -5524,6 +5527,7 @@
  33072. case RF3322:
  33073. case RF5360:
  33074. case RF5370:
  33075. + case RF3070:
  33076. case RF5372:
  33077. case RF5390:
  33078. case RF5392:
  33079. @@ -5979,6 +5983,7 @@
  33080. rt2x00_rf(rt2x00dev, RF3322) ||
  33081. rt2x00_rf(rt2x00dev, RF5360) ||
  33082. rt2x00_rf(rt2x00dev, RF5370) ||
  33083. + rt2x00_rf(rt2x00dev, RF3070) ||
  33084. rt2x00_rf(rt2x00dev, RF5372) ||
  33085. rt2x00_rf(rt2x00dev, RF5390) ||
  33086. rt2x00_rf(rt2x00dev, RF5392)) {
  33087. @@ -6081,6 +6086,7 @@
  33088. case RF3290:
  33089. case RF5360:
  33090. case RF5370:
  33091. + case RF3070:
  33092. case RF5372:
  33093. case RF5390:
  33094. case RF5392:
  33095. diff -Nur linux-3.10.37/drivers/spi/Kconfig linux-rpi/drivers/spi/Kconfig
  33096. --- linux-3.10.37/drivers/spi/Kconfig 2014-04-14 15:42:31.000000000 +0200
  33097. +++ linux-rpi/drivers/spi/Kconfig 2014-04-24 15:35:03.781561409 +0200
  33098. @@ -86,6 +86,14 @@
  33099. is for the regular SPI controller. Slave mode operation is not also
  33100. not supported.
  33101. +config SPI_BCM2708
  33102. + tristate "BCM2708 SPI controller driver (SPI0)"
  33103. + depends on MACH_BCM2708
  33104. + help
  33105. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  33106. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  33107. + device.
  33108. +
  33109. config SPI_BFIN5XX
  33110. tristate "SPI controller driver for ADI Blackfin5xx"
  33111. depends on BLACKFIN
  33112. diff -Nur linux-3.10.37/drivers/spi/Makefile linux-rpi/drivers/spi/Makefile
  33113. --- linux-3.10.37/drivers/spi/Makefile 2014-04-14 15:42:31.000000000 +0200
  33114. +++ linux-rpi/drivers/spi/Makefile 2014-04-24 15:35:03.781561409 +0200
  33115. @@ -17,6 +17,7 @@
  33116. obj-$(CONFIG_SPI_BCM2835) += spi-bcm2835.o
  33117. obj-$(CONFIG_SPI_BCM63XX) += spi-bcm63xx.o
  33118. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  33119. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  33120. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  33121. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  33122. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  33123. diff -Nur linux-3.10.37/drivers/spi/spi-bcm2708.c linux-rpi/drivers/spi/spi-bcm2708.c
  33124. --- linux-3.10.37/drivers/spi/spi-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  33125. +++ linux-rpi/drivers/spi/spi-bcm2708.c 2014-04-24 15:35:03.781561409 +0200
  33126. @@ -0,0 +1,626 @@
  33127. +/*
  33128. + * Driver for Broadcom BCM2708 SPI Controllers
  33129. + *
  33130. + * Copyright (C) 2012 Chris Boot
  33131. + *
  33132. + * This driver is inspired by:
  33133. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  33134. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  33135. + *
  33136. + * This program is free software; you can redistribute it and/or modify
  33137. + * it under the terms of the GNU General Public License as published by
  33138. + * the Free Software Foundation; either version 2 of the License, or
  33139. + * (at your option) any later version.
  33140. + *
  33141. + * This program is distributed in the hope that it will be useful,
  33142. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33143. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33144. + * GNU General Public License for more details.
  33145. + *
  33146. + * You should have received a copy of the GNU General Public License
  33147. + * along with this program; if not, write to the Free Software
  33148. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  33149. + */
  33150. +
  33151. +#include <linux/kernel.h>
  33152. +#include <linux/module.h>
  33153. +#include <linux/spinlock.h>
  33154. +#include <linux/clk.h>
  33155. +#include <linux/err.h>
  33156. +#include <linux/platform_device.h>
  33157. +#include <linux/io.h>
  33158. +#include <linux/spi/spi.h>
  33159. +#include <linux/interrupt.h>
  33160. +#include <linux/delay.h>
  33161. +#include <linux/log2.h>
  33162. +#include <linux/sched.h>
  33163. +#include <linux/wait.h>
  33164. +
  33165. +/* SPI register offsets */
  33166. +#define SPI_CS 0x00
  33167. +#define SPI_FIFO 0x04
  33168. +#define SPI_CLK 0x08
  33169. +#define SPI_DLEN 0x0c
  33170. +#define SPI_LTOH 0x10
  33171. +#define SPI_DC 0x14
  33172. +
  33173. +/* Bitfields in CS */
  33174. +#define SPI_CS_LEN_LONG 0x02000000
  33175. +#define SPI_CS_DMA_LEN 0x01000000
  33176. +#define SPI_CS_CSPOL2 0x00800000
  33177. +#define SPI_CS_CSPOL1 0x00400000
  33178. +#define SPI_CS_CSPOL0 0x00200000
  33179. +#define SPI_CS_RXF 0x00100000
  33180. +#define SPI_CS_RXR 0x00080000
  33181. +#define SPI_CS_TXD 0x00040000
  33182. +#define SPI_CS_RXD 0x00020000
  33183. +#define SPI_CS_DONE 0x00010000
  33184. +#define SPI_CS_LEN 0x00002000
  33185. +#define SPI_CS_REN 0x00001000
  33186. +#define SPI_CS_ADCS 0x00000800
  33187. +#define SPI_CS_INTR 0x00000400
  33188. +#define SPI_CS_INTD 0x00000200
  33189. +#define SPI_CS_DMAEN 0x00000100
  33190. +#define SPI_CS_TA 0x00000080
  33191. +#define SPI_CS_CSPOL 0x00000040
  33192. +#define SPI_CS_CLEAR_RX 0x00000020
  33193. +#define SPI_CS_CLEAR_TX 0x00000010
  33194. +#define SPI_CS_CPOL 0x00000008
  33195. +#define SPI_CS_CPHA 0x00000004
  33196. +#define SPI_CS_CS_10 0x00000002
  33197. +#define SPI_CS_CS_01 0x00000001
  33198. +
  33199. +#define SPI_TIMEOUT_MS 150
  33200. +
  33201. +#define DRV_NAME "bcm2708_spi"
  33202. +
  33203. +struct bcm2708_spi {
  33204. + spinlock_t lock;
  33205. + void __iomem *base;
  33206. + int irq;
  33207. + struct clk *clk;
  33208. + bool stopping;
  33209. +
  33210. + struct list_head queue;
  33211. + struct workqueue_struct *workq;
  33212. + struct work_struct work;
  33213. + struct completion done;
  33214. +
  33215. + const u8 *tx_buf;
  33216. + u8 *rx_buf;
  33217. + int len;
  33218. +};
  33219. +
  33220. +struct bcm2708_spi_state {
  33221. + u32 cs;
  33222. + u16 cdiv;
  33223. +};
  33224. +
  33225. +/*
  33226. + * This function sets the ALT mode on the SPI pins so that we can use them with
  33227. + * the SPI hardware.
  33228. + *
  33229. + * FIXME: This is a hack. Use pinmux / pinctrl.
  33230. + */
  33231. +static void bcm2708_init_pinmode(void)
  33232. +{
  33233. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  33234. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  33235. +
  33236. + int pin;
  33237. + u32 *gpio = ioremap(0x20200000, SZ_16K);
  33238. +
  33239. + /* SPI is on GPIO 7..11 */
  33240. + for (pin = 7; pin <= 11; pin++) {
  33241. + INP_GPIO(pin); /* set mode to GPIO input first */
  33242. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  33243. + }
  33244. +
  33245. + iounmap(gpio);
  33246. +
  33247. +#undef INP_GPIO
  33248. +#undef SET_GPIO_ALT
  33249. +}
  33250. +
  33251. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  33252. +{
  33253. + return readl(bs->base + reg);
  33254. +}
  33255. +
  33256. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  33257. +{
  33258. + writel(val, bs->base + reg);
  33259. +}
  33260. +
  33261. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  33262. +{
  33263. + u8 byte;
  33264. +
  33265. + while (len--) {
  33266. + byte = bcm2708_rd(bs, SPI_FIFO);
  33267. + if (bs->rx_buf)
  33268. + *bs->rx_buf++ = byte;
  33269. + }
  33270. +}
  33271. +
  33272. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  33273. +{
  33274. + u8 byte;
  33275. + u16 val;
  33276. +
  33277. + if (len > bs->len)
  33278. + len = bs->len;
  33279. +
  33280. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  33281. + /* LoSSI mode */
  33282. + if (unlikely(len % 2)) {
  33283. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  33284. + bs->len = 0;
  33285. + return;
  33286. + }
  33287. + while (len) {
  33288. + if (bs->tx_buf) {
  33289. + val = *(const u16 *)bs->tx_buf;
  33290. + bs->tx_buf += 2;
  33291. + } else
  33292. + val = 0;
  33293. + bcm2708_wr(bs, SPI_FIFO, val);
  33294. + bs->len -= 2;
  33295. + len -= 2;
  33296. + }
  33297. + return;
  33298. + }
  33299. +
  33300. + while (len--) {
  33301. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  33302. + bcm2708_wr(bs, SPI_FIFO, byte);
  33303. + bs->len--;
  33304. + }
  33305. +}
  33306. +
  33307. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  33308. +{
  33309. + struct spi_master *master = dev_id;
  33310. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33311. + u32 cs;
  33312. +
  33313. + spin_lock(&bs->lock);
  33314. +
  33315. + cs = bcm2708_rd(bs, SPI_CS);
  33316. +
  33317. + if (cs & SPI_CS_DONE) {
  33318. + if (bs->len) { /* first interrupt in a transfer */
  33319. + /* fill the TX fifo with up to 16 bytes */
  33320. + bcm2708_wr_fifo(bs, 16);
  33321. + } else { /* transfer complete */
  33322. + /* disable interrupts */
  33323. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  33324. + bcm2708_wr(bs, SPI_CS, cs);
  33325. +
  33326. + /* drain RX FIFO */
  33327. + while (cs & SPI_CS_RXD) {
  33328. + bcm2708_rd_fifo(bs, 1);
  33329. + cs = bcm2708_rd(bs, SPI_CS);
  33330. + }
  33331. +
  33332. + /* wake up our bh */
  33333. + complete(&bs->done);
  33334. + }
  33335. + } else if (cs & SPI_CS_RXR) {
  33336. + /* read 12 bytes of data */
  33337. + bcm2708_rd_fifo(bs, 12);
  33338. +
  33339. + /* write up to 12 bytes */
  33340. + bcm2708_wr_fifo(bs, 12);
  33341. + }
  33342. +
  33343. + spin_unlock(&bs->lock);
  33344. +
  33345. + return IRQ_HANDLED;
  33346. +}
  33347. +
  33348. +static int bcm2708_setup_state(struct spi_master *master,
  33349. + struct device *dev, struct bcm2708_spi_state *state,
  33350. + u32 hz, u8 csel, u8 mode, u8 bpw)
  33351. +{
  33352. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33353. + int cdiv;
  33354. + unsigned long bus_hz;
  33355. + u32 cs = 0;
  33356. +
  33357. + bus_hz = clk_get_rate(bs->clk);
  33358. +
  33359. + if (hz >= bus_hz) {
  33360. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  33361. + } else if (hz) {
  33362. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  33363. +
  33364. + /* CDIV must be a power of 2, so round up */
  33365. + cdiv = roundup_pow_of_two(cdiv);
  33366. +
  33367. + if (cdiv > 65536) {
  33368. + dev_dbg(dev,
  33369. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  33370. + hz, cdiv, bus_hz / 65536);
  33371. + return -EINVAL;
  33372. + } else if (cdiv == 65536) {
  33373. + cdiv = 0;
  33374. + } else if (cdiv == 1) {
  33375. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  33376. + }
  33377. + } else {
  33378. + cdiv = 0;
  33379. + }
  33380. +
  33381. + switch (bpw) {
  33382. + case 8:
  33383. + break;
  33384. + case 9:
  33385. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  33386. + cs |= SPI_CS_LEN;
  33387. + break;
  33388. + default:
  33389. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  33390. + bpw);
  33391. + return -EINVAL;
  33392. + }
  33393. +
  33394. + if (mode & SPI_CPOL)
  33395. + cs |= SPI_CS_CPOL;
  33396. + if (mode & SPI_CPHA)
  33397. + cs |= SPI_CS_CPHA;
  33398. +
  33399. + if (!(mode & SPI_NO_CS)) {
  33400. + if (mode & SPI_CS_HIGH) {
  33401. + cs |= SPI_CS_CSPOL;
  33402. + cs |= SPI_CS_CSPOL0 << csel;
  33403. + }
  33404. +
  33405. + cs |= csel;
  33406. + } else {
  33407. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  33408. + }
  33409. +
  33410. + if (state) {
  33411. + state->cs = cs;
  33412. + state->cdiv = cdiv;
  33413. + dev_dbg(dev, "setup: want %d Hz; "
  33414. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  33415. + "mode %u: cs 0x%08X\n",
  33416. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  33417. + }
  33418. +
  33419. + return 0;
  33420. +}
  33421. +
  33422. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  33423. + struct spi_message *msg, struct spi_transfer *xfer)
  33424. +{
  33425. + struct spi_device *spi = msg->spi;
  33426. + struct bcm2708_spi_state state, *stp;
  33427. + int ret;
  33428. + u32 cs;
  33429. +
  33430. + if (bs->stopping)
  33431. + return -ESHUTDOWN;
  33432. +
  33433. + if (xfer->bits_per_word || xfer->speed_hz) {
  33434. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  33435. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  33436. + spi->chip_select, spi->mode,
  33437. + xfer->bits_per_word ? xfer->bits_per_word :
  33438. + spi->bits_per_word);
  33439. + if (ret)
  33440. + return ret;
  33441. +
  33442. + stp = &state;
  33443. + } else {
  33444. + stp = spi->controller_state;
  33445. + }
  33446. +
  33447. + INIT_COMPLETION(bs->done);
  33448. + bs->tx_buf = xfer->tx_buf;
  33449. + bs->rx_buf = xfer->rx_buf;
  33450. + bs->len = xfer->len;
  33451. +
  33452. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  33453. +
  33454. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  33455. + bcm2708_wr(bs, SPI_CS, cs);
  33456. +
  33457. + ret = wait_for_completion_timeout(&bs->done,
  33458. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  33459. + if (ret == 0) {
  33460. + dev_err(&spi->dev, "transfer timed out\n");
  33461. + return -ETIMEDOUT;
  33462. + }
  33463. +
  33464. + if (xfer->delay_usecs)
  33465. + udelay(xfer->delay_usecs);
  33466. +
  33467. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  33468. + xfer->cs_change) {
  33469. + /* clear TA and interrupt flags */
  33470. + bcm2708_wr(bs, SPI_CS, stp->cs);
  33471. + }
  33472. +
  33473. + msg->actual_length += (xfer->len - bs->len);
  33474. +
  33475. + return 0;
  33476. +}
  33477. +
  33478. +static void bcm2708_work(struct work_struct *work)
  33479. +{
  33480. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  33481. + unsigned long flags;
  33482. + struct spi_message *msg;
  33483. + struct spi_transfer *xfer;
  33484. + int status = 0;
  33485. +
  33486. + spin_lock_irqsave(&bs->lock, flags);
  33487. + while (!list_empty(&bs->queue)) {
  33488. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  33489. + list_del_init(&msg->queue);
  33490. + spin_unlock_irqrestore(&bs->lock, flags);
  33491. +
  33492. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  33493. + status = bcm2708_process_transfer(bs, msg, xfer);
  33494. + if (status)
  33495. + break;
  33496. + }
  33497. +
  33498. + msg->status = status;
  33499. + msg->complete(msg->context);
  33500. +
  33501. + spin_lock_irqsave(&bs->lock, flags);
  33502. + }
  33503. + spin_unlock_irqrestore(&bs->lock, flags);
  33504. +}
  33505. +
  33506. +static int bcm2708_spi_setup(struct spi_device *spi)
  33507. +{
  33508. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  33509. + struct bcm2708_spi_state *state;
  33510. + int ret;
  33511. +
  33512. + if (bs->stopping)
  33513. + return -ESHUTDOWN;
  33514. +
  33515. + if (!(spi->mode & SPI_NO_CS) &&
  33516. + (spi->chip_select > spi->master->num_chipselect)) {
  33517. + dev_dbg(&spi->dev,
  33518. + "setup: invalid chipselect %u (%u defined)\n",
  33519. + spi->chip_select, spi->master->num_chipselect);
  33520. + return -EINVAL;
  33521. + }
  33522. +
  33523. + state = spi->controller_state;
  33524. + if (!state) {
  33525. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  33526. + if (!state)
  33527. + return -ENOMEM;
  33528. +
  33529. + spi->controller_state = state;
  33530. + }
  33531. +
  33532. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  33533. + spi->max_speed_hz, spi->chip_select, spi->mode,
  33534. + spi->bits_per_word);
  33535. + if (ret < 0) {
  33536. + kfree(state);
  33537. + spi->controller_state = NULL;
  33538. + return ret;
  33539. + }
  33540. +
  33541. + dev_dbg(&spi->dev,
  33542. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  33543. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  33544. + spi->mode, state->cs, state->cdiv);
  33545. +
  33546. + return 0;
  33547. +}
  33548. +
  33549. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  33550. +{
  33551. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  33552. + struct spi_transfer *xfer;
  33553. + int ret;
  33554. + unsigned long flags;
  33555. +
  33556. + if (unlikely(list_empty(&msg->transfers)))
  33557. + return -EINVAL;
  33558. +
  33559. + if (bs->stopping)
  33560. + return -ESHUTDOWN;
  33561. +
  33562. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  33563. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  33564. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  33565. + return -EINVAL;
  33566. + }
  33567. +
  33568. + if (!xfer->bits_per_word || xfer->speed_hz)
  33569. + continue;
  33570. +
  33571. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  33572. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  33573. + spi->chip_select, spi->mode,
  33574. + xfer->bits_per_word ? xfer->bits_per_word :
  33575. + spi->bits_per_word);
  33576. + if (ret)
  33577. + return ret;
  33578. + }
  33579. +
  33580. + msg->status = -EINPROGRESS;
  33581. + msg->actual_length = 0;
  33582. +
  33583. + spin_lock_irqsave(&bs->lock, flags);
  33584. + list_add_tail(&msg->queue, &bs->queue);
  33585. + queue_work(bs->workq, &bs->work);
  33586. + spin_unlock_irqrestore(&bs->lock, flags);
  33587. +
  33588. + return 0;
  33589. +}
  33590. +
  33591. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  33592. +{
  33593. + if (spi->controller_state) {
  33594. + kfree(spi->controller_state);
  33595. + spi->controller_state = NULL;
  33596. + }
  33597. +}
  33598. +
  33599. +static int bcm2708_spi_probe(struct platform_device *pdev)
  33600. +{
  33601. + struct resource *regs;
  33602. + int irq, err = -ENOMEM;
  33603. + struct clk *clk;
  33604. + struct spi_master *master;
  33605. + struct bcm2708_spi *bs;
  33606. +
  33607. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  33608. + if (!regs) {
  33609. + dev_err(&pdev->dev, "could not get IO memory\n");
  33610. + return -ENXIO;
  33611. + }
  33612. +
  33613. + irq = platform_get_irq(pdev, 0);
  33614. + if (irq < 0) {
  33615. + dev_err(&pdev->dev, "could not get IRQ\n");
  33616. + return irq;
  33617. + }
  33618. +
  33619. + clk = clk_get(&pdev->dev, NULL);
  33620. + if (IS_ERR(clk)) {
  33621. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  33622. + return PTR_ERR(clk);
  33623. + }
  33624. +
  33625. + bcm2708_init_pinmode();
  33626. +
  33627. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  33628. + if (!master) {
  33629. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  33630. + goto out_clk_put;
  33631. + }
  33632. +
  33633. + /* the spi->mode bits understood by this driver: */
  33634. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  33635. +
  33636. + master->bus_num = pdev->id;
  33637. + master->num_chipselect = 3;
  33638. + master->setup = bcm2708_spi_setup;
  33639. + master->transfer = bcm2708_spi_transfer;
  33640. + master->cleanup = bcm2708_spi_cleanup;
  33641. + platform_set_drvdata(pdev, master);
  33642. +
  33643. + bs = spi_master_get_devdata(master);
  33644. +
  33645. + spin_lock_init(&bs->lock);
  33646. + INIT_LIST_HEAD(&bs->queue);
  33647. + init_completion(&bs->done);
  33648. + INIT_WORK(&bs->work, bcm2708_work);
  33649. +
  33650. + bs->base = ioremap(regs->start, resource_size(regs));
  33651. + if (!bs->base) {
  33652. + dev_err(&pdev->dev, "could not remap memory\n");
  33653. + goto out_master_put;
  33654. + }
  33655. +
  33656. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  33657. + if (!bs->workq) {
  33658. + dev_err(&pdev->dev, "could not create workqueue\n");
  33659. + goto out_iounmap;
  33660. + }
  33661. +
  33662. + bs->irq = irq;
  33663. + bs->clk = clk;
  33664. + bs->stopping = false;
  33665. +
  33666. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  33667. + master);
  33668. + if (err) {
  33669. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  33670. + goto out_workqueue;
  33671. + }
  33672. +
  33673. + /* initialise the hardware */
  33674. + clk_enable(clk);
  33675. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  33676. +
  33677. + err = spi_register_master(master);
  33678. + if (err) {
  33679. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  33680. + goto out_free_irq;
  33681. + }
  33682. +
  33683. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  33684. + (unsigned long)regs->start, irq);
  33685. +
  33686. + return 0;
  33687. +
  33688. +out_free_irq:
  33689. + free_irq(bs->irq, master);
  33690. +out_workqueue:
  33691. + destroy_workqueue(bs->workq);
  33692. +out_iounmap:
  33693. + iounmap(bs->base);
  33694. +out_master_put:
  33695. + spi_master_put(master);
  33696. +out_clk_put:
  33697. + clk_put(clk);
  33698. + return err;
  33699. +}
  33700. +
  33701. +static int bcm2708_spi_remove(struct platform_device *pdev)
  33702. +{
  33703. + struct spi_master *master = platform_get_drvdata(pdev);
  33704. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33705. +
  33706. + /* reset the hardware and block queue progress */
  33707. + spin_lock_irq(&bs->lock);
  33708. + bs->stopping = true;
  33709. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  33710. + spin_unlock_irq(&bs->lock);
  33711. +
  33712. + flush_work_sync(&bs->work);
  33713. +
  33714. + clk_disable(bs->clk);
  33715. + clk_put(bs->clk);
  33716. + free_irq(bs->irq, master);
  33717. + iounmap(bs->base);
  33718. +
  33719. + spi_unregister_master(master);
  33720. +
  33721. + return 0;
  33722. +}
  33723. +
  33724. +static struct platform_driver bcm2708_spi_driver = {
  33725. + .driver = {
  33726. + .name = DRV_NAME,
  33727. + .owner = THIS_MODULE,
  33728. + },
  33729. + .probe = bcm2708_spi_probe,
  33730. + .remove = bcm2708_spi_remove,
  33731. +};
  33732. +
  33733. +
  33734. +static int __init bcm2708_spi_init(void)
  33735. +{
  33736. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  33737. +}
  33738. +module_init(bcm2708_spi_init);
  33739. +
  33740. +static void __exit bcm2708_spi_exit(void)
  33741. +{
  33742. + platform_driver_unregister(&bcm2708_spi_driver);
  33743. +}
  33744. +module_exit(bcm2708_spi_exit);
  33745. +
  33746. +
  33747. +//module_platform_driver(bcm2708_spi_driver);
  33748. +
  33749. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  33750. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  33751. +MODULE_LICENSE("GPL v2");
  33752. +MODULE_ALIAS("platform:" DRV_NAME);
  33753. diff -Nur linux-3.10.37/drivers/staging/media/lirc/Kconfig linux-rpi/drivers/staging/media/lirc/Kconfig
  33754. --- linux-3.10.37/drivers/staging/media/lirc/Kconfig 2014-04-14 15:42:31.000000000 +0200
  33755. +++ linux-rpi/drivers/staging/media/lirc/Kconfig 2014-04-24 15:35:03.925563013 +0200
  33756. @@ -38,6 +38,12 @@
  33757. help
  33758. Driver for Homebrew Parallel Port Receivers
  33759. +config LIRC_RPI
  33760. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  33761. + depends on LIRC
  33762. + help
  33763. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  33764. +
  33765. config LIRC_SASEM
  33766. tristate "Sasem USB IR Remote"
  33767. depends on LIRC && USB
  33768. diff -Nur linux-3.10.37/drivers/staging/media/lirc/lirc_rpi.c linux-rpi/drivers/staging/media/lirc/lirc_rpi.c
  33769. --- linux-3.10.37/drivers/staging/media/lirc/lirc_rpi.c 1970-01-01 01:00:00.000000000 +0100
  33770. +++ linux-rpi/drivers/staging/media/lirc/lirc_rpi.c 2014-04-24 15:35:03.925563013 +0200
  33771. @@ -0,0 +1,693 @@
  33772. +/*
  33773. + * lirc_rpi.c
  33774. + *
  33775. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  33776. + * (space-lengths) (just like the lirc_serial driver does)
  33777. + * between GPIO interrupt events on the Raspberry Pi.
  33778. + * Lots of code has been taken from the lirc_serial module,
  33779. + * so I would like say thanks to the authors.
  33780. + *
  33781. + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
  33782. + * Michael Bishop <cleverca22@gmail.com>
  33783. + * This program is free software; you can redistribute it and/or modify
  33784. + * it under the terms of the GNU General Public License as published by
  33785. + * the Free Software Foundation; either version 2 of the License, or
  33786. + * (at your option) any later version.
  33787. + *
  33788. + * This program is distributed in the hope that it will be useful,
  33789. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33790. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33791. + * GNU General Public License for more details.
  33792. + *
  33793. + * You should have received a copy of the GNU General Public License
  33794. + * along with this program; if not, write to the Free Software
  33795. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33796. + */
  33797. +
  33798. +#include <linux/module.h>
  33799. +#include <linux/errno.h>
  33800. +#include <linux/interrupt.h>
  33801. +#include <linux/sched.h>
  33802. +#include <linux/kernel.h>
  33803. +#include <linux/time.h>
  33804. +#include <linux/string.h>
  33805. +#include <linux/delay.h>
  33806. +#include <linux/platform_device.h>
  33807. +#include <linux/irq.h>
  33808. +#include <linux/spinlock.h>
  33809. +#include <media/lirc.h>
  33810. +#include <media/lirc_dev.h>
  33811. +#include <linux/gpio.h>
  33812. +
  33813. +#define LIRC_DRIVER_NAME "lirc_rpi"
  33814. +#define RBUF_LEN 256
  33815. +#define LIRC_TRANSMITTER_LATENCY 256
  33816. +
  33817. +#ifndef MAX_UDELAY_MS
  33818. +#define MAX_UDELAY_US 5000
  33819. +#else
  33820. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  33821. +#endif
  33822. +
  33823. +#define dprintk(fmt, args...) \
  33824. + do { \
  33825. + if (debug) \
  33826. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  33827. + fmt, ## args); \
  33828. + } while (0)
  33829. +
  33830. +/* module parameters */
  33831. +
  33832. +/* set the default GPIO input pin */
  33833. +static int gpio_in_pin = 18;
  33834. +/* set the default GPIO output pin */
  33835. +static int gpio_out_pin = 17;
  33836. +/* enable debugging messages */
  33837. +static bool debug;
  33838. +/* -1 = auto, 0 = active high, 1 = active low */
  33839. +static int sense = -1;
  33840. +/* use softcarrier by default */
  33841. +static bool softcarrier = 1;
  33842. +/* 0 = do not invert output, 1 = invert output */
  33843. +static bool invert = 0;
  33844. +
  33845. +struct gpio_chip *gpiochip;
  33846. +struct irq_chip *irqchip;
  33847. +struct irq_data *irqdata;
  33848. +
  33849. +/* forward declarations */
  33850. +static long send_pulse(unsigned long length);
  33851. +static void send_space(long length);
  33852. +static void lirc_rpi_exit(void);
  33853. +
  33854. +int valid_gpio_pins[] = { 0, 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21,
  33855. + 22, 23, 24, 25 ,27, 28, 29, 30, 31 };
  33856. +
  33857. +static struct platform_device *lirc_rpi_dev;
  33858. +static struct timeval lasttv = { 0, 0 };
  33859. +static struct lirc_buffer rbuf;
  33860. +static spinlock_t lock;
  33861. +
  33862. +/* initialized/set in init_timing_params() */
  33863. +static unsigned int freq = 38000;
  33864. +static unsigned int duty_cycle = 50;
  33865. +static unsigned long period;
  33866. +static unsigned long pulse_width;
  33867. +static unsigned long space_width;
  33868. +
  33869. +static void safe_udelay(unsigned long usecs)
  33870. +{
  33871. + while (usecs > MAX_UDELAY_US) {
  33872. + udelay(MAX_UDELAY_US);
  33873. + usecs -= MAX_UDELAY_US;
  33874. + }
  33875. + udelay(usecs);
  33876. +}
  33877. +
  33878. +static int init_timing_params(unsigned int new_duty_cycle,
  33879. + unsigned int new_freq)
  33880. +{
  33881. + /*
  33882. + * period, pulse/space width are kept with 8 binary places -
  33883. + * IE multiplied by 256.
  33884. + */
  33885. + if (256 * 1000000L / new_freq * new_duty_cycle / 100 <=
  33886. + LIRC_TRANSMITTER_LATENCY)
  33887. + return -EINVAL;
  33888. + if (256 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  33889. + LIRC_TRANSMITTER_LATENCY)
  33890. + return -EINVAL;
  33891. + duty_cycle = new_duty_cycle;
  33892. + freq = new_freq;
  33893. + period = 256 * 1000000L / freq;
  33894. + pulse_width = period * duty_cycle / 100;
  33895. + space_width = period - pulse_width;
  33896. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  33897. + "space=%ld\n", freq, pulse_width, space_width);
  33898. + return 0;
  33899. +}
  33900. +
  33901. +static long send_pulse_softcarrier(unsigned long length)
  33902. +{
  33903. + int flag;
  33904. + unsigned long actual, target, d;
  33905. +
  33906. + length <<= 8;
  33907. +
  33908. + actual = 0; target = 0; flag = 0;
  33909. + while (actual < length) {
  33910. + if (flag) {
  33911. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33912. + target += space_width;
  33913. + } else {
  33914. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  33915. + target += pulse_width;
  33916. + }
  33917. + d = (target - actual -
  33918. + LIRC_TRANSMITTER_LATENCY + 128) >> 8;
  33919. + /*
  33920. + * Note - we've checked in ioctl that the pulse/space
  33921. + * widths are big enough so that d is > 0
  33922. + */
  33923. + udelay(d);
  33924. + actual += (d << 8) + LIRC_TRANSMITTER_LATENCY;
  33925. + flag = !flag;
  33926. + }
  33927. + return (actual-length) >> 8;
  33928. +}
  33929. +
  33930. +static long send_pulse(unsigned long length)
  33931. +{
  33932. + if (length <= 0)
  33933. + return 0;
  33934. +
  33935. + if (softcarrier) {
  33936. + return send_pulse_softcarrier(length);
  33937. + } else {
  33938. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  33939. + safe_udelay(length);
  33940. + return 0;
  33941. + }
  33942. +}
  33943. +
  33944. +static void send_space(long length)
  33945. +{
  33946. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33947. + if (length <= 0)
  33948. + return;
  33949. + safe_udelay(length);
  33950. +}
  33951. +
  33952. +static void rbwrite(int l)
  33953. +{
  33954. + if (lirc_buffer_full(&rbuf)) {
  33955. + /* no new signals will be accepted */
  33956. + dprintk("Buffer overrun\n");
  33957. + return;
  33958. + }
  33959. + lirc_buffer_write(&rbuf, (void *)&l);
  33960. +}
  33961. +
  33962. +static void frbwrite(int l)
  33963. +{
  33964. + /* simple noise filter */
  33965. + static int pulse, space;
  33966. + static unsigned int ptr;
  33967. +
  33968. + if (ptr > 0 && (l & PULSE_BIT)) {
  33969. + pulse += l & PULSE_MASK;
  33970. + if (pulse > 250) {
  33971. + rbwrite(space);
  33972. + rbwrite(pulse | PULSE_BIT);
  33973. + ptr = 0;
  33974. + pulse = 0;
  33975. + }
  33976. + return;
  33977. + }
  33978. + if (!(l & PULSE_BIT)) {
  33979. + if (ptr == 0) {
  33980. + if (l > 20000) {
  33981. + space = l;
  33982. + ptr++;
  33983. + return;
  33984. + }
  33985. + } else {
  33986. + if (l > 20000) {
  33987. + space += pulse;
  33988. + if (space > PULSE_MASK)
  33989. + space = PULSE_MASK;
  33990. + space += l;
  33991. + if (space > PULSE_MASK)
  33992. + space = PULSE_MASK;
  33993. + pulse = 0;
  33994. + return;
  33995. + }
  33996. + rbwrite(space);
  33997. + rbwrite(pulse | PULSE_BIT);
  33998. + ptr = 0;
  33999. + pulse = 0;
  34000. + }
  34001. + }
  34002. + rbwrite(l);
  34003. +}
  34004. +
  34005. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  34006. +{
  34007. + struct timeval tv;
  34008. + long deltv;
  34009. + int data;
  34010. + int signal;
  34011. +
  34012. + /* use the GPIO signal level */
  34013. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  34014. +
  34015. + /* unmask the irq */
  34016. + irqchip->irq_unmask(irqdata);
  34017. +
  34018. + if (sense != -1) {
  34019. + /* get current time */
  34020. + do_gettimeofday(&tv);
  34021. +
  34022. + /* calc time since last interrupt in microseconds */
  34023. + deltv = tv.tv_sec-lasttv.tv_sec;
  34024. + if (tv.tv_sec < lasttv.tv_sec ||
  34025. + (tv.tv_sec == lasttv.tv_sec &&
  34026. + tv.tv_usec < lasttv.tv_usec)) {
  34027. + printk(KERN_WARNING LIRC_DRIVER_NAME
  34028. + ": AIEEEE: your clock just jumped backwards\n");
  34029. + printk(KERN_WARNING LIRC_DRIVER_NAME
  34030. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  34031. + tv.tv_sec, lasttv.tv_sec,
  34032. + tv.tv_usec, lasttv.tv_usec);
  34033. + data = PULSE_MASK;
  34034. + } else if (deltv > 15) {
  34035. + data = PULSE_MASK; /* really long time */
  34036. + if (!(signal^sense)) {
  34037. + /* sanity check */
  34038. + printk(KERN_WARNING LIRC_DRIVER_NAME
  34039. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  34040. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  34041. + tv.tv_usec, lasttv.tv_usec);
  34042. + /*
  34043. + * detecting pulse while this
  34044. + * MUST be a space!
  34045. + */
  34046. + sense = sense ? 0 : 1;
  34047. + }
  34048. + } else {
  34049. + data = (int) (deltv*1000000 +
  34050. + (tv.tv_usec - lasttv.tv_usec));
  34051. + }
  34052. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  34053. + lasttv = tv;
  34054. + wake_up_interruptible(&rbuf.wait_poll);
  34055. + }
  34056. +
  34057. + return IRQ_HANDLED;
  34058. +}
  34059. +
  34060. +static int is_right_chip(struct gpio_chip *chip, void *data)
  34061. +{
  34062. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  34063. +
  34064. + if (strcmp(data, chip->label) == 0)
  34065. + return 1;
  34066. + return 0;
  34067. +}
  34068. +
  34069. +static int init_port(void)
  34070. +{
  34071. + int i, nlow, nhigh, ret, irq;
  34072. +
  34073. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  34074. +
  34075. + if (!gpiochip)
  34076. + return -ENODEV;
  34077. +
  34078. + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
  34079. + printk(KERN_ALERT LIRC_DRIVER_NAME
  34080. + ": cant claim gpio pin %d\n", gpio_out_pin);
  34081. + ret = -ENODEV;
  34082. + goto exit_init_port;
  34083. + }
  34084. +
  34085. + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
  34086. + printk(KERN_ALERT LIRC_DRIVER_NAME
  34087. + ": cant claim gpio pin %d\n", gpio_in_pin);
  34088. + ret = -ENODEV;
  34089. + goto exit_gpio_free_out_pin;
  34090. + }
  34091. +
  34092. + gpiochip->direction_input(gpiochip, gpio_in_pin);
  34093. + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
  34094. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  34095. +
  34096. + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
  34097. + dprintk("to_irq %d\n", irq);
  34098. + irqdata = irq_get_irq_data(irq);
  34099. +
  34100. + if (irqdata && irqdata->chip) {
  34101. + irqchip = irqdata->chip;
  34102. + } else {
  34103. + ret = -ENODEV;
  34104. + goto exit_gpio_free_in_pin;
  34105. + }
  34106. +
  34107. + /* if pin is high, then this must be an active low receiver. */
  34108. + if (sense == -1) {
  34109. + /* wait 1/2 sec for the power supply */
  34110. + msleep(500);
  34111. +
  34112. + /*
  34113. + * probe 9 times every 0.04s, collect "votes" for
  34114. + * active high/low
  34115. + */
  34116. + nlow = 0;
  34117. + nhigh = 0;
  34118. + for (i = 0; i < 9; i++) {
  34119. + if (gpiochip->get(gpiochip, gpio_in_pin))
  34120. + nlow++;
  34121. + else
  34122. + nhigh++;
  34123. + msleep(40);
  34124. + }
  34125. + sense = (nlow >= nhigh ? 1 : 0);
  34126. + printk(KERN_INFO LIRC_DRIVER_NAME
  34127. + ": auto-detected active %s receiver on GPIO pin %d\n",
  34128. + sense ? "low" : "high", gpio_in_pin);
  34129. + } else {
  34130. + printk(KERN_INFO LIRC_DRIVER_NAME
  34131. + ": manually using active %s receiver on GPIO pin %d\n",
  34132. + sense ? "low" : "high", gpio_in_pin);
  34133. + }
  34134. +
  34135. + return 0;
  34136. +
  34137. + exit_gpio_free_in_pin:
  34138. + gpio_free(gpio_in_pin);
  34139. +
  34140. + exit_gpio_free_out_pin:
  34141. + gpio_free(gpio_out_pin);
  34142. +
  34143. + exit_init_port:
  34144. + return ret;
  34145. +}
  34146. +
  34147. +// called when the character device is opened
  34148. +static int set_use_inc(void *data)
  34149. +{
  34150. + int result;
  34151. + unsigned long flags;
  34152. +
  34153. + /* initialize timestamp */
  34154. + do_gettimeofday(&lasttv);
  34155. +
  34156. + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
  34157. + (irq_handler_t) irq_handler, 0,
  34158. + LIRC_DRIVER_NAME, (void*) 0);
  34159. +
  34160. + switch (result) {
  34161. + case -EBUSY:
  34162. + printk(KERN_ERR LIRC_DRIVER_NAME
  34163. + ": IRQ %d is busy\n",
  34164. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  34165. + return -EBUSY;
  34166. + case -EINVAL:
  34167. + printk(KERN_ERR LIRC_DRIVER_NAME
  34168. + ": Bad irq number or handler\n");
  34169. + return -EINVAL;
  34170. + default:
  34171. + dprintk("Interrupt %d obtained\n",
  34172. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  34173. + break;
  34174. + };
  34175. +
  34176. + /* initialize pulse/space widths */
  34177. + init_timing_params(duty_cycle, freq);
  34178. +
  34179. + spin_lock_irqsave(&lock, flags);
  34180. +
  34181. + /* GPIO Pin Falling/Rising Edge Detect Enable */
  34182. + irqchip->irq_set_type(irqdata,
  34183. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
  34184. +
  34185. + /* unmask the irq */
  34186. + irqchip->irq_unmask(irqdata);
  34187. +
  34188. + spin_unlock_irqrestore(&lock, flags);
  34189. +
  34190. + return 0;
  34191. +}
  34192. +
  34193. +static void set_use_dec(void *data)
  34194. +{
  34195. + unsigned long flags;
  34196. +
  34197. + spin_lock_irqsave(&lock, flags);
  34198. +
  34199. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  34200. + irqchip->irq_set_type(irqdata, 0);
  34201. + irqchip->irq_mask(irqdata);
  34202. +
  34203. + spin_unlock_irqrestore(&lock, flags);
  34204. +
  34205. + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
  34206. +
  34207. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  34208. + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
  34209. +}
  34210. +
  34211. +static ssize_t lirc_write(struct file *file, const char *buf,
  34212. + size_t n, loff_t *ppos)
  34213. +{
  34214. + int i, count;
  34215. + unsigned long flags;
  34216. + long delta = 0;
  34217. + int *wbuf;
  34218. +
  34219. + count = n / sizeof(int);
  34220. + if (n % sizeof(int) || count % 2 == 0)
  34221. + return -EINVAL;
  34222. + wbuf = memdup_user(buf, n);
  34223. + if (IS_ERR(wbuf))
  34224. + return PTR_ERR(wbuf);
  34225. + spin_lock_irqsave(&lock, flags);
  34226. +
  34227. + for (i = 0; i < count; i++) {
  34228. + if (i%2)
  34229. + send_space(wbuf[i] - delta);
  34230. + else
  34231. + delta = send_pulse(wbuf[i]);
  34232. + }
  34233. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  34234. +
  34235. + spin_unlock_irqrestore(&lock, flags);
  34236. + kfree(wbuf);
  34237. + return n;
  34238. +}
  34239. +
  34240. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  34241. +{
  34242. + int result;
  34243. + __u32 value;
  34244. +
  34245. + switch (cmd) {
  34246. + case LIRC_GET_SEND_MODE:
  34247. + return -ENOIOCTLCMD;
  34248. + break;
  34249. +
  34250. + case LIRC_SET_SEND_MODE:
  34251. + result = get_user(value, (__u32 *) arg);
  34252. + if (result)
  34253. + return result;
  34254. + /* only LIRC_MODE_PULSE supported */
  34255. + if (value != LIRC_MODE_PULSE)
  34256. + return -ENOSYS;
  34257. + break;
  34258. +
  34259. + case LIRC_GET_LENGTH:
  34260. + return -ENOSYS;
  34261. + break;
  34262. +
  34263. + case LIRC_SET_SEND_DUTY_CYCLE:
  34264. + dprintk("SET_SEND_DUTY_CYCLE\n");
  34265. + result = get_user(value, (__u32 *) arg);
  34266. + if (result)
  34267. + return result;
  34268. + if (value <= 0 || value > 100)
  34269. + return -EINVAL;
  34270. + return init_timing_params(value, freq);
  34271. + break;
  34272. +
  34273. + case LIRC_SET_SEND_CARRIER:
  34274. + dprintk("SET_SEND_CARRIER\n");
  34275. + result = get_user(value, (__u32 *) arg);
  34276. + if (result)
  34277. + return result;
  34278. + if (value > 500000 || value < 20000)
  34279. + return -EINVAL;
  34280. + return init_timing_params(duty_cycle, value);
  34281. + break;
  34282. +
  34283. + default:
  34284. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  34285. + }
  34286. + return 0;
  34287. +}
  34288. +
  34289. +static const struct file_operations lirc_fops = {
  34290. + .owner = THIS_MODULE,
  34291. + .write = lirc_write,
  34292. + .unlocked_ioctl = lirc_ioctl,
  34293. + .read = lirc_dev_fop_read,
  34294. + .poll = lirc_dev_fop_poll,
  34295. + .open = lirc_dev_fop_open,
  34296. + .release = lirc_dev_fop_close,
  34297. + .llseek = no_llseek,
  34298. +};
  34299. +
  34300. +static struct lirc_driver driver = {
  34301. + .name = LIRC_DRIVER_NAME,
  34302. + .minor = -1,
  34303. + .code_length = 1,
  34304. + .sample_rate = 0,
  34305. + .data = NULL,
  34306. + .add_to_buf = NULL,
  34307. + .rbuf = &rbuf,
  34308. + .set_use_inc = set_use_inc,
  34309. + .set_use_dec = set_use_dec,
  34310. + .fops = &lirc_fops,
  34311. + .dev = NULL,
  34312. + .owner = THIS_MODULE,
  34313. +};
  34314. +
  34315. +static struct platform_driver lirc_rpi_driver = {
  34316. + .driver = {
  34317. + .name = LIRC_DRIVER_NAME,
  34318. + .owner = THIS_MODULE,
  34319. + },
  34320. +};
  34321. +
  34322. +static int __init lirc_rpi_init(void)
  34323. +{
  34324. + int result;
  34325. +
  34326. + /* Init read buffer. */
  34327. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  34328. + if (result < 0)
  34329. + return -ENOMEM;
  34330. +
  34331. + result = platform_driver_register(&lirc_rpi_driver);
  34332. + if (result) {
  34333. + printk(KERN_ERR LIRC_DRIVER_NAME
  34334. + ": lirc register returned %d\n", result);
  34335. + goto exit_buffer_free;
  34336. + }
  34337. +
  34338. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  34339. + if (!lirc_rpi_dev) {
  34340. + result = -ENOMEM;
  34341. + goto exit_driver_unregister;
  34342. + }
  34343. +
  34344. + result = platform_device_add(lirc_rpi_dev);
  34345. + if (result)
  34346. + goto exit_device_put;
  34347. +
  34348. + return 0;
  34349. +
  34350. + exit_device_put:
  34351. + platform_device_put(lirc_rpi_dev);
  34352. +
  34353. + exit_driver_unregister:
  34354. + platform_driver_unregister(&lirc_rpi_driver);
  34355. +
  34356. + exit_buffer_free:
  34357. + lirc_buffer_free(&rbuf);
  34358. +
  34359. + return result;
  34360. +}
  34361. +
  34362. +static void lirc_rpi_exit(void)
  34363. +{
  34364. + platform_device_unregister(lirc_rpi_dev);
  34365. + platform_driver_unregister(&lirc_rpi_driver);
  34366. + lirc_buffer_free(&rbuf);
  34367. +}
  34368. +
  34369. +static int __init lirc_rpi_init_module(void)
  34370. +{
  34371. + int result, i;
  34372. +
  34373. + result = lirc_rpi_init();
  34374. + if (result)
  34375. + return result;
  34376. +
  34377. + /* check if the module received valid gpio pin numbers */
  34378. + result = 0;
  34379. + if (gpio_in_pin != gpio_out_pin) {
  34380. + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
  34381. + if (gpio_in_pin == valid_gpio_pins[i] ||
  34382. + gpio_out_pin == valid_gpio_pins[i]) {
  34383. + result++;
  34384. + }
  34385. + }
  34386. + }
  34387. +
  34388. + if (result != 2) {
  34389. + result = -EINVAL;
  34390. + printk(KERN_ERR LIRC_DRIVER_NAME
  34391. + ": invalid GPIO pin(s) specified!\n");
  34392. + goto exit_rpi;
  34393. + }
  34394. +
  34395. + result = init_port();
  34396. + if (result < 0)
  34397. + goto exit_rpi;
  34398. +
  34399. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  34400. + LIRC_CAN_SET_SEND_CARRIER |
  34401. + LIRC_CAN_SEND_PULSE |
  34402. + LIRC_CAN_REC_MODE2;
  34403. +
  34404. + driver.dev = &lirc_rpi_dev->dev;
  34405. + driver.minor = lirc_register_driver(&driver);
  34406. +
  34407. + if (driver.minor < 0) {
  34408. + printk(KERN_ERR LIRC_DRIVER_NAME
  34409. + ": device registration failed with %d\n", result);
  34410. + result = -EIO;
  34411. + goto exit_rpi;
  34412. + }
  34413. +
  34414. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  34415. +
  34416. + return 0;
  34417. +
  34418. + exit_rpi:
  34419. + lirc_rpi_exit();
  34420. +
  34421. + return result;
  34422. +}
  34423. +
  34424. +static void __exit lirc_rpi_exit_module(void)
  34425. +{
  34426. + gpio_free(gpio_out_pin);
  34427. + gpio_free(gpio_in_pin);
  34428. +
  34429. + lirc_rpi_exit();
  34430. +
  34431. + lirc_unregister_driver(driver.minor);
  34432. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  34433. +}
  34434. +
  34435. +module_init(lirc_rpi_init_module);
  34436. +module_exit(lirc_rpi_exit_module);
  34437. +
  34438. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  34439. +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
  34440. +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
  34441. +MODULE_LICENSE("GPL");
  34442. +
  34443. +module_param(gpio_out_pin, int, S_IRUGO);
  34444. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  34445. + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
  34446. + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
  34447. +
  34448. +module_param(gpio_in_pin, int, S_IRUGO);
  34449. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  34450. + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
  34451. + " 17, 18, 21, 22, 23, 24, 25, default 18");
  34452. +
  34453. +module_param(sense, int, S_IRUGO);
  34454. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  34455. + " (0 = active high, 1 = active low )");
  34456. +
  34457. +module_param(softcarrier, bool, S_IRUGO);
  34458. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  34459. +
  34460. +module_param(invert, bool, S_IRUGO);
  34461. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  34462. +
  34463. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  34464. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  34465. diff -Nur linux-3.10.37/drivers/staging/media/lirc/Makefile linux-rpi/drivers/staging/media/lirc/Makefile
  34466. --- linux-3.10.37/drivers/staging/media/lirc/Makefile 2014-04-14 15:42:31.000000000 +0200
  34467. +++ linux-rpi/drivers/staging/media/lirc/Makefile 2014-04-24 15:35:03.925563013 +0200
  34468. @@ -7,6 +7,7 @@
  34469. obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
  34470. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  34471. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  34472. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  34473. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  34474. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  34475. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  34476. diff -Nur linux-3.10.37/drivers/thermal/bcm2835-thermal.c linux-rpi/drivers/thermal/bcm2835-thermal.c
  34477. --- linux-3.10.37/drivers/thermal/bcm2835-thermal.c 1970-01-01 01:00:00.000000000 +0100
  34478. +++ linux-rpi/drivers/thermal/bcm2835-thermal.c 2014-04-24 15:35:04.089564840 +0200
  34479. @@ -0,0 +1,184 @@
  34480. +/*****************************************************************************
  34481. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  34482. +*
  34483. +* Unless you and Broadcom execute a separate written software license
  34484. +* agreement governing use of this software, this software is licensed to you
  34485. +* under the terms of the GNU General Public License version 2, available at
  34486. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  34487. +*
  34488. +* Notwithstanding the above, under no circumstances may you combine this
  34489. +* software in any way with any other Broadcom software provided under a
  34490. +* license other than the GPL, without Broadcom's express prior written
  34491. +* consent.
  34492. +*****************************************************************************/
  34493. +
  34494. +#include <linux/kernel.h>
  34495. +#include <linux/module.h>
  34496. +#include <linux/init.h>
  34497. +#include <linux/platform_device.h>
  34498. +#include <linux/slab.h>
  34499. +#include <linux/sysfs.h>
  34500. +#include <mach/vcio.h>
  34501. +#include <linux/thermal.h>
  34502. +
  34503. +
  34504. +/* --- DEFINITIONS --- */
  34505. +#define MODULE_NAME "bcm2835_thermal"
  34506. +
  34507. +/*#define THERMAL_DEBUG_ENABLE*/
  34508. +
  34509. +#ifdef THERMAL_DEBUG_ENABLE
  34510. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  34511. +#else
  34512. +#define print_debug(fmt,...)
  34513. +#endif
  34514. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  34515. +
  34516. +#define VC_TAG_GET_TEMP 0x00030006
  34517. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  34518. +
  34519. +typedef enum {
  34520. + TEMP,
  34521. + MAX_TEMP,
  34522. +} temp_type;
  34523. +
  34524. +/* --- STRUCTS --- */
  34525. +/* tag part of the message */
  34526. +struct vc_msg_tag {
  34527. + uint32_t tag_id; /* the tag ID for the temperature */
  34528. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  34529. + uint32_t request_code; /* identifies message as a request (should be 0) */
  34530. + uint32_t id; /* extra ID field (should be 0) */
  34531. + uint32_t val; /* returned value of the temperature */
  34532. +};
  34533. +
  34534. +/* message structure to be sent to videocore */
  34535. +struct vc_msg {
  34536. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  34537. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  34538. + struct vc_msg_tag tag; /* the tag structure above to make */
  34539. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  34540. +};
  34541. +
  34542. +struct bcm2835_thermal_data {
  34543. + struct thermal_zone_device *thermal_dev;
  34544. + struct vc_msg msg;
  34545. +};
  34546. +
  34547. +/* --- GLOBALS --- */
  34548. +static struct bcm2835_thermal_data bcm2835_data;
  34549. +
  34550. +/* Thermal Device Operations */
  34551. +static struct thermal_zone_device_ops ops;
  34552. +
  34553. +/* --- FUNCTIONS --- */
  34554. +
  34555. +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
  34556. +{
  34557. + int result = -1, retry = 3;
  34558. + print_debug("IN");
  34559. +
  34560. + *temp = 0;
  34561. + while (result != 0 && retry-- > 0) {
  34562. + /* wipe all previous message data */
  34563. + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
  34564. +
  34565. + /* prepare message */
  34566. + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
  34567. + bcm2835_data.msg.tag.buffer_size = 8;
  34568. + bcm2835_data.msg.tag.tag_id = tag_id;
  34569. +
  34570. + /* send the message */
  34571. + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
  34572. + print_debug("Got %stemperature as %u (%d,%x)\n", tag_id==VC_TAG_GET_MAX_TEMP ? "max ":"", (uint)bcm2835_data.msg.tag.val, result, bcm2835_data.msg.request_code);
  34573. + if (!(bcm2835_data.msg.request_code & 0x80000000))
  34574. + result = -1;
  34575. + }
  34576. +
  34577. + /* check if it was all ok and return the rate in milli degrees C */
  34578. + if (result == 0)
  34579. + *temp = (uint)bcm2835_data.msg.tag.val;
  34580. + else
  34581. + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
  34582. + print_debug("OUT");
  34583. + return result;
  34584. +}
  34585. +
  34586. +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
  34587. +{
  34588. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
  34589. +}
  34590. +
  34591. +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
  34592. +{
  34593. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
  34594. +}
  34595. +
  34596. +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
  34597. +{
  34598. + *trip_type = THERMAL_TRIP_HOT;
  34599. + return 0;
  34600. +}
  34601. +
  34602. +
  34603. +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
  34604. +{
  34605. + *dev_mode = THERMAL_DEVICE_ENABLED;
  34606. + return 0;
  34607. +}
  34608. +
  34609. +
  34610. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  34611. +{
  34612. + print_debug("IN");
  34613. + print_debug("THERMAL Driver has been probed!");
  34614. +
  34615. + /* check that the device isn't null!*/
  34616. + if(pdev == NULL)
  34617. + {
  34618. + print_debug("Platform device is empty!");
  34619. + return -ENODEV;
  34620. + }
  34621. +
  34622. + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
  34623. + {
  34624. + print_debug("Unable to register the thermal device!");
  34625. + return -EFAULT;
  34626. + }
  34627. + return 0;
  34628. +}
  34629. +
  34630. +
  34631. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  34632. +{
  34633. + print_debug("IN");
  34634. +
  34635. + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
  34636. +
  34637. + print_debug("OUT");
  34638. +
  34639. + return 0;
  34640. +}
  34641. +
  34642. +static struct thermal_zone_device_ops ops = {
  34643. + .get_temp = bcm2835_get_temp,
  34644. + .get_trip_temp = bcm2835_get_max_temp,
  34645. + .get_trip_type = bcm2835_get_trip_type,
  34646. + .get_mode = bcm2835_get_mode,
  34647. +};
  34648. +
  34649. +/* Thermal Driver */
  34650. +static struct platform_driver bcm2835_thermal_driver = {
  34651. + .probe = bcm2835_thermal_probe,
  34652. + .remove = bcm2835_thermal_remove,
  34653. + .driver = {
  34654. + .name = "bcm2835_thermal",
  34655. + .owner = THIS_MODULE,
  34656. + },
  34657. +};
  34658. +
  34659. +MODULE_LICENSE("GPL");
  34660. +MODULE_AUTHOR("Dorian Peake");
  34661. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  34662. +
  34663. +module_platform_driver(bcm2835_thermal_driver);
  34664. diff -Nur linux-3.10.37/drivers/thermal/Kconfig linux-rpi/drivers/thermal/Kconfig
  34665. --- linux-3.10.37/drivers/thermal/Kconfig 2014-04-14 15:42:31.000000000 +0200
  34666. +++ linux-rpi/drivers/thermal/Kconfig 2014-04-24 15:35:04.089564840 +0200
  34667. @@ -169,4 +169,11 @@
  34668. enforce idle time which results in more package C-state residency. The
  34669. user interface is exposed via generic thermal framework.
  34670. +config THERMAL_BCM2835
  34671. + tristate "BCM2835 Thermal Driver"
  34672. + help
  34673. + This will enable temperature monitoring for the Broadcom BCM2835
  34674. + chip. If built as a module, it will be called 'bcm2835-thermal'.
  34675. +
  34676. endif
  34677. +
  34678. diff -Nur linux-3.10.37/drivers/thermal/Makefile linux-rpi/drivers/thermal/Makefile
  34679. --- linux-3.10.37/drivers/thermal/Makefile 2014-04-14 15:42:31.000000000 +0200
  34680. +++ linux-rpi/drivers/thermal/Makefile 2014-04-24 15:35:04.089564840 +0200
  34681. @@ -23,4 +23,5 @@
  34682. obj-$(CONFIG_ARMADA_THERMAL) += armada_thermal.o
  34683. obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
  34684. obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
  34685. +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
  34686. diff -Nur linux-3.10.37/drivers/tty/serial/amba-pl011.c linux-rpi/drivers/tty/serial/amba-pl011.c
  34687. --- linux-3.10.37/drivers/tty/serial/amba-pl011.c 2014-04-14 15:42:31.000000000 +0200
  34688. +++ linux-rpi/drivers/tty/serial/amba-pl011.c 2014-04-24 15:35:04.105565018 +0200
  34689. @@ -84,7 +84,7 @@
  34690. static unsigned int get_fifosize_arm(unsigned int periphid)
  34691. {
  34692. - unsigned int rev = (periphid >> 20) & 0xf;
  34693. + unsigned int rev = 0; //(periphid >> 20) & 0xf;
  34694. return rev < 3 ? 16 : 32;
  34695. }
  34696. diff -Nur linux-3.10.37/drivers/usb/core/generic.c linux-rpi/drivers/usb/core/generic.c
  34697. --- linux-3.10.37/drivers/usb/core/generic.c 2014-04-14 15:42:31.000000000 +0200
  34698. +++ linux-rpi/drivers/usb/core/generic.c 2014-04-24 15:35:04.137565375 +0200
  34699. @@ -152,6 +152,7 @@
  34700. dev_warn(&udev->dev,
  34701. "no configuration chosen from %d choice%s\n",
  34702. num_configs, plural(num_configs));
  34703. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  34704. }
  34705. return i;
  34706. }
  34707. diff -Nur linux-3.10.37/drivers/usb/core/message.c linux-rpi/drivers/usb/core/message.c
  34708. --- linux-3.10.37/drivers/usb/core/message.c 2014-04-14 15:42:31.000000000 +0200
  34709. +++ linux-rpi/drivers/usb/core/message.c 2014-04-24 15:35:04.141565419 +0200
  34710. @@ -1875,6 +1875,85 @@
  34711. if (cp->string == NULL &&
  34712. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  34713. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  34714. +/* Uncomment this define to enable the HS Electrical Test support */
  34715. +#define DWC_HS_ELECT_TST 1
  34716. +#ifdef DWC_HS_ELECT_TST
  34717. + /* Here we implement the HS Electrical Test support. The
  34718. + * tester uses a vendor ID of 0x1A0A to indicate we should
  34719. + * run a special test sequence. The product ID tells us
  34720. + * which sequence to run. We invoke the test sequence by
  34721. + * sending a non-standard SetFeature command to our root
  34722. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  34723. + * recognize the command and perform the desired test
  34724. + * sequence.
  34725. + */
  34726. + if (dev->descriptor.idVendor == 0x1A0A) {
  34727. + /* HSOTG Electrical Test */
  34728. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  34729. +
  34730. + if (dev->bus && dev->bus->root_hub) {
  34731. + struct usb_device *hdev = dev->bus->root_hub;
  34732. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  34733. +
  34734. + switch (dev->descriptor.idProduct) {
  34735. + case 0x0101: /* TEST_SE0_NAK */
  34736. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  34737. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34738. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34739. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  34740. + break;
  34741. +
  34742. + case 0x0102: /* TEST_J */
  34743. + dev_warn(&dev->dev, "TEST_J\n");
  34744. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34745. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34746. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  34747. + break;
  34748. +
  34749. + case 0x0103: /* TEST_K */
  34750. + dev_warn(&dev->dev, "TEST_K\n");
  34751. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34752. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34753. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  34754. + break;
  34755. +
  34756. + case 0x0104: /* TEST_PACKET */
  34757. + dev_warn(&dev->dev, "TEST_PACKET\n");
  34758. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34759. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34760. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  34761. + break;
  34762. +
  34763. + case 0x0105: /* TEST_FORCE_ENABLE */
  34764. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  34765. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34766. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34767. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  34768. + break;
  34769. +
  34770. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  34771. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  34772. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34773. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34774. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  34775. + break;
  34776. +
  34777. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  34778. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  34779. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34780. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34781. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  34782. + break;
  34783. +
  34784. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  34785. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  34786. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34787. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34788. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  34789. + }
  34790. + }
  34791. + }
  34792. +#endif /* DWC_HS_ELECT_TST */
  34793. /* Now that the interfaces are installed, re-enable LPM. */
  34794. usb_unlocked_enable_lpm(dev);
  34795. diff -Nur linux-3.10.37/drivers/usb/core/otg_whitelist.h linux-rpi/drivers/usb/core/otg_whitelist.h
  34796. --- linux-3.10.37/drivers/usb/core/otg_whitelist.h 2014-04-14 15:42:31.000000000 +0200
  34797. +++ linux-rpi/drivers/usb/core/otg_whitelist.h 2014-04-24 15:35:04.141565419 +0200
  34798. @@ -19,33 +19,82 @@
  34799. static struct usb_device_id whitelist_table [] = {
  34800. /* hubs are optional in OTG, but very handy ... */
  34801. +#define CERT_WITHOUT_HUBS
  34802. +#if defined(CERT_WITHOUT_HUBS)
  34803. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  34804. +#else
  34805. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  34806. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  34807. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  34808. +#endif
  34809. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  34810. /* FIXME actually, printers are NOT supposed to use device classes;
  34811. * they're supposed to use interface classes...
  34812. */
  34813. -{ USB_DEVICE_INFO(7, 1, 1) },
  34814. -{ USB_DEVICE_INFO(7, 1, 2) },
  34815. -{ USB_DEVICE_INFO(7, 1, 3) },
  34816. +//{ USB_DEVICE_INFO(7, 1, 1) },
  34817. +//{ USB_DEVICE_INFO(7, 1, 2) },
  34818. +//{ USB_DEVICE_INFO(7, 1, 3) },
  34819. #endif
  34820. #ifdef CONFIG_USB_NET_CDCETHER
  34821. /* Linux-USB CDC Ethernet gadget */
  34822. -{ USB_DEVICE(0x0525, 0xa4a1), },
  34823. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  34824. /* Linux-USB CDC Ethernet + RNDIS gadget */
  34825. -{ USB_DEVICE(0x0525, 0xa4a2), },
  34826. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  34827. #endif
  34828. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  34829. /* gadget zero, for testing */
  34830. -{ USB_DEVICE(0x0525, 0xa4a0), },
  34831. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  34832. #endif
  34833. +
  34834. +/* OPT Tester */
  34835. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  34836. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  34837. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  34838. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  34839. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  34840. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  34841. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  34842. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  34843. +
  34844. +/* Sony cameras */
  34845. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  34846. +
  34847. +/* Memory Devices */
  34848. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  34849. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  34850. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  34851. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  34852. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  34853. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  34854. +
  34855. +/* HP Printers */
  34856. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  34857. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  34858. +
  34859. +/* Speakers */
  34860. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  34861. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  34862. { } /* Terminating entry */
  34863. };
  34864. +static inline void report_errors(struct usb_device *dev)
  34865. +{
  34866. + /* OTG MESSAGE: report errors here, customize to match your product */
  34867. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  34868. + le16_to_cpu(dev->descriptor.idVendor),
  34869. + le16_to_cpu(dev->descriptor.idProduct));
  34870. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  34871. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  34872. + } else {
  34873. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  34874. + }
  34875. +}
  34876. +
  34877. +
  34878. static int is_targeted(struct usb_device *dev)
  34879. {
  34880. struct usb_device_id *id = whitelist_table;
  34881. @@ -55,58 +104,83 @@
  34882. return 1;
  34883. /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
  34884. - if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
  34885. - le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
  34886. - return 0;
  34887. + if (dev->descriptor.idVendor == 0x1a0a &&
  34888. + dev->descriptor.idProduct == 0xbadd) {
  34889. + return 0;
  34890. + } else if (!enable_whitelist) {
  34891. + return 1;
  34892. + } else {
  34893. - /* NOTE: can't use usb_match_id() since interface caches
  34894. - * aren't set up yet. this is cut/paste from that code.
  34895. - */
  34896. - for (id = whitelist_table; id->match_flags; id++) {
  34897. - if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  34898. - id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  34899. - continue;
  34900. -
  34901. - if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  34902. - id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  34903. - continue;
  34904. -
  34905. - /* No need to test id->bcdDevice_lo != 0, since 0 is never
  34906. - greater than any unsigned number. */
  34907. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  34908. - (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  34909. - continue;
  34910. -
  34911. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  34912. - (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  34913. - continue;
  34914. -
  34915. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  34916. - (id->bDeviceClass != dev->descriptor.bDeviceClass))
  34917. - continue;
  34918. -
  34919. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  34920. - (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  34921. - continue;
  34922. -
  34923. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  34924. - (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  34925. - continue;
  34926. +#ifdef DEBUG
  34927. + dev_dbg(&dev->dev, "device V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  34928. + dev->descriptor.idVendor,
  34929. + dev->descriptor.idProduct,
  34930. + dev->descriptor.bDeviceClass,
  34931. + dev->descriptor.bDeviceSubClass,
  34932. + dev->descriptor.bDeviceProtocol);
  34933. +#endif
  34934. return 1;
  34935. + /* NOTE: can't use usb_match_id() since interface caches
  34936. + * aren't set up yet. this is cut/paste from that code.
  34937. + */
  34938. + for (id = whitelist_table; id->match_flags; id++) {
  34939. +#ifdef DEBUG
  34940. + dev_dbg(&dev->dev,
  34941. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  34942. + id->idVendor,
  34943. + id->idProduct,
  34944. + id->bDeviceClass,
  34945. + id->bDeviceSubClass,
  34946. + id->bDeviceProtocol);
  34947. +#endif
  34948. +
  34949. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  34950. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  34951. + continue;
  34952. +
  34953. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  34954. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  34955. + continue;
  34956. +
  34957. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  34958. + greater than any unsigned number. */
  34959. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  34960. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  34961. + continue;
  34962. +
  34963. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  34964. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  34965. + continue;
  34966. +
  34967. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  34968. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  34969. + continue;
  34970. +
  34971. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  34972. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  34973. + continue;
  34974. +
  34975. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  34976. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  34977. + continue;
  34978. +
  34979. + return 1;
  34980. + }
  34981. }
  34982. /* add other match criteria here ... */
  34983. -
  34984. - /* OTG MESSAGE: report errors here, customize to match your product */
  34985. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  34986. - le16_to_cpu(dev->descriptor.idVendor),
  34987. - le16_to_cpu(dev->descriptor.idProduct));
  34988. #ifdef CONFIG_USB_OTG_WHITELIST
  34989. + report_errors(dev);
  34990. return 0;
  34991. #else
  34992. - return 1;
  34993. + if (enable_whitelist) {
  34994. + report_errors(dev);
  34995. + return 0;
  34996. + } else {
  34997. + return 1;
  34998. + }
  34999. #endif
  35000. }
  35001. diff -Nur linux-3.10.37/drivers/usb/gadget/file_storage.c linux-rpi/drivers/usb/gadget/file_storage.c
  35002. --- linux-3.10.37/drivers/usb/gadget/file_storage.c 1970-01-01 01:00:00.000000000 +0100
  35003. +++ linux-rpi/drivers/usb/gadget/file_storage.c 2014-04-24 15:35:04.153565553 +0200
  35004. @@ -0,0 +1,3676 @@
  35005. +/*
  35006. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  35007. + *
  35008. + * Copyright (C) 2003-2008 Alan Stern
  35009. + * All rights reserved.
  35010. + *
  35011. + * Redistribution and use in source and binary forms, with or without
  35012. + * modification, are permitted provided that the following conditions
  35013. + * are met:
  35014. + * 1. Redistributions of source code must retain the above copyright
  35015. + * notice, this list of conditions, and the following disclaimer,
  35016. + * without modification.
  35017. + * 2. Redistributions in binary form must reproduce the above copyright
  35018. + * notice, this list of conditions and the following disclaimer in the
  35019. + * documentation and/or other materials provided with the distribution.
  35020. + * 3. The names of the above-listed copyright holders may not be used
  35021. + * to endorse or promote products derived from this software without
  35022. + * specific prior written permission.
  35023. + *
  35024. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35025. + * GNU General Public License ("GPL") as published by the Free Software
  35026. + * Foundation, either version 2 of that License or (at your option) any
  35027. + * later version.
  35028. + *
  35029. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35030. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35031. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35032. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35033. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35034. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35035. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35036. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35037. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35038. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35039. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35040. + */
  35041. +
  35042. +
  35043. +/*
  35044. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  35045. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  35046. + * to providing an example of a genuinely useful gadget driver for a USB
  35047. + * device, it also illustrates a technique of double-buffering for increased
  35048. + * throughput. Last but not least, it gives an easy way to probe the
  35049. + * behavior of the Mass Storage drivers in a USB host.
  35050. + *
  35051. + * Backing storage is provided by a regular file or a block device, specified
  35052. + * by the "file" module parameter. Access can be limited to read-only by
  35053. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  35054. + * access is always read-only.) The gadget will indicate that it has
  35055. + * removable media if the optional "removable" module parameter is set.
  35056. + *
  35057. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  35058. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  35059. + * by the optional "transport" module parameter. It also supports the
  35060. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  35061. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  35062. + * the optional "protocol" module parameter. In addition, the default
  35063. + * Vendor ID, Product ID, release number and serial number can be overridden.
  35064. + *
  35065. + * There is support for multiple logical units (LUNs), each of which has
  35066. + * its own backing file. The number of LUNs can be set using the optional
  35067. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  35068. + * files are specified using comma-separated lists for "file" and "ro".
  35069. + * The default number of LUNs is taken from the number of "file" elements;
  35070. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  35071. + * file must be specified for each LUN. If it is set, then an unspecified
  35072. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  35073. + * each LUN would be settable independently as a disk drive or a CD-ROM
  35074. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  35075. + * emulation includes a single data track and no audio tracks; hence there
  35076. + * need be only one backing file per LUN.
  35077. + *
  35078. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  35079. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  35080. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  35081. + * Support is included for both full-speed and high-speed operation.
  35082. + *
  35083. + * Note that the driver is slightly non-portable in that it assumes a
  35084. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  35085. + * interrupt-in endpoints. With most device controllers this isn't an
  35086. + * issue, but there may be some with hardware restrictions that prevent
  35087. + * a buffer from being used by more than one endpoint.
  35088. + *
  35089. + * Module options:
  35090. + *
  35091. + * file=filename[,filename...]
  35092. + * Required if "removable" is not set, names of
  35093. + * the files or block devices used for
  35094. + * backing storage
  35095. + * serial=HHHH... Required serial number (string of hex chars)
  35096. + * ro=b[,b...] Default false, booleans for read-only access
  35097. + * removable Default false, boolean for removable media
  35098. + * luns=N Default N = number of filenames, number of
  35099. + * LUNs to support
  35100. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  35101. + * in SCSI WRITE(10,12) commands
  35102. + * stall Default determined according to the type of
  35103. + * USB device controller (usually true),
  35104. + * boolean to permit the driver to halt
  35105. + * bulk endpoints
  35106. + * cdrom Default false, boolean for whether to emulate
  35107. + * a CD-ROM drive
  35108. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  35109. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  35110. + * ATAPI, QIC, UFI, 8070, or SCSI;
  35111. + * also 1 - 6)
  35112. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  35113. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  35114. + * release=0xRRRR Override the USB release number (bcdDevice)
  35115. + * buflen=N Default N=16384, buffer size used (will be
  35116. + * rounded down to a multiple of
  35117. + * PAGE_CACHE_SIZE)
  35118. + *
  35119. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  35120. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  35121. + * default values are used for everything else.
  35122. + *
  35123. + * The pathnames of the backing files and the ro settings are available in
  35124. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  35125. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  35126. + * these files will simulate ejecting/loading the medium (writing an empty
  35127. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  35128. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  35129. + * is being used.
  35130. + *
  35131. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  35132. + * The driver's SCSI command interface was based on the "Information
  35133. + * technology - Small Computer System Interface - 2" document from
  35134. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  35135. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  35136. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  35137. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  35138. + * document, Revision 1.0, December 14, 1998, available at
  35139. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  35140. + */
  35141. +
  35142. +
  35143. +/*
  35144. + * Driver Design
  35145. + *
  35146. + * The FSG driver is fairly straightforward. There is a main kernel
  35147. + * thread that handles most of the work. Interrupt routines field
  35148. + * callbacks from the controller driver: bulk- and interrupt-request
  35149. + * completion notifications, endpoint-0 events, and disconnect events.
  35150. + * Completion events are passed to the main thread by wakeup calls. Many
  35151. + * ep0 requests are handled at interrupt time, but SetInterface,
  35152. + * SetConfiguration, and device reset requests are forwarded to the
  35153. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  35154. + * should interrupt any ongoing file I/O operations).
  35155. + *
  35156. + * The thread's main routine implements the standard command/data/status
  35157. + * parts of a SCSI interaction. It and its subroutines are full of tests
  35158. + * for pending signals/exceptions -- all this polling is necessary since
  35159. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  35160. + * indication that the driver really wants to be running in userspace.)
  35161. + * An important point is that so long as the thread is alive it keeps an
  35162. + * open reference to the backing file. This will prevent unmounting
  35163. + * the backing file's underlying filesystem and could cause problems
  35164. + * during system shutdown, for example. To prevent such problems, the
  35165. + * thread catches INT, TERM, and KILL signals and converts them into
  35166. + * an EXIT exception.
  35167. + *
  35168. + * In normal operation the main thread is started during the gadget's
  35169. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  35170. + * exit when it receives a signal, and there's no point leaving the
  35171. + * gadget running when the thread is dead. So just before the thread
  35172. + * exits, it deregisters the gadget driver. This makes things a little
  35173. + * tricky: The driver is deregistered at two places, and the exiting
  35174. + * thread can indirectly call fsg_unbind() which in turn can tell the
  35175. + * thread to exit. The first problem is resolved through the use of the
  35176. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  35177. + * The second problem is resolved by having fsg_unbind() check
  35178. + * fsg->state; it won't try to stop the thread if the state is already
  35179. + * FSG_STATE_TERMINATED.
  35180. + *
  35181. + * To provide maximum throughput, the driver uses a circular pipeline of
  35182. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  35183. + * arbitrarily long; in practice the benefits don't justify having more
  35184. + * than 2 stages (i.e., double buffering). But it helps to think of the
  35185. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  35186. + * a bulk-out request pointer (since the buffer can be used for both
  35187. + * output and input -- directions always are given from the host's
  35188. + * point of view) as well as a pointer to the buffer and various state
  35189. + * variables.
  35190. + *
  35191. + * Use of the pipeline follows a simple protocol. There is a variable
  35192. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  35193. + * At any time that buffer head may still be in use from an earlier
  35194. + * request, so each buffer head has a state variable indicating whether
  35195. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  35196. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  35197. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  35198. + * head FULL when the I/O is complete. Then the buffer will be emptied
  35199. + * (again possibly by USB I/O, during which it is marked BUSY) and
  35200. + * finally marked EMPTY again (possibly by a completion routine).
  35201. + *
  35202. + * A module parameter tells the driver to avoid stalling the bulk
  35203. + * endpoints wherever the transport specification allows. This is
  35204. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  35205. + * halt on a bulk endpoint. However, under certain circumstances the
  35206. + * Bulk-only specification requires a stall. In such cases the driver
  35207. + * will halt the endpoint and set a flag indicating that it should clear
  35208. + * the halt in software during the next device reset. Hopefully this
  35209. + * will permit everything to work correctly. Furthermore, although the
  35210. + * specification allows the bulk-out endpoint to halt when the host sends
  35211. + * too much data, implementing this would cause an unavoidable race.
  35212. + * The driver will always use the "no-stall" approach for OUT transfers.
  35213. + *
  35214. + * One subtle point concerns sending status-stage responses for ep0
  35215. + * requests. Some of these requests, such as device reset, can involve
  35216. + * interrupting an ongoing file I/O operation, which might take an
  35217. + * arbitrarily long time. During that delay the host might give up on
  35218. + * the original ep0 request and issue a new one. When that happens the
  35219. + * driver should not notify the host about completion of the original
  35220. + * request, as the host will no longer be waiting for it. So the driver
  35221. + * assigns to each ep0 request a unique tag, and it keeps track of the
  35222. + * tag value of the request associated with a long-running exception
  35223. + * (device-reset, interface-change, or configuration-change). When the
  35224. + * exception handler is finished, the status-stage response is submitted
  35225. + * only if the current ep0 request tag is equal to the exception request
  35226. + * tag. Thus only the most recently received ep0 request will get a
  35227. + * status-stage response.
  35228. + *
  35229. + * Warning: This driver source file is too long. It ought to be split up
  35230. + * into a header file plus about 3 separate .c files, to handle the details
  35231. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  35232. + */
  35233. +
  35234. +
  35235. +/* #define VERBOSE_DEBUG */
  35236. +/* #define DUMP_MSGS */
  35237. +
  35238. +
  35239. +#include <linux/blkdev.h>
  35240. +#include <linux/completion.h>
  35241. +#include <linux/dcache.h>
  35242. +#include <linux/delay.h>
  35243. +#include <linux/device.h>
  35244. +#include <linux/fcntl.h>
  35245. +#include <linux/file.h>
  35246. +#include <linux/fs.h>
  35247. +#include <linux/kref.h>
  35248. +#include <linux/kthread.h>
  35249. +#include <linux/limits.h>
  35250. +#include <linux/module.h>
  35251. +#include <linux/rwsem.h>
  35252. +#include <linux/slab.h>
  35253. +#include <linux/spinlock.h>
  35254. +#include <linux/string.h>
  35255. +#include <linux/freezer.h>
  35256. +#include <linux/utsname.h>
  35257. +
  35258. +#include <linux/usb/ch9.h>
  35259. +#include <linux/usb/gadget.h>
  35260. +
  35261. +#include "gadget_chips.h"
  35262. +
  35263. +
  35264. +
  35265. +/*
  35266. + * Kbuild is not very cooperative with respect to linking separately
  35267. + * compiled library objects into one module. So for now we won't use
  35268. + * separate compilation ... ensuring init/exit sections work to shrink
  35269. + * the runtime footprint, and giving us at least some parts of what
  35270. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  35271. + */
  35272. +#include "usbstring.c"
  35273. +#include "config.c"
  35274. +#include "epautoconf.c"
  35275. +
  35276. +/*-------------------------------------------------------------------------*/
  35277. +
  35278. +#define DRIVER_DESC "File-backed Storage Gadget"
  35279. +#define DRIVER_NAME "g_file_storage"
  35280. +#define DRIVER_VERSION "1 September 2010"
  35281. +
  35282. +static char fsg_string_manufacturer[64];
  35283. +static const char fsg_string_product[] = DRIVER_DESC;
  35284. +static const char fsg_string_config[] = "Self-powered";
  35285. +static const char fsg_string_interface[] = "Mass Storage";
  35286. +
  35287. +
  35288. +#include "storage_common.c"
  35289. +
  35290. +
  35291. +MODULE_DESCRIPTION(DRIVER_DESC);
  35292. +MODULE_AUTHOR("Alan Stern");
  35293. +MODULE_LICENSE("Dual BSD/GPL");
  35294. +
  35295. +/*
  35296. + * This driver assumes self-powered hardware and has no way for users to
  35297. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  35298. + * and endpoint addresses.
  35299. + */
  35300. +
  35301. +
  35302. +/*-------------------------------------------------------------------------*/
  35303. +
  35304. +
  35305. +/* Encapsulate the module parameter settings */
  35306. +
  35307. +static struct {
  35308. + char *file[FSG_MAX_LUNS];
  35309. + char *serial;
  35310. + bool ro[FSG_MAX_LUNS];
  35311. + bool nofua[FSG_MAX_LUNS];
  35312. + unsigned int num_filenames;
  35313. + unsigned int num_ros;
  35314. + unsigned int num_nofuas;
  35315. + unsigned int nluns;
  35316. +
  35317. + bool removable;
  35318. + bool can_stall;
  35319. + bool cdrom;
  35320. +
  35321. + char *transport_parm;
  35322. + char *protocol_parm;
  35323. + unsigned short vendor;
  35324. + unsigned short product;
  35325. + unsigned short release;
  35326. + unsigned int buflen;
  35327. +
  35328. + int transport_type;
  35329. + char *transport_name;
  35330. + int protocol_type;
  35331. + char *protocol_name;
  35332. +
  35333. +} mod_data = { // Default values
  35334. + .transport_parm = "BBB",
  35335. + .protocol_parm = "SCSI",
  35336. + .removable = 0,
  35337. + .can_stall = 1,
  35338. + .cdrom = 0,
  35339. + .vendor = FSG_VENDOR_ID,
  35340. + .product = FSG_PRODUCT_ID,
  35341. + .release = 0xffff, // Use controller chip type
  35342. + .buflen = 16384,
  35343. + };
  35344. +
  35345. +
  35346. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  35347. + S_IRUGO);
  35348. +MODULE_PARM_DESC(file, "names of backing files or devices");
  35349. +
  35350. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  35351. +MODULE_PARM_DESC(serial, "USB serial number");
  35352. +
  35353. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  35354. +MODULE_PARM_DESC(ro, "true to force read-only");
  35355. +
  35356. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  35357. + S_IRUGO);
  35358. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  35359. +
  35360. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  35361. +MODULE_PARM_DESC(luns, "number of LUNs");
  35362. +
  35363. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  35364. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  35365. +
  35366. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  35367. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  35368. +
  35369. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  35370. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  35371. +
  35372. +/* In the non-TEST version, only the module parameters listed above
  35373. + * are available. */
  35374. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35375. +
  35376. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  35377. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  35378. +
  35379. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  35380. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  35381. + "8070, or SCSI)");
  35382. +
  35383. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  35384. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  35385. +
  35386. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  35387. +MODULE_PARM_DESC(product, "USB Product ID");
  35388. +
  35389. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  35390. +MODULE_PARM_DESC(release, "USB release number");
  35391. +
  35392. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  35393. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  35394. +
  35395. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35396. +
  35397. +
  35398. +/*
  35399. + * These definitions will permit the compiler to avoid generating code for
  35400. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  35401. + * can recognize when a test of a constant expression yields a dead code
  35402. + * path.
  35403. + */
  35404. +
  35405. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35406. +
  35407. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  35408. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  35409. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  35410. +
  35411. +#else
  35412. +
  35413. +#define transport_is_bbb() 1
  35414. +#define transport_is_cbi() 0
  35415. +#define protocol_is_scsi() 1
  35416. +
  35417. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35418. +
  35419. +
  35420. +/*-------------------------------------------------------------------------*/
  35421. +
  35422. +
  35423. +struct fsg_dev {
  35424. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  35425. + spinlock_t lock;
  35426. + struct usb_gadget *gadget;
  35427. +
  35428. + /* filesem protects: backing files in use */
  35429. + struct rw_semaphore filesem;
  35430. +
  35431. + /* reference counting: wait until all LUNs are released */
  35432. + struct kref ref;
  35433. +
  35434. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  35435. + struct usb_request *ep0req; // For control responses
  35436. + unsigned int ep0_req_tag;
  35437. + const char *ep0req_name;
  35438. +
  35439. + struct usb_request *intreq; // For interrupt responses
  35440. + int intreq_busy;
  35441. + struct fsg_buffhd *intr_buffhd;
  35442. +
  35443. + unsigned int bulk_out_maxpacket;
  35444. + enum fsg_state state; // For exception handling
  35445. + unsigned int exception_req_tag;
  35446. +
  35447. + u8 config, new_config;
  35448. +
  35449. + unsigned int running : 1;
  35450. + unsigned int bulk_in_enabled : 1;
  35451. + unsigned int bulk_out_enabled : 1;
  35452. + unsigned int intr_in_enabled : 1;
  35453. + unsigned int phase_error : 1;
  35454. + unsigned int short_packet_received : 1;
  35455. + unsigned int bad_lun_okay : 1;
  35456. +
  35457. + unsigned long atomic_bitflags;
  35458. +#define REGISTERED 0
  35459. +#define IGNORE_BULK_OUT 1
  35460. +#define SUSPENDED 2
  35461. +
  35462. + struct usb_ep *bulk_in;
  35463. + struct usb_ep *bulk_out;
  35464. + struct usb_ep *intr_in;
  35465. +
  35466. + struct fsg_buffhd *next_buffhd_to_fill;
  35467. + struct fsg_buffhd *next_buffhd_to_drain;
  35468. +
  35469. + int thread_wakeup_needed;
  35470. + struct completion thread_notifier;
  35471. + struct task_struct *thread_task;
  35472. +
  35473. + int cmnd_size;
  35474. + u8 cmnd[MAX_COMMAND_SIZE];
  35475. + enum data_direction data_dir;
  35476. + u32 data_size;
  35477. + u32 data_size_from_cmnd;
  35478. + u32 tag;
  35479. + unsigned int lun;
  35480. + u32 residue;
  35481. + u32 usb_amount_left;
  35482. +
  35483. + /* The CB protocol offers no way for a host to know when a command
  35484. + * has completed. As a result the next command may arrive early,
  35485. + * and we will still have to handle it. For that reason we need
  35486. + * a buffer to store new commands when using CB (or CBI, which
  35487. + * does not oblige a host to wait for command completion either). */
  35488. + int cbbuf_cmnd_size;
  35489. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  35490. +
  35491. + unsigned int nluns;
  35492. + struct fsg_lun *luns;
  35493. + struct fsg_lun *curlun;
  35494. + /* Must be the last entry */
  35495. + struct fsg_buffhd buffhds[];
  35496. +};
  35497. +
  35498. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  35499. +
  35500. +static int exception_in_progress(struct fsg_dev *fsg)
  35501. +{
  35502. + return (fsg->state > FSG_STATE_IDLE);
  35503. +}
  35504. +
  35505. +/* Make bulk-out requests be divisible by the maxpacket size */
  35506. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  35507. + struct fsg_buffhd *bh, unsigned int length)
  35508. +{
  35509. + unsigned int rem;
  35510. +
  35511. + bh->bulk_out_intended_length = length;
  35512. + rem = length % fsg->bulk_out_maxpacket;
  35513. + if (rem > 0)
  35514. + length += fsg->bulk_out_maxpacket - rem;
  35515. + bh->outreq->length = length;
  35516. +}
  35517. +
  35518. +static struct fsg_dev *the_fsg;
  35519. +static struct usb_gadget_driver fsg_driver;
  35520. +
  35521. +
  35522. +/*-------------------------------------------------------------------------*/
  35523. +
  35524. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  35525. +{
  35526. + const char *name;
  35527. +
  35528. + if (ep == fsg->bulk_in)
  35529. + name = "bulk-in";
  35530. + else if (ep == fsg->bulk_out)
  35531. + name = "bulk-out";
  35532. + else
  35533. + name = ep->name;
  35534. + DBG(fsg, "%s set halt\n", name);
  35535. + return usb_ep_set_halt(ep);
  35536. +}
  35537. +
  35538. +
  35539. +/*-------------------------------------------------------------------------*/
  35540. +
  35541. +/*
  35542. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  35543. + * descriptors are built on demand. Also the (static) config and interface
  35544. + * descriptors are adjusted during fsg_bind().
  35545. + */
  35546. +
  35547. +/* There is only one configuration. */
  35548. +#define CONFIG_VALUE 1
  35549. +
  35550. +static struct usb_device_descriptor
  35551. +device_desc = {
  35552. + .bLength = sizeof device_desc,
  35553. + .bDescriptorType = USB_DT_DEVICE,
  35554. +
  35555. + .bcdUSB = cpu_to_le16(0x0200),
  35556. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  35557. +
  35558. + /* The next three values can be overridden by module parameters */
  35559. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  35560. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  35561. + .bcdDevice = cpu_to_le16(0xffff),
  35562. +
  35563. + .iManufacturer = FSG_STRING_MANUFACTURER,
  35564. + .iProduct = FSG_STRING_PRODUCT,
  35565. + .iSerialNumber = FSG_STRING_SERIAL,
  35566. + .bNumConfigurations = 1,
  35567. +};
  35568. +
  35569. +static struct usb_config_descriptor
  35570. +config_desc = {
  35571. + .bLength = sizeof config_desc,
  35572. + .bDescriptorType = USB_DT_CONFIG,
  35573. +
  35574. + /* wTotalLength computed by usb_gadget_config_buf() */
  35575. + .bNumInterfaces = 1,
  35576. + .bConfigurationValue = CONFIG_VALUE,
  35577. + .iConfiguration = FSG_STRING_CONFIG,
  35578. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  35579. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  35580. +};
  35581. +
  35582. +
  35583. +static struct usb_qualifier_descriptor
  35584. +dev_qualifier = {
  35585. + .bLength = sizeof dev_qualifier,
  35586. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  35587. +
  35588. + .bcdUSB = cpu_to_le16(0x0200),
  35589. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  35590. +
  35591. + .bNumConfigurations = 1,
  35592. +};
  35593. +
  35594. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  35595. +{
  35596. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  35597. + buf += USB_DT_BOS_SIZE;
  35598. +
  35599. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  35600. + buf += USB_DT_USB_EXT_CAP_SIZE;
  35601. +
  35602. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  35603. +
  35604. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  35605. + + USB_DT_USB_EXT_CAP_SIZE;
  35606. +}
  35607. +
  35608. +/*
  35609. + * Config descriptors must agree with the code that sets configurations
  35610. + * and with code managing interfaces and their altsettings. They must
  35611. + * also handle different speeds and other-speed requests.
  35612. + */
  35613. +static int populate_config_buf(struct usb_gadget *gadget,
  35614. + u8 *buf, u8 type, unsigned index)
  35615. +{
  35616. + enum usb_device_speed speed = gadget->speed;
  35617. + int len;
  35618. + const struct usb_descriptor_header **function;
  35619. +
  35620. + if (index > 0)
  35621. + return -EINVAL;
  35622. +
  35623. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  35624. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  35625. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  35626. + ? (const struct usb_descriptor_header **)fsg_hs_function
  35627. + : (const struct usb_descriptor_header **)fsg_fs_function;
  35628. +
  35629. + /* for now, don't advertise srp-only devices */
  35630. + if (!gadget_is_otg(gadget))
  35631. + function++;
  35632. +
  35633. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  35634. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  35635. + return len;
  35636. +}
  35637. +
  35638. +
  35639. +/*-------------------------------------------------------------------------*/
  35640. +
  35641. +/* These routines may be called in process context or in_irq */
  35642. +
  35643. +/* Caller must hold fsg->lock */
  35644. +static void wakeup_thread(struct fsg_dev *fsg)
  35645. +{
  35646. + /* Tell the main thread that something has happened */
  35647. + fsg->thread_wakeup_needed = 1;
  35648. + if (fsg->thread_task)
  35649. + wake_up_process(fsg->thread_task);
  35650. +}
  35651. +
  35652. +
  35653. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  35654. +{
  35655. + unsigned long flags;
  35656. +
  35657. + /* Do nothing if a higher-priority exception is already in progress.
  35658. + * If a lower-or-equal priority exception is in progress, preempt it
  35659. + * and notify the main thread by sending it a signal. */
  35660. + spin_lock_irqsave(&fsg->lock, flags);
  35661. + if (fsg->state <= new_state) {
  35662. + fsg->exception_req_tag = fsg->ep0_req_tag;
  35663. + fsg->state = new_state;
  35664. + if (fsg->thread_task)
  35665. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  35666. + fsg->thread_task);
  35667. + }
  35668. + spin_unlock_irqrestore(&fsg->lock, flags);
  35669. +}
  35670. +
  35671. +
  35672. +/*-------------------------------------------------------------------------*/
  35673. +
  35674. +/* The disconnect callback and ep0 routines. These always run in_irq,
  35675. + * except that ep0_queue() is called in the main thread to acknowledge
  35676. + * completion of various requests: set config, set interface, and
  35677. + * Bulk-only device reset. */
  35678. +
  35679. +static void fsg_disconnect(struct usb_gadget *gadget)
  35680. +{
  35681. + struct fsg_dev *fsg = get_gadget_data(gadget);
  35682. +
  35683. + DBG(fsg, "disconnect or port reset\n");
  35684. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  35685. +}
  35686. +
  35687. +
  35688. +static int ep0_queue(struct fsg_dev *fsg)
  35689. +{
  35690. + int rc;
  35691. +
  35692. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  35693. + if (rc != 0 && rc != -ESHUTDOWN) {
  35694. +
  35695. + /* We can't do much more than wait for a reset */
  35696. + WARNING(fsg, "error in submission: %s --> %d\n",
  35697. + fsg->ep0->name, rc);
  35698. + }
  35699. + return rc;
  35700. +}
  35701. +
  35702. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  35703. +{
  35704. + struct fsg_dev *fsg = ep->driver_data;
  35705. +
  35706. + if (req->actual > 0)
  35707. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  35708. + if (req->status || req->actual != req->length)
  35709. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35710. + req->status, req->actual, req->length);
  35711. + if (req->status == -ECONNRESET) // Request was cancelled
  35712. + usb_ep_fifo_flush(ep);
  35713. +
  35714. + if (req->status == 0 && req->context)
  35715. + ((fsg_routine_t) (req->context))(fsg);
  35716. +}
  35717. +
  35718. +
  35719. +/*-------------------------------------------------------------------------*/
  35720. +
  35721. +/* Bulk and interrupt endpoint completion handlers.
  35722. + * These always run in_irq. */
  35723. +
  35724. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  35725. +{
  35726. + struct fsg_dev *fsg = ep->driver_data;
  35727. + struct fsg_buffhd *bh = req->context;
  35728. +
  35729. + if (req->status || req->actual != req->length)
  35730. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35731. + req->status, req->actual, req->length);
  35732. + if (req->status == -ECONNRESET) // Request was cancelled
  35733. + usb_ep_fifo_flush(ep);
  35734. +
  35735. + /* Hold the lock while we update the request and buffer states */
  35736. + smp_wmb();
  35737. + spin_lock(&fsg->lock);
  35738. + bh->inreq_busy = 0;
  35739. + bh->state = BUF_STATE_EMPTY;
  35740. + wakeup_thread(fsg);
  35741. + spin_unlock(&fsg->lock);
  35742. +}
  35743. +
  35744. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  35745. +{
  35746. + struct fsg_dev *fsg = ep->driver_data;
  35747. + struct fsg_buffhd *bh = req->context;
  35748. +
  35749. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  35750. + if (req->status || req->actual != bh->bulk_out_intended_length)
  35751. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35752. + req->status, req->actual,
  35753. + bh->bulk_out_intended_length);
  35754. + if (req->status == -ECONNRESET) // Request was cancelled
  35755. + usb_ep_fifo_flush(ep);
  35756. +
  35757. + /* Hold the lock while we update the request and buffer states */
  35758. + smp_wmb();
  35759. + spin_lock(&fsg->lock);
  35760. + bh->outreq_busy = 0;
  35761. + bh->state = BUF_STATE_FULL;
  35762. + wakeup_thread(fsg);
  35763. + spin_unlock(&fsg->lock);
  35764. +}
  35765. +
  35766. +
  35767. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35768. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  35769. +{
  35770. + struct fsg_dev *fsg = ep->driver_data;
  35771. + struct fsg_buffhd *bh = req->context;
  35772. +
  35773. + if (req->status || req->actual != req->length)
  35774. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35775. + req->status, req->actual, req->length);
  35776. + if (req->status == -ECONNRESET) // Request was cancelled
  35777. + usb_ep_fifo_flush(ep);
  35778. +
  35779. + /* Hold the lock while we update the request and buffer states */
  35780. + smp_wmb();
  35781. + spin_lock(&fsg->lock);
  35782. + fsg->intreq_busy = 0;
  35783. + bh->state = BUF_STATE_EMPTY;
  35784. + wakeup_thread(fsg);
  35785. + spin_unlock(&fsg->lock);
  35786. +}
  35787. +
  35788. +#else
  35789. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  35790. +{}
  35791. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35792. +
  35793. +
  35794. +/*-------------------------------------------------------------------------*/
  35795. +
  35796. +/* Ep0 class-specific handlers. These always run in_irq. */
  35797. +
  35798. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35799. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35800. +{
  35801. + struct usb_request *req = fsg->ep0req;
  35802. + static u8 cbi_reset_cmnd[6] = {
  35803. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  35804. +
  35805. + /* Error in command transfer? */
  35806. + if (req->status || req->length != req->actual ||
  35807. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  35808. +
  35809. + /* Not all controllers allow a protocol stall after
  35810. + * receiving control-out data, but we'll try anyway. */
  35811. + fsg_set_halt(fsg, fsg->ep0);
  35812. + return; // Wait for reset
  35813. + }
  35814. +
  35815. + /* Is it the special reset command? */
  35816. + if (req->actual >= sizeof cbi_reset_cmnd &&
  35817. + memcmp(req->buf, cbi_reset_cmnd,
  35818. + sizeof cbi_reset_cmnd) == 0) {
  35819. +
  35820. + /* Raise an exception to stop the current operation
  35821. + * and reinitialize our state. */
  35822. + DBG(fsg, "cbi reset request\n");
  35823. + raise_exception(fsg, FSG_STATE_RESET);
  35824. + return;
  35825. + }
  35826. +
  35827. + VDBG(fsg, "CB[I] accept device-specific command\n");
  35828. + spin_lock(&fsg->lock);
  35829. +
  35830. + /* Save the command for later */
  35831. + if (fsg->cbbuf_cmnd_size)
  35832. + WARNING(fsg, "CB[I] overwriting previous command\n");
  35833. + fsg->cbbuf_cmnd_size = req->actual;
  35834. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  35835. +
  35836. + wakeup_thread(fsg);
  35837. + spin_unlock(&fsg->lock);
  35838. +}
  35839. +
  35840. +#else
  35841. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35842. +{}
  35843. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35844. +
  35845. +
  35846. +static int class_setup_req(struct fsg_dev *fsg,
  35847. + const struct usb_ctrlrequest *ctrl)
  35848. +{
  35849. + struct usb_request *req = fsg->ep0req;
  35850. + int value = -EOPNOTSUPP;
  35851. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  35852. + u16 w_value = le16_to_cpu(ctrl->wValue);
  35853. + u16 w_length = le16_to_cpu(ctrl->wLength);
  35854. +
  35855. + if (!fsg->config)
  35856. + return value;
  35857. +
  35858. + /* Handle Bulk-only class-specific requests */
  35859. + if (transport_is_bbb()) {
  35860. + switch (ctrl->bRequest) {
  35861. +
  35862. + case US_BULK_RESET_REQUEST:
  35863. + if (ctrl->bRequestType != (USB_DIR_OUT |
  35864. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35865. + break;
  35866. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  35867. + value = -EDOM;
  35868. + break;
  35869. + }
  35870. +
  35871. + /* Raise an exception to stop the current operation
  35872. + * and reinitialize our state. */
  35873. + DBG(fsg, "bulk reset request\n");
  35874. + raise_exception(fsg, FSG_STATE_RESET);
  35875. + value = DELAYED_STATUS;
  35876. + break;
  35877. +
  35878. + case US_BULK_GET_MAX_LUN:
  35879. + if (ctrl->bRequestType != (USB_DIR_IN |
  35880. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35881. + break;
  35882. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  35883. + value = -EDOM;
  35884. + break;
  35885. + }
  35886. + VDBG(fsg, "get max LUN\n");
  35887. + *(u8 *) req->buf = fsg->nluns - 1;
  35888. + value = 1;
  35889. + break;
  35890. + }
  35891. + }
  35892. +
  35893. + /* Handle CBI class-specific requests */
  35894. + else {
  35895. + switch (ctrl->bRequest) {
  35896. +
  35897. + case USB_CBI_ADSC_REQUEST:
  35898. + if (ctrl->bRequestType != (USB_DIR_OUT |
  35899. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35900. + break;
  35901. + if (w_index != 0 || w_value != 0) {
  35902. + value = -EDOM;
  35903. + break;
  35904. + }
  35905. + if (w_length > MAX_COMMAND_SIZE) {
  35906. + value = -EOVERFLOW;
  35907. + break;
  35908. + }
  35909. + value = w_length;
  35910. + fsg->ep0req->context = received_cbi_adsc;
  35911. + break;
  35912. + }
  35913. + }
  35914. +
  35915. + if (value == -EOPNOTSUPP)
  35916. + VDBG(fsg,
  35917. + "unknown class-specific control req "
  35918. + "%02x.%02x v%04x i%04x l%u\n",
  35919. + ctrl->bRequestType, ctrl->bRequest,
  35920. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  35921. + return value;
  35922. +}
  35923. +
  35924. +
  35925. +/*-------------------------------------------------------------------------*/
  35926. +
  35927. +/* Ep0 standard request handlers. These always run in_irq. */
  35928. +
  35929. +static int standard_setup_req(struct fsg_dev *fsg,
  35930. + const struct usb_ctrlrequest *ctrl)
  35931. +{
  35932. + struct usb_request *req = fsg->ep0req;
  35933. + int value = -EOPNOTSUPP;
  35934. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  35935. + u16 w_value = le16_to_cpu(ctrl->wValue);
  35936. +
  35937. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  35938. + * but config change events will also reconfigure hardware. */
  35939. + switch (ctrl->bRequest) {
  35940. +
  35941. + case USB_REQ_GET_DESCRIPTOR:
  35942. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  35943. + USB_RECIP_DEVICE))
  35944. + break;
  35945. + switch (w_value >> 8) {
  35946. +
  35947. + case USB_DT_DEVICE:
  35948. + VDBG(fsg, "get device descriptor\n");
  35949. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  35950. + value = sizeof device_desc;
  35951. + memcpy(req->buf, &device_desc, value);
  35952. + break;
  35953. + case USB_DT_DEVICE_QUALIFIER:
  35954. + VDBG(fsg, "get device qualifier\n");
  35955. + if (!gadget_is_dualspeed(fsg->gadget) ||
  35956. + fsg->gadget->speed == USB_SPEED_SUPER)
  35957. + break;
  35958. + /*
  35959. + * Assume ep0 uses the same maxpacket value for both
  35960. + * speeds
  35961. + */
  35962. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  35963. + value = sizeof dev_qualifier;
  35964. + memcpy(req->buf, &dev_qualifier, value);
  35965. + break;
  35966. +
  35967. + case USB_DT_OTHER_SPEED_CONFIG:
  35968. + VDBG(fsg, "get other-speed config descriptor\n");
  35969. + if (!gadget_is_dualspeed(fsg->gadget) ||
  35970. + fsg->gadget->speed == USB_SPEED_SUPER)
  35971. + break;
  35972. + goto get_config;
  35973. + case USB_DT_CONFIG:
  35974. + VDBG(fsg, "get configuration descriptor\n");
  35975. +get_config:
  35976. + value = populate_config_buf(fsg->gadget,
  35977. + req->buf,
  35978. + w_value >> 8,
  35979. + w_value & 0xff);
  35980. + break;
  35981. +
  35982. + case USB_DT_STRING:
  35983. + VDBG(fsg, "get string descriptor\n");
  35984. +
  35985. + /* wIndex == language code */
  35986. + value = usb_gadget_get_string(&fsg_stringtab,
  35987. + w_value & 0xff, req->buf);
  35988. + break;
  35989. +
  35990. + case USB_DT_BOS:
  35991. + VDBG(fsg, "get bos descriptor\n");
  35992. +
  35993. + if (gadget_is_superspeed(fsg->gadget))
  35994. + value = populate_bos(fsg, req->buf);
  35995. + break;
  35996. + }
  35997. +
  35998. + break;
  35999. +
  36000. + /* One config, two speeds */
  36001. + case USB_REQ_SET_CONFIGURATION:
  36002. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  36003. + USB_RECIP_DEVICE))
  36004. + break;
  36005. + VDBG(fsg, "set configuration\n");
  36006. + if (w_value == CONFIG_VALUE || w_value == 0) {
  36007. + fsg->new_config = w_value;
  36008. +
  36009. + /* Raise an exception to wipe out previous transaction
  36010. + * state (queued bufs, etc) and set the new config. */
  36011. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  36012. + value = DELAYED_STATUS;
  36013. + }
  36014. + break;
  36015. + case USB_REQ_GET_CONFIGURATION:
  36016. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  36017. + USB_RECIP_DEVICE))
  36018. + break;
  36019. + VDBG(fsg, "get configuration\n");
  36020. + *(u8 *) req->buf = fsg->config;
  36021. + value = 1;
  36022. + break;
  36023. +
  36024. + case USB_REQ_SET_INTERFACE:
  36025. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  36026. + USB_RECIP_INTERFACE))
  36027. + break;
  36028. + if (fsg->config && w_index == 0) {
  36029. +
  36030. + /* Raise an exception to wipe out previous transaction
  36031. + * state (queued bufs, etc) and install the new
  36032. + * interface altsetting. */
  36033. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  36034. + value = DELAYED_STATUS;
  36035. + }
  36036. + break;
  36037. + case USB_REQ_GET_INTERFACE:
  36038. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  36039. + USB_RECIP_INTERFACE))
  36040. + break;
  36041. + if (!fsg->config)
  36042. + break;
  36043. + if (w_index != 0) {
  36044. + value = -EDOM;
  36045. + break;
  36046. + }
  36047. + VDBG(fsg, "get interface\n");
  36048. + *(u8 *) req->buf = 0;
  36049. + value = 1;
  36050. + break;
  36051. +
  36052. + default:
  36053. + VDBG(fsg,
  36054. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  36055. + ctrl->bRequestType, ctrl->bRequest,
  36056. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  36057. + }
  36058. +
  36059. + return value;
  36060. +}
  36061. +
  36062. +
  36063. +static int fsg_setup(struct usb_gadget *gadget,
  36064. + const struct usb_ctrlrequest *ctrl)
  36065. +{
  36066. + struct fsg_dev *fsg = get_gadget_data(gadget);
  36067. + int rc;
  36068. + int w_length = le16_to_cpu(ctrl->wLength);
  36069. +
  36070. + ++fsg->ep0_req_tag; // Record arrival of a new request
  36071. + fsg->ep0req->context = NULL;
  36072. + fsg->ep0req->length = 0;
  36073. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  36074. +
  36075. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  36076. + rc = class_setup_req(fsg, ctrl);
  36077. + else
  36078. + rc = standard_setup_req(fsg, ctrl);
  36079. +
  36080. + /* Respond with data/status or defer until later? */
  36081. + if (rc >= 0 && rc != DELAYED_STATUS) {
  36082. + rc = min(rc, w_length);
  36083. + fsg->ep0req->length = rc;
  36084. + fsg->ep0req->zero = rc < w_length;
  36085. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  36086. + "ep0-in" : "ep0-out");
  36087. + rc = ep0_queue(fsg);
  36088. + }
  36089. +
  36090. + /* Device either stalls (rc < 0) or reports success */
  36091. + return rc;
  36092. +}
  36093. +
  36094. +
  36095. +/*-------------------------------------------------------------------------*/
  36096. +
  36097. +/* All the following routines run in process context */
  36098. +
  36099. +
  36100. +/* Use this for bulk or interrupt transfers, not ep0 */
  36101. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  36102. + struct usb_request *req, int *pbusy,
  36103. + enum fsg_buffer_state *state)
  36104. +{
  36105. + int rc;
  36106. +
  36107. + if (ep == fsg->bulk_in)
  36108. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  36109. + else if (ep == fsg->intr_in)
  36110. + dump_msg(fsg, "intr-in", req->buf, req->length);
  36111. +
  36112. + spin_lock_irq(&fsg->lock);
  36113. + *pbusy = 1;
  36114. + *state = BUF_STATE_BUSY;
  36115. + spin_unlock_irq(&fsg->lock);
  36116. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  36117. + if (rc != 0) {
  36118. + *pbusy = 0;
  36119. + *state = BUF_STATE_EMPTY;
  36120. +
  36121. + /* We can't do much more than wait for a reset */
  36122. +
  36123. + /* Note: currently the net2280 driver fails zero-length
  36124. + * submissions if DMA is enabled. */
  36125. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  36126. + req->length == 0))
  36127. + WARNING(fsg, "error in submission: %s --> %d\n",
  36128. + ep->name, rc);
  36129. + }
  36130. +}
  36131. +
  36132. +
  36133. +static int sleep_thread(struct fsg_dev *fsg)
  36134. +{
  36135. + int rc = 0;
  36136. +
  36137. + /* Wait until a signal arrives or we are woken up */
  36138. + for (;;) {
  36139. + try_to_freeze();
  36140. + set_current_state(TASK_INTERRUPTIBLE);
  36141. + if (signal_pending(current)) {
  36142. + rc = -EINTR;
  36143. + break;
  36144. + }
  36145. + if (fsg->thread_wakeup_needed)
  36146. + break;
  36147. + schedule();
  36148. + }
  36149. + __set_current_state(TASK_RUNNING);
  36150. + fsg->thread_wakeup_needed = 0;
  36151. + return rc;
  36152. +}
  36153. +
  36154. +
  36155. +/*-------------------------------------------------------------------------*/
  36156. +
  36157. +static int do_read(struct fsg_dev *fsg)
  36158. +{
  36159. + struct fsg_lun *curlun = fsg->curlun;
  36160. + u32 lba;
  36161. + struct fsg_buffhd *bh;
  36162. + int rc;
  36163. + u32 amount_left;
  36164. + loff_t file_offset, file_offset_tmp;
  36165. + unsigned int amount;
  36166. + ssize_t nread;
  36167. +
  36168. + /* Get the starting Logical Block Address and check that it's
  36169. + * not too big */
  36170. + if (fsg->cmnd[0] == READ_6)
  36171. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  36172. + else {
  36173. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  36174. +
  36175. + /* We allow DPO (Disable Page Out = don't save data in the
  36176. + * cache) and FUA (Force Unit Access = don't read from the
  36177. + * cache), but we don't implement them. */
  36178. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  36179. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36180. + return -EINVAL;
  36181. + }
  36182. + }
  36183. + if (lba >= curlun->num_sectors) {
  36184. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36185. + return -EINVAL;
  36186. + }
  36187. + file_offset = ((loff_t) lba) << curlun->blkbits;
  36188. +
  36189. + /* Carry out the file reads */
  36190. + amount_left = fsg->data_size_from_cmnd;
  36191. + if (unlikely(amount_left == 0))
  36192. + return -EIO; // No default reply
  36193. +
  36194. + for (;;) {
  36195. +
  36196. + /* Figure out how much we need to read:
  36197. + * Try to read the remaining amount.
  36198. + * But don't read more than the buffer size.
  36199. + * And don't try to read past the end of the file.
  36200. + */
  36201. + amount = min((unsigned int) amount_left, mod_data.buflen);
  36202. + amount = min((loff_t) amount,
  36203. + curlun->file_length - file_offset);
  36204. +
  36205. + /* Wait for the next buffer to become available */
  36206. + bh = fsg->next_buffhd_to_fill;
  36207. + while (bh->state != BUF_STATE_EMPTY) {
  36208. + rc = sleep_thread(fsg);
  36209. + if (rc)
  36210. + return rc;
  36211. + }
  36212. +
  36213. + /* If we were asked to read past the end of file,
  36214. + * end with an empty buffer. */
  36215. + if (amount == 0) {
  36216. + curlun->sense_data =
  36217. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36218. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36219. + curlun->info_valid = 1;
  36220. + bh->inreq->length = 0;
  36221. + bh->state = BUF_STATE_FULL;
  36222. + break;
  36223. + }
  36224. +
  36225. + /* Perform the read */
  36226. + file_offset_tmp = file_offset;
  36227. + nread = vfs_read(curlun->filp,
  36228. + (char __user *) bh->buf,
  36229. + amount, &file_offset_tmp);
  36230. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  36231. + (unsigned long long) file_offset,
  36232. + (int) nread);
  36233. + if (signal_pending(current))
  36234. + return -EINTR;
  36235. +
  36236. + if (nread < 0) {
  36237. + LDBG(curlun, "error in file read: %d\n",
  36238. + (int) nread);
  36239. + nread = 0;
  36240. + } else if (nread < amount) {
  36241. + LDBG(curlun, "partial file read: %d/%u\n",
  36242. + (int) nread, amount);
  36243. + nread = round_down(nread, curlun->blksize);
  36244. + }
  36245. + file_offset += nread;
  36246. + amount_left -= nread;
  36247. + fsg->residue -= nread;
  36248. +
  36249. + /* Except at the end of the transfer, nread will be
  36250. + * equal to the buffer size, which is divisible by the
  36251. + * bulk-in maxpacket size.
  36252. + */
  36253. + bh->inreq->length = nread;
  36254. + bh->state = BUF_STATE_FULL;
  36255. +
  36256. + /* If an error occurred, report it and its position */
  36257. + if (nread < amount) {
  36258. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  36259. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36260. + curlun->info_valid = 1;
  36261. + break;
  36262. + }
  36263. +
  36264. + if (amount_left == 0)
  36265. + break; // No more left to read
  36266. +
  36267. + /* Send this buffer and go read some more */
  36268. + bh->inreq->zero = 0;
  36269. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  36270. + &bh->inreq_busy, &bh->state);
  36271. + fsg->next_buffhd_to_fill = bh->next;
  36272. + }
  36273. +
  36274. + return -EIO; // No default reply
  36275. +}
  36276. +
  36277. +
  36278. +/*-------------------------------------------------------------------------*/
  36279. +
  36280. +static int do_write(struct fsg_dev *fsg)
  36281. +{
  36282. + struct fsg_lun *curlun = fsg->curlun;
  36283. + u32 lba;
  36284. + struct fsg_buffhd *bh;
  36285. + int get_some_more;
  36286. + u32 amount_left_to_req, amount_left_to_write;
  36287. + loff_t usb_offset, file_offset, file_offset_tmp;
  36288. + unsigned int amount;
  36289. + ssize_t nwritten;
  36290. + int rc;
  36291. +
  36292. + if (curlun->ro) {
  36293. + curlun->sense_data = SS_WRITE_PROTECTED;
  36294. + return -EINVAL;
  36295. + }
  36296. + spin_lock(&curlun->filp->f_lock);
  36297. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  36298. + spin_unlock(&curlun->filp->f_lock);
  36299. +
  36300. + /* Get the starting Logical Block Address and check that it's
  36301. + * not too big */
  36302. + if (fsg->cmnd[0] == WRITE_6)
  36303. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  36304. + else {
  36305. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  36306. +
  36307. + /* We allow DPO (Disable Page Out = don't save data in the
  36308. + * cache) and FUA (Force Unit Access = write directly to the
  36309. + * medium). We don't implement DPO; we implement FUA by
  36310. + * performing synchronous output. */
  36311. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  36312. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36313. + return -EINVAL;
  36314. + }
  36315. + /* FUA */
  36316. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  36317. + spin_lock(&curlun->filp->f_lock);
  36318. + curlun->filp->f_flags |= O_DSYNC;
  36319. + spin_unlock(&curlun->filp->f_lock);
  36320. + }
  36321. + }
  36322. + if (lba >= curlun->num_sectors) {
  36323. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36324. + return -EINVAL;
  36325. + }
  36326. +
  36327. + /* Carry out the file writes */
  36328. + get_some_more = 1;
  36329. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  36330. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  36331. +
  36332. + while (amount_left_to_write > 0) {
  36333. +
  36334. + /* Queue a request for more data from the host */
  36335. + bh = fsg->next_buffhd_to_fill;
  36336. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  36337. +
  36338. + /* Figure out how much we want to get:
  36339. + * Try to get the remaining amount,
  36340. + * but not more than the buffer size.
  36341. + */
  36342. + amount = min(amount_left_to_req, mod_data.buflen);
  36343. +
  36344. + /* Beyond the end of the backing file? */
  36345. + if (usb_offset >= curlun->file_length) {
  36346. + get_some_more = 0;
  36347. + curlun->sense_data =
  36348. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36349. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  36350. + curlun->info_valid = 1;
  36351. + continue;
  36352. + }
  36353. +
  36354. + /* Get the next buffer */
  36355. + usb_offset += amount;
  36356. + fsg->usb_amount_left -= amount;
  36357. + amount_left_to_req -= amount;
  36358. + if (amount_left_to_req == 0)
  36359. + get_some_more = 0;
  36360. +
  36361. + /* Except at the end of the transfer, amount will be
  36362. + * equal to the buffer size, which is divisible by
  36363. + * the bulk-out maxpacket size.
  36364. + */
  36365. + set_bulk_out_req_length(fsg, bh, amount);
  36366. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  36367. + &bh->outreq_busy, &bh->state);
  36368. + fsg->next_buffhd_to_fill = bh->next;
  36369. + continue;
  36370. + }
  36371. +
  36372. + /* Write the received data to the backing file */
  36373. + bh = fsg->next_buffhd_to_drain;
  36374. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  36375. + break; // We stopped early
  36376. + if (bh->state == BUF_STATE_FULL) {
  36377. + smp_rmb();
  36378. + fsg->next_buffhd_to_drain = bh->next;
  36379. + bh->state = BUF_STATE_EMPTY;
  36380. +
  36381. + /* Did something go wrong with the transfer? */
  36382. + if (bh->outreq->status != 0) {
  36383. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  36384. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36385. + curlun->info_valid = 1;
  36386. + break;
  36387. + }
  36388. +
  36389. + amount = bh->outreq->actual;
  36390. + if (curlun->file_length - file_offset < amount) {
  36391. + LERROR(curlun,
  36392. + "write %u @ %llu beyond end %llu\n",
  36393. + amount, (unsigned long long) file_offset,
  36394. + (unsigned long long) curlun->file_length);
  36395. + amount = curlun->file_length - file_offset;
  36396. + }
  36397. +
  36398. + /* Don't accept excess data. The spec doesn't say
  36399. + * what to do in this case. We'll ignore the error.
  36400. + */
  36401. + amount = min(amount, bh->bulk_out_intended_length);
  36402. +
  36403. + /* Don't write a partial block */
  36404. + amount = round_down(amount, curlun->blksize);
  36405. + if (amount == 0)
  36406. + goto empty_write;
  36407. +
  36408. + /* Perform the write */
  36409. + file_offset_tmp = file_offset;
  36410. + nwritten = vfs_write(curlun->filp,
  36411. + (char __user *) bh->buf,
  36412. + amount, &file_offset_tmp);
  36413. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  36414. + (unsigned long long) file_offset,
  36415. + (int) nwritten);
  36416. + if (signal_pending(current))
  36417. + return -EINTR; // Interrupted!
  36418. +
  36419. + if (nwritten < 0) {
  36420. + LDBG(curlun, "error in file write: %d\n",
  36421. + (int) nwritten);
  36422. + nwritten = 0;
  36423. + } else if (nwritten < amount) {
  36424. + LDBG(curlun, "partial file write: %d/%u\n",
  36425. + (int) nwritten, amount);
  36426. + nwritten = round_down(nwritten, curlun->blksize);
  36427. + }
  36428. + file_offset += nwritten;
  36429. + amount_left_to_write -= nwritten;
  36430. + fsg->residue -= nwritten;
  36431. +
  36432. + /* If an error occurred, report it and its position */
  36433. + if (nwritten < amount) {
  36434. + curlun->sense_data = SS_WRITE_ERROR;
  36435. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36436. + curlun->info_valid = 1;
  36437. + break;
  36438. + }
  36439. +
  36440. + empty_write:
  36441. + /* Did the host decide to stop early? */
  36442. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  36443. + fsg->short_packet_received = 1;
  36444. + break;
  36445. + }
  36446. + continue;
  36447. + }
  36448. +
  36449. + /* Wait for something to happen */
  36450. + rc = sleep_thread(fsg);
  36451. + if (rc)
  36452. + return rc;
  36453. + }
  36454. +
  36455. + return -EIO; // No default reply
  36456. +}
  36457. +
  36458. +
  36459. +/*-------------------------------------------------------------------------*/
  36460. +
  36461. +static int do_synchronize_cache(struct fsg_dev *fsg)
  36462. +{
  36463. + struct fsg_lun *curlun = fsg->curlun;
  36464. + int rc;
  36465. +
  36466. + /* We ignore the requested LBA and write out all file's
  36467. + * dirty data buffers. */
  36468. + rc = fsg_lun_fsync_sub(curlun);
  36469. + if (rc)
  36470. + curlun->sense_data = SS_WRITE_ERROR;
  36471. + return 0;
  36472. +}
  36473. +
  36474. +
  36475. +/*-------------------------------------------------------------------------*/
  36476. +
  36477. +static void invalidate_sub(struct fsg_lun *curlun)
  36478. +{
  36479. + struct file *filp = curlun->filp;
  36480. + struct inode *inode = filp->f_path.dentry->d_inode;
  36481. + unsigned long rc;
  36482. +
  36483. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  36484. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  36485. +}
  36486. +
  36487. +static int do_verify(struct fsg_dev *fsg)
  36488. +{
  36489. + struct fsg_lun *curlun = fsg->curlun;
  36490. + u32 lba;
  36491. + u32 verification_length;
  36492. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  36493. + loff_t file_offset, file_offset_tmp;
  36494. + u32 amount_left;
  36495. + unsigned int amount;
  36496. + ssize_t nread;
  36497. +
  36498. + /* Get the starting Logical Block Address and check that it's
  36499. + * not too big */
  36500. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  36501. + if (lba >= curlun->num_sectors) {
  36502. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36503. + return -EINVAL;
  36504. + }
  36505. +
  36506. + /* We allow DPO (Disable Page Out = don't save data in the
  36507. + * cache) but we don't implement it. */
  36508. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  36509. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36510. + return -EINVAL;
  36511. + }
  36512. +
  36513. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  36514. + if (unlikely(verification_length == 0))
  36515. + return -EIO; // No default reply
  36516. +
  36517. + /* Prepare to carry out the file verify */
  36518. + amount_left = verification_length << curlun->blkbits;
  36519. + file_offset = ((loff_t) lba) << curlun->blkbits;
  36520. +
  36521. + /* Write out all the dirty buffers before invalidating them */
  36522. + fsg_lun_fsync_sub(curlun);
  36523. + if (signal_pending(current))
  36524. + return -EINTR;
  36525. +
  36526. + invalidate_sub(curlun);
  36527. + if (signal_pending(current))
  36528. + return -EINTR;
  36529. +
  36530. + /* Just try to read the requested blocks */
  36531. + while (amount_left > 0) {
  36532. +
  36533. + /* Figure out how much we need to read:
  36534. + * Try to read the remaining amount, but not more than
  36535. + * the buffer size.
  36536. + * And don't try to read past the end of the file.
  36537. + */
  36538. + amount = min((unsigned int) amount_left, mod_data.buflen);
  36539. + amount = min((loff_t) amount,
  36540. + curlun->file_length - file_offset);
  36541. + if (amount == 0) {
  36542. + curlun->sense_data =
  36543. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36544. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36545. + curlun->info_valid = 1;
  36546. + break;
  36547. + }
  36548. +
  36549. + /* Perform the read */
  36550. + file_offset_tmp = file_offset;
  36551. + nread = vfs_read(curlun->filp,
  36552. + (char __user *) bh->buf,
  36553. + amount, &file_offset_tmp);
  36554. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  36555. + (unsigned long long) file_offset,
  36556. + (int) nread);
  36557. + if (signal_pending(current))
  36558. + return -EINTR;
  36559. +
  36560. + if (nread < 0) {
  36561. + LDBG(curlun, "error in file verify: %d\n",
  36562. + (int) nread);
  36563. + nread = 0;
  36564. + } else if (nread < amount) {
  36565. + LDBG(curlun, "partial file verify: %d/%u\n",
  36566. + (int) nread, amount);
  36567. + nread = round_down(nread, curlun->blksize);
  36568. + }
  36569. + if (nread == 0) {
  36570. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  36571. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  36572. + curlun->info_valid = 1;
  36573. + break;
  36574. + }
  36575. + file_offset += nread;
  36576. + amount_left -= nread;
  36577. + }
  36578. + return 0;
  36579. +}
  36580. +
  36581. +
  36582. +/*-------------------------------------------------------------------------*/
  36583. +
  36584. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36585. +{
  36586. + u8 *buf = (u8 *) bh->buf;
  36587. +
  36588. + static char vendor_id[] = "Linux ";
  36589. + static char product_disk_id[] = "File-Stor Gadget";
  36590. + static char product_cdrom_id[] = "File-CD Gadget ";
  36591. +
  36592. + if (!fsg->curlun) { // Unsupported LUNs are okay
  36593. + fsg->bad_lun_okay = 1;
  36594. + memset(buf, 0, 36);
  36595. + buf[0] = 0x7f; // Unsupported, no device-type
  36596. + buf[4] = 31; // Additional length
  36597. + return 36;
  36598. + }
  36599. +
  36600. + memset(buf, 0, 8);
  36601. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  36602. + if (mod_data.removable)
  36603. + buf[1] = 0x80;
  36604. + buf[2] = 2; // ANSI SCSI level 2
  36605. + buf[3] = 2; // SCSI-2 INQUIRY data format
  36606. + buf[4] = 31; // Additional length
  36607. + // No special options
  36608. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  36609. + (mod_data.cdrom ? product_cdrom_id :
  36610. + product_disk_id),
  36611. + mod_data.release);
  36612. + return 36;
  36613. +}
  36614. +
  36615. +
  36616. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36617. +{
  36618. + struct fsg_lun *curlun = fsg->curlun;
  36619. + u8 *buf = (u8 *) bh->buf;
  36620. + u32 sd, sdinfo;
  36621. + int valid;
  36622. +
  36623. + /*
  36624. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  36625. + *
  36626. + * If a REQUEST SENSE command is received from an initiator
  36627. + * with a pending unit attention condition (before the target
  36628. + * generates the contingent allegiance condition), then the
  36629. + * target shall either:
  36630. + * a) report any pending sense data and preserve the unit
  36631. + * attention condition on the logical unit, or,
  36632. + * b) report the unit attention condition, may discard any
  36633. + * pending sense data, and clear the unit attention
  36634. + * condition on the logical unit for that initiator.
  36635. + *
  36636. + * FSG normally uses option a); enable this code to use option b).
  36637. + */
  36638. +#if 0
  36639. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  36640. + curlun->sense_data = curlun->unit_attention_data;
  36641. + curlun->unit_attention_data = SS_NO_SENSE;
  36642. + }
  36643. +#endif
  36644. +
  36645. + if (!curlun) { // Unsupported LUNs are okay
  36646. + fsg->bad_lun_okay = 1;
  36647. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  36648. + sdinfo = 0;
  36649. + valid = 0;
  36650. + } else {
  36651. + sd = curlun->sense_data;
  36652. + sdinfo = curlun->sense_data_info;
  36653. + valid = curlun->info_valid << 7;
  36654. + curlun->sense_data = SS_NO_SENSE;
  36655. + curlun->sense_data_info = 0;
  36656. + curlun->info_valid = 0;
  36657. + }
  36658. +
  36659. + memset(buf, 0, 18);
  36660. + buf[0] = valid | 0x70; // Valid, current error
  36661. + buf[2] = SK(sd);
  36662. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  36663. + buf[7] = 18 - 8; // Additional sense length
  36664. + buf[12] = ASC(sd);
  36665. + buf[13] = ASCQ(sd);
  36666. + return 18;
  36667. +}
  36668. +
  36669. +
  36670. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36671. +{
  36672. + struct fsg_lun *curlun = fsg->curlun;
  36673. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  36674. + int pmi = fsg->cmnd[8];
  36675. + u8 *buf = (u8 *) bh->buf;
  36676. +
  36677. + /* Check the PMI and LBA fields */
  36678. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  36679. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36680. + return -EINVAL;
  36681. + }
  36682. +
  36683. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  36684. + /* Max logical block */
  36685. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  36686. + return 8;
  36687. +}
  36688. +
  36689. +
  36690. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36691. +{
  36692. + struct fsg_lun *curlun = fsg->curlun;
  36693. + int msf = fsg->cmnd[1] & 0x02;
  36694. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  36695. + u8 *buf = (u8 *) bh->buf;
  36696. +
  36697. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  36698. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36699. + return -EINVAL;
  36700. + }
  36701. + if (lba >= curlun->num_sectors) {
  36702. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36703. + return -EINVAL;
  36704. + }
  36705. +
  36706. + memset(buf, 0, 8);
  36707. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  36708. + store_cdrom_address(&buf[4], msf, lba);
  36709. + return 8;
  36710. +}
  36711. +
  36712. +
  36713. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36714. +{
  36715. + struct fsg_lun *curlun = fsg->curlun;
  36716. + int msf = fsg->cmnd[1] & 0x02;
  36717. + int start_track = fsg->cmnd[6];
  36718. + u8 *buf = (u8 *) bh->buf;
  36719. +
  36720. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  36721. + start_track > 1) {
  36722. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36723. + return -EINVAL;
  36724. + }
  36725. +
  36726. + memset(buf, 0, 20);
  36727. + buf[1] = (20-2); /* TOC data length */
  36728. + buf[2] = 1; /* First track number */
  36729. + buf[3] = 1; /* Last track number */
  36730. + buf[5] = 0x16; /* Data track, copying allowed */
  36731. + buf[6] = 0x01; /* Only track is number 1 */
  36732. + store_cdrom_address(&buf[8], msf, 0);
  36733. +
  36734. + buf[13] = 0x16; /* Lead-out track is data */
  36735. + buf[14] = 0xAA; /* Lead-out track number */
  36736. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  36737. + return 20;
  36738. +}
  36739. +
  36740. +
  36741. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36742. +{
  36743. + struct fsg_lun *curlun = fsg->curlun;
  36744. + int mscmnd = fsg->cmnd[0];
  36745. + u8 *buf = (u8 *) bh->buf;
  36746. + u8 *buf0 = buf;
  36747. + int pc, page_code;
  36748. + int changeable_values, all_pages;
  36749. + int valid_page = 0;
  36750. + int len, limit;
  36751. +
  36752. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  36753. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36754. + return -EINVAL;
  36755. + }
  36756. + pc = fsg->cmnd[2] >> 6;
  36757. + page_code = fsg->cmnd[2] & 0x3f;
  36758. + if (pc == 3) {
  36759. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  36760. + return -EINVAL;
  36761. + }
  36762. + changeable_values = (pc == 1);
  36763. + all_pages = (page_code == 0x3f);
  36764. +
  36765. + /* Write the mode parameter header. Fixed values are: default
  36766. + * medium type, no cache control (DPOFUA), and no block descriptors.
  36767. + * The only variable value is the WriteProtect bit. We will fill in
  36768. + * the mode data length later. */
  36769. + memset(buf, 0, 8);
  36770. + if (mscmnd == MODE_SENSE) {
  36771. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  36772. + buf += 4;
  36773. + limit = 255;
  36774. + } else { // MODE_SENSE_10
  36775. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  36776. + buf += 8;
  36777. + limit = 65535; // Should really be mod_data.buflen
  36778. + }
  36779. +
  36780. + /* No block descriptors */
  36781. +
  36782. + /* The mode pages, in numerical order. The only page we support
  36783. + * is the Caching page. */
  36784. + if (page_code == 0x08 || all_pages) {
  36785. + valid_page = 1;
  36786. + buf[0] = 0x08; // Page code
  36787. + buf[1] = 10; // Page length
  36788. + memset(buf+2, 0, 10); // None of the fields are changeable
  36789. +
  36790. + if (!changeable_values) {
  36791. + buf[2] = 0x04; // Write cache enable,
  36792. + // Read cache not disabled
  36793. + // No cache retention priorities
  36794. + put_unaligned_be16(0xffff, &buf[4]);
  36795. + /* Don't disable prefetch */
  36796. + /* Minimum prefetch = 0 */
  36797. + put_unaligned_be16(0xffff, &buf[8]);
  36798. + /* Maximum prefetch */
  36799. + put_unaligned_be16(0xffff, &buf[10]);
  36800. + /* Maximum prefetch ceiling */
  36801. + }
  36802. + buf += 12;
  36803. + }
  36804. +
  36805. + /* Check that a valid page was requested and the mode data length
  36806. + * isn't too long. */
  36807. + len = buf - buf0;
  36808. + if (!valid_page || len > limit) {
  36809. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36810. + return -EINVAL;
  36811. + }
  36812. +
  36813. + /* Store the mode data length */
  36814. + if (mscmnd == MODE_SENSE)
  36815. + buf0[0] = len - 1;
  36816. + else
  36817. + put_unaligned_be16(len - 2, buf0);
  36818. + return len;
  36819. +}
  36820. +
  36821. +
  36822. +static int do_start_stop(struct fsg_dev *fsg)
  36823. +{
  36824. + struct fsg_lun *curlun = fsg->curlun;
  36825. + int loej, start;
  36826. +
  36827. + if (!mod_data.removable) {
  36828. + curlun->sense_data = SS_INVALID_COMMAND;
  36829. + return -EINVAL;
  36830. + }
  36831. +
  36832. + // int immed = fsg->cmnd[1] & 0x01;
  36833. + loej = fsg->cmnd[4] & 0x02;
  36834. + start = fsg->cmnd[4] & 0x01;
  36835. +
  36836. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  36837. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  36838. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  36839. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36840. + return -EINVAL;
  36841. + }
  36842. +
  36843. + if (!start) {
  36844. +
  36845. + /* Are we allowed to unload the media? */
  36846. + if (curlun->prevent_medium_removal) {
  36847. + LDBG(curlun, "unload attempt prevented\n");
  36848. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  36849. + return -EINVAL;
  36850. + }
  36851. + if (loej) { // Simulate an unload/eject
  36852. + up_read(&fsg->filesem);
  36853. + down_write(&fsg->filesem);
  36854. + fsg_lun_close(curlun);
  36855. + up_write(&fsg->filesem);
  36856. + down_read(&fsg->filesem);
  36857. + }
  36858. + } else {
  36859. +
  36860. + /* Our emulation doesn't support mounting; the medium is
  36861. + * available for use as soon as it is loaded. */
  36862. + if (!fsg_lun_is_open(curlun)) {
  36863. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  36864. + return -EINVAL;
  36865. + }
  36866. + }
  36867. +#endif
  36868. + return 0;
  36869. +}
  36870. +
  36871. +
  36872. +static int do_prevent_allow(struct fsg_dev *fsg)
  36873. +{
  36874. + struct fsg_lun *curlun = fsg->curlun;
  36875. + int prevent;
  36876. +
  36877. + if (!mod_data.removable) {
  36878. + curlun->sense_data = SS_INVALID_COMMAND;
  36879. + return -EINVAL;
  36880. + }
  36881. +
  36882. + prevent = fsg->cmnd[4] & 0x01;
  36883. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  36884. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36885. + return -EINVAL;
  36886. + }
  36887. +
  36888. + if (curlun->prevent_medium_removal && !prevent)
  36889. + fsg_lun_fsync_sub(curlun);
  36890. + curlun->prevent_medium_removal = prevent;
  36891. + return 0;
  36892. +}
  36893. +
  36894. +
  36895. +static int do_read_format_capacities(struct fsg_dev *fsg,
  36896. + struct fsg_buffhd *bh)
  36897. +{
  36898. + struct fsg_lun *curlun = fsg->curlun;
  36899. + u8 *buf = (u8 *) bh->buf;
  36900. +
  36901. + buf[0] = buf[1] = buf[2] = 0;
  36902. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  36903. + buf += 4;
  36904. +
  36905. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  36906. + /* Number of blocks */
  36907. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  36908. + buf[4] = 0x02; /* Current capacity */
  36909. + return 12;
  36910. +}
  36911. +
  36912. +
  36913. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36914. +{
  36915. + struct fsg_lun *curlun = fsg->curlun;
  36916. +
  36917. + /* We don't support MODE SELECT */
  36918. + curlun->sense_data = SS_INVALID_COMMAND;
  36919. + return -EINVAL;
  36920. +}
  36921. +
  36922. +
  36923. +/*-------------------------------------------------------------------------*/
  36924. +
  36925. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  36926. +{
  36927. + int rc;
  36928. +
  36929. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  36930. + if (rc == -EAGAIN)
  36931. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  36932. + while (rc != 0) {
  36933. + if (rc != -EAGAIN) {
  36934. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  36935. + rc = 0;
  36936. + break;
  36937. + }
  36938. +
  36939. + /* Wait for a short time and then try again */
  36940. + if (msleep_interruptible(100) != 0)
  36941. + return -EINTR;
  36942. + rc = usb_ep_set_halt(fsg->bulk_in);
  36943. + }
  36944. + return rc;
  36945. +}
  36946. +
  36947. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  36948. +{
  36949. + int rc;
  36950. +
  36951. + DBG(fsg, "bulk-in set wedge\n");
  36952. + rc = usb_ep_set_wedge(fsg->bulk_in);
  36953. + if (rc == -EAGAIN)
  36954. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  36955. + while (rc != 0) {
  36956. + if (rc != -EAGAIN) {
  36957. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  36958. + rc = 0;
  36959. + break;
  36960. + }
  36961. +
  36962. + /* Wait for a short time and then try again */
  36963. + if (msleep_interruptible(100) != 0)
  36964. + return -EINTR;
  36965. + rc = usb_ep_set_wedge(fsg->bulk_in);
  36966. + }
  36967. + return rc;
  36968. +}
  36969. +
  36970. +static int throw_away_data(struct fsg_dev *fsg)
  36971. +{
  36972. + struct fsg_buffhd *bh;
  36973. + u32 amount;
  36974. + int rc;
  36975. +
  36976. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  36977. + fsg->usb_amount_left > 0) {
  36978. +
  36979. + /* Throw away the data in a filled buffer */
  36980. + if (bh->state == BUF_STATE_FULL) {
  36981. + smp_rmb();
  36982. + bh->state = BUF_STATE_EMPTY;
  36983. + fsg->next_buffhd_to_drain = bh->next;
  36984. +
  36985. + /* A short packet or an error ends everything */
  36986. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  36987. + bh->outreq->status != 0) {
  36988. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  36989. + return -EINTR;
  36990. + }
  36991. + continue;
  36992. + }
  36993. +
  36994. + /* Try to submit another request if we need one */
  36995. + bh = fsg->next_buffhd_to_fill;
  36996. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  36997. + amount = min(fsg->usb_amount_left,
  36998. + (u32) mod_data.buflen);
  36999. +
  37000. + /* Except at the end of the transfer, amount will be
  37001. + * equal to the buffer size, which is divisible by
  37002. + * the bulk-out maxpacket size.
  37003. + */
  37004. + set_bulk_out_req_length(fsg, bh, amount);
  37005. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  37006. + &bh->outreq_busy, &bh->state);
  37007. + fsg->next_buffhd_to_fill = bh->next;
  37008. + fsg->usb_amount_left -= amount;
  37009. + continue;
  37010. + }
  37011. +
  37012. + /* Otherwise wait for something to happen */
  37013. + rc = sleep_thread(fsg);
  37014. + if (rc)
  37015. + return rc;
  37016. + }
  37017. + return 0;
  37018. +}
  37019. +
  37020. +
  37021. +static int finish_reply(struct fsg_dev *fsg)
  37022. +{
  37023. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  37024. + int rc = 0;
  37025. +
  37026. + switch (fsg->data_dir) {
  37027. + case DATA_DIR_NONE:
  37028. + break; // Nothing to send
  37029. +
  37030. + /* If we don't know whether the host wants to read or write,
  37031. + * this must be CB or CBI with an unknown command. We mustn't
  37032. + * try to send or receive any data. So stall both bulk pipes
  37033. + * if we can and wait for a reset. */
  37034. + case DATA_DIR_UNKNOWN:
  37035. + if (mod_data.can_stall) {
  37036. + fsg_set_halt(fsg, fsg->bulk_out);
  37037. + rc = halt_bulk_in_endpoint(fsg);
  37038. + }
  37039. + break;
  37040. +
  37041. + /* All but the last buffer of data must have already been sent */
  37042. + case DATA_DIR_TO_HOST:
  37043. + if (fsg->data_size == 0)
  37044. + ; // Nothing to send
  37045. +
  37046. + /* If there's no residue, simply send the last buffer */
  37047. + else if (fsg->residue == 0) {
  37048. + bh->inreq->zero = 0;
  37049. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37050. + &bh->inreq_busy, &bh->state);
  37051. + fsg->next_buffhd_to_fill = bh->next;
  37052. + }
  37053. +
  37054. + /* There is a residue. For CB and CBI, simply mark the end
  37055. + * of the data with a short packet. However, if we are
  37056. + * allowed to stall, there was no data at all (residue ==
  37057. + * data_size), and the command failed (invalid LUN or
  37058. + * sense data is set), then halt the bulk-in endpoint
  37059. + * instead. */
  37060. + else if (!transport_is_bbb()) {
  37061. + if (mod_data.can_stall &&
  37062. + fsg->residue == fsg->data_size &&
  37063. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  37064. + bh->state = BUF_STATE_EMPTY;
  37065. + rc = halt_bulk_in_endpoint(fsg);
  37066. + } else {
  37067. + bh->inreq->zero = 1;
  37068. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37069. + &bh->inreq_busy, &bh->state);
  37070. + fsg->next_buffhd_to_fill = bh->next;
  37071. + }
  37072. + }
  37073. +
  37074. + /*
  37075. + * For Bulk-only, mark the end of the data with a short
  37076. + * packet. If we are allowed to stall, halt the bulk-in
  37077. + * endpoint. (Note: This violates the Bulk-Only Transport
  37078. + * specification, which requires us to pad the data if we
  37079. + * don't halt the endpoint. Presumably nobody will mind.)
  37080. + */
  37081. + else {
  37082. + bh->inreq->zero = 1;
  37083. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37084. + &bh->inreq_busy, &bh->state);
  37085. + fsg->next_buffhd_to_fill = bh->next;
  37086. + if (mod_data.can_stall)
  37087. + rc = halt_bulk_in_endpoint(fsg);
  37088. + }
  37089. + break;
  37090. +
  37091. + /* We have processed all we want from the data the host has sent.
  37092. + * There may still be outstanding bulk-out requests. */
  37093. + case DATA_DIR_FROM_HOST:
  37094. + if (fsg->residue == 0)
  37095. + ; // Nothing to receive
  37096. +
  37097. + /* Did the host stop sending unexpectedly early? */
  37098. + else if (fsg->short_packet_received) {
  37099. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  37100. + rc = -EINTR;
  37101. + }
  37102. +
  37103. + /* We haven't processed all the incoming data. Even though
  37104. + * we may be allowed to stall, doing so would cause a race.
  37105. + * The controller may already have ACK'ed all the remaining
  37106. + * bulk-out packets, in which case the host wouldn't see a
  37107. + * STALL. Not realizing the endpoint was halted, it wouldn't
  37108. + * clear the halt -- leading to problems later on. */
  37109. +#if 0
  37110. + else if (mod_data.can_stall) {
  37111. + fsg_set_halt(fsg, fsg->bulk_out);
  37112. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  37113. + rc = -EINTR;
  37114. + }
  37115. +#endif
  37116. +
  37117. + /* We can't stall. Read in the excess data and throw it
  37118. + * all away. */
  37119. + else
  37120. + rc = throw_away_data(fsg);
  37121. + break;
  37122. + }
  37123. + return rc;
  37124. +}
  37125. +
  37126. +
  37127. +static int send_status(struct fsg_dev *fsg)
  37128. +{
  37129. + struct fsg_lun *curlun = fsg->curlun;
  37130. + struct fsg_buffhd *bh;
  37131. + int rc;
  37132. + u8 status = US_BULK_STAT_OK;
  37133. + u32 sd, sdinfo = 0;
  37134. +
  37135. + /* Wait for the next buffer to become available */
  37136. + bh = fsg->next_buffhd_to_fill;
  37137. + while (bh->state != BUF_STATE_EMPTY) {
  37138. + rc = sleep_thread(fsg);
  37139. + if (rc)
  37140. + return rc;
  37141. + }
  37142. +
  37143. + if (curlun) {
  37144. + sd = curlun->sense_data;
  37145. + sdinfo = curlun->sense_data_info;
  37146. + } else if (fsg->bad_lun_okay)
  37147. + sd = SS_NO_SENSE;
  37148. + else
  37149. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  37150. +
  37151. + if (fsg->phase_error) {
  37152. + DBG(fsg, "sending phase-error status\n");
  37153. + status = US_BULK_STAT_PHASE;
  37154. + sd = SS_INVALID_COMMAND;
  37155. + } else if (sd != SS_NO_SENSE) {
  37156. + DBG(fsg, "sending command-failure status\n");
  37157. + status = US_BULK_STAT_FAIL;
  37158. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  37159. + " info x%x\n",
  37160. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  37161. + }
  37162. +
  37163. + if (transport_is_bbb()) {
  37164. + struct bulk_cs_wrap *csw = bh->buf;
  37165. +
  37166. + /* Store and send the Bulk-only CSW */
  37167. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  37168. + csw->Tag = fsg->tag;
  37169. + csw->Residue = cpu_to_le32(fsg->residue);
  37170. + csw->Status = status;
  37171. +
  37172. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  37173. + bh->inreq->zero = 0;
  37174. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  37175. + &bh->inreq_busy, &bh->state);
  37176. +
  37177. + } else if (mod_data.transport_type == USB_PR_CB) {
  37178. +
  37179. + /* Control-Bulk transport has no status phase! */
  37180. + return 0;
  37181. +
  37182. + } else { // USB_PR_CBI
  37183. + struct interrupt_data *buf = bh->buf;
  37184. +
  37185. + /* Store and send the Interrupt data. UFI sends the ASC
  37186. + * and ASCQ bytes. Everything else sends a Type (which
  37187. + * is always 0) and the status Value. */
  37188. + if (mod_data.protocol_type == USB_SC_UFI) {
  37189. + buf->bType = ASC(sd);
  37190. + buf->bValue = ASCQ(sd);
  37191. + } else {
  37192. + buf->bType = 0;
  37193. + buf->bValue = status;
  37194. + }
  37195. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  37196. +
  37197. + fsg->intr_buffhd = bh; // Point to the right buffhd
  37198. + fsg->intreq->buf = bh->inreq->buf;
  37199. + fsg->intreq->context = bh;
  37200. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  37201. + &fsg->intreq_busy, &bh->state);
  37202. + }
  37203. +
  37204. + fsg->next_buffhd_to_fill = bh->next;
  37205. + return 0;
  37206. +}
  37207. +
  37208. +
  37209. +/*-------------------------------------------------------------------------*/
  37210. +
  37211. +/* Check whether the command is properly formed and whether its data size
  37212. + * and direction agree with the values we already have. */
  37213. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  37214. + enum data_direction data_dir, unsigned int mask,
  37215. + int needs_medium, const char *name)
  37216. +{
  37217. + int i;
  37218. + int lun = fsg->cmnd[1] >> 5;
  37219. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  37220. + char hdlen[20];
  37221. + struct fsg_lun *curlun;
  37222. +
  37223. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  37224. + * Transparent SCSI doesn't pad. */
  37225. + if (protocol_is_scsi())
  37226. + ;
  37227. +
  37228. + /* There's some disagreement as to whether RBC pads commands or not.
  37229. + * We'll play it safe and accept either form. */
  37230. + else if (mod_data.protocol_type == USB_SC_RBC) {
  37231. + if (fsg->cmnd_size == 12)
  37232. + cmnd_size = 12;
  37233. +
  37234. + /* All the other protocols pad to 12 bytes */
  37235. + } else
  37236. + cmnd_size = 12;
  37237. +
  37238. + hdlen[0] = 0;
  37239. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  37240. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  37241. + fsg->data_size);
  37242. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  37243. + name, cmnd_size, dirletter[(int) data_dir],
  37244. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  37245. +
  37246. + /* We can't reply at all until we know the correct data direction
  37247. + * and size. */
  37248. + if (fsg->data_size_from_cmnd == 0)
  37249. + data_dir = DATA_DIR_NONE;
  37250. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  37251. + fsg->data_dir = data_dir;
  37252. + fsg->data_size = fsg->data_size_from_cmnd;
  37253. +
  37254. + } else { // Bulk-only
  37255. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  37256. +
  37257. + /* Host data size < Device data size is a phase error.
  37258. + * Carry out the command, but only transfer as much
  37259. + * as we are allowed. */
  37260. + fsg->data_size_from_cmnd = fsg->data_size;
  37261. + fsg->phase_error = 1;
  37262. + }
  37263. + }
  37264. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  37265. +
  37266. + /* Conflicting data directions is a phase error */
  37267. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  37268. + fsg->phase_error = 1;
  37269. + return -EINVAL;
  37270. + }
  37271. +
  37272. + /* Verify the length of the command itself */
  37273. + if (cmnd_size != fsg->cmnd_size) {
  37274. +
  37275. + /* Special case workaround: There are plenty of buggy SCSI
  37276. + * implementations. Many have issues with cbw->Length
  37277. + * field passing a wrong command size. For those cases we
  37278. + * always try to work around the problem by using the length
  37279. + * sent by the host side provided it is at least as large
  37280. + * as the correct command length.
  37281. + * Examples of such cases would be MS-Windows, which issues
  37282. + * REQUEST SENSE with cbw->Length == 12 where it should
  37283. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  37284. + * REQUEST SENSE with cbw->Length == 10 where it should
  37285. + * be 6 as well.
  37286. + */
  37287. + if (cmnd_size <= fsg->cmnd_size) {
  37288. + DBG(fsg, "%s is buggy! Expected length %d "
  37289. + "but we got %d\n", name,
  37290. + cmnd_size, fsg->cmnd_size);
  37291. + cmnd_size = fsg->cmnd_size;
  37292. + } else {
  37293. + fsg->phase_error = 1;
  37294. + return -EINVAL;
  37295. + }
  37296. + }
  37297. +
  37298. + /* Check that the LUN values are consistent */
  37299. + if (transport_is_bbb()) {
  37300. + if (fsg->lun != lun)
  37301. + DBG(fsg, "using LUN %d from CBW, "
  37302. + "not LUN %d from CDB\n",
  37303. + fsg->lun, lun);
  37304. + }
  37305. +
  37306. + /* Check the LUN */
  37307. + curlun = fsg->curlun;
  37308. + if (curlun) {
  37309. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  37310. + curlun->sense_data = SS_NO_SENSE;
  37311. + curlun->sense_data_info = 0;
  37312. + curlun->info_valid = 0;
  37313. + }
  37314. + } else {
  37315. + fsg->bad_lun_okay = 0;
  37316. +
  37317. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  37318. + * to use unsupported LUNs; all others may not. */
  37319. + if (fsg->cmnd[0] != INQUIRY &&
  37320. + fsg->cmnd[0] != REQUEST_SENSE) {
  37321. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  37322. + return -EINVAL;
  37323. + }
  37324. + }
  37325. +
  37326. + /* If a unit attention condition exists, only INQUIRY and
  37327. + * REQUEST SENSE commands are allowed; anything else must fail. */
  37328. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  37329. + fsg->cmnd[0] != INQUIRY &&
  37330. + fsg->cmnd[0] != REQUEST_SENSE) {
  37331. + curlun->sense_data = curlun->unit_attention_data;
  37332. + curlun->unit_attention_data = SS_NO_SENSE;
  37333. + return -EINVAL;
  37334. + }
  37335. +
  37336. + /* Check that only command bytes listed in the mask are non-zero */
  37337. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  37338. + for (i = 1; i < cmnd_size; ++i) {
  37339. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  37340. + if (curlun)
  37341. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  37342. + return -EINVAL;
  37343. + }
  37344. + }
  37345. +
  37346. + /* If the medium isn't mounted and the command needs to access
  37347. + * it, return an error. */
  37348. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  37349. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  37350. + return -EINVAL;
  37351. + }
  37352. +
  37353. + return 0;
  37354. +}
  37355. +
  37356. +/* wrapper of check_command for data size in blocks handling */
  37357. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  37358. + enum data_direction data_dir, unsigned int mask,
  37359. + int needs_medium, const char *name)
  37360. +{
  37361. + if (fsg->curlun)
  37362. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  37363. + return check_command(fsg, cmnd_size, data_dir,
  37364. + mask, needs_medium, name);
  37365. +}
  37366. +
  37367. +static int do_scsi_command(struct fsg_dev *fsg)
  37368. +{
  37369. + struct fsg_buffhd *bh;
  37370. + int rc;
  37371. + int reply = -EINVAL;
  37372. + int i;
  37373. + static char unknown[16];
  37374. +
  37375. + dump_cdb(fsg);
  37376. +
  37377. + /* Wait for the next buffer to become available for data or status */
  37378. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  37379. + while (bh->state != BUF_STATE_EMPTY) {
  37380. + rc = sleep_thread(fsg);
  37381. + if (rc)
  37382. + return rc;
  37383. + }
  37384. + fsg->phase_error = 0;
  37385. + fsg->short_packet_received = 0;
  37386. +
  37387. + down_read(&fsg->filesem); // We're using the backing file
  37388. + switch (fsg->cmnd[0]) {
  37389. +
  37390. + case INQUIRY:
  37391. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37392. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  37393. + (1<<4), 0,
  37394. + "INQUIRY")) == 0)
  37395. + reply = do_inquiry(fsg, bh);
  37396. + break;
  37397. +
  37398. + case MODE_SELECT:
  37399. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37400. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  37401. + (1<<1) | (1<<4), 0,
  37402. + "MODE SELECT(6)")) == 0)
  37403. + reply = do_mode_select(fsg, bh);
  37404. + break;
  37405. +
  37406. + case MODE_SELECT_10:
  37407. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37408. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  37409. + (1<<1) | (3<<7), 0,
  37410. + "MODE SELECT(10)")) == 0)
  37411. + reply = do_mode_select(fsg, bh);
  37412. + break;
  37413. +
  37414. + case MODE_SENSE:
  37415. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37416. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  37417. + (1<<1) | (1<<2) | (1<<4), 0,
  37418. + "MODE SENSE(6)")) == 0)
  37419. + reply = do_mode_sense(fsg, bh);
  37420. + break;
  37421. +
  37422. + case MODE_SENSE_10:
  37423. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37424. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37425. + (1<<1) | (1<<2) | (3<<7), 0,
  37426. + "MODE SENSE(10)")) == 0)
  37427. + reply = do_mode_sense(fsg, bh);
  37428. + break;
  37429. +
  37430. + case ALLOW_MEDIUM_REMOVAL:
  37431. + fsg->data_size_from_cmnd = 0;
  37432. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  37433. + (1<<4), 0,
  37434. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  37435. + reply = do_prevent_allow(fsg);
  37436. + break;
  37437. +
  37438. + case READ_6:
  37439. + i = fsg->cmnd[4];
  37440. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  37441. + if ((reply = check_command_size_in_blocks(fsg, 6,
  37442. + DATA_DIR_TO_HOST,
  37443. + (7<<1) | (1<<4), 1,
  37444. + "READ(6)")) == 0)
  37445. + reply = do_read(fsg);
  37446. + break;
  37447. +
  37448. + case READ_10:
  37449. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37450. + if ((reply = check_command_size_in_blocks(fsg, 10,
  37451. + DATA_DIR_TO_HOST,
  37452. + (1<<1) | (0xf<<2) | (3<<7), 1,
  37453. + "READ(10)")) == 0)
  37454. + reply = do_read(fsg);
  37455. + break;
  37456. +
  37457. + case READ_12:
  37458. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  37459. + if ((reply = check_command_size_in_blocks(fsg, 12,
  37460. + DATA_DIR_TO_HOST,
  37461. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  37462. + "READ(12)")) == 0)
  37463. + reply = do_read(fsg);
  37464. + break;
  37465. +
  37466. + case READ_CAPACITY:
  37467. + fsg->data_size_from_cmnd = 8;
  37468. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37469. + (0xf<<2) | (1<<8), 1,
  37470. + "READ CAPACITY")) == 0)
  37471. + reply = do_read_capacity(fsg, bh);
  37472. + break;
  37473. +
  37474. + case READ_HEADER:
  37475. + if (!mod_data.cdrom)
  37476. + goto unknown_cmnd;
  37477. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37478. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37479. + (3<<7) | (0x1f<<1), 1,
  37480. + "READ HEADER")) == 0)
  37481. + reply = do_read_header(fsg, bh);
  37482. + break;
  37483. +
  37484. + case READ_TOC:
  37485. + if (!mod_data.cdrom)
  37486. + goto unknown_cmnd;
  37487. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37488. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37489. + (7<<6) | (1<<1), 1,
  37490. + "READ TOC")) == 0)
  37491. + reply = do_read_toc(fsg, bh);
  37492. + break;
  37493. +
  37494. + case READ_FORMAT_CAPACITIES:
  37495. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37496. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  37497. + (3<<7), 1,
  37498. + "READ FORMAT CAPACITIES")) == 0)
  37499. + reply = do_read_format_capacities(fsg, bh);
  37500. + break;
  37501. +
  37502. + case REQUEST_SENSE:
  37503. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  37504. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  37505. + (1<<4), 0,
  37506. + "REQUEST SENSE")) == 0)
  37507. + reply = do_request_sense(fsg, bh);
  37508. + break;
  37509. +
  37510. + case START_STOP:
  37511. + fsg->data_size_from_cmnd = 0;
  37512. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  37513. + (1<<1) | (1<<4), 0,
  37514. + "START-STOP UNIT")) == 0)
  37515. + reply = do_start_stop(fsg);
  37516. + break;
  37517. +
  37518. + case SYNCHRONIZE_CACHE:
  37519. + fsg->data_size_from_cmnd = 0;
  37520. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  37521. + (0xf<<2) | (3<<7), 1,
  37522. + "SYNCHRONIZE CACHE")) == 0)
  37523. + reply = do_synchronize_cache(fsg);
  37524. + break;
  37525. +
  37526. + case TEST_UNIT_READY:
  37527. + fsg->data_size_from_cmnd = 0;
  37528. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  37529. + 0, 1,
  37530. + "TEST UNIT READY");
  37531. + break;
  37532. +
  37533. + /* Although optional, this command is used by MS-Windows. We
  37534. + * support a minimal version: BytChk must be 0. */
  37535. + case VERIFY:
  37536. + fsg->data_size_from_cmnd = 0;
  37537. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  37538. + (1<<1) | (0xf<<2) | (3<<7), 1,
  37539. + "VERIFY")) == 0)
  37540. + reply = do_verify(fsg);
  37541. + break;
  37542. +
  37543. + case WRITE_6:
  37544. + i = fsg->cmnd[4];
  37545. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  37546. + if ((reply = check_command_size_in_blocks(fsg, 6,
  37547. + DATA_DIR_FROM_HOST,
  37548. + (7<<1) | (1<<4), 1,
  37549. + "WRITE(6)")) == 0)
  37550. + reply = do_write(fsg);
  37551. + break;
  37552. +
  37553. + case WRITE_10:
  37554. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  37555. + if ((reply = check_command_size_in_blocks(fsg, 10,
  37556. + DATA_DIR_FROM_HOST,
  37557. + (1<<1) | (0xf<<2) | (3<<7), 1,
  37558. + "WRITE(10)")) == 0)
  37559. + reply = do_write(fsg);
  37560. + break;
  37561. +
  37562. + case WRITE_12:
  37563. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  37564. + if ((reply = check_command_size_in_blocks(fsg, 12,
  37565. + DATA_DIR_FROM_HOST,
  37566. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  37567. + "WRITE(12)")) == 0)
  37568. + reply = do_write(fsg);
  37569. + break;
  37570. +
  37571. + /* Some mandatory commands that we recognize but don't implement.
  37572. + * They don't mean much in this setting. It's left as an exercise
  37573. + * for anyone interested to implement RESERVE and RELEASE in terms
  37574. + * of Posix locks. */
  37575. + case FORMAT_UNIT:
  37576. + case RELEASE:
  37577. + case RESERVE:
  37578. + case SEND_DIAGNOSTIC:
  37579. + // Fall through
  37580. +
  37581. + default:
  37582. + unknown_cmnd:
  37583. + fsg->data_size_from_cmnd = 0;
  37584. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  37585. + if ((reply = check_command(fsg, fsg->cmnd_size,
  37586. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  37587. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  37588. + reply = -EINVAL;
  37589. + }
  37590. + break;
  37591. + }
  37592. + up_read(&fsg->filesem);
  37593. +
  37594. + if (reply == -EINTR || signal_pending(current))
  37595. + return -EINTR;
  37596. +
  37597. + /* Set up the single reply buffer for finish_reply() */
  37598. + if (reply == -EINVAL)
  37599. + reply = 0; // Error reply length
  37600. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  37601. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  37602. + bh->inreq->length = reply;
  37603. + bh->state = BUF_STATE_FULL;
  37604. + fsg->residue -= reply;
  37605. + } // Otherwise it's already set
  37606. +
  37607. + return 0;
  37608. +}
  37609. +
  37610. +
  37611. +/*-------------------------------------------------------------------------*/
  37612. +
  37613. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  37614. +{
  37615. + struct usb_request *req = bh->outreq;
  37616. + struct bulk_cb_wrap *cbw = req->buf;
  37617. +
  37618. + /* Was this a real packet? Should it be ignored? */
  37619. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  37620. + return -EINVAL;
  37621. +
  37622. + /* Is the CBW valid? */
  37623. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  37624. + cbw->Signature != cpu_to_le32(
  37625. + US_BULK_CB_SIGN)) {
  37626. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  37627. + req->actual,
  37628. + le32_to_cpu(cbw->Signature));
  37629. +
  37630. + /* The Bulk-only spec says we MUST stall the IN endpoint
  37631. + * (6.6.1), so it's unavoidable. It also says we must
  37632. + * retain this state until the next reset, but there's
  37633. + * no way to tell the controller driver it should ignore
  37634. + * Clear-Feature(HALT) requests.
  37635. + *
  37636. + * We aren't required to halt the OUT endpoint; instead
  37637. + * we can simply accept and discard any data received
  37638. + * until the next reset. */
  37639. + wedge_bulk_in_endpoint(fsg);
  37640. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  37641. + return -EINVAL;
  37642. + }
  37643. +
  37644. + /* Is the CBW meaningful? */
  37645. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  37646. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  37647. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  37648. + "cmdlen %u\n",
  37649. + cbw->Lun, cbw->Flags, cbw->Length);
  37650. +
  37651. + /* We can do anything we want here, so let's stall the
  37652. + * bulk pipes if we are allowed to. */
  37653. + if (mod_data.can_stall) {
  37654. + fsg_set_halt(fsg, fsg->bulk_out);
  37655. + halt_bulk_in_endpoint(fsg);
  37656. + }
  37657. + return -EINVAL;
  37658. + }
  37659. +
  37660. + /* Save the command for later */
  37661. + fsg->cmnd_size = cbw->Length;
  37662. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  37663. + if (cbw->Flags & US_BULK_FLAG_IN)
  37664. + fsg->data_dir = DATA_DIR_TO_HOST;
  37665. + else
  37666. + fsg->data_dir = DATA_DIR_FROM_HOST;
  37667. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  37668. + if (fsg->data_size == 0)
  37669. + fsg->data_dir = DATA_DIR_NONE;
  37670. + fsg->lun = cbw->Lun;
  37671. + fsg->tag = cbw->Tag;
  37672. + return 0;
  37673. +}
  37674. +
  37675. +
  37676. +static int get_next_command(struct fsg_dev *fsg)
  37677. +{
  37678. + struct fsg_buffhd *bh;
  37679. + int rc = 0;
  37680. +
  37681. + if (transport_is_bbb()) {
  37682. +
  37683. + /* Wait for the next buffer to become available */
  37684. + bh = fsg->next_buffhd_to_fill;
  37685. + while (bh->state != BUF_STATE_EMPTY) {
  37686. + rc = sleep_thread(fsg);
  37687. + if (rc)
  37688. + return rc;
  37689. + }
  37690. +
  37691. + /* Queue a request to read a Bulk-only CBW */
  37692. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  37693. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  37694. + &bh->outreq_busy, &bh->state);
  37695. +
  37696. + /* We will drain the buffer in software, which means we
  37697. + * can reuse it for the next filling. No need to advance
  37698. + * next_buffhd_to_fill. */
  37699. +
  37700. + /* Wait for the CBW to arrive */
  37701. + while (bh->state != BUF_STATE_FULL) {
  37702. + rc = sleep_thread(fsg);
  37703. + if (rc)
  37704. + return rc;
  37705. + }
  37706. + smp_rmb();
  37707. + rc = received_cbw(fsg, bh);
  37708. + bh->state = BUF_STATE_EMPTY;
  37709. +
  37710. + } else { // USB_PR_CB or USB_PR_CBI
  37711. +
  37712. + /* Wait for the next command to arrive */
  37713. + while (fsg->cbbuf_cmnd_size == 0) {
  37714. + rc = sleep_thread(fsg);
  37715. + if (rc)
  37716. + return rc;
  37717. + }
  37718. +
  37719. + /* Is the previous status interrupt request still busy?
  37720. + * The host is allowed to skip reading the status,
  37721. + * so we must cancel it. */
  37722. + if (fsg->intreq_busy)
  37723. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  37724. +
  37725. + /* Copy the command and mark the buffer empty */
  37726. + fsg->data_dir = DATA_DIR_UNKNOWN;
  37727. + spin_lock_irq(&fsg->lock);
  37728. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  37729. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  37730. + fsg->cbbuf_cmnd_size = 0;
  37731. + spin_unlock_irq(&fsg->lock);
  37732. +
  37733. + /* Use LUN from the command */
  37734. + fsg->lun = fsg->cmnd[1] >> 5;
  37735. + }
  37736. +
  37737. + /* Update current lun */
  37738. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  37739. + fsg->curlun = &fsg->luns[fsg->lun];
  37740. + else
  37741. + fsg->curlun = NULL;
  37742. +
  37743. + return rc;
  37744. +}
  37745. +
  37746. +
  37747. +/*-------------------------------------------------------------------------*/
  37748. +
  37749. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  37750. + const struct usb_endpoint_descriptor *d)
  37751. +{
  37752. + int rc;
  37753. +
  37754. + ep->driver_data = fsg;
  37755. + ep->desc = d;
  37756. + rc = usb_ep_enable(ep);
  37757. + if (rc)
  37758. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  37759. + return rc;
  37760. +}
  37761. +
  37762. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  37763. + struct usb_request **preq)
  37764. +{
  37765. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  37766. + if (*preq)
  37767. + return 0;
  37768. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  37769. + return -ENOMEM;
  37770. +}
  37771. +
  37772. +/*
  37773. + * Reset interface setting and re-init endpoint state (toggle etc).
  37774. + * Call with altsetting < 0 to disable the interface. The only other
  37775. + * available altsetting is 0, which enables the interface.
  37776. + */
  37777. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  37778. +{
  37779. + int rc = 0;
  37780. + int i;
  37781. + const struct usb_endpoint_descriptor *d;
  37782. +
  37783. + if (fsg->running)
  37784. + DBG(fsg, "reset interface\n");
  37785. +
  37786. +reset:
  37787. + /* Deallocate the requests */
  37788. + for (i = 0; i < fsg_num_buffers; ++i) {
  37789. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37790. +
  37791. + if (bh->inreq) {
  37792. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  37793. + bh->inreq = NULL;
  37794. + }
  37795. + if (bh->outreq) {
  37796. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  37797. + bh->outreq = NULL;
  37798. + }
  37799. + }
  37800. + if (fsg->intreq) {
  37801. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  37802. + fsg->intreq = NULL;
  37803. + }
  37804. +
  37805. + /* Disable the endpoints */
  37806. + if (fsg->bulk_in_enabled) {
  37807. + usb_ep_disable(fsg->bulk_in);
  37808. + fsg->bulk_in_enabled = 0;
  37809. + }
  37810. + if (fsg->bulk_out_enabled) {
  37811. + usb_ep_disable(fsg->bulk_out);
  37812. + fsg->bulk_out_enabled = 0;
  37813. + }
  37814. + if (fsg->intr_in_enabled) {
  37815. + usb_ep_disable(fsg->intr_in);
  37816. + fsg->intr_in_enabled = 0;
  37817. + }
  37818. +
  37819. + fsg->running = 0;
  37820. + if (altsetting < 0 || rc != 0)
  37821. + return rc;
  37822. +
  37823. + DBG(fsg, "set interface %d\n", altsetting);
  37824. +
  37825. + /* Enable the endpoints */
  37826. + d = fsg_ep_desc(fsg->gadget,
  37827. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  37828. + &fsg_ss_bulk_in_desc);
  37829. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  37830. + goto reset;
  37831. + fsg->bulk_in_enabled = 1;
  37832. +
  37833. + d = fsg_ep_desc(fsg->gadget,
  37834. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  37835. + &fsg_ss_bulk_out_desc);
  37836. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  37837. + goto reset;
  37838. + fsg->bulk_out_enabled = 1;
  37839. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  37840. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  37841. +
  37842. + if (transport_is_cbi()) {
  37843. + d = fsg_ep_desc(fsg->gadget,
  37844. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  37845. + &fsg_ss_intr_in_desc);
  37846. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  37847. + goto reset;
  37848. + fsg->intr_in_enabled = 1;
  37849. + }
  37850. +
  37851. + /* Allocate the requests */
  37852. + for (i = 0; i < fsg_num_buffers; ++i) {
  37853. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37854. +
  37855. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  37856. + goto reset;
  37857. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  37858. + goto reset;
  37859. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  37860. + bh->inreq->context = bh->outreq->context = bh;
  37861. + bh->inreq->complete = bulk_in_complete;
  37862. + bh->outreq->complete = bulk_out_complete;
  37863. + }
  37864. + if (transport_is_cbi()) {
  37865. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  37866. + goto reset;
  37867. + fsg->intreq->complete = intr_in_complete;
  37868. + }
  37869. +
  37870. + fsg->running = 1;
  37871. + for (i = 0; i < fsg->nluns; ++i)
  37872. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  37873. + return rc;
  37874. +}
  37875. +
  37876. +
  37877. +/*
  37878. + * Change our operational configuration. This code must agree with the code
  37879. + * that returns config descriptors, and with interface altsetting code.
  37880. + *
  37881. + * It's also responsible for power management interactions. Some
  37882. + * configurations might not work with our current power sources.
  37883. + * For now we just assume the gadget is always self-powered.
  37884. + */
  37885. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  37886. +{
  37887. + int rc = 0;
  37888. +
  37889. + /* Disable the single interface */
  37890. + if (fsg->config != 0) {
  37891. + DBG(fsg, "reset config\n");
  37892. + fsg->config = 0;
  37893. + rc = do_set_interface(fsg, -1);
  37894. + }
  37895. +
  37896. + /* Enable the interface */
  37897. + if (new_config != 0) {
  37898. + fsg->config = new_config;
  37899. + if ((rc = do_set_interface(fsg, 0)) != 0)
  37900. + fsg->config = 0; // Reset on errors
  37901. + else
  37902. + INFO(fsg, "%s config #%d\n",
  37903. + usb_speed_string(fsg->gadget->speed),
  37904. + fsg->config);
  37905. + }
  37906. + return rc;
  37907. +}
  37908. +
  37909. +
  37910. +/*-------------------------------------------------------------------------*/
  37911. +
  37912. +static void handle_exception(struct fsg_dev *fsg)
  37913. +{
  37914. + siginfo_t info;
  37915. + int sig;
  37916. + int i;
  37917. + int num_active;
  37918. + struct fsg_buffhd *bh;
  37919. + enum fsg_state old_state;
  37920. + u8 new_config;
  37921. + struct fsg_lun *curlun;
  37922. + unsigned int exception_req_tag;
  37923. + int rc;
  37924. +
  37925. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  37926. + * into a high-priority EXIT exception. */
  37927. + for (;;) {
  37928. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  37929. + if (!sig)
  37930. + break;
  37931. + if (sig != SIGUSR1) {
  37932. + if (fsg->state < FSG_STATE_EXIT)
  37933. + DBG(fsg, "Main thread exiting on signal\n");
  37934. + raise_exception(fsg, FSG_STATE_EXIT);
  37935. + }
  37936. + }
  37937. +
  37938. + /* Cancel all the pending transfers */
  37939. + if (fsg->intreq_busy)
  37940. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  37941. + for (i = 0; i < fsg_num_buffers; ++i) {
  37942. + bh = &fsg->buffhds[i];
  37943. + if (bh->inreq_busy)
  37944. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  37945. + if (bh->outreq_busy)
  37946. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  37947. + }
  37948. +
  37949. + /* Wait until everything is idle */
  37950. + for (;;) {
  37951. + num_active = fsg->intreq_busy;
  37952. + for (i = 0; i < fsg_num_buffers; ++i) {
  37953. + bh = &fsg->buffhds[i];
  37954. + num_active += bh->inreq_busy + bh->outreq_busy;
  37955. + }
  37956. + if (num_active == 0)
  37957. + break;
  37958. + if (sleep_thread(fsg))
  37959. + return;
  37960. + }
  37961. +
  37962. + /* Clear out the controller's fifos */
  37963. + if (fsg->bulk_in_enabled)
  37964. + usb_ep_fifo_flush(fsg->bulk_in);
  37965. + if (fsg->bulk_out_enabled)
  37966. + usb_ep_fifo_flush(fsg->bulk_out);
  37967. + if (fsg->intr_in_enabled)
  37968. + usb_ep_fifo_flush(fsg->intr_in);
  37969. +
  37970. + /* Reset the I/O buffer states and pointers, the SCSI
  37971. + * state, and the exception. Then invoke the handler. */
  37972. + spin_lock_irq(&fsg->lock);
  37973. +
  37974. + for (i = 0; i < fsg_num_buffers; ++i) {
  37975. + bh = &fsg->buffhds[i];
  37976. + bh->state = BUF_STATE_EMPTY;
  37977. + }
  37978. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  37979. + &fsg->buffhds[0];
  37980. +
  37981. + exception_req_tag = fsg->exception_req_tag;
  37982. + new_config = fsg->new_config;
  37983. + old_state = fsg->state;
  37984. +
  37985. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  37986. + fsg->state = FSG_STATE_STATUS_PHASE;
  37987. + else {
  37988. + for (i = 0; i < fsg->nluns; ++i) {
  37989. + curlun = &fsg->luns[i];
  37990. + curlun->prevent_medium_removal = 0;
  37991. + curlun->sense_data = curlun->unit_attention_data =
  37992. + SS_NO_SENSE;
  37993. + curlun->sense_data_info = 0;
  37994. + curlun->info_valid = 0;
  37995. + }
  37996. + fsg->state = FSG_STATE_IDLE;
  37997. + }
  37998. + spin_unlock_irq(&fsg->lock);
  37999. +
  38000. + /* Carry out any extra actions required for the exception */
  38001. + switch (old_state) {
  38002. + default:
  38003. + break;
  38004. +
  38005. + case FSG_STATE_ABORT_BULK_OUT:
  38006. + send_status(fsg);
  38007. + spin_lock_irq(&fsg->lock);
  38008. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  38009. + fsg->state = FSG_STATE_IDLE;
  38010. + spin_unlock_irq(&fsg->lock);
  38011. + break;
  38012. +
  38013. + case FSG_STATE_RESET:
  38014. + /* In case we were forced against our will to halt a
  38015. + * bulk endpoint, clear the halt now. (The SuperH UDC
  38016. + * requires this.) */
  38017. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  38018. + usb_ep_clear_halt(fsg->bulk_in);
  38019. +
  38020. + if (transport_is_bbb()) {
  38021. + if (fsg->ep0_req_tag == exception_req_tag)
  38022. + ep0_queue(fsg); // Complete the status stage
  38023. +
  38024. + } else if (transport_is_cbi())
  38025. + send_status(fsg); // Status by interrupt pipe
  38026. +
  38027. + /* Technically this should go here, but it would only be
  38028. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  38029. + * CONFIG_CHANGE cases. */
  38030. + // for (i = 0; i < fsg->nluns; ++i)
  38031. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  38032. + break;
  38033. +
  38034. + case FSG_STATE_INTERFACE_CHANGE:
  38035. + rc = do_set_interface(fsg, 0);
  38036. + if (fsg->ep0_req_tag != exception_req_tag)
  38037. + break;
  38038. + if (rc != 0) // STALL on errors
  38039. + fsg_set_halt(fsg, fsg->ep0);
  38040. + else // Complete the status stage
  38041. + ep0_queue(fsg);
  38042. + break;
  38043. +
  38044. + case FSG_STATE_CONFIG_CHANGE:
  38045. + rc = do_set_config(fsg, new_config);
  38046. + if (fsg->ep0_req_tag != exception_req_tag)
  38047. + break;
  38048. + if (rc != 0) // STALL on errors
  38049. + fsg_set_halt(fsg, fsg->ep0);
  38050. + else // Complete the status stage
  38051. + ep0_queue(fsg);
  38052. + break;
  38053. +
  38054. + case FSG_STATE_DISCONNECT:
  38055. + for (i = 0; i < fsg->nluns; ++i)
  38056. + fsg_lun_fsync_sub(fsg->luns + i);
  38057. + do_set_config(fsg, 0); // Unconfigured state
  38058. + break;
  38059. +
  38060. + case FSG_STATE_EXIT:
  38061. + case FSG_STATE_TERMINATED:
  38062. + do_set_config(fsg, 0); // Free resources
  38063. + spin_lock_irq(&fsg->lock);
  38064. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  38065. + spin_unlock_irq(&fsg->lock);
  38066. + break;
  38067. + }
  38068. +}
  38069. +
  38070. +
  38071. +/*-------------------------------------------------------------------------*/
  38072. +
  38073. +static int fsg_main_thread(void *fsg_)
  38074. +{
  38075. + struct fsg_dev *fsg = fsg_;
  38076. +
  38077. + /* Allow the thread to be killed by a signal, but set the signal mask
  38078. + * to block everything but INT, TERM, KILL, and USR1. */
  38079. + allow_signal(SIGINT);
  38080. + allow_signal(SIGTERM);
  38081. + allow_signal(SIGKILL);
  38082. + allow_signal(SIGUSR1);
  38083. +
  38084. + /* Allow the thread to be frozen */
  38085. + set_freezable();
  38086. +
  38087. + /* Arrange for userspace references to be interpreted as kernel
  38088. + * pointers. That way we can pass a kernel pointer to a routine
  38089. + * that expects a __user pointer and it will work okay. */
  38090. + set_fs(get_ds());
  38091. +
  38092. + /* The main loop */
  38093. + while (fsg->state != FSG_STATE_TERMINATED) {
  38094. + if (exception_in_progress(fsg) || signal_pending(current)) {
  38095. + handle_exception(fsg);
  38096. + continue;
  38097. + }
  38098. +
  38099. + if (!fsg->running) {
  38100. + sleep_thread(fsg);
  38101. + continue;
  38102. + }
  38103. +
  38104. + if (get_next_command(fsg))
  38105. + continue;
  38106. +
  38107. + spin_lock_irq(&fsg->lock);
  38108. + if (!exception_in_progress(fsg))
  38109. + fsg->state = FSG_STATE_DATA_PHASE;
  38110. + spin_unlock_irq(&fsg->lock);
  38111. +
  38112. + if (do_scsi_command(fsg) || finish_reply(fsg))
  38113. + continue;
  38114. +
  38115. + spin_lock_irq(&fsg->lock);
  38116. + if (!exception_in_progress(fsg))
  38117. + fsg->state = FSG_STATE_STATUS_PHASE;
  38118. + spin_unlock_irq(&fsg->lock);
  38119. +
  38120. + if (send_status(fsg))
  38121. + continue;
  38122. +
  38123. + spin_lock_irq(&fsg->lock);
  38124. + if (!exception_in_progress(fsg))
  38125. + fsg->state = FSG_STATE_IDLE;
  38126. + spin_unlock_irq(&fsg->lock);
  38127. + }
  38128. +
  38129. + spin_lock_irq(&fsg->lock);
  38130. + fsg->thread_task = NULL;
  38131. + spin_unlock_irq(&fsg->lock);
  38132. +
  38133. + /* If we are exiting because of a signal, unregister the
  38134. + * gadget driver. */
  38135. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  38136. + usb_gadget_unregister_driver(&fsg_driver);
  38137. +
  38138. + /* Let the unbind and cleanup routines know the thread has exited */
  38139. + complete_and_exit(&fsg->thread_notifier, 0);
  38140. +}
  38141. +
  38142. +
  38143. +/*-------------------------------------------------------------------------*/
  38144. +
  38145. +
  38146. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  38147. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  38148. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  38149. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  38150. +
  38151. +
  38152. +/*-------------------------------------------------------------------------*/
  38153. +
  38154. +static void fsg_release(struct kref *ref)
  38155. +{
  38156. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  38157. +
  38158. + kfree(fsg->luns);
  38159. + kfree(fsg);
  38160. +}
  38161. +
  38162. +static void lun_release(struct device *dev)
  38163. +{
  38164. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  38165. + struct fsg_dev *fsg =
  38166. + container_of(filesem, struct fsg_dev, filesem);
  38167. +
  38168. + kref_put(&fsg->ref, fsg_release);
  38169. +}
  38170. +
  38171. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  38172. +{
  38173. + struct fsg_dev *fsg = get_gadget_data(gadget);
  38174. + int i;
  38175. + struct fsg_lun *curlun;
  38176. + struct usb_request *req = fsg->ep0req;
  38177. +
  38178. + DBG(fsg, "unbind\n");
  38179. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  38180. +
  38181. + /* If the thread isn't already dead, tell it to exit now */
  38182. + if (fsg->state != FSG_STATE_TERMINATED) {
  38183. + raise_exception(fsg, FSG_STATE_EXIT);
  38184. + wait_for_completion(&fsg->thread_notifier);
  38185. +
  38186. + /* The cleanup routine waits for this completion also */
  38187. + complete(&fsg->thread_notifier);
  38188. + }
  38189. +
  38190. + /* Unregister the sysfs attribute files and the LUNs */
  38191. + for (i = 0; i < fsg->nluns; ++i) {
  38192. + curlun = &fsg->luns[i];
  38193. + if (curlun->registered) {
  38194. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  38195. + device_remove_file(&curlun->dev, &dev_attr_ro);
  38196. + device_remove_file(&curlun->dev, &dev_attr_file);
  38197. + fsg_lun_close(curlun);
  38198. + device_unregister(&curlun->dev);
  38199. + curlun->registered = 0;
  38200. + }
  38201. + }
  38202. +
  38203. + /* Free the data buffers */
  38204. + for (i = 0; i < fsg_num_buffers; ++i)
  38205. + kfree(fsg->buffhds[i].buf);
  38206. +
  38207. + /* Free the request and buffer for endpoint 0 */
  38208. + if (req) {
  38209. + kfree(req->buf);
  38210. + usb_ep_free_request(fsg->ep0, req);
  38211. + }
  38212. +
  38213. + set_gadget_data(gadget, NULL);
  38214. +}
  38215. +
  38216. +
  38217. +static int __init check_parameters(struct fsg_dev *fsg)
  38218. +{
  38219. + int prot;
  38220. + int gcnum;
  38221. +
  38222. + /* Store the default values */
  38223. + mod_data.transport_type = USB_PR_BULK;
  38224. + mod_data.transport_name = "Bulk-only";
  38225. + mod_data.protocol_type = USB_SC_SCSI;
  38226. + mod_data.protocol_name = "Transparent SCSI";
  38227. +
  38228. + /* Some peripheral controllers are known not to be able to
  38229. + * halt bulk endpoints correctly. If one of them is present,
  38230. + * disable stalls.
  38231. + */
  38232. + if (gadget_is_at91(fsg->gadget))
  38233. + mod_data.can_stall = 0;
  38234. +
  38235. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  38236. + gcnum = usb_gadget_controller_number(fsg->gadget);
  38237. + if (gcnum >= 0)
  38238. + mod_data.release = 0x0300 + gcnum;
  38239. + else {
  38240. + WARNING(fsg, "controller '%s' not recognized\n",
  38241. + fsg->gadget->name);
  38242. + mod_data.release = 0x0399;
  38243. + }
  38244. + }
  38245. +
  38246. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  38247. +
  38248. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  38249. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  38250. + ; // Use default setting
  38251. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  38252. + mod_data.transport_type = USB_PR_CB;
  38253. + mod_data.transport_name = "Control-Bulk";
  38254. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  38255. + mod_data.transport_type = USB_PR_CBI;
  38256. + mod_data.transport_name = "Control-Bulk-Interrupt";
  38257. + } else {
  38258. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  38259. + return -EINVAL;
  38260. + }
  38261. +
  38262. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  38263. + prot == USB_SC_SCSI) {
  38264. + ; // Use default setting
  38265. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  38266. + prot == USB_SC_RBC) {
  38267. + mod_data.protocol_type = USB_SC_RBC;
  38268. + mod_data.protocol_name = "RBC";
  38269. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  38270. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  38271. + prot == USB_SC_8020) {
  38272. + mod_data.protocol_type = USB_SC_8020;
  38273. + mod_data.protocol_name = "8020i (ATAPI)";
  38274. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  38275. + prot == USB_SC_QIC) {
  38276. + mod_data.protocol_type = USB_SC_QIC;
  38277. + mod_data.protocol_name = "QIC-157";
  38278. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  38279. + prot == USB_SC_UFI) {
  38280. + mod_data.protocol_type = USB_SC_UFI;
  38281. + mod_data.protocol_name = "UFI";
  38282. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  38283. + prot == USB_SC_8070) {
  38284. + mod_data.protocol_type = USB_SC_8070;
  38285. + mod_data.protocol_name = "8070i";
  38286. + } else {
  38287. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  38288. + return -EINVAL;
  38289. + }
  38290. +
  38291. + mod_data.buflen &= PAGE_CACHE_MASK;
  38292. + if (mod_data.buflen <= 0) {
  38293. + ERROR(fsg, "invalid buflen\n");
  38294. + return -ETOOSMALL;
  38295. + }
  38296. +
  38297. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  38298. +
  38299. + /* Serial string handling.
  38300. + * On a real device, the serial string would be loaded
  38301. + * from permanent storage. */
  38302. + if (mod_data.serial) {
  38303. + const char *ch;
  38304. + unsigned len = 0;
  38305. +
  38306. + /* Sanity check :
  38307. + * The CB[I] specification limits the serial string to
  38308. + * 12 uppercase hexadecimal characters.
  38309. + * BBB need at least 12 uppercase hexadecimal characters,
  38310. + * with a maximum of 126. */
  38311. + for (ch = mod_data.serial; *ch; ++ch) {
  38312. + ++len;
  38313. + if ((*ch < '0' || *ch > '9') &&
  38314. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  38315. + WARNING(fsg,
  38316. + "Invalid serial string character: %c\n",
  38317. + *ch);
  38318. + goto no_serial;
  38319. + }
  38320. + }
  38321. + if (len > 126 ||
  38322. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  38323. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  38324. + WARNING(fsg, "Invalid serial string length!\n");
  38325. + goto no_serial;
  38326. + }
  38327. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  38328. + } else {
  38329. + WARNING(fsg, "No serial-number string provided!\n");
  38330. + no_serial:
  38331. + device_desc.iSerialNumber = 0;
  38332. + }
  38333. +
  38334. + return 0;
  38335. +}
  38336. +
  38337. +
  38338. +static int __init fsg_bind(struct usb_gadget *gadget)
  38339. +{
  38340. + struct fsg_dev *fsg = the_fsg;
  38341. + int rc;
  38342. + int i;
  38343. + struct fsg_lun *curlun;
  38344. + struct usb_ep *ep;
  38345. + struct usb_request *req;
  38346. + char *pathbuf, *p;
  38347. +
  38348. + fsg->gadget = gadget;
  38349. + set_gadget_data(gadget, fsg);
  38350. + fsg->ep0 = gadget->ep0;
  38351. + fsg->ep0->driver_data = fsg;
  38352. +
  38353. + if ((rc = check_parameters(fsg)) != 0)
  38354. + goto out;
  38355. +
  38356. + if (mod_data.removable) { // Enable the store_xxx attributes
  38357. + dev_attr_file.attr.mode = 0644;
  38358. + dev_attr_file.store = fsg_store_file;
  38359. + if (!mod_data.cdrom) {
  38360. + dev_attr_ro.attr.mode = 0644;
  38361. + dev_attr_ro.store = fsg_store_ro;
  38362. + }
  38363. + }
  38364. +
  38365. + /* Only for removable media? */
  38366. + dev_attr_nofua.attr.mode = 0644;
  38367. + dev_attr_nofua.store = fsg_store_nofua;
  38368. +
  38369. + /* Find out how many LUNs there should be */
  38370. + i = mod_data.nluns;
  38371. + if (i == 0)
  38372. + i = max(mod_data.num_filenames, 1u);
  38373. + if (i > FSG_MAX_LUNS) {
  38374. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  38375. + rc = -EINVAL;
  38376. + goto out;
  38377. + }
  38378. +
  38379. + /* Create the LUNs, open their backing files, and register the
  38380. + * LUN devices in sysfs. */
  38381. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  38382. + if (!fsg->luns) {
  38383. + rc = -ENOMEM;
  38384. + goto out;
  38385. + }
  38386. + fsg->nluns = i;
  38387. +
  38388. + for (i = 0; i < fsg->nluns; ++i) {
  38389. + curlun = &fsg->luns[i];
  38390. + curlun->cdrom = !!mod_data.cdrom;
  38391. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  38392. + curlun->initially_ro = curlun->ro;
  38393. + curlun->removable = mod_data.removable;
  38394. + curlun->nofua = mod_data.nofua[i];
  38395. + curlun->dev.release = lun_release;
  38396. + curlun->dev.parent = &gadget->dev;
  38397. + curlun->dev.driver = &fsg_driver.driver;
  38398. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  38399. + dev_set_name(&curlun->dev,"%s-lun%d",
  38400. + dev_name(&gadget->dev), i);
  38401. +
  38402. + kref_get(&fsg->ref);
  38403. + rc = device_register(&curlun->dev);
  38404. + if (rc) {
  38405. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  38406. + put_device(&curlun->dev);
  38407. + goto out;
  38408. + }
  38409. + curlun->registered = 1;
  38410. +
  38411. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  38412. + if (rc)
  38413. + goto out;
  38414. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  38415. + if (rc)
  38416. + goto out;
  38417. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  38418. + if (rc)
  38419. + goto out;
  38420. +
  38421. + if (mod_data.file[i] && *mod_data.file[i]) {
  38422. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  38423. + if (rc)
  38424. + goto out;
  38425. + } else if (!mod_data.removable) {
  38426. + ERROR(fsg, "no file given for LUN%d\n", i);
  38427. + rc = -EINVAL;
  38428. + goto out;
  38429. + }
  38430. + }
  38431. +
  38432. + /* Find all the endpoints we will use */
  38433. + usb_ep_autoconfig_reset(gadget);
  38434. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  38435. + if (!ep)
  38436. + goto autoconf_fail;
  38437. + ep->driver_data = fsg; // claim the endpoint
  38438. + fsg->bulk_in = ep;
  38439. +
  38440. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  38441. + if (!ep)
  38442. + goto autoconf_fail;
  38443. + ep->driver_data = fsg; // claim the endpoint
  38444. + fsg->bulk_out = ep;
  38445. +
  38446. + if (transport_is_cbi()) {
  38447. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  38448. + if (!ep)
  38449. + goto autoconf_fail;
  38450. + ep->driver_data = fsg; // claim the endpoint
  38451. + fsg->intr_in = ep;
  38452. + }
  38453. +
  38454. + /* Fix up the descriptors */
  38455. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  38456. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  38457. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  38458. +
  38459. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  38460. + fsg_intf_desc.bNumEndpoints = i;
  38461. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  38462. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  38463. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  38464. +
  38465. + if (gadget_is_dualspeed(gadget)) {
  38466. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  38467. +
  38468. + /* Assume endpoint addresses are the same for both speeds */
  38469. + fsg_hs_bulk_in_desc.bEndpointAddress =
  38470. + fsg_fs_bulk_in_desc.bEndpointAddress;
  38471. + fsg_hs_bulk_out_desc.bEndpointAddress =
  38472. + fsg_fs_bulk_out_desc.bEndpointAddress;
  38473. + fsg_hs_intr_in_desc.bEndpointAddress =
  38474. + fsg_fs_intr_in_desc.bEndpointAddress;
  38475. + }
  38476. +
  38477. + if (gadget_is_superspeed(gadget)) {
  38478. + unsigned max_burst;
  38479. +
  38480. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  38481. +
  38482. + /* Calculate bMaxBurst, we know packet size is 1024 */
  38483. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  38484. +
  38485. + /* Assume endpoint addresses are the same for both speeds */
  38486. + fsg_ss_bulk_in_desc.bEndpointAddress =
  38487. + fsg_fs_bulk_in_desc.bEndpointAddress;
  38488. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  38489. +
  38490. + fsg_ss_bulk_out_desc.bEndpointAddress =
  38491. + fsg_fs_bulk_out_desc.bEndpointAddress;
  38492. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  38493. + }
  38494. +
  38495. + if (gadget_is_otg(gadget))
  38496. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  38497. +
  38498. + rc = -ENOMEM;
  38499. +
  38500. + /* Allocate the request and buffer for endpoint 0 */
  38501. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  38502. + if (!req)
  38503. + goto out;
  38504. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  38505. + if (!req->buf)
  38506. + goto out;
  38507. + req->complete = ep0_complete;
  38508. +
  38509. + /* Allocate the data buffers */
  38510. + for (i = 0; i < fsg_num_buffers; ++i) {
  38511. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  38512. +
  38513. + /* Allocate for the bulk-in endpoint. We assume that
  38514. + * the buffer will also work with the bulk-out (and
  38515. + * interrupt-in) endpoint. */
  38516. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  38517. + if (!bh->buf)
  38518. + goto out;
  38519. + bh->next = bh + 1;
  38520. + }
  38521. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  38522. +
  38523. + /* This should reflect the actual gadget power source */
  38524. + usb_gadget_set_selfpowered(gadget);
  38525. +
  38526. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  38527. + "%s %s with %s",
  38528. + init_utsname()->sysname, init_utsname()->release,
  38529. + gadget->name);
  38530. +
  38531. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  38532. + "file-storage-gadget");
  38533. + if (IS_ERR(fsg->thread_task)) {
  38534. + rc = PTR_ERR(fsg->thread_task);
  38535. + goto out;
  38536. + }
  38537. +
  38538. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  38539. + INFO(fsg, "NOTE: This driver is deprecated. "
  38540. + "Consider using g_mass_storage instead.\n");
  38541. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  38542. +
  38543. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  38544. + for (i = 0; i < fsg->nluns; ++i) {
  38545. + curlun = &fsg->luns[i];
  38546. + if (fsg_lun_is_open(curlun)) {
  38547. + p = NULL;
  38548. + if (pathbuf) {
  38549. + p = d_path(&curlun->filp->f_path,
  38550. + pathbuf, PATH_MAX);
  38551. + if (IS_ERR(p))
  38552. + p = NULL;
  38553. + }
  38554. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  38555. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  38556. + }
  38557. + }
  38558. + kfree(pathbuf);
  38559. +
  38560. + DBG(fsg, "transport=%s (x%02x)\n",
  38561. + mod_data.transport_name, mod_data.transport_type);
  38562. + DBG(fsg, "protocol=%s (x%02x)\n",
  38563. + mod_data.protocol_name, mod_data.protocol_type);
  38564. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  38565. + mod_data.vendor, mod_data.product, mod_data.release);
  38566. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  38567. + mod_data.removable, mod_data.can_stall,
  38568. + mod_data.cdrom, mod_data.buflen);
  38569. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  38570. +
  38571. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  38572. +
  38573. + /* Tell the thread to start working */
  38574. + wake_up_process(fsg->thread_task);
  38575. + return 0;
  38576. +
  38577. +autoconf_fail:
  38578. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  38579. + rc = -ENOTSUPP;
  38580. +
  38581. +out:
  38582. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  38583. + fsg_unbind(gadget);
  38584. + complete(&fsg->thread_notifier);
  38585. + return rc;
  38586. +}
  38587. +
  38588. +
  38589. +/*-------------------------------------------------------------------------*/
  38590. +
  38591. +static void fsg_suspend(struct usb_gadget *gadget)
  38592. +{
  38593. + struct fsg_dev *fsg = get_gadget_data(gadget);
  38594. +
  38595. + DBG(fsg, "suspend\n");
  38596. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  38597. +}
  38598. +
  38599. +static void fsg_resume(struct usb_gadget *gadget)
  38600. +{
  38601. + struct fsg_dev *fsg = get_gadget_data(gadget);
  38602. +
  38603. + DBG(fsg, "resume\n");
  38604. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  38605. +}
  38606. +
  38607. +
  38608. +/*-------------------------------------------------------------------------*/
  38609. +
  38610. +static struct usb_gadget_driver fsg_driver = {
  38611. + .max_speed = USB_SPEED_SUPER,
  38612. + .function = (char *) fsg_string_product,
  38613. + .unbind = fsg_unbind,
  38614. + .disconnect = fsg_disconnect,
  38615. + .setup = fsg_setup,
  38616. + .suspend = fsg_suspend,
  38617. + .resume = fsg_resume,
  38618. +
  38619. + .driver = {
  38620. + .name = DRIVER_NAME,
  38621. + .owner = THIS_MODULE,
  38622. + // .release = ...
  38623. + // .suspend = ...
  38624. + // .resume = ...
  38625. + },
  38626. +};
  38627. +
  38628. +
  38629. +static int __init fsg_alloc(void)
  38630. +{
  38631. + struct fsg_dev *fsg;
  38632. +
  38633. + fsg = kzalloc(sizeof *fsg +
  38634. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  38635. +
  38636. + if (!fsg)
  38637. + return -ENOMEM;
  38638. + spin_lock_init(&fsg->lock);
  38639. + init_rwsem(&fsg->filesem);
  38640. + kref_init(&fsg->ref);
  38641. + init_completion(&fsg->thread_notifier);
  38642. +
  38643. + the_fsg = fsg;
  38644. + return 0;
  38645. +}
  38646. +
  38647. +
  38648. +static int __init fsg_init(void)
  38649. +{
  38650. + int rc;
  38651. + struct fsg_dev *fsg;
  38652. +
  38653. + rc = fsg_num_buffers_validate();
  38654. + if (rc != 0)
  38655. + return rc;
  38656. +
  38657. + if ((rc = fsg_alloc()) != 0)
  38658. + return rc;
  38659. + fsg = the_fsg;
  38660. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  38661. + kref_put(&fsg->ref, fsg_release);
  38662. + return rc;
  38663. +}
  38664. +module_init(fsg_init);
  38665. +
  38666. +
  38667. +static void __exit fsg_cleanup(void)
  38668. +{
  38669. + struct fsg_dev *fsg = the_fsg;
  38670. +
  38671. + /* Unregister the driver iff the thread hasn't already done so */
  38672. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  38673. + usb_gadget_unregister_driver(&fsg_driver);
  38674. +
  38675. + /* Wait for the thread to finish up */
  38676. + wait_for_completion(&fsg->thread_notifier);
  38677. +
  38678. + kref_put(&fsg->ref, fsg_release);
  38679. +}
  38680. +module_exit(fsg_cleanup);
  38681. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/changes.txt linux-rpi/drivers/usb/host/dwc_common_port/changes.txt
  38682. --- linux-3.10.37/drivers/usb/host/dwc_common_port/changes.txt 1970-01-01 01:00:00.000000000 +0100
  38683. +++ linux-rpi/drivers/usb/host/dwc_common_port/changes.txt 2014-04-24 15:35:04.169565731 +0200
  38684. @@ -0,0 +1,174 @@
  38685. +
  38686. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  38687. +IO context struct. The IO context struct should live in an os-dependent struct
  38688. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  38689. +named 'os_dep' embedded in the main device struct. So there these calls look
  38690. +like this:
  38691. +
  38692. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  38693. +
  38694. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  38695. + &pcd->dev_global_regs->dcfg, 0);
  38696. +
  38697. +Note that for the existing Linux driver ports, it is not necessary to actually
  38698. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  38699. +require an IO context, its macros for dwc_read_reg32() and friends do not
  38700. +use the context pointer, so it is optimized away by the compiler. But it is
  38701. +necessary to add the pointer parameter to all of the call sites, to be ready
  38702. +for any future ports (such as FreeBSD) which do require an IO context.
  38703. +
  38704. +
  38705. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  38706. +take an additional parameter, a pointer to a memory context. Examples:
  38707. +
  38708. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  38709. +
  38710. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  38711. +
  38712. +Again, for the Linux ports, it is not necessary to actually define the memctx
  38713. +member, but it is necessary to add the pointer parameter to all of the call
  38714. +sites.
  38715. +
  38716. +
  38717. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  38718. +
  38719. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  38720. +
  38721. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  38722. +
  38723. +
  38724. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  38725. +
  38726. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  38727. +
  38728. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  38729. +
  38730. +
  38731. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  38732. +
  38733. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  38734. +
  38735. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  38736. +
  38737. +
  38738. +Same for dwc_timer_alloc(). Example:
  38739. +
  38740. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  38741. + cb_func, cb_data);
  38742. +
  38743. +
  38744. +Same for dwc_waitq_alloc(). Example:
  38745. +
  38746. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  38747. +
  38748. +
  38749. +Same for dwc_thread_run(). Example:
  38750. +
  38751. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  38752. + "dwc_usb3_thd1", data);
  38753. +
  38754. +
  38755. +Same for dwc_workq_alloc(). Example:
  38756. +
  38757. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  38758. +
  38759. +
  38760. +Same for dwc_task_alloc(). Example:
  38761. +
  38762. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  38763. + cb_func, cb_data);
  38764. +
  38765. +
  38766. +In addition to the context pointer additions, a few core functions have had
  38767. +other changes made to their parameters:
  38768. +
  38769. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  38770. +has been changed from a uint64_t to a dwc_irqflags_t.
  38771. +
  38772. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  38773. +FreeBSD equivalent of that function requires it.
  38774. +
  38775. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  38776. +'char *name' parameter, to be consistent with dwc_thread_run() and
  38777. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  38778. +requires a unique name.
  38779. +
  38780. +
  38781. +Here is a complete list of the core functions that now take a pointer to a
  38782. +context as their first parameter:
  38783. +
  38784. + dwc_read_reg32
  38785. + dwc_read_reg64
  38786. + dwc_write_reg32
  38787. + dwc_write_reg64
  38788. + dwc_modify_reg32
  38789. + dwc_modify_reg64
  38790. + dwc_alloc
  38791. + dwc_alloc_atomic
  38792. + dwc_strdup
  38793. + dwc_free
  38794. + dwc_dma_alloc
  38795. + dwc_dma_free
  38796. + dwc_mutex_alloc
  38797. + dwc_mutex_free
  38798. + dwc_spinlock_alloc
  38799. + dwc_spinlock_free
  38800. + dwc_timer_alloc
  38801. + dwc_waitq_alloc
  38802. + dwc_thread_run
  38803. + dwc_workq_alloc
  38804. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  38805. +
  38806. +And here are the core functions that have other changes to their parameters:
  38807. +
  38808. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  38809. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  38810. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  38811. +
  38812. +
  38813. +
  38814. +The changes to the core functions also require some of the other library
  38815. +functions to change:
  38816. +
  38817. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  38818. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  38819. + (for mutex allocation) as the 2nd param.
  38820. +
  38821. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  38822. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  38823. + 'void *memctx' as the 1st param.
  38824. +
  38825. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  38826. + 'void *memctx' as the 1st param.
  38827. +
  38828. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  38829. +
  38830. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  38831. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  38832. + param, and also now returns an integer value that is non-zero if
  38833. + allocation of its data structures or work queue fails.
  38834. +
  38835. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  38836. +
  38837. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  38838. + param, and also now returns an integer value that is non-zero if
  38839. + allocation of its data structures fails.
  38840. +
  38841. +
  38842. +
  38843. +Other miscellaneous changes:
  38844. +
  38845. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  38846. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  38847. +
  38848. +The following #define's have been added to allow selectively compiling library
  38849. +features:
  38850. +
  38851. + DWC_CCLIB
  38852. + DWC_CRYPTOLIB
  38853. + DWC_NOTIFYLIB
  38854. + DWC_UTFLIB
  38855. +
  38856. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  38857. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  38858. +library code directly into a driver module, instead of as a standalone module.
  38859. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/doc/doxygen.cfg linux-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  38860. --- linux-3.10.37/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  38861. +++ linux-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 2014-04-24 15:35:04.169565731 +0200
  38862. @@ -0,0 +1,270 @@
  38863. +# Doxyfile 1.4.5
  38864. +
  38865. +#---------------------------------------------------------------------------
  38866. +# Project related configuration options
  38867. +#---------------------------------------------------------------------------
  38868. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  38869. +PROJECT_NUMBER =
  38870. +OUTPUT_DIRECTORY = doc
  38871. +CREATE_SUBDIRS = NO
  38872. +OUTPUT_LANGUAGE = English
  38873. +BRIEF_MEMBER_DESC = YES
  38874. +REPEAT_BRIEF = YES
  38875. +ABBREVIATE_BRIEF = "The $name class" \
  38876. + "The $name widget" \
  38877. + "The $name file" \
  38878. + is \
  38879. + provides \
  38880. + specifies \
  38881. + contains \
  38882. + represents \
  38883. + a \
  38884. + an \
  38885. + the
  38886. +ALWAYS_DETAILED_SEC = YES
  38887. +INLINE_INHERITED_MEMB = NO
  38888. +FULL_PATH_NAMES = NO
  38889. +STRIP_FROM_PATH = ..
  38890. +STRIP_FROM_INC_PATH =
  38891. +SHORT_NAMES = NO
  38892. +JAVADOC_AUTOBRIEF = YES
  38893. +MULTILINE_CPP_IS_BRIEF = NO
  38894. +DETAILS_AT_TOP = YES
  38895. +INHERIT_DOCS = YES
  38896. +SEPARATE_MEMBER_PAGES = NO
  38897. +TAB_SIZE = 8
  38898. +ALIASES =
  38899. +OPTIMIZE_OUTPUT_FOR_C = YES
  38900. +OPTIMIZE_OUTPUT_JAVA = NO
  38901. +BUILTIN_STL_SUPPORT = NO
  38902. +DISTRIBUTE_GROUP_DOC = NO
  38903. +SUBGROUPING = NO
  38904. +#---------------------------------------------------------------------------
  38905. +# Build related configuration options
  38906. +#---------------------------------------------------------------------------
  38907. +EXTRACT_ALL = NO
  38908. +EXTRACT_PRIVATE = NO
  38909. +EXTRACT_STATIC = YES
  38910. +EXTRACT_LOCAL_CLASSES = NO
  38911. +EXTRACT_LOCAL_METHODS = NO
  38912. +HIDE_UNDOC_MEMBERS = NO
  38913. +HIDE_UNDOC_CLASSES = NO
  38914. +HIDE_FRIEND_COMPOUNDS = NO
  38915. +HIDE_IN_BODY_DOCS = NO
  38916. +INTERNAL_DOCS = NO
  38917. +CASE_SENSE_NAMES = YES
  38918. +HIDE_SCOPE_NAMES = NO
  38919. +SHOW_INCLUDE_FILES = NO
  38920. +INLINE_INFO = YES
  38921. +SORT_MEMBER_DOCS = NO
  38922. +SORT_BRIEF_DOCS = NO
  38923. +SORT_BY_SCOPE_NAME = NO
  38924. +GENERATE_TODOLIST = YES
  38925. +GENERATE_TESTLIST = YES
  38926. +GENERATE_BUGLIST = YES
  38927. +GENERATE_DEPRECATEDLIST= YES
  38928. +ENABLED_SECTIONS =
  38929. +MAX_INITIALIZER_LINES = 30
  38930. +SHOW_USED_FILES = YES
  38931. +SHOW_DIRECTORIES = YES
  38932. +FILE_VERSION_FILTER =
  38933. +#---------------------------------------------------------------------------
  38934. +# configuration options related to warning and progress messages
  38935. +#---------------------------------------------------------------------------
  38936. +QUIET = YES
  38937. +WARNINGS = YES
  38938. +WARN_IF_UNDOCUMENTED = NO
  38939. +WARN_IF_DOC_ERROR = YES
  38940. +WARN_NO_PARAMDOC = YES
  38941. +WARN_FORMAT = "$file:$line: $text"
  38942. +WARN_LOGFILE =
  38943. +#---------------------------------------------------------------------------
  38944. +# configuration options related to the input files
  38945. +#---------------------------------------------------------------------------
  38946. +INPUT = .
  38947. +FILE_PATTERNS = *.c \
  38948. + *.cc \
  38949. + *.cxx \
  38950. + *.cpp \
  38951. + *.c++ \
  38952. + *.d \
  38953. + *.java \
  38954. + *.ii \
  38955. + *.ixx \
  38956. + *.ipp \
  38957. + *.i++ \
  38958. + *.inl \
  38959. + *.h \
  38960. + *.hh \
  38961. + *.hxx \
  38962. + *.hpp \
  38963. + *.h++ \
  38964. + *.idl \
  38965. + *.odl \
  38966. + *.cs \
  38967. + *.php \
  38968. + *.php3 \
  38969. + *.inc \
  38970. + *.m \
  38971. + *.mm \
  38972. + *.dox \
  38973. + *.py \
  38974. + *.C \
  38975. + *.CC \
  38976. + *.C++ \
  38977. + *.II \
  38978. + *.I++ \
  38979. + *.H \
  38980. + *.HH \
  38981. + *.H++ \
  38982. + *.CS \
  38983. + *.PHP \
  38984. + *.PHP3 \
  38985. + *.M \
  38986. + *.MM \
  38987. + *.PY
  38988. +RECURSIVE = NO
  38989. +EXCLUDE =
  38990. +EXCLUDE_SYMLINKS = NO
  38991. +EXCLUDE_PATTERNS =
  38992. +EXAMPLE_PATH =
  38993. +EXAMPLE_PATTERNS = *
  38994. +EXAMPLE_RECURSIVE = NO
  38995. +IMAGE_PATH =
  38996. +INPUT_FILTER =
  38997. +FILTER_PATTERNS =
  38998. +FILTER_SOURCE_FILES = NO
  38999. +#---------------------------------------------------------------------------
  39000. +# configuration options related to source browsing
  39001. +#---------------------------------------------------------------------------
  39002. +SOURCE_BROWSER = NO
  39003. +INLINE_SOURCES = NO
  39004. +STRIP_CODE_COMMENTS = YES
  39005. +REFERENCED_BY_RELATION = YES
  39006. +REFERENCES_RELATION = YES
  39007. +USE_HTAGS = NO
  39008. +VERBATIM_HEADERS = NO
  39009. +#---------------------------------------------------------------------------
  39010. +# configuration options related to the alphabetical class index
  39011. +#---------------------------------------------------------------------------
  39012. +ALPHABETICAL_INDEX = NO
  39013. +COLS_IN_ALPHA_INDEX = 5
  39014. +IGNORE_PREFIX =
  39015. +#---------------------------------------------------------------------------
  39016. +# configuration options related to the HTML output
  39017. +#---------------------------------------------------------------------------
  39018. +GENERATE_HTML = YES
  39019. +HTML_OUTPUT = html
  39020. +HTML_FILE_EXTENSION = .html
  39021. +HTML_HEADER =
  39022. +HTML_FOOTER =
  39023. +HTML_STYLESHEET =
  39024. +HTML_ALIGN_MEMBERS = YES
  39025. +GENERATE_HTMLHELP = NO
  39026. +CHM_FILE =
  39027. +HHC_LOCATION =
  39028. +GENERATE_CHI = NO
  39029. +BINARY_TOC = NO
  39030. +TOC_EXPAND = NO
  39031. +DISABLE_INDEX = NO
  39032. +ENUM_VALUES_PER_LINE = 4
  39033. +GENERATE_TREEVIEW = YES
  39034. +TREEVIEW_WIDTH = 250
  39035. +#---------------------------------------------------------------------------
  39036. +# configuration options related to the LaTeX output
  39037. +#---------------------------------------------------------------------------
  39038. +GENERATE_LATEX = NO
  39039. +LATEX_OUTPUT = latex
  39040. +LATEX_CMD_NAME = latex
  39041. +MAKEINDEX_CMD_NAME = makeindex
  39042. +COMPACT_LATEX = NO
  39043. +PAPER_TYPE = a4wide
  39044. +EXTRA_PACKAGES =
  39045. +LATEX_HEADER =
  39046. +PDF_HYPERLINKS = NO
  39047. +USE_PDFLATEX = NO
  39048. +LATEX_BATCHMODE = NO
  39049. +LATEX_HIDE_INDICES = NO
  39050. +#---------------------------------------------------------------------------
  39051. +# configuration options related to the RTF output
  39052. +#---------------------------------------------------------------------------
  39053. +GENERATE_RTF = NO
  39054. +RTF_OUTPUT = rtf
  39055. +COMPACT_RTF = NO
  39056. +RTF_HYPERLINKS = NO
  39057. +RTF_STYLESHEET_FILE =
  39058. +RTF_EXTENSIONS_FILE =
  39059. +#---------------------------------------------------------------------------
  39060. +# configuration options related to the man page output
  39061. +#---------------------------------------------------------------------------
  39062. +GENERATE_MAN = NO
  39063. +MAN_OUTPUT = man
  39064. +MAN_EXTENSION = .3
  39065. +MAN_LINKS = NO
  39066. +#---------------------------------------------------------------------------
  39067. +# configuration options related to the XML output
  39068. +#---------------------------------------------------------------------------
  39069. +GENERATE_XML = NO
  39070. +XML_OUTPUT = xml
  39071. +XML_SCHEMA =
  39072. +XML_DTD =
  39073. +XML_PROGRAMLISTING = YES
  39074. +#---------------------------------------------------------------------------
  39075. +# configuration options for the AutoGen Definitions output
  39076. +#---------------------------------------------------------------------------
  39077. +GENERATE_AUTOGEN_DEF = NO
  39078. +#---------------------------------------------------------------------------
  39079. +# configuration options related to the Perl module output
  39080. +#---------------------------------------------------------------------------
  39081. +GENERATE_PERLMOD = NO
  39082. +PERLMOD_LATEX = NO
  39083. +PERLMOD_PRETTY = YES
  39084. +PERLMOD_MAKEVAR_PREFIX =
  39085. +#---------------------------------------------------------------------------
  39086. +# Configuration options related to the preprocessor
  39087. +#---------------------------------------------------------------------------
  39088. +ENABLE_PREPROCESSING = YES
  39089. +MACRO_EXPANSION = NO
  39090. +EXPAND_ONLY_PREDEF = NO
  39091. +SEARCH_INCLUDES = YES
  39092. +INCLUDE_PATH =
  39093. +INCLUDE_FILE_PATTERNS =
  39094. +PREDEFINED = DEBUG DEBUG_MEMORY
  39095. +EXPAND_AS_DEFINED =
  39096. +SKIP_FUNCTION_MACROS = YES
  39097. +#---------------------------------------------------------------------------
  39098. +# Configuration::additions related to external references
  39099. +#---------------------------------------------------------------------------
  39100. +TAGFILES =
  39101. +GENERATE_TAGFILE =
  39102. +ALLEXTERNALS = NO
  39103. +EXTERNAL_GROUPS = YES
  39104. +PERL_PATH = /usr/bin/perl
  39105. +#---------------------------------------------------------------------------
  39106. +# Configuration options related to the dot tool
  39107. +#---------------------------------------------------------------------------
  39108. +CLASS_DIAGRAMS = YES
  39109. +HIDE_UNDOC_RELATIONS = YES
  39110. +HAVE_DOT = NO
  39111. +CLASS_GRAPH = YES
  39112. +COLLABORATION_GRAPH = YES
  39113. +GROUP_GRAPHS = YES
  39114. +UML_LOOK = NO
  39115. +TEMPLATE_RELATIONS = NO
  39116. +INCLUDE_GRAPH = NO
  39117. +INCLUDED_BY_GRAPH = YES
  39118. +CALL_GRAPH = NO
  39119. +GRAPHICAL_HIERARCHY = YES
  39120. +DIRECTORY_GRAPH = YES
  39121. +DOT_IMAGE_FORMAT = png
  39122. +DOT_PATH =
  39123. +DOTFILE_DIRS =
  39124. +MAX_DOT_GRAPH_DEPTH = 1000
  39125. +DOT_TRANSPARENT = NO
  39126. +DOT_MULTI_TARGETS = NO
  39127. +GENERATE_LEGEND = YES
  39128. +DOT_CLEANUP = YES
  39129. +#---------------------------------------------------------------------------
  39130. +# Configuration::additions related to the search engine
  39131. +#---------------------------------------------------------------------------
  39132. +SEARCHENGINE = NO
  39133. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_cc.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c
  39134. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_cc.c 1970-01-01 01:00:00.000000000 +0100
  39135. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c 2014-04-24 15:35:04.169565731 +0200
  39136. @@ -0,0 +1,532 @@
  39137. +/* =========================================================================
  39138. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  39139. + * $Revision: #4 $
  39140. + * $Date: 2010/11/04 $
  39141. + * $Change: 1621692 $
  39142. + *
  39143. + * Synopsys Portability Library Software and documentation
  39144. + * (hereinafter, "Software") is an Unsupported proprietary work of
  39145. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  39146. + * between Synopsys and you.
  39147. + *
  39148. + * The Software IS NOT an item of Licensed Software or Licensed Product
  39149. + * under any End User Software License Agreement or Agreement for
  39150. + * Licensed Product with Synopsys or any supplement thereto. You are
  39151. + * permitted to use and redistribute this Software in source and binary
  39152. + * forms, with or without modification, provided that redistributions
  39153. + * of source code must retain this notice. You may not view, use,
  39154. + * disclose, copy or distribute this file or any information contained
  39155. + * herein except pursuant to this license grant from Synopsys. If you
  39156. + * do not agree with this notice, including the disclaimer below, then
  39157. + * you are not authorized to use the Software.
  39158. + *
  39159. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  39160. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39161. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39162. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  39163. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39164. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39165. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  39166. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39167. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39168. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39169. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39170. + * DAMAGE.
  39171. + * ========================================================================= */
  39172. +#ifdef DWC_CCLIB
  39173. +
  39174. +#include "dwc_cc.h"
  39175. +
  39176. +typedef struct dwc_cc
  39177. +{
  39178. + uint32_t uid;
  39179. + uint8_t chid[16];
  39180. + uint8_t cdid[16];
  39181. + uint8_t ck[16];
  39182. + uint8_t *name;
  39183. + uint8_t length;
  39184. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  39185. +} dwc_cc_t;
  39186. +
  39187. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  39188. +
  39189. +/** The main structure for CC management. */
  39190. +struct dwc_cc_if
  39191. +{
  39192. + dwc_mutex_t *mutex;
  39193. + char *filename;
  39194. +
  39195. + unsigned is_host:1;
  39196. +
  39197. + dwc_notifier_t *notifier;
  39198. +
  39199. + struct context_list list;
  39200. +};
  39201. +
  39202. +#ifdef DEBUG
  39203. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  39204. +{
  39205. + int i;
  39206. + DWC_PRINTF("%s: ", name);
  39207. + for (i=0; i<len; i++) {
  39208. + DWC_PRINTF("%02x ", bytes[i]);
  39209. + }
  39210. + DWC_PRINTF("\n");
  39211. +}
  39212. +#else
  39213. +#define dump_bytes(x...)
  39214. +#endif
  39215. +
  39216. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  39217. +{
  39218. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  39219. + if (!cc) {
  39220. + return NULL;
  39221. + }
  39222. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  39223. +
  39224. + if (name) {
  39225. + cc->length = length;
  39226. + cc->name = dwc_alloc(mem_ctx, length);
  39227. + if (!cc->name) {
  39228. + dwc_free(mem_ctx, cc);
  39229. + return NULL;
  39230. + }
  39231. +
  39232. + DWC_MEMCPY(cc->name, name, length);
  39233. + }
  39234. +
  39235. + return cc;
  39236. +}
  39237. +
  39238. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  39239. +{
  39240. + if (cc->name) {
  39241. + dwc_free(mem_ctx, cc->name);
  39242. + }
  39243. + dwc_free(mem_ctx, cc);
  39244. +}
  39245. +
  39246. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  39247. +{
  39248. + uint32_t uid = 0;
  39249. + dwc_cc_t *cc;
  39250. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39251. + if (cc->uid > uid) {
  39252. + uid = cc->uid;
  39253. + }
  39254. + }
  39255. +
  39256. + if (uid == 0) {
  39257. + uid = 255;
  39258. + }
  39259. +
  39260. + return uid + 1;
  39261. +}
  39262. +
  39263. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  39264. +{
  39265. + dwc_cc_t *cc;
  39266. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39267. + if (cc->uid == uid) {
  39268. + return cc;
  39269. + }
  39270. + }
  39271. + return NULL;
  39272. +}
  39273. +
  39274. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  39275. +{
  39276. + unsigned int size = 0;
  39277. + dwc_cc_t *cc;
  39278. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39279. + size += (48 + 1);
  39280. + if (cc->name) {
  39281. + size += cc->length;
  39282. + }
  39283. + }
  39284. + return size;
  39285. +}
  39286. +
  39287. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  39288. +{
  39289. + uint32_t uid = 0;
  39290. + dwc_cc_t *cc;
  39291. +
  39292. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39293. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  39294. + uid = cc->uid;
  39295. + break;
  39296. + }
  39297. + }
  39298. + return uid;
  39299. +}
  39300. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  39301. +{
  39302. + uint32_t uid = 0;
  39303. + dwc_cc_t *cc;
  39304. +
  39305. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39306. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  39307. + uid = cc->uid;
  39308. + break;
  39309. + }
  39310. + }
  39311. + return uid;
  39312. +}
  39313. +
  39314. +/* Internal cc_add */
  39315. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39316. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  39317. +{
  39318. + dwc_cc_t *cc;
  39319. + uint32_t uid;
  39320. +
  39321. + if (cc_if->is_host) {
  39322. + uid = cc_match_cdid(cc_if, cdid);
  39323. + }
  39324. + else {
  39325. + uid = cc_match_chid(cc_if, chid);
  39326. + }
  39327. +
  39328. + if (uid) {
  39329. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  39330. + cc = cc_find(cc_if, uid);
  39331. + }
  39332. + else {
  39333. + cc = alloc_cc(mem_ctx, name, length);
  39334. + cc->uid = next_uid(cc_if);
  39335. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  39336. + }
  39337. +
  39338. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  39339. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  39340. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  39341. +
  39342. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  39343. + dump_bytes("CHID", cc->chid, 16);
  39344. + dump_bytes("CDID", cc->cdid, 16);
  39345. + dump_bytes("CK", cc->ck, 16);
  39346. + return cc->uid;
  39347. +}
  39348. +
  39349. +/* Internal cc_clear */
  39350. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  39351. +{
  39352. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  39353. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  39354. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  39355. + free_cc(mem_ctx, cc);
  39356. + }
  39357. +}
  39358. +
  39359. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  39360. + dwc_notifier_t *notifier, unsigned is_host)
  39361. +{
  39362. + dwc_cc_if_t *cc_if = NULL;
  39363. +
  39364. + /* Allocate a common_cc_if structure */
  39365. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  39366. +
  39367. + if (!cc_if)
  39368. + return NULL;
  39369. +
  39370. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  39371. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  39372. +#else
  39373. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  39374. +#endif
  39375. + if (!cc_if->mutex) {
  39376. + dwc_free(mem_ctx, cc_if);
  39377. + return NULL;
  39378. + }
  39379. +
  39380. + DWC_CIRCLEQ_INIT(&cc_if->list);
  39381. + cc_if->is_host = is_host;
  39382. + cc_if->notifier = notifier;
  39383. + return cc_if;
  39384. +}
  39385. +
  39386. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  39387. +{
  39388. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  39389. + DWC_MUTEX_FREE(cc_if->mutex);
  39390. +#else
  39391. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  39392. +#endif
  39393. + cc_clear(mem_ctx, cc_if);
  39394. + dwc_free(mem_ctx, cc_if);
  39395. +}
  39396. +
  39397. +static void cc_changed(dwc_cc_if_t *cc_if)
  39398. +{
  39399. + if (cc_if->notifier) {
  39400. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  39401. + }
  39402. +}
  39403. +
  39404. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  39405. +{
  39406. + DWC_MUTEX_LOCK(cc_if->mutex);
  39407. + cc_clear(mem_ctx, cc_if);
  39408. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39409. + cc_changed(cc_if);
  39410. +}
  39411. +
  39412. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39413. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  39414. +{
  39415. + uint32_t uid;
  39416. +
  39417. + DWC_MUTEX_LOCK(cc_if->mutex);
  39418. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  39419. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39420. + cc_changed(cc_if);
  39421. +
  39422. + return uid;
  39423. +}
  39424. +
  39425. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  39426. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  39427. +{
  39428. + dwc_cc_t* cc;
  39429. +
  39430. + DWC_DEBUGC("Change connection context %d", id);
  39431. +
  39432. + DWC_MUTEX_LOCK(cc_if->mutex);
  39433. + cc = cc_find(cc_if, id);
  39434. + if (!cc) {
  39435. + DWC_ERROR("Uid %d not found in cc list\n", id);
  39436. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39437. + return;
  39438. + }
  39439. +
  39440. + if (chid) {
  39441. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  39442. + }
  39443. + if (cdid) {
  39444. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  39445. + }
  39446. + if (ck) {
  39447. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  39448. + }
  39449. +
  39450. + if (name) {
  39451. + if (cc->name) {
  39452. + dwc_free(mem_ctx, cc->name);
  39453. + }
  39454. + cc->name = dwc_alloc(mem_ctx, length);
  39455. + if (!cc->name) {
  39456. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  39457. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39458. + return;
  39459. + }
  39460. + cc->length = length;
  39461. + DWC_MEMCPY(cc->name, name, length);
  39462. + }
  39463. +
  39464. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39465. +
  39466. + cc_changed(cc_if);
  39467. +
  39468. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  39469. + dump_bytes("New CHID", cc->chid, 16);
  39470. + dump_bytes("New CDID", cc->cdid, 16);
  39471. + dump_bytes("New CK", cc->ck, 16);
  39472. +}
  39473. +
  39474. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  39475. +{
  39476. + dwc_cc_t *cc;
  39477. +
  39478. + DWC_DEBUGC("Removing connection context %d", id);
  39479. +
  39480. + DWC_MUTEX_LOCK(cc_if->mutex);
  39481. + cc = cc_find(cc_if, id);
  39482. + if (!cc) {
  39483. + DWC_ERROR("Uid %d not found in cc list\n", id);
  39484. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39485. + return;
  39486. + }
  39487. +
  39488. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  39489. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39490. + free_cc(mem_ctx, cc);
  39491. +
  39492. + cc_changed(cc_if);
  39493. +}
  39494. +
  39495. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  39496. +{
  39497. + uint8_t *buf, *x;
  39498. + uint8_t zero = 0;
  39499. + dwc_cc_t *cc;
  39500. +
  39501. + DWC_MUTEX_LOCK(cc_if->mutex);
  39502. + *length = cc_data_size(cc_if);
  39503. + if (!(*length)) {
  39504. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39505. + return NULL;
  39506. + }
  39507. +
  39508. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  39509. +
  39510. + buf = dwc_alloc(mem_ctx, *length);
  39511. + if (!buf) {
  39512. + *length = 0;
  39513. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39514. + return NULL;
  39515. + }
  39516. +
  39517. + x = buf;
  39518. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  39519. + DWC_MEMCPY(x, cc->chid, 16);
  39520. + x += 16;
  39521. + DWC_MEMCPY(x, cc->cdid, 16);
  39522. + x += 16;
  39523. + DWC_MEMCPY(x, cc->ck, 16);
  39524. + x += 16;
  39525. + if (cc->name) {
  39526. + DWC_MEMCPY(x, &cc->length, 1);
  39527. + x += 1;
  39528. + DWC_MEMCPY(x, cc->name, cc->length);
  39529. + x += cc->length;
  39530. + }
  39531. + else {
  39532. + DWC_MEMCPY(x, &zero, 1);
  39533. + x += 1;
  39534. + }
  39535. + }
  39536. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39537. +
  39538. + return buf;
  39539. +}
  39540. +
  39541. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  39542. +{
  39543. + uint8_t name_length;
  39544. + uint8_t *name;
  39545. + uint8_t *chid;
  39546. + uint8_t *cdid;
  39547. + uint8_t *ck;
  39548. + uint32_t i = 0;
  39549. +
  39550. + DWC_MUTEX_LOCK(cc_if->mutex);
  39551. + cc_clear(mem_ctx, cc_if);
  39552. +
  39553. + while (i < length) {
  39554. + chid = &data[i];
  39555. + i += 16;
  39556. + cdid = &data[i];
  39557. + i += 16;
  39558. + ck = &data[i];
  39559. + i += 16;
  39560. +
  39561. + name_length = data[i];
  39562. + i ++;
  39563. +
  39564. + if (name_length) {
  39565. + name = &data[i];
  39566. + i += name_length;
  39567. + }
  39568. + else {
  39569. + name = NULL;
  39570. + }
  39571. +
  39572. + /* check to see if we haven't overflown the buffer */
  39573. + if (i > length) {
  39574. + DWC_ERROR("Data format error while attempting to load CCs "
  39575. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  39576. + break;
  39577. + }
  39578. +
  39579. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  39580. + }
  39581. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39582. +
  39583. + cc_changed(cc_if);
  39584. +}
  39585. +
  39586. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  39587. +{
  39588. + uint32_t uid = 0;
  39589. +
  39590. + DWC_MUTEX_LOCK(cc_if->mutex);
  39591. + uid = cc_match_chid(cc_if, chid);
  39592. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39593. + return uid;
  39594. +}
  39595. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  39596. +{
  39597. + uint32_t uid = 0;
  39598. +
  39599. + DWC_MUTEX_LOCK(cc_if->mutex);
  39600. + uid = cc_match_cdid(cc_if, cdid);
  39601. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39602. + return uid;
  39603. +}
  39604. +
  39605. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  39606. +{
  39607. + uint8_t *ck = NULL;
  39608. + dwc_cc_t *cc;
  39609. +
  39610. + DWC_MUTEX_LOCK(cc_if->mutex);
  39611. + cc = cc_find(cc_if, id);
  39612. + if (cc) {
  39613. + ck = cc->ck;
  39614. + }
  39615. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39616. +
  39617. + return ck;
  39618. +
  39619. +}
  39620. +
  39621. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  39622. +{
  39623. + uint8_t *retval = NULL;
  39624. + dwc_cc_t *cc;
  39625. +
  39626. + DWC_MUTEX_LOCK(cc_if->mutex);
  39627. + cc = cc_find(cc_if, id);
  39628. + if (cc) {
  39629. + retval = cc->chid;
  39630. + }
  39631. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39632. +
  39633. + return retval;
  39634. +}
  39635. +
  39636. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  39637. +{
  39638. + uint8_t *retval = NULL;
  39639. + dwc_cc_t *cc;
  39640. +
  39641. + DWC_MUTEX_LOCK(cc_if->mutex);
  39642. + cc = cc_find(cc_if, id);
  39643. + if (cc) {
  39644. + retval = cc->cdid;
  39645. + }
  39646. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39647. +
  39648. + return retval;
  39649. +}
  39650. +
  39651. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  39652. +{
  39653. + uint8_t *retval = NULL;
  39654. + dwc_cc_t *cc;
  39655. +
  39656. + DWC_MUTEX_LOCK(cc_if->mutex);
  39657. + *length = 0;
  39658. + cc = cc_find(cc_if, id);
  39659. + if (cc) {
  39660. + *length = cc->length;
  39661. + retval = cc->name;
  39662. + }
  39663. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39664. +
  39665. + return retval;
  39666. +}
  39667. +
  39668. +#endif /* DWC_CCLIB */
  39669. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_cc.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h
  39670. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_cc.h 1970-01-01 01:00:00.000000000 +0100
  39671. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h 2014-04-24 15:35:04.169565731 +0200
  39672. @@ -0,0 +1,225 @@
  39673. +/* =========================================================================
  39674. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  39675. + * $Revision: #4 $
  39676. + * $Date: 2010/09/28 $
  39677. + * $Change: 1596182 $
  39678. + *
  39679. + * Synopsys Portability Library Software and documentation
  39680. + * (hereinafter, "Software") is an Unsupported proprietary work of
  39681. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  39682. + * between Synopsys and you.
  39683. + *
  39684. + * The Software IS NOT an item of Licensed Software or Licensed Product
  39685. + * under any End User Software License Agreement or Agreement for
  39686. + * Licensed Product with Synopsys or any supplement thereto. You are
  39687. + * permitted to use and redistribute this Software in source and binary
  39688. + * forms, with or without modification, provided that redistributions
  39689. + * of source code must retain this notice. You may not view, use,
  39690. + * disclose, copy or distribute this file or any information contained
  39691. + * herein except pursuant to this license grant from Synopsys. If you
  39692. + * do not agree with this notice, including the disclaimer below, then
  39693. + * you are not authorized to use the Software.
  39694. + *
  39695. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  39696. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39697. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39698. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  39699. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39700. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39701. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  39702. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39703. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39704. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39705. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39706. + * DAMAGE.
  39707. + * ========================================================================= */
  39708. +#ifndef _DWC_CC_H_
  39709. +#define _DWC_CC_H_
  39710. +
  39711. +#ifdef __cplusplus
  39712. +extern "C" {
  39713. +#endif
  39714. +
  39715. +/** @file
  39716. + *
  39717. + * This file defines the Context Context library.
  39718. + *
  39719. + * The main data structure is dwc_cc_if_t which is returned by either the
  39720. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  39721. + * function. The data structure is opaque and should only be manipulated via the
  39722. + * functions provied in this API.
  39723. + *
  39724. + * It manages a list of connection contexts and operations can be performed to
  39725. + * add, remove, query, search, and change, those contexts. Additionally,
  39726. + * a dwc_notifier_t object can be requested from the manager so that
  39727. + * the user can be notified whenever the context list has changed.
  39728. + */
  39729. +
  39730. +#include "dwc_os.h"
  39731. +#include "dwc_list.h"
  39732. +#include "dwc_notifier.h"
  39733. +
  39734. +
  39735. +/* Notifications */
  39736. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  39737. +
  39738. +struct dwc_cc_if;
  39739. +typedef struct dwc_cc_if dwc_cc_if_t;
  39740. +
  39741. +
  39742. +/** @name Connection Context Operations */
  39743. +/** @{ */
  39744. +
  39745. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  39746. + * fields to default values, and returns a pointer to the structure or NULL on
  39747. + * error. */
  39748. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  39749. + dwc_notifier_t *notifier, unsigned is_host);
  39750. +
  39751. +/** Frees the memory for the specified CC structure allocated from
  39752. + * dwc_cc_if_alloc(). */
  39753. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  39754. +
  39755. +/** Removes all contexts from the connection context list */
  39756. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  39757. +
  39758. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  39759. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  39760. + * not overwritten.
  39761. + *
  39762. + * @param cc_if The cc_if structure.
  39763. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  39764. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  39765. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  39766. + * @param name An optional host friendly name as defined in the association model
  39767. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  39768. + * @param length The length othe unicode string.
  39769. + * @return A unique identifier used to refer to this context that is valid for
  39770. + * as long as this context is still in the list. */
  39771. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39772. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  39773. + uint8_t length);
  39774. +
  39775. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  39776. + * list, preserving any accumulated statistics. This would typically be called
  39777. + * if the host decideds to change the context with a SET_CONNECTION request.
  39778. + *
  39779. + * @param cc_if The cc_if structure.
  39780. + * @param id The identifier of the connection context.
  39781. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  39782. + * indicates no change.
  39783. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  39784. + * indicates no change.
  39785. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  39786. + * indicates no change.
  39787. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  39788. + * @param length Length of name. */
  39789. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  39790. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  39791. + uint8_t *name, uint8_t length);
  39792. +
  39793. +/** Remove the specified connection context.
  39794. + * @param cc_if The cc_if structure.
  39795. + * @param id The identifier of the connection context to remove. */
  39796. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  39797. +
  39798. +/** Get a binary block of data for the connection context list and attributes.
  39799. + * This data can be used by the OS specific driver to save the connection
  39800. + * context list into non-volatile memory.
  39801. + *
  39802. + * @param cc_if The cc_if structure.
  39803. + * @param length Return the length of the data buffer.
  39804. + * @return A pointer to the data buffer. The memory for this buffer should be
  39805. + * freed with DWC_FREE() after use. */
  39806. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  39807. + unsigned int *length);
  39808. +
  39809. +/** Restore the connection context list from the binary data that was previously
  39810. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  39811. + * driver to load a connection context list from non-volatile memory.
  39812. + *
  39813. + * @param cc_if The cc_if structure.
  39814. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  39815. + * @param length The length of the data. */
  39816. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  39817. + uint8_t *data, unsigned int length);
  39818. +
  39819. +/** Find the connection context from the specified CHID.
  39820. + *
  39821. + * @param cc_if The cc_if structure.
  39822. + * @param chid A pointer to the CHID data.
  39823. + * @return A non-zero identifier of the connection context if the CHID matches.
  39824. + * Otherwise returns 0. */
  39825. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  39826. +
  39827. +/** Find the connection context from the specified CDID.
  39828. + *
  39829. + * @param cc_if The cc_if structure.
  39830. + * @param cdid A pointer to the CDID data.
  39831. + * @return A non-zero identifier of the connection context if the CHID matches.
  39832. + * Otherwise returns 0. */
  39833. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  39834. +
  39835. +/** Retrieve the CK from the specified connection context.
  39836. + *
  39837. + * @param cc_if The cc_if structure.
  39838. + * @param id The identifier of the connection context.
  39839. + * @return A pointer to the CK data. The memory does not need to be freed. */
  39840. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  39841. +
  39842. +/** Retrieve the CHID from the specified connection context.
  39843. + *
  39844. + * @param cc_if The cc_if structure.
  39845. + * @param id The identifier of the connection context.
  39846. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  39847. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  39848. +
  39849. +/** Retrieve the CDID from the specified connection context.
  39850. + *
  39851. + * @param cc_if The cc_if structure.
  39852. + * @param id The identifier of the connection context.
  39853. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  39854. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  39855. +
  39856. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  39857. +
  39858. +/** Checks a buffer for non-zero.
  39859. + * @param id A pointer to a 16 byte buffer.
  39860. + * @return true if the 16 byte value is non-zero. */
  39861. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  39862. + int i;
  39863. + for (i=0; i<16; i++) {
  39864. + if (id[i]) return 1;
  39865. + }
  39866. + return 0;
  39867. +}
  39868. +
  39869. +/** Checks a buffer for zero.
  39870. + * @param id A pointer to a 16 byte buffer.
  39871. + * @return true if the 16 byte value is zero. */
  39872. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  39873. + return !dwc_assoc_is_not_zero_id(id);
  39874. +}
  39875. +
  39876. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  39877. + * buffer. */
  39878. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  39879. + char *ptr = buffer;
  39880. + int i;
  39881. + for (i=0; i<16; i++) {
  39882. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  39883. + if (i < 15) {
  39884. + ptr += DWC_SPRINTF(ptr, " ");
  39885. + }
  39886. + }
  39887. + return ptr - buffer;
  39888. +}
  39889. +
  39890. +/** @} */
  39891. +
  39892. +#ifdef __cplusplus
  39893. +}
  39894. +#endif
  39895. +
  39896. +#endif /* _DWC_CC_H_ */
  39897. +
  39898. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  39899. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 1970-01-01 01:00:00.000000000 +0100
  39900. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 2014-04-24 15:35:04.169565731 +0200
  39901. @@ -0,0 +1,1308 @@
  39902. +#include "dwc_os.h"
  39903. +#include "dwc_list.h"
  39904. +
  39905. +#ifdef DWC_CCLIB
  39906. +# include "dwc_cc.h"
  39907. +#endif
  39908. +
  39909. +#ifdef DWC_CRYPTOLIB
  39910. +# include "dwc_modpow.h"
  39911. +# include "dwc_dh.h"
  39912. +# include "dwc_crypto.h"
  39913. +#endif
  39914. +
  39915. +#ifdef DWC_NOTIFYLIB
  39916. +# include "dwc_notifier.h"
  39917. +#endif
  39918. +
  39919. +/* OS-Level Implementations */
  39920. +
  39921. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  39922. +
  39923. +
  39924. +/* MISC */
  39925. +
  39926. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  39927. +{
  39928. + return memset(dest, byte, size);
  39929. +}
  39930. +
  39931. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  39932. +{
  39933. + return memcpy(dest, src, size);
  39934. +}
  39935. +
  39936. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  39937. +{
  39938. + bcopy(src, dest, size);
  39939. + return dest;
  39940. +}
  39941. +
  39942. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  39943. +{
  39944. + return memcmp(m1, m2, size);
  39945. +}
  39946. +
  39947. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  39948. +{
  39949. + return strncmp(s1, s2, size);
  39950. +}
  39951. +
  39952. +int DWC_STRCMP(void *s1, void *s2)
  39953. +{
  39954. + return strcmp(s1, s2);
  39955. +}
  39956. +
  39957. +int DWC_STRLEN(char const *str)
  39958. +{
  39959. + return strlen(str);
  39960. +}
  39961. +
  39962. +char *DWC_STRCPY(char *to, char const *from)
  39963. +{
  39964. + return strcpy(to, from);
  39965. +}
  39966. +
  39967. +char *DWC_STRDUP(char const *str)
  39968. +{
  39969. + int len = DWC_STRLEN(str) + 1;
  39970. + char *new = DWC_ALLOC_ATOMIC(len);
  39971. +
  39972. + if (!new) {
  39973. + return NULL;
  39974. + }
  39975. +
  39976. + DWC_MEMCPY(new, str, len);
  39977. + return new;
  39978. +}
  39979. +
  39980. +int DWC_ATOI(char *str, int32_t *value)
  39981. +{
  39982. + char *end = NULL;
  39983. +
  39984. + *value = strtol(str, &end, 0);
  39985. + if (*end == '\0') {
  39986. + return 0;
  39987. + }
  39988. +
  39989. + return -1;
  39990. +}
  39991. +
  39992. +int DWC_ATOUI(char *str, uint32_t *value)
  39993. +{
  39994. + char *end = NULL;
  39995. +
  39996. + *value = strtoul(str, &end, 0);
  39997. + if (*end == '\0') {
  39998. + return 0;
  39999. + }
  40000. +
  40001. + return -1;
  40002. +}
  40003. +
  40004. +
  40005. +#ifdef DWC_UTFLIB
  40006. +/* From usbstring.c */
  40007. +
  40008. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  40009. +{
  40010. + int count = 0;
  40011. + u8 c;
  40012. + u16 uchar;
  40013. +
  40014. + /* this insists on correct encodings, though not minimal ones.
  40015. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  40016. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  40017. + */
  40018. + while (len != 0 && (c = (u8) *s++) != 0) {
  40019. + if (unlikely(c & 0x80)) {
  40020. + // 2-byte sequence:
  40021. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  40022. + if ((c & 0xe0) == 0xc0) {
  40023. + uchar = (c & 0x1f) << 6;
  40024. +
  40025. + c = (u8) *s++;
  40026. + if ((c & 0xc0) != 0xc0)
  40027. + goto fail;
  40028. + c &= 0x3f;
  40029. + uchar |= c;
  40030. +
  40031. + // 3-byte sequence (most CJKV characters):
  40032. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  40033. + } else if ((c & 0xf0) == 0xe0) {
  40034. + uchar = (c & 0x0f) << 12;
  40035. +
  40036. + c = (u8) *s++;
  40037. + if ((c & 0xc0) != 0xc0)
  40038. + goto fail;
  40039. + c &= 0x3f;
  40040. + uchar |= c << 6;
  40041. +
  40042. + c = (u8) *s++;
  40043. + if ((c & 0xc0) != 0xc0)
  40044. + goto fail;
  40045. + c &= 0x3f;
  40046. + uchar |= c;
  40047. +
  40048. + /* no bogus surrogates */
  40049. + if (0xd800 <= uchar && uchar <= 0xdfff)
  40050. + goto fail;
  40051. +
  40052. + // 4-byte sequence (surrogate pairs, currently rare):
  40053. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  40054. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  40055. + // (uuuuu = wwww + 1)
  40056. + // FIXME accept the surrogate code points (only)
  40057. + } else
  40058. + goto fail;
  40059. + } else
  40060. + uchar = c;
  40061. + put_unaligned (cpu_to_le16 (uchar), cp++);
  40062. + count++;
  40063. + len--;
  40064. + }
  40065. + return count;
  40066. +fail:
  40067. + return -1;
  40068. +}
  40069. +
  40070. +#endif /* DWC_UTFLIB */
  40071. +
  40072. +
  40073. +/* dwc_debug.h */
  40074. +
  40075. +dwc_bool_t DWC_IN_IRQ(void)
  40076. +{
  40077. +// return in_irq();
  40078. + return 0;
  40079. +}
  40080. +
  40081. +dwc_bool_t DWC_IN_BH(void)
  40082. +{
  40083. +// return in_softirq();
  40084. + return 0;
  40085. +}
  40086. +
  40087. +void DWC_VPRINTF(char *format, va_list args)
  40088. +{
  40089. + vprintf(format, args);
  40090. +}
  40091. +
  40092. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  40093. +{
  40094. + return vsnprintf(str, size, format, args);
  40095. +}
  40096. +
  40097. +void DWC_PRINTF(char *format, ...)
  40098. +{
  40099. + va_list args;
  40100. +
  40101. + va_start(args, format);
  40102. + DWC_VPRINTF(format, args);
  40103. + va_end(args);
  40104. +}
  40105. +
  40106. +int DWC_SPRINTF(char *buffer, char *format, ...)
  40107. +{
  40108. + int retval;
  40109. + va_list args;
  40110. +
  40111. + va_start(args, format);
  40112. + retval = vsprintf(buffer, format, args);
  40113. + va_end(args);
  40114. + return retval;
  40115. +}
  40116. +
  40117. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  40118. +{
  40119. + int retval;
  40120. + va_list args;
  40121. +
  40122. + va_start(args, format);
  40123. + retval = vsnprintf(buffer, size, format, args);
  40124. + va_end(args);
  40125. + return retval;
  40126. +}
  40127. +
  40128. +void __DWC_WARN(char *format, ...)
  40129. +{
  40130. + va_list args;
  40131. +
  40132. + va_start(args, format);
  40133. + DWC_VPRINTF(format, args);
  40134. + va_end(args);
  40135. +}
  40136. +
  40137. +void __DWC_ERROR(char *format, ...)
  40138. +{
  40139. + va_list args;
  40140. +
  40141. + va_start(args, format);
  40142. + DWC_VPRINTF(format, args);
  40143. + va_end(args);
  40144. +}
  40145. +
  40146. +void DWC_EXCEPTION(char *format, ...)
  40147. +{
  40148. + va_list args;
  40149. +
  40150. + va_start(args, format);
  40151. + DWC_VPRINTF(format, args);
  40152. + va_end(args);
  40153. +// BUG_ON(1); ???
  40154. +}
  40155. +
  40156. +#ifdef DEBUG
  40157. +void __DWC_DEBUG(char *format, ...)
  40158. +{
  40159. + va_list args;
  40160. +
  40161. + va_start(args, format);
  40162. + DWC_VPRINTF(format, args);
  40163. + va_end(args);
  40164. +}
  40165. +#endif
  40166. +
  40167. +
  40168. +/* dwc_mem.h */
  40169. +
  40170. +#if 0
  40171. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  40172. + uint32_t align,
  40173. + uint32_t alloc)
  40174. +{
  40175. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  40176. + size, align, alloc);
  40177. + return (dwc_pool_t *)pool;
  40178. +}
  40179. +
  40180. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  40181. +{
  40182. + dma_pool_destroy((struct dma_pool *)pool);
  40183. +}
  40184. +
  40185. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  40186. +{
  40187. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  40188. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  40189. +}
  40190. +
  40191. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  40192. +{
  40193. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  40194. + memset(..);
  40195. +}
  40196. +
  40197. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  40198. +{
  40199. + dma_pool_free(pool, vaddr, daddr);
  40200. +}
  40201. +#endif
  40202. +
  40203. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  40204. +{
  40205. + if (error)
  40206. + return;
  40207. + *(bus_addr_t *)arg = segs[0].ds_addr;
  40208. +}
  40209. +
  40210. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  40211. +{
  40212. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  40213. + int error;
  40214. +
  40215. + error = bus_dma_tag_create(
  40216. +#if __FreeBSD_version >= 700000
  40217. + bus_get_dma_tag(dma->dev), /* parent */
  40218. +#else
  40219. + NULL, /* parent */
  40220. +#endif
  40221. + 4, 0, /* alignment, bounds */
  40222. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  40223. + BUS_SPACE_MAXADDR, /* highaddr */
  40224. + NULL, NULL, /* filter, filterarg */
  40225. + size, /* maxsize */
  40226. + 1, /* nsegments */
  40227. + size, /* maxsegsize */
  40228. + 0, /* flags */
  40229. + NULL, /* lockfunc */
  40230. + NULL, /* lockarg */
  40231. + &dma->dma_tag);
  40232. + if (error) {
  40233. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  40234. + __func__, error);
  40235. + goto fail_0;
  40236. + }
  40237. +
  40238. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  40239. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  40240. + if (error) {
  40241. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  40242. + __func__, (uintmax_t)size, error);
  40243. + goto fail_1;
  40244. + }
  40245. +
  40246. + dma->dma_paddr = 0;
  40247. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  40248. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  40249. + if (error || dma->dma_paddr == 0) {
  40250. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  40251. + __func__, error);
  40252. + goto fail_2;
  40253. + }
  40254. +
  40255. + *dma_addr = dma->dma_paddr;
  40256. + return dma->dma_vaddr;
  40257. +
  40258. +fail_2:
  40259. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  40260. +fail_1:
  40261. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  40262. + bus_dma_tag_destroy(dma->dma_tag);
  40263. +fail_0:
  40264. + dma->dma_map = NULL;
  40265. + dma->dma_tag = NULL;
  40266. +
  40267. + return NULL;
  40268. +}
  40269. +
  40270. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  40271. +{
  40272. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  40273. +
  40274. + if (dma->dma_tag == NULL)
  40275. + return;
  40276. + if (dma->dma_map != NULL) {
  40277. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  40278. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  40279. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  40280. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  40281. + dma->dma_map = NULL;
  40282. + }
  40283. +
  40284. + bus_dma_tag_destroy(dma->dma_tag);
  40285. + dma->dma_tag = NULL;
  40286. +}
  40287. +
  40288. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  40289. +{
  40290. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  40291. +}
  40292. +
  40293. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  40294. +{
  40295. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  40296. +}
  40297. +
  40298. +void __DWC_FREE(void *mem_ctx, void *addr)
  40299. +{
  40300. + free(addr, M_DEVBUF);
  40301. +}
  40302. +
  40303. +
  40304. +#ifdef DWC_CRYPTOLIB
  40305. +/* dwc_crypto.h */
  40306. +
  40307. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  40308. +{
  40309. + get_random_bytes(buffer, length);
  40310. +}
  40311. +
  40312. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  40313. +{
  40314. + struct crypto_blkcipher *tfm;
  40315. + struct blkcipher_desc desc;
  40316. + struct scatterlist sgd;
  40317. + struct scatterlist sgs;
  40318. +
  40319. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  40320. + if (tfm == NULL) {
  40321. + printk("failed to load transform for aes CBC\n");
  40322. + return -1;
  40323. + }
  40324. +
  40325. + crypto_blkcipher_setkey(tfm, key, keylen);
  40326. + crypto_blkcipher_set_iv(tfm, iv, 16);
  40327. +
  40328. + sg_init_one(&sgd, out, messagelen);
  40329. + sg_init_one(&sgs, message, messagelen);
  40330. +
  40331. + desc.tfm = tfm;
  40332. + desc.flags = 0;
  40333. +
  40334. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  40335. + crypto_free_blkcipher(tfm);
  40336. + DWC_ERROR("AES CBC encryption failed");
  40337. + return -1;
  40338. + }
  40339. +
  40340. + crypto_free_blkcipher(tfm);
  40341. + return 0;
  40342. +}
  40343. +
  40344. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  40345. +{
  40346. + struct crypto_hash *tfm;
  40347. + struct hash_desc desc;
  40348. + struct scatterlist sg;
  40349. +
  40350. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  40351. + if (IS_ERR(tfm)) {
  40352. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  40353. + return 0;
  40354. + }
  40355. + desc.tfm = tfm;
  40356. + desc.flags = 0;
  40357. +
  40358. + sg_init_one(&sg, message, len);
  40359. + crypto_hash_digest(&desc, &sg, len, out);
  40360. + crypto_free_hash(tfm);
  40361. +
  40362. + return 1;
  40363. +}
  40364. +
  40365. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  40366. + uint8_t *key, uint32_t keylen, uint8_t *out)
  40367. +{
  40368. + struct crypto_hash *tfm;
  40369. + struct hash_desc desc;
  40370. + struct scatterlist sg;
  40371. +
  40372. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  40373. + if (IS_ERR(tfm)) {
  40374. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  40375. + return 0;
  40376. + }
  40377. + desc.tfm = tfm;
  40378. + desc.flags = 0;
  40379. +
  40380. + sg_init_one(&sg, message, messagelen);
  40381. + crypto_hash_setkey(tfm, key, keylen);
  40382. + crypto_hash_digest(&desc, &sg, messagelen, out);
  40383. + crypto_free_hash(tfm);
  40384. +
  40385. + return 1;
  40386. +}
  40387. +
  40388. +#endif /* DWC_CRYPTOLIB */
  40389. +
  40390. +
  40391. +/* Byte Ordering Conversions */
  40392. +
  40393. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  40394. +{
  40395. +#ifdef __LITTLE_ENDIAN
  40396. + return *p;
  40397. +#else
  40398. + uint8_t *u_p = (uint8_t *)p;
  40399. +
  40400. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40401. +#endif
  40402. +}
  40403. +
  40404. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  40405. +{
  40406. +#ifdef __BIG_ENDIAN
  40407. + return *p;
  40408. +#else
  40409. + uint8_t *u_p = (uint8_t *)p;
  40410. +
  40411. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40412. +#endif
  40413. +}
  40414. +
  40415. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  40416. +{
  40417. +#ifdef __LITTLE_ENDIAN
  40418. + return *p;
  40419. +#else
  40420. + uint8_t *u_p = (uint8_t *)p;
  40421. +
  40422. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40423. +#endif
  40424. +}
  40425. +
  40426. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  40427. +{
  40428. +#ifdef __BIG_ENDIAN
  40429. + return *p;
  40430. +#else
  40431. + uint8_t *u_p = (uint8_t *)p;
  40432. +
  40433. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  40434. +#endif
  40435. +}
  40436. +
  40437. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  40438. +{
  40439. +#ifdef __LITTLE_ENDIAN
  40440. + return *p;
  40441. +#else
  40442. + uint8_t *u_p = (uint8_t *)p;
  40443. + return (u_p[1] | (u_p[0] << 8));
  40444. +#endif
  40445. +}
  40446. +
  40447. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  40448. +{
  40449. +#ifdef __BIG_ENDIAN
  40450. + return *p;
  40451. +#else
  40452. + uint8_t *u_p = (uint8_t *)p;
  40453. + return (u_p[1] | (u_p[0] << 8));
  40454. +#endif
  40455. +}
  40456. +
  40457. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  40458. +{
  40459. +#ifdef __LITTLE_ENDIAN
  40460. + return *p;
  40461. +#else
  40462. + uint8_t *u_p = (uint8_t *)p;
  40463. + return (u_p[1] | (u_p[0] << 8));
  40464. +#endif
  40465. +}
  40466. +
  40467. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  40468. +{
  40469. +#ifdef __BIG_ENDIAN
  40470. + return *p;
  40471. +#else
  40472. + uint8_t *u_p = (uint8_t *)p;
  40473. + return (u_p[1] | (u_p[0] << 8));
  40474. +#endif
  40475. +}
  40476. +
  40477. +
  40478. +/* Registers */
  40479. +
  40480. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  40481. +{
  40482. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40483. + bus_size_t ior = (bus_size_t)reg;
  40484. +
  40485. + return bus_space_read_4(io->iot, io->ioh, ior);
  40486. +}
  40487. +
  40488. +#if 0
  40489. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  40490. +{
  40491. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40492. + bus_size_t ior = (bus_size_t)reg;
  40493. +
  40494. + return bus_space_read_8(io->iot, io->ioh, ior);
  40495. +}
  40496. +#endif
  40497. +
  40498. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  40499. +{
  40500. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40501. + bus_size_t ior = (bus_size_t)reg;
  40502. +
  40503. + bus_space_write_4(io->iot, io->ioh, ior, value);
  40504. +}
  40505. +
  40506. +#if 0
  40507. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  40508. +{
  40509. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40510. + bus_size_t ior = (bus_size_t)reg;
  40511. +
  40512. + bus_space_write_8(io->iot, io->ioh, ior, value);
  40513. +}
  40514. +#endif
  40515. +
  40516. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  40517. + uint32_t set_mask)
  40518. +{
  40519. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40520. + bus_size_t ior = (bus_size_t)reg;
  40521. +
  40522. + bus_space_write_4(io->iot, io->ioh, ior,
  40523. + (bus_space_read_4(io->iot, io->ioh, ior) &
  40524. + ~clear_mask) | set_mask);
  40525. +}
  40526. +
  40527. +#if 0
  40528. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  40529. + uint64_t set_mask)
  40530. +{
  40531. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  40532. + bus_size_t ior = (bus_size_t)reg;
  40533. +
  40534. + bus_space_write_8(io->iot, io->ioh, ior,
  40535. + (bus_space_read_8(io->iot, io->ioh, ior) &
  40536. + ~clear_mask) | set_mask);
  40537. +}
  40538. +#endif
  40539. +
  40540. +
  40541. +/* Locking */
  40542. +
  40543. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  40544. +{
  40545. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  40546. +
  40547. + if (!sl) {
  40548. + DWC_ERROR("Cannot allocate memory for spinlock");
  40549. + return NULL;
  40550. + }
  40551. +
  40552. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  40553. + return (dwc_spinlock_t *)sl;
  40554. +}
  40555. +
  40556. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  40557. +{
  40558. + struct mtx *sl = (struct mtx *)lock;
  40559. +
  40560. + mtx_destroy(sl);
  40561. + DWC_FREE(sl);
  40562. +}
  40563. +
  40564. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  40565. +{
  40566. + mtx_lock_spin((struct mtx *)lock); // ???
  40567. +}
  40568. +
  40569. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  40570. +{
  40571. + mtx_unlock_spin((struct mtx *)lock); // ???
  40572. +}
  40573. +
  40574. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  40575. +{
  40576. + mtx_lock_spin((struct mtx *)lock);
  40577. +}
  40578. +
  40579. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  40580. +{
  40581. + mtx_unlock_spin((struct mtx *)lock);
  40582. +}
  40583. +
  40584. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  40585. +{
  40586. + struct mtx *m;
  40587. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  40588. +
  40589. + if (!mutex) {
  40590. + DWC_ERROR("Cannot allocate memory for mutex");
  40591. + return NULL;
  40592. + }
  40593. +
  40594. + m = (struct mtx *)mutex;
  40595. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  40596. + return mutex;
  40597. +}
  40598. +
  40599. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  40600. +#else
  40601. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  40602. +{
  40603. + mtx_destroy((struct mtx *)mutex);
  40604. + DWC_FREE(mutex);
  40605. +}
  40606. +#endif
  40607. +
  40608. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  40609. +{
  40610. + struct mtx *m = (struct mtx *)mutex;
  40611. +
  40612. + mtx_lock(m);
  40613. +}
  40614. +
  40615. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  40616. +{
  40617. + struct mtx *m = (struct mtx *)mutex;
  40618. +
  40619. + return mtx_trylock(m);
  40620. +}
  40621. +
  40622. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  40623. +{
  40624. + struct mtx *m = (struct mtx *)mutex;
  40625. +
  40626. + mtx_unlock(m);
  40627. +}
  40628. +
  40629. +
  40630. +/* Timing */
  40631. +
  40632. +void DWC_UDELAY(uint32_t usecs)
  40633. +{
  40634. + DELAY(usecs);
  40635. +}
  40636. +
  40637. +void DWC_MDELAY(uint32_t msecs)
  40638. +{
  40639. + do {
  40640. + DELAY(1000);
  40641. + } while (--msecs);
  40642. +}
  40643. +
  40644. +void DWC_MSLEEP(uint32_t msecs)
  40645. +{
  40646. + struct timeval tv;
  40647. +
  40648. + tv.tv_sec = msecs / 1000;
  40649. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  40650. + pause("dw3slp", tvtohz(&tv));
  40651. +}
  40652. +
  40653. +uint32_t DWC_TIME(void)
  40654. +{
  40655. + struct timeval tv;
  40656. +
  40657. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  40658. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  40659. +}
  40660. +
  40661. +
  40662. +/* Timers */
  40663. +
  40664. +struct dwc_timer {
  40665. + struct callout t;
  40666. + char *name;
  40667. + dwc_spinlock_t *lock;
  40668. + dwc_timer_callback_t cb;
  40669. + void *data;
  40670. +};
  40671. +
  40672. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  40673. +{
  40674. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  40675. +
  40676. + if (!t) {
  40677. + DWC_ERROR("Cannot allocate memory for timer");
  40678. + return NULL;
  40679. + }
  40680. +
  40681. + callout_init(&t->t, 1);
  40682. +
  40683. + t->name = DWC_STRDUP(name);
  40684. + if (!t->name) {
  40685. + DWC_ERROR("Cannot allocate memory for timer->name");
  40686. + goto no_name;
  40687. + }
  40688. +
  40689. + t->lock = DWC_SPINLOCK_ALLOC();
  40690. + if (!t->lock) {
  40691. + DWC_ERROR("Cannot allocate memory for lock");
  40692. + goto no_lock;
  40693. + }
  40694. +
  40695. + t->cb = cb;
  40696. + t->data = data;
  40697. +
  40698. + return t;
  40699. +
  40700. + no_lock:
  40701. + DWC_FREE(t->name);
  40702. + no_name:
  40703. + DWC_FREE(t);
  40704. +
  40705. + return NULL;
  40706. +}
  40707. +
  40708. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  40709. +{
  40710. + callout_stop(&timer->t);
  40711. + DWC_SPINLOCK_FREE(timer->lock);
  40712. + DWC_FREE(timer->name);
  40713. + DWC_FREE(timer);
  40714. +}
  40715. +
  40716. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  40717. +{
  40718. + struct timeval tv;
  40719. +
  40720. + tv.tv_sec = time / 1000;
  40721. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  40722. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  40723. +}
  40724. +
  40725. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  40726. +{
  40727. + callout_stop(&timer->t);
  40728. +}
  40729. +
  40730. +
  40731. +/* Wait Queues */
  40732. +
  40733. +struct dwc_waitq {
  40734. + struct mtx lock;
  40735. + int abort;
  40736. +};
  40737. +
  40738. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  40739. +{
  40740. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  40741. +
  40742. + if (!wq) {
  40743. + DWC_ERROR("Cannot allocate memory for waitqueue");
  40744. + return NULL;
  40745. + }
  40746. +
  40747. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  40748. + wq->abort = 0;
  40749. +
  40750. + return wq;
  40751. +}
  40752. +
  40753. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  40754. +{
  40755. + mtx_destroy(&wq->lock);
  40756. + DWC_FREE(wq);
  40757. +}
  40758. +
  40759. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  40760. +{
  40761. +// intrmask_t ipl;
  40762. + int result = 0;
  40763. +
  40764. + mtx_lock(&wq->lock);
  40765. +// ipl = splbio();
  40766. +
  40767. + /* Skip the sleep if already aborted or triggered */
  40768. + if (!wq->abort && !cond(data)) {
  40769. +// splx(ipl);
  40770. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  40771. +// ipl = splbio();
  40772. + }
  40773. +
  40774. + if (result == ERESTART) { // signaled - restart
  40775. + result = -DWC_E_RESTART;
  40776. +
  40777. + } else if (result == EINTR) { // signaled - interrupt
  40778. + result = -DWC_E_ABORT;
  40779. +
  40780. + } else if (wq->abort) {
  40781. + result = -DWC_E_ABORT;
  40782. +
  40783. + } else {
  40784. + result = 0;
  40785. + }
  40786. +
  40787. + wq->abort = 0;
  40788. +// splx(ipl);
  40789. + mtx_unlock(&wq->lock);
  40790. + return result;
  40791. +}
  40792. +
  40793. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  40794. + void *data, int32_t msecs)
  40795. +{
  40796. + struct timeval tv, tv1, tv2;
  40797. +// intrmask_t ipl;
  40798. + int result = 0;
  40799. +
  40800. + tv.tv_sec = msecs / 1000;
  40801. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  40802. +
  40803. + mtx_lock(&wq->lock);
  40804. +// ipl = splbio();
  40805. +
  40806. + /* Skip the sleep if already aborted or triggered */
  40807. + if (!wq->abort && !cond(data)) {
  40808. +// splx(ipl);
  40809. + getmicrouptime(&tv1);
  40810. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  40811. + getmicrouptime(&tv2);
  40812. +// ipl = splbio();
  40813. + }
  40814. +
  40815. + if (result == 0) { // awoken
  40816. + if (wq->abort) {
  40817. + result = -DWC_E_ABORT;
  40818. + } else {
  40819. + tv2.tv_usec -= tv1.tv_usec;
  40820. + if (tv2.tv_usec < 0) {
  40821. + tv2.tv_usec += 1000000;
  40822. + tv2.tv_sec--;
  40823. + }
  40824. +
  40825. + tv2.tv_sec -= tv1.tv_sec;
  40826. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  40827. + result = msecs - result;
  40828. + if (result <= 0)
  40829. + result = 1;
  40830. + }
  40831. + } else if (result == ERESTART) { // signaled - restart
  40832. + result = -DWC_E_RESTART;
  40833. +
  40834. + } else if (result == EINTR) { // signaled - interrupt
  40835. + result = -DWC_E_ABORT;
  40836. +
  40837. + } else { // timed out
  40838. + result = -DWC_E_TIMEOUT;
  40839. + }
  40840. +
  40841. + wq->abort = 0;
  40842. +// splx(ipl);
  40843. + mtx_unlock(&wq->lock);
  40844. + return result;
  40845. +}
  40846. +
  40847. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  40848. +{
  40849. + wakeup(wq);
  40850. +}
  40851. +
  40852. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  40853. +{
  40854. +// intrmask_t ipl;
  40855. +
  40856. + mtx_lock(&wq->lock);
  40857. +// ipl = splbio();
  40858. + wq->abort = 1;
  40859. + wakeup(wq);
  40860. +// splx(ipl);
  40861. + mtx_unlock(&wq->lock);
  40862. +}
  40863. +
  40864. +
  40865. +/* Threading */
  40866. +
  40867. +struct dwc_thread {
  40868. + struct proc *proc;
  40869. + int abort;
  40870. +};
  40871. +
  40872. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  40873. +{
  40874. + int retval;
  40875. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  40876. +
  40877. + if (!thread) {
  40878. + return NULL;
  40879. + }
  40880. +
  40881. + thread->abort = 0;
  40882. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  40883. + RFPROC | RFNOWAIT, 0, "%s", name);
  40884. + if (retval) {
  40885. + DWC_FREE(thread);
  40886. + return NULL;
  40887. + }
  40888. +
  40889. + return thread;
  40890. +}
  40891. +
  40892. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  40893. +{
  40894. + int retval;
  40895. +
  40896. + thread->abort = 1;
  40897. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  40898. +
  40899. + if (retval == 0) {
  40900. + /* DWC_THREAD_EXIT() will free the thread struct */
  40901. + return 0;
  40902. + }
  40903. +
  40904. + /* NOTE: We leak the thread struct if thread doesn't die */
  40905. +
  40906. + if (retval == EWOULDBLOCK) {
  40907. + return -DWC_E_TIMEOUT;
  40908. + }
  40909. +
  40910. + return -DWC_E_UNKNOWN;
  40911. +}
  40912. +
  40913. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  40914. +{
  40915. + return thread->abort;
  40916. +}
  40917. +
  40918. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  40919. +{
  40920. + wakeup(&thread->abort);
  40921. + DWC_FREE(thread);
  40922. + kthread_exit(0);
  40923. +}
  40924. +
  40925. +
  40926. +/* tasklets
  40927. + - Runs in interrupt context (cannot sleep)
  40928. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  40929. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  40930. + */
  40931. +struct dwc_tasklet {
  40932. + struct task t;
  40933. + dwc_tasklet_callback_t cb;
  40934. + void *data;
  40935. +};
  40936. +
  40937. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  40938. +{
  40939. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  40940. +
  40941. + task->cb(task->data);
  40942. +}
  40943. +
  40944. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  40945. +{
  40946. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  40947. +
  40948. + if (task) {
  40949. + task->cb = cb;
  40950. + task->data = data;
  40951. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  40952. + } else {
  40953. + DWC_ERROR("Cannot allocate memory for tasklet");
  40954. + }
  40955. +
  40956. + return task;
  40957. +}
  40958. +
  40959. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  40960. +{
  40961. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  40962. + DWC_FREE(task);
  40963. +}
  40964. +
  40965. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  40966. +{
  40967. + /* Uses predefined system queue */
  40968. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  40969. +}
  40970. +
  40971. +
  40972. +/* workqueues
  40973. + - Runs in process context (can sleep)
  40974. + */
  40975. +typedef struct work_container {
  40976. + dwc_work_callback_t cb;
  40977. + void *data;
  40978. + dwc_workq_t *wq;
  40979. + char *name;
  40980. + int hz;
  40981. +
  40982. +#ifdef DEBUG
  40983. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  40984. +#endif
  40985. + struct task task;
  40986. +} work_container_t;
  40987. +
  40988. +#ifdef DEBUG
  40989. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  40990. +#endif
  40991. +
  40992. +struct dwc_workq {
  40993. + struct taskqueue *taskq;
  40994. + dwc_spinlock_t *lock;
  40995. + dwc_waitq_t *waitq;
  40996. + int pending;
  40997. +
  40998. +#ifdef DEBUG
  40999. + struct work_container_queue entries;
  41000. +#endif
  41001. +};
  41002. +
  41003. +static void do_work(void *data, int pending) // what to do with pending ???
  41004. +{
  41005. + work_container_t *container = (work_container_t *)data;
  41006. + dwc_workq_t *wq = container->wq;
  41007. + dwc_irqflags_t flags;
  41008. +
  41009. + if (container->hz) {
  41010. + pause("dw3wrk", container->hz);
  41011. + }
  41012. +
  41013. + container->cb(container->data);
  41014. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  41015. +
  41016. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41017. +
  41018. +#ifdef DEBUG
  41019. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  41020. +#endif
  41021. + if (container->name)
  41022. + DWC_FREE(container->name);
  41023. + DWC_FREE(container);
  41024. + wq->pending--;
  41025. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41026. + DWC_WAITQ_TRIGGER(wq->waitq);
  41027. +}
  41028. +
  41029. +static int work_done(void *data)
  41030. +{
  41031. + dwc_workq_t *workq = (dwc_workq_t *)data;
  41032. +
  41033. + return workq->pending == 0;
  41034. +}
  41035. +
  41036. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  41037. +{
  41038. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  41039. +}
  41040. +
  41041. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  41042. +{
  41043. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  41044. +
  41045. + if (!wq) {
  41046. + DWC_ERROR("Cannot allocate memory for workqueue");
  41047. + return NULL;
  41048. + }
  41049. +
  41050. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  41051. + if (!wq->taskq) {
  41052. + DWC_ERROR("Cannot allocate memory for taskqueue");
  41053. + goto no_taskq;
  41054. + }
  41055. +
  41056. + wq->pending = 0;
  41057. +
  41058. + wq->lock = DWC_SPINLOCK_ALLOC();
  41059. + if (!wq->lock) {
  41060. + DWC_ERROR("Cannot allocate memory for spinlock");
  41061. + goto no_lock;
  41062. + }
  41063. +
  41064. + wq->waitq = DWC_WAITQ_ALLOC();
  41065. + if (!wq->waitq) {
  41066. + DWC_ERROR("Cannot allocate memory for waitqueue");
  41067. + goto no_waitq;
  41068. + }
  41069. +
  41070. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  41071. +
  41072. +#ifdef DEBUG
  41073. + DWC_CIRCLEQ_INIT(&wq->entries);
  41074. +#endif
  41075. + return wq;
  41076. +
  41077. + no_waitq:
  41078. + DWC_SPINLOCK_FREE(wq->lock);
  41079. + no_lock:
  41080. + taskqueue_free(wq->taskq);
  41081. + no_taskq:
  41082. + DWC_FREE(wq);
  41083. +
  41084. + return NULL;
  41085. +}
  41086. +
  41087. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  41088. +{
  41089. +#ifdef DEBUG
  41090. + dwc_irqflags_t flags;
  41091. +
  41092. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41093. +
  41094. + if (wq->pending != 0) {
  41095. + struct work_container *container;
  41096. +
  41097. + DWC_ERROR("Destroying work queue with pending work");
  41098. +
  41099. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  41100. + DWC_ERROR("Work %s still pending", container->name);
  41101. + }
  41102. + }
  41103. +
  41104. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41105. +#endif
  41106. + DWC_WAITQ_FREE(wq->waitq);
  41107. + DWC_SPINLOCK_FREE(wq->lock);
  41108. + taskqueue_free(wq->taskq);
  41109. + DWC_FREE(wq);
  41110. +}
  41111. +
  41112. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  41113. + char *format, ...)
  41114. +{
  41115. + dwc_irqflags_t flags;
  41116. + work_container_t *container;
  41117. + static char name[128];
  41118. + va_list args;
  41119. +
  41120. + va_start(args, format);
  41121. + DWC_VSNPRINTF(name, 128, format, args);
  41122. + va_end(args);
  41123. +
  41124. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41125. + wq->pending++;
  41126. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41127. + DWC_WAITQ_TRIGGER(wq->waitq);
  41128. +
  41129. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  41130. + if (!container) {
  41131. + DWC_ERROR("Cannot allocate memory for container");
  41132. + return;
  41133. + }
  41134. +
  41135. + container->name = DWC_STRDUP(name);
  41136. + if (!container->name) {
  41137. + DWC_ERROR("Cannot allocate memory for container->name");
  41138. + DWC_FREE(container);
  41139. + return;
  41140. + }
  41141. +
  41142. + container->cb = cb;
  41143. + container->data = data;
  41144. + container->wq = wq;
  41145. + container->hz = 0;
  41146. +
  41147. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  41148. +
  41149. + TASK_INIT(&container->task, 0, do_work, container);
  41150. +
  41151. +#ifdef DEBUG
  41152. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  41153. +#endif
  41154. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  41155. +}
  41156. +
  41157. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  41158. + void *data, uint32_t time, char *format, ...)
  41159. +{
  41160. + dwc_irqflags_t flags;
  41161. + work_container_t *container;
  41162. + static char name[128];
  41163. + struct timeval tv;
  41164. + va_list args;
  41165. +
  41166. + va_start(args, format);
  41167. + DWC_VSNPRINTF(name, 128, format, args);
  41168. + va_end(args);
  41169. +
  41170. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41171. + wq->pending++;
  41172. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41173. + DWC_WAITQ_TRIGGER(wq->waitq);
  41174. +
  41175. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  41176. + if (!container) {
  41177. + DWC_ERROR("Cannot allocate memory for container");
  41178. + return;
  41179. + }
  41180. +
  41181. + container->name = DWC_STRDUP(name);
  41182. + if (!container->name) {
  41183. + DWC_ERROR("Cannot allocate memory for container->name");
  41184. + DWC_FREE(container);
  41185. + return;
  41186. + }
  41187. +
  41188. + container->cb = cb;
  41189. + container->data = data;
  41190. + container->wq = wq;
  41191. +
  41192. + tv.tv_sec = time / 1000;
  41193. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  41194. + container->hz = tvtohz(&tv);
  41195. +
  41196. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  41197. +
  41198. + TASK_INIT(&container->task, 0, do_work, container);
  41199. +
  41200. +#ifdef DEBUG
  41201. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  41202. +#endif
  41203. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  41204. +}
  41205. +
  41206. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  41207. +{
  41208. + return wq->pending;
  41209. +}
  41210. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_common_linux.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  41211. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_common_linux.c 1970-01-01 01:00:00.000000000 +0100
  41212. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c 2014-04-24 15:35:04.169565731 +0200
  41213. @@ -0,0 +1,1431 @@
  41214. +#include <linux/kernel.h>
  41215. +#include <linux/init.h>
  41216. +#include <linux/module.h>
  41217. +#include <linux/kthread.h>
  41218. +
  41219. +#ifdef DWC_CCLIB
  41220. +# include "dwc_cc.h"
  41221. +#endif
  41222. +
  41223. +#ifdef DWC_CRYPTOLIB
  41224. +# include "dwc_modpow.h"
  41225. +# include "dwc_dh.h"
  41226. +# include "dwc_crypto.h"
  41227. +#endif
  41228. +
  41229. +#ifdef DWC_NOTIFYLIB
  41230. +# include "dwc_notifier.h"
  41231. +#endif
  41232. +
  41233. +/* OS-Level Implementations */
  41234. +
  41235. +/* This is the Linux kernel implementation of the DWC platform library. */
  41236. +#include <linux/moduleparam.h>
  41237. +#include <linux/ctype.h>
  41238. +#include <linux/crypto.h>
  41239. +#include <linux/delay.h>
  41240. +#include <linux/device.h>
  41241. +#include <linux/dma-mapping.h>
  41242. +#include <linux/cdev.h>
  41243. +#include <linux/errno.h>
  41244. +#include <linux/interrupt.h>
  41245. +#include <linux/jiffies.h>
  41246. +#include <linux/list.h>
  41247. +#include <linux/pci.h>
  41248. +#include <linux/random.h>
  41249. +#include <linux/scatterlist.h>
  41250. +#include <linux/slab.h>
  41251. +#include <linux/stat.h>
  41252. +#include <linux/string.h>
  41253. +#include <linux/timer.h>
  41254. +#include <linux/usb.h>
  41255. +
  41256. +#include <linux/version.h>
  41257. +
  41258. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  41259. +# include <linux/usb/gadget.h>
  41260. +#else
  41261. +# include <linux/usb_gadget.h>
  41262. +#endif
  41263. +
  41264. +#include <asm/io.h>
  41265. +#include <asm/page.h>
  41266. +#include <asm/uaccess.h>
  41267. +#include <asm/unaligned.h>
  41268. +
  41269. +#include "dwc_os.h"
  41270. +#include "dwc_list.h"
  41271. +
  41272. +
  41273. +/* MISC */
  41274. +
  41275. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  41276. +{
  41277. + return memset(dest, byte, size);
  41278. +}
  41279. +
  41280. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  41281. +{
  41282. + return memcpy(dest, src, size);
  41283. +}
  41284. +
  41285. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  41286. +{
  41287. + return memmove(dest, src, size);
  41288. +}
  41289. +
  41290. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  41291. +{
  41292. + return memcmp(m1, m2, size);
  41293. +}
  41294. +
  41295. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  41296. +{
  41297. + return strncmp(s1, s2, size);
  41298. +}
  41299. +
  41300. +int DWC_STRCMP(void *s1, void *s2)
  41301. +{
  41302. + return strcmp(s1, s2);
  41303. +}
  41304. +
  41305. +int DWC_STRLEN(char const *str)
  41306. +{
  41307. + return strlen(str);
  41308. +}
  41309. +
  41310. +char *DWC_STRCPY(char *to, char const *from)
  41311. +{
  41312. + return strcpy(to, from);
  41313. +}
  41314. +
  41315. +char *DWC_STRDUP(char const *str)
  41316. +{
  41317. + int len = DWC_STRLEN(str) + 1;
  41318. + char *new = DWC_ALLOC_ATOMIC(len);
  41319. +
  41320. + if (!new) {
  41321. + return NULL;
  41322. + }
  41323. +
  41324. + DWC_MEMCPY(new, str, len);
  41325. + return new;
  41326. +}
  41327. +
  41328. +int DWC_ATOI(const char *str, int32_t *value)
  41329. +{
  41330. + char *end = NULL;
  41331. +
  41332. + *value = simple_strtol(str, &end, 0);
  41333. + if (*end == '\0') {
  41334. + return 0;
  41335. + }
  41336. +
  41337. + return -1;
  41338. +}
  41339. +
  41340. +int DWC_ATOUI(const char *str, uint32_t *value)
  41341. +{
  41342. + char *end = NULL;
  41343. +
  41344. + *value = simple_strtoul(str, &end, 0);
  41345. + if (*end == '\0') {
  41346. + return 0;
  41347. + }
  41348. +
  41349. + return -1;
  41350. +}
  41351. +
  41352. +
  41353. +#ifdef DWC_UTFLIB
  41354. +/* From usbstring.c */
  41355. +
  41356. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  41357. +{
  41358. + int count = 0;
  41359. + u8 c;
  41360. + u16 uchar;
  41361. +
  41362. + /* this insists on correct encodings, though not minimal ones.
  41363. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  41364. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  41365. + */
  41366. + while (len != 0 && (c = (u8) *s++) != 0) {
  41367. + if (unlikely(c & 0x80)) {
  41368. + // 2-byte sequence:
  41369. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  41370. + if ((c & 0xe0) == 0xc0) {
  41371. + uchar = (c & 0x1f) << 6;
  41372. +
  41373. + c = (u8) *s++;
  41374. + if ((c & 0xc0) != 0xc0)
  41375. + goto fail;
  41376. + c &= 0x3f;
  41377. + uchar |= c;
  41378. +
  41379. + // 3-byte sequence (most CJKV characters):
  41380. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  41381. + } else if ((c & 0xf0) == 0xe0) {
  41382. + uchar = (c & 0x0f) << 12;
  41383. +
  41384. + c = (u8) *s++;
  41385. + if ((c & 0xc0) != 0xc0)
  41386. + goto fail;
  41387. + c &= 0x3f;
  41388. + uchar |= c << 6;
  41389. +
  41390. + c = (u8) *s++;
  41391. + if ((c & 0xc0) != 0xc0)
  41392. + goto fail;
  41393. + c &= 0x3f;
  41394. + uchar |= c;
  41395. +
  41396. + /* no bogus surrogates */
  41397. + if (0xd800 <= uchar && uchar <= 0xdfff)
  41398. + goto fail;
  41399. +
  41400. + // 4-byte sequence (surrogate pairs, currently rare):
  41401. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  41402. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  41403. + // (uuuuu = wwww + 1)
  41404. + // FIXME accept the surrogate code points (only)
  41405. + } else
  41406. + goto fail;
  41407. + } else
  41408. + uchar = c;
  41409. + put_unaligned (cpu_to_le16 (uchar), cp++);
  41410. + count++;
  41411. + len--;
  41412. + }
  41413. + return count;
  41414. +fail:
  41415. + return -1;
  41416. +}
  41417. +#endif /* DWC_UTFLIB */
  41418. +
  41419. +
  41420. +/* dwc_debug.h */
  41421. +
  41422. +dwc_bool_t DWC_IN_IRQ(void)
  41423. +{
  41424. + return in_irq();
  41425. +}
  41426. +
  41427. +dwc_bool_t DWC_IN_BH(void)
  41428. +{
  41429. + return in_softirq();
  41430. +}
  41431. +
  41432. +void DWC_VPRINTF(char *format, va_list args)
  41433. +{
  41434. + vprintk(format, args);
  41435. +}
  41436. +
  41437. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  41438. +{
  41439. + return vsnprintf(str, size, format, args);
  41440. +}
  41441. +
  41442. +void DWC_PRINTF(char *format, ...)
  41443. +{
  41444. + va_list args;
  41445. +
  41446. + va_start(args, format);
  41447. + DWC_VPRINTF(format, args);
  41448. + va_end(args);
  41449. +}
  41450. +
  41451. +int DWC_SPRINTF(char *buffer, char *format, ...)
  41452. +{
  41453. + int retval;
  41454. + va_list args;
  41455. +
  41456. + va_start(args, format);
  41457. + retval = vsprintf(buffer, format, args);
  41458. + va_end(args);
  41459. + return retval;
  41460. +}
  41461. +
  41462. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  41463. +{
  41464. + int retval;
  41465. + va_list args;
  41466. +
  41467. + va_start(args, format);
  41468. + retval = vsnprintf(buffer, size, format, args);
  41469. + va_end(args);
  41470. + return retval;
  41471. +}
  41472. +
  41473. +void __DWC_WARN(char *format, ...)
  41474. +{
  41475. + va_list args;
  41476. +
  41477. + va_start(args, format);
  41478. + DWC_PRINTF(KERN_WARNING);
  41479. + DWC_VPRINTF(format, args);
  41480. + va_end(args);
  41481. +}
  41482. +
  41483. +void __DWC_ERROR(char *format, ...)
  41484. +{
  41485. + va_list args;
  41486. +
  41487. + va_start(args, format);
  41488. + DWC_PRINTF(KERN_ERR);
  41489. + DWC_VPRINTF(format, args);
  41490. + va_end(args);
  41491. +}
  41492. +
  41493. +void DWC_EXCEPTION(char *format, ...)
  41494. +{
  41495. + va_list args;
  41496. +
  41497. + va_start(args, format);
  41498. + DWC_PRINTF(KERN_ERR);
  41499. + DWC_VPRINTF(format, args);
  41500. + va_end(args);
  41501. + BUG_ON(1);
  41502. +}
  41503. +
  41504. +#ifdef DEBUG
  41505. +void __DWC_DEBUG(char *format, ...)
  41506. +{
  41507. + va_list args;
  41508. +
  41509. + va_start(args, format);
  41510. + DWC_PRINTF(KERN_DEBUG);
  41511. + DWC_VPRINTF(format, args);
  41512. + va_end(args);
  41513. +}
  41514. +#endif
  41515. +
  41516. +
  41517. +/* dwc_mem.h */
  41518. +
  41519. +#if 0
  41520. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  41521. + uint32_t align,
  41522. + uint32_t alloc)
  41523. +{
  41524. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  41525. + size, align, alloc);
  41526. + return (dwc_pool_t *)pool;
  41527. +}
  41528. +
  41529. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  41530. +{
  41531. + dma_pool_destroy((struct dma_pool *)pool);
  41532. +}
  41533. +
  41534. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  41535. +{
  41536. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  41537. +}
  41538. +
  41539. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  41540. +{
  41541. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  41542. + memset(..);
  41543. +}
  41544. +
  41545. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  41546. +{
  41547. + dma_pool_free(pool, vaddr, daddr);
  41548. +}
  41549. +#endif
  41550. +
  41551. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  41552. +{
  41553. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  41554. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  41555. +#else
  41556. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  41557. +#endif
  41558. + if (!buf) {
  41559. + return NULL;
  41560. + }
  41561. +
  41562. + memset(buf, 0, (size_t)size);
  41563. + return buf;
  41564. +}
  41565. +
  41566. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  41567. +{
  41568. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  41569. + if (!buf) {
  41570. + return NULL;
  41571. + }
  41572. + memset(buf, 0, (size_t)size);
  41573. + return buf;
  41574. +}
  41575. +
  41576. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  41577. +{
  41578. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  41579. +}
  41580. +
  41581. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  41582. +{
  41583. + return kzalloc(size, GFP_KERNEL);
  41584. +}
  41585. +
  41586. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  41587. +{
  41588. + return kzalloc(size, GFP_ATOMIC);
  41589. +}
  41590. +
  41591. +void __DWC_FREE(void *mem_ctx, void *addr)
  41592. +{
  41593. + kfree(addr);
  41594. +}
  41595. +
  41596. +
  41597. +#ifdef DWC_CRYPTOLIB
  41598. +/* dwc_crypto.h */
  41599. +
  41600. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  41601. +{
  41602. + get_random_bytes(buffer, length);
  41603. +}
  41604. +
  41605. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  41606. +{
  41607. + struct crypto_blkcipher *tfm;
  41608. + struct blkcipher_desc desc;
  41609. + struct scatterlist sgd;
  41610. + struct scatterlist sgs;
  41611. +
  41612. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  41613. + if (tfm == NULL) {
  41614. + printk("failed to load transform for aes CBC\n");
  41615. + return -1;
  41616. + }
  41617. +
  41618. + crypto_blkcipher_setkey(tfm, key, keylen);
  41619. + crypto_blkcipher_set_iv(tfm, iv, 16);
  41620. +
  41621. + sg_init_one(&sgd, out, messagelen);
  41622. + sg_init_one(&sgs, message, messagelen);
  41623. +
  41624. + desc.tfm = tfm;
  41625. + desc.flags = 0;
  41626. +
  41627. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  41628. + crypto_free_blkcipher(tfm);
  41629. + DWC_ERROR("AES CBC encryption failed");
  41630. + return -1;
  41631. + }
  41632. +
  41633. + crypto_free_blkcipher(tfm);
  41634. + return 0;
  41635. +}
  41636. +
  41637. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  41638. +{
  41639. + struct crypto_hash *tfm;
  41640. + struct hash_desc desc;
  41641. + struct scatterlist sg;
  41642. +
  41643. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  41644. + if (IS_ERR(tfm)) {
  41645. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  41646. + return 0;
  41647. + }
  41648. + desc.tfm = tfm;
  41649. + desc.flags = 0;
  41650. +
  41651. + sg_init_one(&sg, message, len);
  41652. + crypto_hash_digest(&desc, &sg, len, out);
  41653. + crypto_free_hash(tfm);
  41654. +
  41655. + return 1;
  41656. +}
  41657. +
  41658. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  41659. + uint8_t *key, uint32_t keylen, uint8_t *out)
  41660. +{
  41661. + struct crypto_hash *tfm;
  41662. + struct hash_desc desc;
  41663. + struct scatterlist sg;
  41664. +
  41665. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  41666. + if (IS_ERR(tfm)) {
  41667. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  41668. + return 0;
  41669. + }
  41670. + desc.tfm = tfm;
  41671. + desc.flags = 0;
  41672. +
  41673. + sg_init_one(&sg, message, messagelen);
  41674. + crypto_hash_setkey(tfm, key, keylen);
  41675. + crypto_hash_digest(&desc, &sg, messagelen, out);
  41676. + crypto_free_hash(tfm);
  41677. +
  41678. + return 1;
  41679. +}
  41680. +#endif /* DWC_CRYPTOLIB */
  41681. +
  41682. +
  41683. +/* Byte Ordering Conversions */
  41684. +
  41685. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  41686. +{
  41687. +#ifdef __LITTLE_ENDIAN
  41688. + return *p;
  41689. +#else
  41690. + uint8_t *u_p = (uint8_t *)p;
  41691. +
  41692. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41693. +#endif
  41694. +}
  41695. +
  41696. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  41697. +{
  41698. +#ifdef __BIG_ENDIAN
  41699. + return *p;
  41700. +#else
  41701. + uint8_t *u_p = (uint8_t *)p;
  41702. +
  41703. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41704. +#endif
  41705. +}
  41706. +
  41707. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  41708. +{
  41709. +#ifdef __LITTLE_ENDIAN
  41710. + return *p;
  41711. +#else
  41712. + uint8_t *u_p = (uint8_t *)p;
  41713. +
  41714. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41715. +#endif
  41716. +}
  41717. +
  41718. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  41719. +{
  41720. +#ifdef __BIG_ENDIAN
  41721. + return *p;
  41722. +#else
  41723. + uint8_t *u_p = (uint8_t *)p;
  41724. +
  41725. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41726. +#endif
  41727. +}
  41728. +
  41729. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  41730. +{
  41731. +#ifdef __LITTLE_ENDIAN
  41732. + return *p;
  41733. +#else
  41734. + uint8_t *u_p = (uint8_t *)p;
  41735. + return (u_p[1] | (u_p[0] << 8));
  41736. +#endif
  41737. +}
  41738. +
  41739. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  41740. +{
  41741. +#ifdef __BIG_ENDIAN
  41742. + return *p;
  41743. +#else
  41744. + uint8_t *u_p = (uint8_t *)p;
  41745. + return (u_p[1] | (u_p[0] << 8));
  41746. +#endif
  41747. +}
  41748. +
  41749. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  41750. +{
  41751. +#ifdef __LITTLE_ENDIAN
  41752. + return *p;
  41753. +#else
  41754. + uint8_t *u_p = (uint8_t *)p;
  41755. + return (u_p[1] | (u_p[0] << 8));
  41756. +#endif
  41757. +}
  41758. +
  41759. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  41760. +{
  41761. +#ifdef __BIG_ENDIAN
  41762. + return *p;
  41763. +#else
  41764. + uint8_t *u_p = (uint8_t *)p;
  41765. + return (u_p[1] | (u_p[0] << 8));
  41766. +#endif
  41767. +}
  41768. +
  41769. +
  41770. +/* Registers */
  41771. +
  41772. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  41773. +{
  41774. + return readl(reg);
  41775. +}
  41776. +
  41777. +#if 0
  41778. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  41779. +{
  41780. +}
  41781. +#endif
  41782. +
  41783. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  41784. +{
  41785. + writel(value, reg);
  41786. +}
  41787. +
  41788. +#if 0
  41789. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  41790. +{
  41791. +}
  41792. +#endif
  41793. +
  41794. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  41795. +{
  41796. + unsigned long flags;
  41797. +
  41798. + local_irq_save(flags);
  41799. + local_fiq_disable();
  41800. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  41801. + local_irq_restore(flags);
  41802. +}
  41803. +
  41804. +#if 0
  41805. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  41806. +{
  41807. +}
  41808. +#endif
  41809. +
  41810. +
  41811. +/* Locking */
  41812. +
  41813. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  41814. +{
  41815. + spinlock_t *sl = (spinlock_t *)1;
  41816. +
  41817. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41818. + sl = DWC_ALLOC(sizeof(*sl));
  41819. + if (!sl) {
  41820. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  41821. + return NULL;
  41822. + }
  41823. +
  41824. + spin_lock_init(sl);
  41825. +#endif
  41826. + return (dwc_spinlock_t *)sl;
  41827. +}
  41828. +
  41829. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  41830. +{
  41831. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41832. + DWC_FREE(lock);
  41833. +#endif
  41834. +}
  41835. +
  41836. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  41837. +{
  41838. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41839. + spin_lock((spinlock_t *)lock);
  41840. +#endif
  41841. +}
  41842. +
  41843. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  41844. +{
  41845. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41846. + spin_unlock((spinlock_t *)lock);
  41847. +#endif
  41848. +}
  41849. +
  41850. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  41851. +{
  41852. + dwc_irqflags_t f;
  41853. +
  41854. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41855. + spin_lock_irqsave((spinlock_t *)lock, f);
  41856. +#else
  41857. + local_irq_save(f);
  41858. +#endif
  41859. + *flags = f;
  41860. +}
  41861. +
  41862. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  41863. +{
  41864. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41865. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  41866. +#else
  41867. + local_irq_restore(flags);
  41868. +#endif
  41869. +}
  41870. +
  41871. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  41872. +{
  41873. + struct mutex *m;
  41874. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  41875. +
  41876. + if (!mutex) {
  41877. + DWC_ERROR("Cannot allocate memory for mutex\n");
  41878. + return NULL;
  41879. + }
  41880. +
  41881. + m = (struct mutex *)mutex;
  41882. + mutex_init(m);
  41883. + return mutex;
  41884. +}
  41885. +
  41886. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  41887. +#else
  41888. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  41889. +{
  41890. + mutex_destroy((struct mutex *)mutex);
  41891. + DWC_FREE(mutex);
  41892. +}
  41893. +#endif
  41894. +
  41895. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  41896. +{
  41897. + struct mutex *m = (struct mutex *)mutex;
  41898. + mutex_lock(m);
  41899. +}
  41900. +
  41901. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  41902. +{
  41903. + struct mutex *m = (struct mutex *)mutex;
  41904. + return mutex_trylock(m);
  41905. +}
  41906. +
  41907. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  41908. +{
  41909. + struct mutex *m = (struct mutex *)mutex;
  41910. + mutex_unlock(m);
  41911. +}
  41912. +
  41913. +
  41914. +/* Timing */
  41915. +
  41916. +void DWC_UDELAY(uint32_t usecs)
  41917. +{
  41918. + udelay(usecs);
  41919. +}
  41920. +
  41921. +void DWC_MDELAY(uint32_t msecs)
  41922. +{
  41923. + mdelay(msecs);
  41924. +}
  41925. +
  41926. +void DWC_MSLEEP(uint32_t msecs)
  41927. +{
  41928. + msleep(msecs);
  41929. +}
  41930. +
  41931. +uint32_t DWC_TIME(void)
  41932. +{
  41933. + return jiffies_to_msecs(jiffies);
  41934. +}
  41935. +
  41936. +
  41937. +/* Timers */
  41938. +
  41939. +struct dwc_timer {
  41940. + struct timer_list *t;
  41941. + char *name;
  41942. + dwc_timer_callback_t cb;
  41943. + void *data;
  41944. + uint8_t scheduled;
  41945. + dwc_spinlock_t *lock;
  41946. +};
  41947. +
  41948. +static void timer_callback(unsigned long data)
  41949. +{
  41950. + dwc_timer_t *timer = (dwc_timer_t *)data;
  41951. + dwc_irqflags_t flags;
  41952. +
  41953. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  41954. + timer->scheduled = 0;
  41955. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  41956. + DWC_DEBUGC("Timer %s callback", timer->name);
  41957. + timer->cb(timer->data);
  41958. +}
  41959. +
  41960. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  41961. +{
  41962. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  41963. +
  41964. + if (!t) {
  41965. + DWC_ERROR("Cannot allocate memory for timer");
  41966. + return NULL;
  41967. + }
  41968. +
  41969. + t->t = DWC_ALLOC(sizeof(*t->t));
  41970. + if (!t->t) {
  41971. + DWC_ERROR("Cannot allocate memory for timer->t");
  41972. + goto no_timer;
  41973. + }
  41974. +
  41975. + t->name = DWC_STRDUP(name);
  41976. + if (!t->name) {
  41977. + DWC_ERROR("Cannot allocate memory for timer->name");
  41978. + goto no_name;
  41979. + }
  41980. +
  41981. + t->lock = DWC_SPINLOCK_ALLOC();
  41982. + if (!t->lock) {
  41983. + DWC_ERROR("Cannot allocate memory for lock");
  41984. + goto no_lock;
  41985. + }
  41986. +
  41987. + t->scheduled = 0;
  41988. + t->t->base = &boot_tvec_bases;
  41989. + t->t->expires = jiffies;
  41990. + setup_timer(t->t, timer_callback, (unsigned long)t);
  41991. +
  41992. + t->cb = cb;
  41993. + t->data = data;
  41994. +
  41995. + return t;
  41996. +
  41997. + no_lock:
  41998. + DWC_FREE(t->name);
  41999. + no_name:
  42000. + DWC_FREE(t->t);
  42001. + no_timer:
  42002. + DWC_FREE(t);
  42003. + return NULL;
  42004. +}
  42005. +
  42006. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  42007. +{
  42008. + dwc_irqflags_t flags;
  42009. +
  42010. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  42011. +
  42012. + if (timer->scheduled) {
  42013. + del_timer(timer->t);
  42014. + timer->scheduled = 0;
  42015. + }
  42016. +
  42017. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  42018. + DWC_SPINLOCK_FREE(timer->lock);
  42019. + DWC_FREE(timer->t);
  42020. + DWC_FREE(timer->name);
  42021. + DWC_FREE(timer);
  42022. +}
  42023. +
  42024. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  42025. +{
  42026. + dwc_irqflags_t flags;
  42027. +
  42028. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  42029. +
  42030. + if (!timer->scheduled) {
  42031. + timer->scheduled = 1;
  42032. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  42033. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  42034. + add_timer(timer->t);
  42035. + } else {
  42036. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  42037. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  42038. + }
  42039. +
  42040. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  42041. +}
  42042. +
  42043. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  42044. +{
  42045. + del_timer(timer->t);
  42046. +}
  42047. +
  42048. +
  42049. +/* Wait Queues */
  42050. +
  42051. +struct dwc_waitq {
  42052. + wait_queue_head_t queue;
  42053. + int abort;
  42054. +};
  42055. +
  42056. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  42057. +{
  42058. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  42059. +
  42060. + if (!wq) {
  42061. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  42062. + return NULL;
  42063. + }
  42064. +
  42065. + init_waitqueue_head(&wq->queue);
  42066. + wq->abort = 0;
  42067. + return wq;
  42068. +}
  42069. +
  42070. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  42071. +{
  42072. + DWC_FREE(wq);
  42073. +}
  42074. +
  42075. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  42076. +{
  42077. + int result = wait_event_interruptible(wq->queue,
  42078. + cond(data) || wq->abort);
  42079. + if (result == -ERESTARTSYS) {
  42080. + wq->abort = 0;
  42081. + return -DWC_E_RESTART;
  42082. + }
  42083. +
  42084. + if (wq->abort == 1) {
  42085. + wq->abort = 0;
  42086. + return -DWC_E_ABORT;
  42087. + }
  42088. +
  42089. + wq->abort = 0;
  42090. +
  42091. + if (result == 0) {
  42092. + return 0;
  42093. + }
  42094. +
  42095. + return -DWC_E_UNKNOWN;
  42096. +}
  42097. +
  42098. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  42099. + void *data, int32_t msecs)
  42100. +{
  42101. + int32_t tmsecs;
  42102. + int result = wait_event_interruptible_timeout(wq->queue,
  42103. + cond(data) || wq->abort,
  42104. + msecs_to_jiffies(msecs));
  42105. + if (result == -ERESTARTSYS) {
  42106. + wq->abort = 0;
  42107. + return -DWC_E_RESTART;
  42108. + }
  42109. +
  42110. + if (wq->abort == 1) {
  42111. + wq->abort = 0;
  42112. + return -DWC_E_ABORT;
  42113. + }
  42114. +
  42115. + wq->abort = 0;
  42116. +
  42117. + if (result > 0) {
  42118. + tmsecs = jiffies_to_msecs(result);
  42119. + if (!tmsecs) {
  42120. + return 1;
  42121. + }
  42122. +
  42123. + return tmsecs;
  42124. + }
  42125. +
  42126. + if (result == 0) {
  42127. + return -DWC_E_TIMEOUT;
  42128. + }
  42129. +
  42130. + return -DWC_E_UNKNOWN;
  42131. +}
  42132. +
  42133. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  42134. +{
  42135. + wq->abort = 0;
  42136. + wake_up_interruptible(&wq->queue);
  42137. +}
  42138. +
  42139. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  42140. +{
  42141. + wq->abort = 1;
  42142. + wake_up_interruptible(&wq->queue);
  42143. +}
  42144. +
  42145. +
  42146. +/* Threading */
  42147. +
  42148. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  42149. +{
  42150. + struct task_struct *thread = kthread_run(func, data, name);
  42151. +
  42152. + if (thread == ERR_PTR(-ENOMEM)) {
  42153. + return NULL;
  42154. + }
  42155. +
  42156. + return (dwc_thread_t *)thread;
  42157. +}
  42158. +
  42159. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  42160. +{
  42161. + return kthread_stop((struct task_struct *)thread);
  42162. +}
  42163. +
  42164. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  42165. +{
  42166. + return kthread_should_stop();
  42167. +}
  42168. +
  42169. +
  42170. +/* tasklets
  42171. + - run in interrupt context (cannot sleep)
  42172. + - each tasklet runs on a single CPU
  42173. + - different tasklets can be running simultaneously on different CPUs
  42174. + */
  42175. +struct dwc_tasklet {
  42176. + struct tasklet_struct t;
  42177. + dwc_tasklet_callback_t cb;
  42178. + void *data;
  42179. +};
  42180. +
  42181. +static void tasklet_callback(unsigned long data)
  42182. +{
  42183. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  42184. + t->cb(t->data);
  42185. +}
  42186. +
  42187. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  42188. +{
  42189. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  42190. +
  42191. + if (t) {
  42192. + t->cb = cb;
  42193. + t->data = data;
  42194. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  42195. + } else {
  42196. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  42197. + }
  42198. +
  42199. + return t;
  42200. +}
  42201. +
  42202. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  42203. +{
  42204. + DWC_FREE(task);
  42205. +}
  42206. +
  42207. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  42208. +{
  42209. + tasklet_schedule(&task->t);
  42210. +}
  42211. +
  42212. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  42213. +{
  42214. + tasklet_hi_schedule(&task->t);
  42215. +}
  42216. +
  42217. +
  42218. +/* workqueues
  42219. + - run in process context (can sleep)
  42220. + */
  42221. +typedef struct work_container {
  42222. + dwc_work_callback_t cb;
  42223. + void *data;
  42224. + dwc_workq_t *wq;
  42225. + char *name;
  42226. +
  42227. +#ifdef DEBUG
  42228. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  42229. +#endif
  42230. + struct delayed_work work;
  42231. +} work_container_t;
  42232. +
  42233. +#ifdef DEBUG
  42234. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  42235. +#endif
  42236. +
  42237. +struct dwc_workq {
  42238. + struct workqueue_struct *wq;
  42239. + dwc_spinlock_t *lock;
  42240. + dwc_waitq_t *waitq;
  42241. + int pending;
  42242. +
  42243. +#ifdef DEBUG
  42244. + struct work_container_queue entries;
  42245. +#endif
  42246. +};
  42247. +
  42248. +static void do_work(struct work_struct *work)
  42249. +{
  42250. + dwc_irqflags_t flags;
  42251. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  42252. + work_container_t *container = container_of(dw, struct work_container, work);
  42253. + dwc_workq_t *wq = container->wq;
  42254. +
  42255. + container->cb(container->data);
  42256. +
  42257. +#ifdef DEBUG
  42258. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  42259. +#endif
  42260. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  42261. + if (container->name) {
  42262. + DWC_FREE(container->name);
  42263. + }
  42264. + DWC_FREE(container);
  42265. +
  42266. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  42267. + wq->pending--;
  42268. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  42269. + DWC_WAITQ_TRIGGER(wq->waitq);
  42270. +}
  42271. +
  42272. +static int work_done(void *data)
  42273. +{
  42274. + dwc_workq_t *workq = (dwc_workq_t *)data;
  42275. + return workq->pending == 0;
  42276. +}
  42277. +
  42278. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  42279. +{
  42280. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  42281. +}
  42282. +
  42283. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  42284. +{
  42285. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  42286. +
  42287. + if (!wq) {
  42288. + return NULL;
  42289. + }
  42290. +
  42291. + wq->wq = create_singlethread_workqueue(name);
  42292. + if (!wq->wq) {
  42293. + goto no_wq;
  42294. + }
  42295. +
  42296. + wq->pending = 0;
  42297. +
  42298. + wq->lock = DWC_SPINLOCK_ALLOC();
  42299. + if (!wq->lock) {
  42300. + goto no_lock;
  42301. + }
  42302. +
  42303. + wq->waitq = DWC_WAITQ_ALLOC();
  42304. + if (!wq->waitq) {
  42305. + goto no_waitq;
  42306. + }
  42307. +
  42308. +#ifdef DEBUG
  42309. + DWC_CIRCLEQ_INIT(&wq->entries);
  42310. +#endif
  42311. + return wq;
  42312. +
  42313. + no_waitq:
  42314. + DWC_SPINLOCK_FREE(wq->lock);
  42315. + no_lock:
  42316. + destroy_workqueue(wq->wq);
  42317. + no_wq:
  42318. + DWC_FREE(wq);
  42319. +
  42320. + return NULL;
  42321. +}
  42322. +
  42323. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  42324. +{
  42325. +#ifdef DEBUG
  42326. + if (wq->pending != 0) {
  42327. + struct work_container *wc;
  42328. + DWC_ERROR("Destroying work queue with pending work");
  42329. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  42330. + DWC_ERROR("Work %s still pending", wc->name);
  42331. + }
  42332. + }
  42333. +#endif
  42334. + destroy_workqueue(wq->wq);
  42335. + DWC_SPINLOCK_FREE(wq->lock);
  42336. + DWC_WAITQ_FREE(wq->waitq);
  42337. + DWC_FREE(wq);
  42338. +}
  42339. +
  42340. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  42341. + char *format, ...)
  42342. +{
  42343. + dwc_irqflags_t flags;
  42344. + work_container_t *container;
  42345. + static char name[128];
  42346. + va_list args;
  42347. +
  42348. + va_start(args, format);
  42349. + DWC_VSNPRINTF(name, 128, format, args);
  42350. + va_end(args);
  42351. +
  42352. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  42353. + wq->pending++;
  42354. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  42355. + DWC_WAITQ_TRIGGER(wq->waitq);
  42356. +
  42357. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  42358. + if (!container) {
  42359. + DWC_ERROR("Cannot allocate memory for container\n");
  42360. + return;
  42361. + }
  42362. +
  42363. + container->name = DWC_STRDUP(name);
  42364. + if (!container->name) {
  42365. + DWC_ERROR("Cannot allocate memory for container->name\n");
  42366. + DWC_FREE(container);
  42367. + return;
  42368. + }
  42369. +
  42370. + container->cb = cb;
  42371. + container->data = data;
  42372. + container->wq = wq;
  42373. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  42374. + INIT_WORK(&container->work.work, do_work);
  42375. +
  42376. +#ifdef DEBUG
  42377. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  42378. +#endif
  42379. + queue_work(wq->wq, &container->work.work);
  42380. +}
  42381. +
  42382. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  42383. + void *data, uint32_t time, char *format, ...)
  42384. +{
  42385. + dwc_irqflags_t flags;
  42386. + work_container_t *container;
  42387. + static char name[128];
  42388. + va_list args;
  42389. +
  42390. + va_start(args, format);
  42391. + DWC_VSNPRINTF(name, 128, format, args);
  42392. + va_end(args);
  42393. +
  42394. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  42395. + wq->pending++;
  42396. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  42397. + DWC_WAITQ_TRIGGER(wq->waitq);
  42398. +
  42399. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  42400. + if (!container) {
  42401. + DWC_ERROR("Cannot allocate memory for container\n");
  42402. + return;
  42403. + }
  42404. +
  42405. + container->name = DWC_STRDUP(name);
  42406. + if (!container->name) {
  42407. + DWC_ERROR("Cannot allocate memory for container->name\n");
  42408. + DWC_FREE(container);
  42409. + return;
  42410. + }
  42411. +
  42412. + container->cb = cb;
  42413. + container->data = data;
  42414. + container->wq = wq;
  42415. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  42416. + INIT_DELAYED_WORK(&container->work, do_work);
  42417. +
  42418. +#ifdef DEBUG
  42419. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  42420. +#endif
  42421. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  42422. +}
  42423. +
  42424. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  42425. +{
  42426. + return wq->pending;
  42427. +}
  42428. +
  42429. +
  42430. +#ifdef DWC_LIBMODULE
  42431. +
  42432. +#ifdef DWC_CCLIB
  42433. +/* CC */
  42434. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  42435. +EXPORT_SYMBOL(dwc_cc_if_free);
  42436. +EXPORT_SYMBOL(dwc_cc_clear);
  42437. +EXPORT_SYMBOL(dwc_cc_add);
  42438. +EXPORT_SYMBOL(dwc_cc_remove);
  42439. +EXPORT_SYMBOL(dwc_cc_change);
  42440. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  42441. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  42442. +EXPORT_SYMBOL(dwc_cc_match_chid);
  42443. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  42444. +EXPORT_SYMBOL(dwc_cc_ck);
  42445. +EXPORT_SYMBOL(dwc_cc_chid);
  42446. +EXPORT_SYMBOL(dwc_cc_cdid);
  42447. +EXPORT_SYMBOL(dwc_cc_name);
  42448. +#endif /* DWC_CCLIB */
  42449. +
  42450. +#ifdef DWC_CRYPTOLIB
  42451. +# ifndef CONFIG_MACH_IPMATE
  42452. +/* Modpow */
  42453. +EXPORT_SYMBOL(dwc_modpow);
  42454. +
  42455. +/* DH */
  42456. +EXPORT_SYMBOL(dwc_dh_modpow);
  42457. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  42458. +EXPORT_SYMBOL(dwc_dh_pk);
  42459. +# endif /* CONFIG_MACH_IPMATE */
  42460. +
  42461. +/* Crypto */
  42462. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  42463. +EXPORT_SYMBOL(dwc_wusb_cmf);
  42464. +EXPORT_SYMBOL(dwc_wusb_prf);
  42465. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  42466. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  42467. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  42468. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  42469. +#endif /* DWC_CRYPTOLIB */
  42470. +
  42471. +/* Notification */
  42472. +#ifdef DWC_NOTIFYLIB
  42473. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  42474. +EXPORT_SYMBOL(dwc_free_notification_manager);
  42475. +EXPORT_SYMBOL(dwc_register_notifier);
  42476. +EXPORT_SYMBOL(dwc_unregister_notifier);
  42477. +EXPORT_SYMBOL(dwc_add_observer);
  42478. +EXPORT_SYMBOL(dwc_remove_observer);
  42479. +EXPORT_SYMBOL(dwc_notify);
  42480. +#endif
  42481. +
  42482. +/* Memory Debugging Routines */
  42483. +#ifdef DWC_DEBUG_MEMORY
  42484. +EXPORT_SYMBOL(dwc_alloc_debug);
  42485. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  42486. +EXPORT_SYMBOL(dwc_free_debug);
  42487. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  42488. +EXPORT_SYMBOL(dwc_dma_free_debug);
  42489. +#endif
  42490. +
  42491. +EXPORT_SYMBOL(DWC_MEMSET);
  42492. +EXPORT_SYMBOL(DWC_MEMCPY);
  42493. +EXPORT_SYMBOL(DWC_MEMMOVE);
  42494. +EXPORT_SYMBOL(DWC_MEMCMP);
  42495. +EXPORT_SYMBOL(DWC_STRNCMP);
  42496. +EXPORT_SYMBOL(DWC_STRCMP);
  42497. +EXPORT_SYMBOL(DWC_STRLEN);
  42498. +EXPORT_SYMBOL(DWC_STRCPY);
  42499. +EXPORT_SYMBOL(DWC_STRDUP);
  42500. +EXPORT_SYMBOL(DWC_ATOI);
  42501. +EXPORT_SYMBOL(DWC_ATOUI);
  42502. +
  42503. +#ifdef DWC_UTFLIB
  42504. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  42505. +#endif /* DWC_UTFLIB */
  42506. +
  42507. +EXPORT_SYMBOL(DWC_IN_IRQ);
  42508. +EXPORT_SYMBOL(DWC_IN_BH);
  42509. +EXPORT_SYMBOL(DWC_VPRINTF);
  42510. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  42511. +EXPORT_SYMBOL(DWC_PRINTF);
  42512. +EXPORT_SYMBOL(DWC_SPRINTF);
  42513. +EXPORT_SYMBOL(DWC_SNPRINTF);
  42514. +EXPORT_SYMBOL(__DWC_WARN);
  42515. +EXPORT_SYMBOL(__DWC_ERROR);
  42516. +EXPORT_SYMBOL(DWC_EXCEPTION);
  42517. +
  42518. +#ifdef DEBUG
  42519. +EXPORT_SYMBOL(__DWC_DEBUG);
  42520. +#endif
  42521. +
  42522. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  42523. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  42524. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  42525. +EXPORT_SYMBOL(__DWC_ALLOC);
  42526. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  42527. +EXPORT_SYMBOL(__DWC_FREE);
  42528. +
  42529. +#ifdef DWC_CRYPTOLIB
  42530. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  42531. +EXPORT_SYMBOL(DWC_AES_CBC);
  42532. +EXPORT_SYMBOL(DWC_SHA256);
  42533. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  42534. +#endif
  42535. +
  42536. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  42537. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  42538. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  42539. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  42540. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  42541. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  42542. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  42543. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  42544. +EXPORT_SYMBOL(DWC_READ_REG32);
  42545. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  42546. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  42547. +
  42548. +#if 0
  42549. +EXPORT_SYMBOL(DWC_READ_REG64);
  42550. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  42551. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  42552. +#endif
  42553. +
  42554. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  42555. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  42556. +EXPORT_SYMBOL(DWC_SPINLOCK);
  42557. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  42558. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  42559. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  42560. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  42561. +
  42562. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  42563. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  42564. +#endif
  42565. +
  42566. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  42567. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  42568. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  42569. +EXPORT_SYMBOL(DWC_UDELAY);
  42570. +EXPORT_SYMBOL(DWC_MDELAY);
  42571. +EXPORT_SYMBOL(DWC_MSLEEP);
  42572. +EXPORT_SYMBOL(DWC_TIME);
  42573. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  42574. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  42575. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  42576. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  42577. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  42578. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  42579. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  42580. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  42581. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  42582. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  42583. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  42584. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  42585. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  42586. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  42587. +EXPORT_SYMBOL(DWC_TASK_FREE);
  42588. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  42589. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  42590. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  42591. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  42592. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  42593. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  42594. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  42595. +
  42596. +static int dwc_common_port_init_module(void)
  42597. +{
  42598. + int result = 0;
  42599. +
  42600. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  42601. +
  42602. +#ifdef DWC_DEBUG_MEMORY
  42603. + result = dwc_memory_debug_start(NULL);
  42604. + if (result) {
  42605. + printk(KERN_ERR
  42606. + "dwc_memory_debug_start() failed with error %d\n",
  42607. + result);
  42608. + return result;
  42609. + }
  42610. +#endif
  42611. +
  42612. +#ifdef DWC_NOTIFYLIB
  42613. + result = dwc_alloc_notification_manager(NULL, NULL);
  42614. + if (result) {
  42615. + printk(KERN_ERR
  42616. + "dwc_alloc_notification_manager() failed with error %d\n",
  42617. + result);
  42618. + return result;
  42619. + }
  42620. +#endif
  42621. + return result;
  42622. +}
  42623. +
  42624. +static void dwc_common_port_exit_module(void)
  42625. +{
  42626. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  42627. +
  42628. +#ifdef DWC_NOTIFYLIB
  42629. + dwc_free_notification_manager();
  42630. +#endif
  42631. +
  42632. +#ifdef DWC_DEBUG_MEMORY
  42633. + dwc_memory_debug_stop();
  42634. +#endif
  42635. +}
  42636. +
  42637. +module_init(dwc_common_port_init_module);
  42638. +module_exit(dwc_common_port_exit_module);
  42639. +
  42640. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  42641. +MODULE_AUTHOR("Synopsys Inc.");
  42642. +MODULE_LICENSE ("GPL");
  42643. +
  42644. +#endif /* DWC_LIBMODULE */
  42645. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  42646. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 1970-01-01 01:00:00.000000000 +0100
  42647. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 2014-04-24 15:35:04.169565731 +0200
  42648. @@ -0,0 +1,1275 @@
  42649. +#include "dwc_os.h"
  42650. +#include "dwc_list.h"
  42651. +
  42652. +#ifdef DWC_CCLIB
  42653. +# include "dwc_cc.h"
  42654. +#endif
  42655. +
  42656. +#ifdef DWC_CRYPTOLIB
  42657. +# include "dwc_modpow.h"
  42658. +# include "dwc_dh.h"
  42659. +# include "dwc_crypto.h"
  42660. +#endif
  42661. +
  42662. +#ifdef DWC_NOTIFYLIB
  42663. +# include "dwc_notifier.h"
  42664. +#endif
  42665. +
  42666. +/* OS-Level Implementations */
  42667. +
  42668. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  42669. +
  42670. +
  42671. +/* MISC */
  42672. +
  42673. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  42674. +{
  42675. + return memset(dest, byte, size);
  42676. +}
  42677. +
  42678. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  42679. +{
  42680. + return memcpy(dest, src, size);
  42681. +}
  42682. +
  42683. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  42684. +{
  42685. + bcopy(src, dest, size);
  42686. + return dest;
  42687. +}
  42688. +
  42689. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  42690. +{
  42691. + return memcmp(m1, m2, size);
  42692. +}
  42693. +
  42694. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  42695. +{
  42696. + return strncmp(s1, s2, size);
  42697. +}
  42698. +
  42699. +int DWC_STRCMP(void *s1, void *s2)
  42700. +{
  42701. + return strcmp(s1, s2);
  42702. +}
  42703. +
  42704. +int DWC_STRLEN(char const *str)
  42705. +{
  42706. + return strlen(str);
  42707. +}
  42708. +
  42709. +char *DWC_STRCPY(char *to, char const *from)
  42710. +{
  42711. + return strcpy(to, from);
  42712. +}
  42713. +
  42714. +char *DWC_STRDUP(char const *str)
  42715. +{
  42716. + int len = DWC_STRLEN(str) + 1;
  42717. + char *new = DWC_ALLOC_ATOMIC(len);
  42718. +
  42719. + if (!new) {
  42720. + return NULL;
  42721. + }
  42722. +
  42723. + DWC_MEMCPY(new, str, len);
  42724. + return new;
  42725. +}
  42726. +
  42727. +int DWC_ATOI(char *str, int32_t *value)
  42728. +{
  42729. + char *end = NULL;
  42730. +
  42731. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  42732. + * should be equivalent on 2's complement machines
  42733. + */
  42734. + *value = strtoul(str, &end, 0);
  42735. + if (*end == '\0') {
  42736. + return 0;
  42737. + }
  42738. +
  42739. + return -1;
  42740. +}
  42741. +
  42742. +int DWC_ATOUI(char *str, uint32_t *value)
  42743. +{
  42744. + char *end = NULL;
  42745. +
  42746. + *value = strtoul(str, &end, 0);
  42747. + if (*end == '\0') {
  42748. + return 0;
  42749. + }
  42750. +
  42751. + return -1;
  42752. +}
  42753. +
  42754. +
  42755. +#ifdef DWC_UTFLIB
  42756. +/* From usbstring.c */
  42757. +
  42758. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  42759. +{
  42760. + int count = 0;
  42761. + u8 c;
  42762. + u16 uchar;
  42763. +
  42764. + /* this insists on correct encodings, though not minimal ones.
  42765. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  42766. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  42767. + */
  42768. + while (len != 0 && (c = (u8) *s++) != 0) {
  42769. + if (unlikely(c & 0x80)) {
  42770. + // 2-byte sequence:
  42771. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  42772. + if ((c & 0xe0) == 0xc0) {
  42773. + uchar = (c & 0x1f) << 6;
  42774. +
  42775. + c = (u8) *s++;
  42776. + if ((c & 0xc0) != 0xc0)
  42777. + goto fail;
  42778. + c &= 0x3f;
  42779. + uchar |= c;
  42780. +
  42781. + // 3-byte sequence (most CJKV characters):
  42782. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  42783. + } else if ((c & 0xf0) == 0xe0) {
  42784. + uchar = (c & 0x0f) << 12;
  42785. +
  42786. + c = (u8) *s++;
  42787. + if ((c & 0xc0) != 0xc0)
  42788. + goto fail;
  42789. + c &= 0x3f;
  42790. + uchar |= c << 6;
  42791. +
  42792. + c = (u8) *s++;
  42793. + if ((c & 0xc0) != 0xc0)
  42794. + goto fail;
  42795. + c &= 0x3f;
  42796. + uchar |= c;
  42797. +
  42798. + /* no bogus surrogates */
  42799. + if (0xd800 <= uchar && uchar <= 0xdfff)
  42800. + goto fail;
  42801. +
  42802. + // 4-byte sequence (surrogate pairs, currently rare):
  42803. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  42804. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  42805. + // (uuuuu = wwww + 1)
  42806. + // FIXME accept the surrogate code points (only)
  42807. + } else
  42808. + goto fail;
  42809. + } else
  42810. + uchar = c;
  42811. + put_unaligned (cpu_to_le16 (uchar), cp++);
  42812. + count++;
  42813. + len--;
  42814. + }
  42815. + return count;
  42816. +fail:
  42817. + return -1;
  42818. +}
  42819. +
  42820. +#endif /* DWC_UTFLIB */
  42821. +
  42822. +
  42823. +/* dwc_debug.h */
  42824. +
  42825. +dwc_bool_t DWC_IN_IRQ(void)
  42826. +{
  42827. +// return in_irq();
  42828. + return 0;
  42829. +}
  42830. +
  42831. +dwc_bool_t DWC_IN_BH(void)
  42832. +{
  42833. +// return in_softirq();
  42834. + return 0;
  42835. +}
  42836. +
  42837. +void DWC_VPRINTF(char *format, va_list args)
  42838. +{
  42839. + vprintf(format, args);
  42840. +}
  42841. +
  42842. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  42843. +{
  42844. + return vsnprintf(str, size, format, args);
  42845. +}
  42846. +
  42847. +void DWC_PRINTF(char *format, ...)
  42848. +{
  42849. + va_list args;
  42850. +
  42851. + va_start(args, format);
  42852. + DWC_VPRINTF(format, args);
  42853. + va_end(args);
  42854. +}
  42855. +
  42856. +int DWC_SPRINTF(char *buffer, char *format, ...)
  42857. +{
  42858. + int retval;
  42859. + va_list args;
  42860. +
  42861. + va_start(args, format);
  42862. + retval = vsprintf(buffer, format, args);
  42863. + va_end(args);
  42864. + return retval;
  42865. +}
  42866. +
  42867. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  42868. +{
  42869. + int retval;
  42870. + va_list args;
  42871. +
  42872. + va_start(args, format);
  42873. + retval = vsnprintf(buffer, size, format, args);
  42874. + va_end(args);
  42875. + return retval;
  42876. +}
  42877. +
  42878. +void __DWC_WARN(char *format, ...)
  42879. +{
  42880. + va_list args;
  42881. +
  42882. + va_start(args, format);
  42883. + DWC_VPRINTF(format, args);
  42884. + va_end(args);
  42885. +}
  42886. +
  42887. +void __DWC_ERROR(char *format, ...)
  42888. +{
  42889. + va_list args;
  42890. +
  42891. + va_start(args, format);
  42892. + DWC_VPRINTF(format, args);
  42893. + va_end(args);
  42894. +}
  42895. +
  42896. +void DWC_EXCEPTION(char *format, ...)
  42897. +{
  42898. + va_list args;
  42899. +
  42900. + va_start(args, format);
  42901. + DWC_VPRINTF(format, args);
  42902. + va_end(args);
  42903. +// BUG_ON(1); ???
  42904. +}
  42905. +
  42906. +#ifdef DEBUG
  42907. +void __DWC_DEBUG(char *format, ...)
  42908. +{
  42909. + va_list args;
  42910. +
  42911. + va_start(args, format);
  42912. + DWC_VPRINTF(format, args);
  42913. + va_end(args);
  42914. +}
  42915. +#endif
  42916. +
  42917. +
  42918. +/* dwc_mem.h */
  42919. +
  42920. +#if 0
  42921. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  42922. + uint32_t align,
  42923. + uint32_t alloc)
  42924. +{
  42925. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  42926. + size, align, alloc);
  42927. + return (dwc_pool_t *)pool;
  42928. +}
  42929. +
  42930. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  42931. +{
  42932. + dma_pool_destroy((struct dma_pool *)pool);
  42933. +}
  42934. +
  42935. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  42936. +{
  42937. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  42938. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  42939. +}
  42940. +
  42941. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  42942. +{
  42943. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  42944. + memset(..);
  42945. +}
  42946. +
  42947. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  42948. +{
  42949. + dma_pool_free(pool, vaddr, daddr);
  42950. +}
  42951. +#endif
  42952. +
  42953. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  42954. +{
  42955. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  42956. + int error;
  42957. +
  42958. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  42959. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  42960. + &dma->nsegs, BUS_DMA_NOWAIT);
  42961. + if (error) {
  42962. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  42963. + (uintmax_t)size, error);
  42964. + goto fail_0;
  42965. + }
  42966. +
  42967. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  42968. + (caddr_t *)&dma->dma_vaddr,
  42969. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  42970. + if (error) {
  42971. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  42972. + goto fail_1;
  42973. + }
  42974. +
  42975. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  42976. + BUS_DMA_NOWAIT, &dma->dma_map);
  42977. + if (error) {
  42978. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  42979. + goto fail_2;
  42980. + }
  42981. +
  42982. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  42983. + size, NULL, BUS_DMA_NOWAIT);
  42984. + if (error) {
  42985. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  42986. + goto fail_3;
  42987. + }
  42988. +
  42989. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  42990. + *dma_addr = dma->dma_paddr;
  42991. + return dma->dma_vaddr;
  42992. +
  42993. +fail_3:
  42994. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  42995. +fail_2:
  42996. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  42997. +fail_1:
  42998. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  42999. +fail_0:
  43000. + dma->dma_map = NULL;
  43001. + dma->dma_vaddr = NULL;
  43002. + dma->nsegs = 0;
  43003. +
  43004. + return NULL;
  43005. +}
  43006. +
  43007. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  43008. +{
  43009. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  43010. +
  43011. + if (dma->dma_map != NULL) {
  43012. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  43013. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  43014. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  43015. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  43016. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  43017. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  43018. + dma->dma_paddr = 0;
  43019. + dma->dma_map = NULL;
  43020. + dma->dma_vaddr = NULL;
  43021. + dma->nsegs = 0;
  43022. + }
  43023. +}
  43024. +
  43025. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  43026. +{
  43027. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  43028. +}
  43029. +
  43030. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  43031. +{
  43032. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  43033. +}
  43034. +
  43035. +void __DWC_FREE(void *mem_ctx, void *addr)
  43036. +{
  43037. + free(addr, M_DEVBUF);
  43038. +}
  43039. +
  43040. +
  43041. +#ifdef DWC_CRYPTOLIB
  43042. +/* dwc_crypto.h */
  43043. +
  43044. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  43045. +{
  43046. + get_random_bytes(buffer, length);
  43047. +}
  43048. +
  43049. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  43050. +{
  43051. + struct crypto_blkcipher *tfm;
  43052. + struct blkcipher_desc desc;
  43053. + struct scatterlist sgd;
  43054. + struct scatterlist sgs;
  43055. +
  43056. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  43057. + if (tfm == NULL) {
  43058. + printk("failed to load transform for aes CBC\n");
  43059. + return -1;
  43060. + }
  43061. +
  43062. + crypto_blkcipher_setkey(tfm, key, keylen);
  43063. + crypto_blkcipher_set_iv(tfm, iv, 16);
  43064. +
  43065. + sg_init_one(&sgd, out, messagelen);
  43066. + sg_init_one(&sgs, message, messagelen);
  43067. +
  43068. + desc.tfm = tfm;
  43069. + desc.flags = 0;
  43070. +
  43071. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  43072. + crypto_free_blkcipher(tfm);
  43073. + DWC_ERROR("AES CBC encryption failed");
  43074. + return -1;
  43075. + }
  43076. +
  43077. + crypto_free_blkcipher(tfm);
  43078. + return 0;
  43079. +}
  43080. +
  43081. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  43082. +{
  43083. + struct crypto_hash *tfm;
  43084. + struct hash_desc desc;
  43085. + struct scatterlist sg;
  43086. +
  43087. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  43088. + if (IS_ERR(tfm)) {
  43089. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  43090. + return 0;
  43091. + }
  43092. + desc.tfm = tfm;
  43093. + desc.flags = 0;
  43094. +
  43095. + sg_init_one(&sg, message, len);
  43096. + crypto_hash_digest(&desc, &sg, len, out);
  43097. + crypto_free_hash(tfm);
  43098. +
  43099. + return 1;
  43100. +}
  43101. +
  43102. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  43103. + uint8_t *key, uint32_t keylen, uint8_t *out)
  43104. +{
  43105. + struct crypto_hash *tfm;
  43106. + struct hash_desc desc;
  43107. + struct scatterlist sg;
  43108. +
  43109. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  43110. + if (IS_ERR(tfm)) {
  43111. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  43112. + return 0;
  43113. + }
  43114. + desc.tfm = tfm;
  43115. + desc.flags = 0;
  43116. +
  43117. + sg_init_one(&sg, message, messagelen);
  43118. + crypto_hash_setkey(tfm, key, keylen);
  43119. + crypto_hash_digest(&desc, &sg, messagelen, out);
  43120. + crypto_free_hash(tfm);
  43121. +
  43122. + return 1;
  43123. +}
  43124. +
  43125. +#endif /* DWC_CRYPTOLIB */
  43126. +
  43127. +
  43128. +/* Byte Ordering Conversions */
  43129. +
  43130. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  43131. +{
  43132. +#ifdef __LITTLE_ENDIAN
  43133. + return *p;
  43134. +#else
  43135. + uint8_t *u_p = (uint8_t *)p;
  43136. +
  43137. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43138. +#endif
  43139. +}
  43140. +
  43141. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  43142. +{
  43143. +#ifdef __BIG_ENDIAN
  43144. + return *p;
  43145. +#else
  43146. + uint8_t *u_p = (uint8_t *)p;
  43147. +
  43148. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43149. +#endif
  43150. +}
  43151. +
  43152. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  43153. +{
  43154. +#ifdef __LITTLE_ENDIAN
  43155. + return *p;
  43156. +#else
  43157. + uint8_t *u_p = (uint8_t *)p;
  43158. +
  43159. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43160. +#endif
  43161. +}
  43162. +
  43163. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  43164. +{
  43165. +#ifdef __BIG_ENDIAN
  43166. + return *p;
  43167. +#else
  43168. + uint8_t *u_p = (uint8_t *)p;
  43169. +
  43170. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  43171. +#endif
  43172. +}
  43173. +
  43174. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  43175. +{
  43176. +#ifdef __LITTLE_ENDIAN
  43177. + return *p;
  43178. +#else
  43179. + uint8_t *u_p = (uint8_t *)p;
  43180. + return (u_p[1] | (u_p[0] << 8));
  43181. +#endif
  43182. +}
  43183. +
  43184. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  43185. +{
  43186. +#ifdef __BIG_ENDIAN
  43187. + return *p;
  43188. +#else
  43189. + uint8_t *u_p = (uint8_t *)p;
  43190. + return (u_p[1] | (u_p[0] << 8));
  43191. +#endif
  43192. +}
  43193. +
  43194. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  43195. +{
  43196. +#ifdef __LITTLE_ENDIAN
  43197. + return *p;
  43198. +#else
  43199. + uint8_t *u_p = (uint8_t *)p;
  43200. + return (u_p[1] | (u_p[0] << 8));
  43201. +#endif
  43202. +}
  43203. +
  43204. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  43205. +{
  43206. +#ifdef __BIG_ENDIAN
  43207. + return *p;
  43208. +#else
  43209. + uint8_t *u_p = (uint8_t *)p;
  43210. + return (u_p[1] | (u_p[0] << 8));
  43211. +#endif
  43212. +}
  43213. +
  43214. +
  43215. +/* Registers */
  43216. +
  43217. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  43218. +{
  43219. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43220. + bus_size_t ior = (bus_size_t)reg;
  43221. +
  43222. + return bus_space_read_4(io->iot, io->ioh, ior);
  43223. +}
  43224. +
  43225. +#if 0
  43226. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  43227. +{
  43228. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43229. + bus_size_t ior = (bus_size_t)reg;
  43230. +
  43231. + return bus_space_read_8(io->iot, io->ioh, ior);
  43232. +}
  43233. +#endif
  43234. +
  43235. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  43236. +{
  43237. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43238. + bus_size_t ior = (bus_size_t)reg;
  43239. +
  43240. + bus_space_write_4(io->iot, io->ioh, ior, value);
  43241. +}
  43242. +
  43243. +#if 0
  43244. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  43245. +{
  43246. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43247. + bus_size_t ior = (bus_size_t)reg;
  43248. +
  43249. + bus_space_write_8(io->iot, io->ioh, ior, value);
  43250. +}
  43251. +#endif
  43252. +
  43253. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  43254. + uint32_t set_mask)
  43255. +{
  43256. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43257. + bus_size_t ior = (bus_size_t)reg;
  43258. +
  43259. + bus_space_write_4(io->iot, io->ioh, ior,
  43260. + (bus_space_read_4(io->iot, io->ioh, ior) &
  43261. + ~clear_mask) | set_mask);
  43262. +}
  43263. +
  43264. +#if 0
  43265. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  43266. + uint64_t set_mask)
  43267. +{
  43268. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  43269. + bus_size_t ior = (bus_size_t)reg;
  43270. +
  43271. + bus_space_write_8(io->iot, io->ioh, ior,
  43272. + (bus_space_read_8(io->iot, io->ioh, ior) &
  43273. + ~clear_mask) | set_mask);
  43274. +}
  43275. +#endif
  43276. +
  43277. +
  43278. +/* Locking */
  43279. +
  43280. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  43281. +{
  43282. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  43283. +
  43284. + if (!sl) {
  43285. + DWC_ERROR("Cannot allocate memory for spinlock");
  43286. + return NULL;
  43287. + }
  43288. +
  43289. + simple_lock_init(sl);
  43290. + return (dwc_spinlock_t *)sl;
  43291. +}
  43292. +
  43293. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  43294. +{
  43295. + struct simplelock *sl = (struct simplelock *)lock;
  43296. +
  43297. + DWC_FREE(sl);
  43298. +}
  43299. +
  43300. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  43301. +{
  43302. + simple_lock((struct simplelock *)lock);
  43303. +}
  43304. +
  43305. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  43306. +{
  43307. + simple_unlock((struct simplelock *)lock);
  43308. +}
  43309. +
  43310. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  43311. +{
  43312. + simple_lock((struct simplelock *)lock);
  43313. + *flags = splbio();
  43314. +}
  43315. +
  43316. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  43317. +{
  43318. + splx(flags);
  43319. + simple_unlock((struct simplelock *)lock);
  43320. +}
  43321. +
  43322. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  43323. +{
  43324. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  43325. +
  43326. + if (!mutex) {
  43327. + DWC_ERROR("Cannot allocate memory for mutex");
  43328. + return NULL;
  43329. + }
  43330. +
  43331. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  43332. + return mutex;
  43333. +}
  43334. +
  43335. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  43336. +#else
  43337. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  43338. +{
  43339. + DWC_FREE(mutex);
  43340. +}
  43341. +#endif
  43342. +
  43343. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  43344. +{
  43345. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  43346. +}
  43347. +
  43348. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  43349. +{
  43350. + int status;
  43351. +
  43352. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  43353. + return status == 0;
  43354. +}
  43355. +
  43356. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  43357. +{
  43358. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  43359. +}
  43360. +
  43361. +
  43362. +/* Timing */
  43363. +
  43364. +void DWC_UDELAY(uint32_t usecs)
  43365. +{
  43366. + DELAY(usecs);
  43367. +}
  43368. +
  43369. +void DWC_MDELAY(uint32_t msecs)
  43370. +{
  43371. + do {
  43372. + DELAY(1000);
  43373. + } while (--msecs);
  43374. +}
  43375. +
  43376. +void DWC_MSLEEP(uint32_t msecs)
  43377. +{
  43378. + struct timeval tv;
  43379. +
  43380. + tv.tv_sec = msecs / 1000;
  43381. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  43382. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  43383. +}
  43384. +
  43385. +uint32_t DWC_TIME(void)
  43386. +{
  43387. + struct timeval tv;
  43388. +
  43389. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  43390. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  43391. +}
  43392. +
  43393. +
  43394. +/* Timers */
  43395. +
  43396. +struct dwc_timer {
  43397. + struct callout t;
  43398. + char *name;
  43399. + dwc_spinlock_t *lock;
  43400. + dwc_timer_callback_t cb;
  43401. + void *data;
  43402. +};
  43403. +
  43404. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  43405. +{
  43406. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  43407. +
  43408. + if (!t) {
  43409. + DWC_ERROR("Cannot allocate memory for timer");
  43410. + return NULL;
  43411. + }
  43412. +
  43413. + callout_init(&t->t);
  43414. +
  43415. + t->name = DWC_STRDUP(name);
  43416. + if (!t->name) {
  43417. + DWC_ERROR("Cannot allocate memory for timer->name");
  43418. + goto no_name;
  43419. + }
  43420. +
  43421. + t->lock = DWC_SPINLOCK_ALLOC();
  43422. + if (!t->lock) {
  43423. + DWC_ERROR("Cannot allocate memory for timer->lock");
  43424. + goto no_lock;
  43425. + }
  43426. +
  43427. + t->cb = cb;
  43428. + t->data = data;
  43429. +
  43430. + return t;
  43431. +
  43432. + no_lock:
  43433. + DWC_FREE(t->name);
  43434. + no_name:
  43435. + DWC_FREE(t);
  43436. +
  43437. + return NULL;
  43438. +}
  43439. +
  43440. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  43441. +{
  43442. + callout_stop(&timer->t);
  43443. + DWC_SPINLOCK_FREE(timer->lock);
  43444. + DWC_FREE(timer->name);
  43445. + DWC_FREE(timer);
  43446. +}
  43447. +
  43448. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  43449. +{
  43450. + struct timeval tv;
  43451. +
  43452. + tv.tv_sec = time / 1000;
  43453. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  43454. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  43455. +}
  43456. +
  43457. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  43458. +{
  43459. + callout_stop(&timer->t);
  43460. +}
  43461. +
  43462. +
  43463. +/* Wait Queues */
  43464. +
  43465. +struct dwc_waitq {
  43466. + struct simplelock lock;
  43467. + int abort;
  43468. +};
  43469. +
  43470. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  43471. +{
  43472. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  43473. +
  43474. + if (!wq) {
  43475. + DWC_ERROR("Cannot allocate memory for waitqueue");
  43476. + return NULL;
  43477. + }
  43478. +
  43479. + simple_lock_init(&wq->lock);
  43480. + wq->abort = 0;
  43481. +
  43482. + return wq;
  43483. +}
  43484. +
  43485. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  43486. +{
  43487. + DWC_FREE(wq);
  43488. +}
  43489. +
  43490. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  43491. +{
  43492. + int ipl;
  43493. + int result = 0;
  43494. +
  43495. + simple_lock(&wq->lock);
  43496. + ipl = splbio();
  43497. +
  43498. + /* Skip the sleep if already aborted or triggered */
  43499. + if (!wq->abort && !cond(data)) {
  43500. + splx(ipl);
  43501. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  43502. + ipl = splbio();
  43503. + }
  43504. +
  43505. + if (result == 0) { // awoken
  43506. + if (wq->abort) {
  43507. + wq->abort = 0;
  43508. + result = -DWC_E_ABORT;
  43509. + } else {
  43510. + result = 0;
  43511. + }
  43512. +
  43513. + splx(ipl);
  43514. + simple_unlock(&wq->lock);
  43515. + } else {
  43516. + wq->abort = 0;
  43517. + splx(ipl);
  43518. + simple_unlock(&wq->lock);
  43519. +
  43520. + if (result == ERESTART) { // signaled - restart
  43521. + result = -DWC_E_RESTART;
  43522. + } else { // signaled - must be EINTR
  43523. + result = -DWC_E_ABORT;
  43524. + }
  43525. + }
  43526. +
  43527. + return result;
  43528. +}
  43529. +
  43530. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  43531. + void *data, int32_t msecs)
  43532. +{
  43533. + struct timeval tv, tv1, tv2;
  43534. + int ipl;
  43535. + int result = 0;
  43536. +
  43537. + tv.tv_sec = msecs / 1000;
  43538. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  43539. +
  43540. + simple_lock(&wq->lock);
  43541. + ipl = splbio();
  43542. +
  43543. + /* Skip the sleep if already aborted or triggered */
  43544. + if (!wq->abort && !cond(data)) {
  43545. + splx(ipl);
  43546. + getmicrouptime(&tv1);
  43547. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  43548. + getmicrouptime(&tv2);
  43549. + ipl = splbio();
  43550. + }
  43551. +
  43552. + if (result == 0) { // awoken
  43553. + if (wq->abort) {
  43554. + wq->abort = 0;
  43555. + splx(ipl);
  43556. + simple_unlock(&wq->lock);
  43557. + result = -DWC_E_ABORT;
  43558. + } else {
  43559. + splx(ipl);
  43560. + simple_unlock(&wq->lock);
  43561. +
  43562. + tv2.tv_usec -= tv1.tv_usec;
  43563. + if (tv2.tv_usec < 0) {
  43564. + tv2.tv_usec += 1000000;
  43565. + tv2.tv_sec--;
  43566. + }
  43567. +
  43568. + tv2.tv_sec -= tv1.tv_sec;
  43569. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  43570. + result = msecs - result;
  43571. + if (result <= 0)
  43572. + result = 1;
  43573. + }
  43574. + } else {
  43575. + wq->abort = 0;
  43576. + splx(ipl);
  43577. + simple_unlock(&wq->lock);
  43578. +
  43579. + if (result == ERESTART) { // signaled - restart
  43580. + result = -DWC_E_RESTART;
  43581. +
  43582. + } else if (result == EINTR) { // signaled - interrupt
  43583. + result = -DWC_E_ABORT;
  43584. +
  43585. + } else { // timed out
  43586. + result = -DWC_E_TIMEOUT;
  43587. + }
  43588. + }
  43589. +
  43590. + return result;
  43591. +}
  43592. +
  43593. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  43594. +{
  43595. + wakeup(wq);
  43596. +}
  43597. +
  43598. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  43599. +{
  43600. + int ipl;
  43601. +
  43602. + simple_lock(&wq->lock);
  43603. + ipl = splbio();
  43604. + wq->abort = 1;
  43605. + wakeup(wq);
  43606. + splx(ipl);
  43607. + simple_unlock(&wq->lock);
  43608. +}
  43609. +
  43610. +
  43611. +/* Threading */
  43612. +
  43613. +struct dwc_thread {
  43614. + struct proc *proc;
  43615. + int abort;
  43616. +};
  43617. +
  43618. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  43619. +{
  43620. + int retval;
  43621. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  43622. +
  43623. + if (!thread) {
  43624. + return NULL;
  43625. + }
  43626. +
  43627. + thread->abort = 0;
  43628. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  43629. + "%s", name);
  43630. + if (retval) {
  43631. + DWC_FREE(thread);
  43632. + return NULL;
  43633. + }
  43634. +
  43635. + return thread;
  43636. +}
  43637. +
  43638. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  43639. +{
  43640. + int retval;
  43641. +
  43642. + thread->abort = 1;
  43643. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  43644. +
  43645. + if (retval == 0) {
  43646. + /* DWC_THREAD_EXIT() will free the thread struct */
  43647. + return 0;
  43648. + }
  43649. +
  43650. + /* NOTE: We leak the thread struct if thread doesn't die */
  43651. +
  43652. + if (retval == EWOULDBLOCK) {
  43653. + return -DWC_E_TIMEOUT;
  43654. + }
  43655. +
  43656. + return -DWC_E_UNKNOWN;
  43657. +}
  43658. +
  43659. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  43660. +{
  43661. + return thread->abort;
  43662. +}
  43663. +
  43664. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  43665. +{
  43666. + wakeup(&thread->abort);
  43667. + DWC_FREE(thread);
  43668. + kthread_exit(0);
  43669. +}
  43670. +
  43671. +/* tasklets
  43672. + - Runs in interrupt context (cannot sleep)
  43673. + - Each tasklet runs on a single CPU
  43674. + - Different tasklets can be running simultaneously on different CPUs
  43675. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  43676. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  43677. + */
  43678. +struct dwc_tasklet {
  43679. + dwc_tasklet_callback_t cb;
  43680. + void *data;
  43681. +};
  43682. +
  43683. +static void tasklet_callback(void *data)
  43684. +{
  43685. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  43686. +
  43687. + task->cb(task->data);
  43688. +}
  43689. +
  43690. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  43691. +{
  43692. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  43693. +
  43694. + if (task) {
  43695. + task->cb = cb;
  43696. + task->data = data;
  43697. + } else {
  43698. + DWC_ERROR("Cannot allocate memory for tasklet");
  43699. + }
  43700. +
  43701. + return task;
  43702. +}
  43703. +
  43704. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  43705. +{
  43706. + DWC_FREE(task);
  43707. +}
  43708. +
  43709. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  43710. +{
  43711. + tasklet_callback(task);
  43712. +}
  43713. +
  43714. +
  43715. +/* workqueues
  43716. + - Runs in process context (can sleep)
  43717. + */
  43718. +typedef struct work_container {
  43719. + dwc_work_callback_t cb;
  43720. + void *data;
  43721. + dwc_workq_t *wq;
  43722. + char *name;
  43723. + int hz;
  43724. + struct work task;
  43725. +} work_container_t;
  43726. +
  43727. +struct dwc_workq {
  43728. + struct workqueue *taskq;
  43729. + dwc_spinlock_t *lock;
  43730. + dwc_waitq_t *waitq;
  43731. + int pending;
  43732. + struct work_container *container;
  43733. +};
  43734. +
  43735. +static void do_work(struct work *task, void *data)
  43736. +{
  43737. + dwc_workq_t *wq = (dwc_workq_t *)data;
  43738. + work_container_t *container = wq->container;
  43739. + dwc_irqflags_t flags;
  43740. +
  43741. + if (container->hz) {
  43742. + tsleep(container, 0, "dw3wrk", container->hz);
  43743. + }
  43744. +
  43745. + container->cb(container->data);
  43746. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  43747. +
  43748. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43749. + if (container->name)
  43750. + DWC_FREE(container->name);
  43751. + DWC_FREE(container);
  43752. + wq->pending--;
  43753. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43754. + DWC_WAITQ_TRIGGER(wq->waitq);
  43755. +}
  43756. +
  43757. +static int work_done(void *data)
  43758. +{
  43759. + dwc_workq_t *workq = (dwc_workq_t *)data;
  43760. +
  43761. + return workq->pending == 0;
  43762. +}
  43763. +
  43764. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  43765. +{
  43766. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  43767. +}
  43768. +
  43769. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  43770. +{
  43771. + int result;
  43772. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  43773. +
  43774. + if (!wq) {
  43775. + DWC_ERROR("Cannot allocate memory for workqueue");
  43776. + return NULL;
  43777. + }
  43778. +
  43779. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  43780. + IPL_BIO, 0);
  43781. + if (result) {
  43782. + DWC_ERROR("Cannot create workqueue");
  43783. + goto no_taskq;
  43784. + }
  43785. +
  43786. + wq->pending = 0;
  43787. +
  43788. + wq->lock = DWC_SPINLOCK_ALLOC();
  43789. + if (!wq->lock) {
  43790. + DWC_ERROR("Cannot allocate memory for spinlock");
  43791. + goto no_lock;
  43792. + }
  43793. +
  43794. + wq->waitq = DWC_WAITQ_ALLOC();
  43795. + if (!wq->waitq) {
  43796. + DWC_ERROR("Cannot allocate memory for waitqueue");
  43797. + goto no_waitq;
  43798. + }
  43799. +
  43800. + return wq;
  43801. +
  43802. + no_waitq:
  43803. + DWC_SPINLOCK_FREE(wq->lock);
  43804. + no_lock:
  43805. + workqueue_destroy(wq->taskq);
  43806. + no_taskq:
  43807. + DWC_FREE(wq);
  43808. +
  43809. + return NULL;
  43810. +}
  43811. +
  43812. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  43813. +{
  43814. +#ifdef DEBUG
  43815. + dwc_irqflags_t flags;
  43816. +
  43817. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43818. +
  43819. + if (wq->pending != 0) {
  43820. + struct work_container *container = wq->container;
  43821. +
  43822. + DWC_ERROR("Destroying work queue with pending work");
  43823. +
  43824. + if (container && container->name) {
  43825. + DWC_ERROR("Work %s still pending", container->name);
  43826. + }
  43827. + }
  43828. +
  43829. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43830. +#endif
  43831. + DWC_WAITQ_FREE(wq->waitq);
  43832. + DWC_SPINLOCK_FREE(wq->lock);
  43833. + workqueue_destroy(wq->taskq);
  43834. + DWC_FREE(wq);
  43835. +}
  43836. +
  43837. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  43838. + char *format, ...)
  43839. +{
  43840. + dwc_irqflags_t flags;
  43841. + work_container_t *container;
  43842. + static char name[128];
  43843. + va_list args;
  43844. +
  43845. + va_start(args, format);
  43846. + DWC_VSNPRINTF(name, 128, format, args);
  43847. + va_end(args);
  43848. +
  43849. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43850. + wq->pending++;
  43851. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43852. + DWC_WAITQ_TRIGGER(wq->waitq);
  43853. +
  43854. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  43855. + if (!container) {
  43856. + DWC_ERROR("Cannot allocate memory for container");
  43857. + return;
  43858. + }
  43859. +
  43860. + container->name = DWC_STRDUP(name);
  43861. + if (!container->name) {
  43862. + DWC_ERROR("Cannot allocate memory for container->name");
  43863. + DWC_FREE(container);
  43864. + return;
  43865. + }
  43866. +
  43867. + container->cb = cb;
  43868. + container->data = data;
  43869. + container->wq = wq;
  43870. + container->hz = 0;
  43871. + wq->container = container;
  43872. +
  43873. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  43874. + workqueue_enqueue(wq->taskq, &container->task);
  43875. +}
  43876. +
  43877. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  43878. + void *data, uint32_t time, char *format, ...)
  43879. +{
  43880. + dwc_irqflags_t flags;
  43881. + work_container_t *container;
  43882. + static char name[128];
  43883. + struct timeval tv;
  43884. + va_list args;
  43885. +
  43886. + va_start(args, format);
  43887. + DWC_VSNPRINTF(name, 128, format, args);
  43888. + va_end(args);
  43889. +
  43890. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43891. + wq->pending++;
  43892. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43893. + DWC_WAITQ_TRIGGER(wq->waitq);
  43894. +
  43895. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  43896. + if (!container) {
  43897. + DWC_ERROR("Cannot allocate memory for container");
  43898. + return;
  43899. + }
  43900. +
  43901. + container->name = DWC_STRDUP(name);
  43902. + if (!container->name) {
  43903. + DWC_ERROR("Cannot allocate memory for container->name");
  43904. + DWC_FREE(container);
  43905. + return;
  43906. + }
  43907. +
  43908. + container->cb = cb;
  43909. + container->data = data;
  43910. + container->wq = wq;
  43911. + tv.tv_sec = time / 1000;
  43912. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  43913. + container->hz = tvtohz(&tv);
  43914. + wq->container = container;
  43915. +
  43916. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  43917. + workqueue_enqueue(wq->taskq, &container->task);
  43918. +}
  43919. +
  43920. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  43921. +{
  43922. + return wq->pending;
  43923. +}
  43924. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_crypto.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c
  43925. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_crypto.c 1970-01-01 01:00:00.000000000 +0100
  43926. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c 2014-04-24 15:35:04.169565731 +0200
  43927. @@ -0,0 +1,308 @@
  43928. +/* =========================================================================
  43929. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  43930. + * $Revision: #5 $
  43931. + * $Date: 2010/09/28 $
  43932. + * $Change: 1596182 $
  43933. + *
  43934. + * Synopsys Portability Library Software and documentation
  43935. + * (hereinafter, "Software") is an Unsupported proprietary work of
  43936. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  43937. + * between Synopsys and you.
  43938. + *
  43939. + * The Software IS NOT an item of Licensed Software or Licensed Product
  43940. + * under any End User Software License Agreement or Agreement for
  43941. + * Licensed Product with Synopsys or any supplement thereto. You are
  43942. + * permitted to use and redistribute this Software in source and binary
  43943. + * forms, with or without modification, provided that redistributions
  43944. + * of source code must retain this notice. You may not view, use,
  43945. + * disclose, copy or distribute this file or any information contained
  43946. + * herein except pursuant to this license grant from Synopsys. If you
  43947. + * do not agree with this notice, including the disclaimer below, then
  43948. + * you are not authorized to use the Software.
  43949. + *
  43950. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  43951. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43952. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43953. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  43954. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43955. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43956. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43957. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  43958. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43959. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  43960. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43961. + * DAMAGE.
  43962. + * ========================================================================= */
  43963. +
  43964. +/** @file
  43965. + * This file contains the WUSB cryptographic routines.
  43966. + */
  43967. +
  43968. +#ifdef DWC_CRYPTOLIB
  43969. +
  43970. +#include "dwc_crypto.h"
  43971. +#include "usb.h"
  43972. +
  43973. +#ifdef DEBUG
  43974. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  43975. +{
  43976. + int i;
  43977. + DWC_PRINTF("%s: ", name);
  43978. + for (i=0; i<len; i++) {
  43979. + DWC_PRINTF("%02x ", bytes[i]);
  43980. + }
  43981. + DWC_PRINTF("\n");
  43982. +}
  43983. +#else
  43984. +#define dump_bytes(x...)
  43985. +#endif
  43986. +
  43987. +/* Display a block */
  43988. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  43989. +{
  43990. +#ifdef DWC_DEBUG_CRYPTO
  43991. + int i, blksize = 16;
  43992. +
  43993. + DWC_DEBUG("%s", prefix);
  43994. +
  43995. + if (suffix == NULL) {
  43996. + suffix = "\n";
  43997. + blksize = a;
  43998. + }
  43999. +
  44000. + for (i = 0; i < blksize; i++)
  44001. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  44002. + DWC_PRINT(suffix);
  44003. +#endif
  44004. +}
  44005. +
  44006. +/**
  44007. + * Encrypts an array of bytes using the AES encryption engine.
  44008. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  44009. + * in-place.
  44010. + *
  44011. + * @return 0 on success, negative error code on error.
  44012. + */
  44013. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  44014. +{
  44015. + u8 block_t[16];
  44016. + DWC_MEMSET(block_t, 0, 16);
  44017. +
  44018. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  44019. +}
  44020. +
  44021. +/**
  44022. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  44023. + * This function takes a data string and returns the encrypted CBC
  44024. + * Counter-mode MIC.
  44025. + *
  44026. + * @param key The 128-bit symmetric key.
  44027. + * @param nonce The CCM nonce.
  44028. + * @param label The unique 14-byte ASCII text label.
  44029. + * @param bytes The byte array to be encrypted.
  44030. + * @param len Length of the byte array.
  44031. + * @param result Byte array to receive the 8-byte encrypted MIC.
  44032. + */
  44033. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  44034. + char *label, u8 *bytes, int len, u8 *result)
  44035. +{
  44036. + u8 block_m[16];
  44037. + u8 block_x[16];
  44038. + u8 block_t[8];
  44039. + int idx, blkNum;
  44040. + u16 la = (u16)(len + 14);
  44041. +
  44042. + /* Set the AES-128 key */
  44043. + //dwc_aes_setkey(tfm, key, 16);
  44044. +
  44045. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  44046. + block_m[0] = 0x59;
  44047. + for (idx = 0; idx < 13; idx++)
  44048. + block_m[idx + 1] = nonce[idx];
  44049. + block_m[14] = 0;
  44050. + block_m[15] = 0;
  44051. +
  44052. + /* Produce the CBC IV */
  44053. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  44054. + show_block(block_m, "CBC IV in: ", "\n", 0);
  44055. + show_block(block_x, "CBC IV out:", "\n", 0);
  44056. +
  44057. + /* Fill block B1 from l(a) = Blen + 14, and A */
  44058. + block_x[0] ^= (u8)(la >> 8);
  44059. + block_x[1] ^= (u8)la;
  44060. + for (idx = 0; idx < 14; idx++)
  44061. + block_x[idx + 2] ^= label[idx];
  44062. + show_block(block_x, "After xor: ", "b1\n", 16);
  44063. +
  44064. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  44065. + show_block(block_x, "After AES: ", "b1\n", 16);
  44066. +
  44067. + idx = 0;
  44068. + blkNum = 0;
  44069. +
  44070. + /* Fill remaining blocks with B */
  44071. + while (len-- > 0) {
  44072. + block_x[idx] ^= *bytes++;
  44073. + if (++idx >= 16) {
  44074. + idx = 0;
  44075. + show_block(block_x, "After xor: ", "\n", blkNum);
  44076. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  44077. + show_block(block_x, "After AES: ", "\n", blkNum);
  44078. + blkNum++;
  44079. + }
  44080. + }
  44081. +
  44082. + /* Handle partial last block */
  44083. + if (idx > 0) {
  44084. + show_block(block_x, "After xor: ", "\n", blkNum);
  44085. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  44086. + show_block(block_x, "After AES: ", "\n", blkNum);
  44087. + }
  44088. +
  44089. + /* Save the MIC tag */
  44090. + DWC_MEMCPY(block_t, block_x, 8);
  44091. + show_block(block_t, "MIC tag : ", NULL, 8);
  44092. +
  44093. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  44094. + block_m[0] = 0x01;
  44095. + block_m[14] = 0;
  44096. + block_m[15] = 0;
  44097. +
  44098. + /* Encrypt the counter */
  44099. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  44100. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  44101. +
  44102. + /* XOR with MIC tag */
  44103. + for (idx = 0; idx < 8; idx++) {
  44104. + block_t[idx] ^= block_x[idx];
  44105. + }
  44106. +
  44107. + /* Return result to caller */
  44108. + DWC_MEMCPY(result, block_t, 8);
  44109. + show_block(result, "CCM-MIC : ", NULL, 8);
  44110. +
  44111. +}
  44112. +
  44113. +/**
  44114. + * The PRF function described in section 6.5 of the WUSB spec. This function
  44115. + * concatenates MIC values returned from dwc_cmf() to create a value of
  44116. + * the requested length.
  44117. + *
  44118. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  44119. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  44120. + * @param result Byte array to receive the result.
  44121. + */
  44122. +void dwc_wusb_prf(int prf_len, u8 *key,
  44123. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  44124. +{
  44125. + int i;
  44126. +
  44127. + nonce[0] = 0;
  44128. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  44129. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  44130. + result += 8;
  44131. + }
  44132. +}
  44133. +
  44134. +/**
  44135. + * Fills in CCM Nonce per the WUSB spec.
  44136. + *
  44137. + * @param[in] haddr Host address.
  44138. + * @param[in] daddr Device address.
  44139. + * @param[in] tkid Session Key(PTK) identifier.
  44140. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  44141. + */
  44142. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  44143. + uint8_t *nonce)
  44144. +{
  44145. +
  44146. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  44147. +
  44148. + DWC_MEMSET(&nonce[0], 0, 16);
  44149. +
  44150. + DWC_MEMCPY(&nonce[6], tkid, 3);
  44151. + nonce[9] = daddr & 0xFF;
  44152. + nonce[10] = (daddr >> 8) & 0xFF;
  44153. + nonce[11] = haddr & 0xFF;
  44154. + nonce[12] = (haddr >> 8) & 0xFF;
  44155. +
  44156. + dump_bytes("CCM nonce", nonce, 16);
  44157. +}
  44158. +
  44159. +/**
  44160. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  44161. + * Nonce.
  44162. + */
  44163. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  44164. +{
  44165. + uint8_t inonce[16];
  44166. + uint32_t temp[4];
  44167. +
  44168. + /* Fill in the Nonce */
  44169. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  44170. + inonce[9] = addr & 0xFF;
  44171. + inonce[10] = (addr >> 8) & 0xFF;
  44172. + inonce[11] = inonce[9];
  44173. + inonce[12] = inonce[10];
  44174. +
  44175. + /* Collect "randomness samples" */
  44176. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  44177. +
  44178. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  44179. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  44180. + nonce);
  44181. +}
  44182. +
  44183. +/**
  44184. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  44185. + * WUSB spec.
  44186. + *
  44187. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  44188. + * @param[in] mk Master Key to derive the session from
  44189. + * @param[in] hnonce Pointer to Host Nonce.
  44190. + * @param[in] dnonce Pointer to Device Nonce.
  44191. + * @param[out] kck Pointer to where the KCK output is to be written.
  44192. + * @param[out] ptk Pointer to where the PTK output is to be written.
  44193. + */
  44194. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  44195. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  44196. +{
  44197. + uint8_t idata[32];
  44198. + uint8_t odata[32];
  44199. +
  44200. + dump_bytes("ck", mk, 16);
  44201. + dump_bytes("hnonce", hnonce, 16);
  44202. + dump_bytes("dnonce", dnonce, 16);
  44203. +
  44204. + /* The data is the HNonce and DNonce concatenated */
  44205. + DWC_MEMCPY(&idata[0], hnonce, 16);
  44206. + DWC_MEMCPY(&idata[16], dnonce, 16);
  44207. +
  44208. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  44209. +
  44210. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  44211. + DWC_MEMCPY(kck, &odata[0], 16);
  44212. + DWC_MEMCPY(ptk, &odata[16], 16);
  44213. +
  44214. + dump_bytes("kck", kck, 16);
  44215. + dump_bytes("ptk", ptk, 16);
  44216. +}
  44217. +
  44218. +/**
  44219. + * Generates the Message Integrity Code over the Handshake data per the
  44220. + * WUSB spec.
  44221. + *
  44222. + * @param ccm_nonce Pointer to CCM Nonce.
  44223. + * @param kck Pointer to Key Confirmation Key.
  44224. + * @param data Pointer to Handshake data to be checked.
  44225. + * @param mic Pointer to where the MIC output is to be written.
  44226. + */
  44227. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  44228. + uint8_t *data, uint8_t *mic)
  44229. +{
  44230. +
  44231. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  44232. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  44233. +}
  44234. +
  44235. +#endif /* DWC_CRYPTOLIB */
  44236. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_crypto.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h
  44237. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_crypto.h 1970-01-01 01:00:00.000000000 +0100
  44238. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h 2014-04-24 15:35:04.169565731 +0200
  44239. @@ -0,0 +1,111 @@
  44240. +/* =========================================================================
  44241. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  44242. + * $Revision: #3 $
  44243. + * $Date: 2010/09/28 $
  44244. + * $Change: 1596182 $
  44245. + *
  44246. + * Synopsys Portability Library Software and documentation
  44247. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44248. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44249. + * between Synopsys and you.
  44250. + *
  44251. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44252. + * under any End User Software License Agreement or Agreement for
  44253. + * Licensed Product with Synopsys or any supplement thereto. You are
  44254. + * permitted to use and redistribute this Software in source and binary
  44255. + * forms, with or without modification, provided that redistributions
  44256. + * of source code must retain this notice. You may not view, use,
  44257. + * disclose, copy or distribute this file or any information contained
  44258. + * herein except pursuant to this license grant from Synopsys. If you
  44259. + * do not agree with this notice, including the disclaimer below, then
  44260. + * you are not authorized to use the Software.
  44261. + *
  44262. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44263. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44264. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44265. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44266. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44267. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44268. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44269. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44270. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44271. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44272. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44273. + * DAMAGE.
  44274. + * ========================================================================= */
  44275. +
  44276. +#ifndef _DWC_CRYPTO_H_
  44277. +#define _DWC_CRYPTO_H_
  44278. +
  44279. +#ifdef __cplusplus
  44280. +extern "C" {
  44281. +#endif
  44282. +
  44283. +/** @file
  44284. + *
  44285. + * This file contains declarations for the WUSB Cryptographic routines as
  44286. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  44287. + * modules.
  44288. + */
  44289. +
  44290. +#include "dwc_os.h"
  44291. +
  44292. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  44293. +
  44294. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  44295. + char *label, u8 *bytes, int len, u8 *result);
  44296. +void dwc_wusb_prf(int prf_len, u8 *key,
  44297. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  44298. +
  44299. +/**
  44300. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  44301. + *
  44302. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  44303. + */
  44304. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  44305. + char *label, u8 *bytes, int len, u8 *result)
  44306. +{
  44307. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  44308. +}
  44309. +
  44310. +/**
  44311. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  44312. + *
  44313. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  44314. + */
  44315. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  44316. + char *label, u8 *bytes, int len, u8 *result)
  44317. +{
  44318. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  44319. +}
  44320. +
  44321. +/**
  44322. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  44323. + *
  44324. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  44325. + */
  44326. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  44327. + char *label, u8 *bytes, int len, u8 *result)
  44328. +{
  44329. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  44330. +}
  44331. +
  44332. +
  44333. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  44334. + uint8_t *nonce);
  44335. +void dwc_wusb_gen_nonce(uint16_t addr,
  44336. + uint8_t *nonce);
  44337. +
  44338. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  44339. + uint8_t *hnonce, uint8_t *dnonce,
  44340. + uint8_t *kck, uint8_t *ptk);
  44341. +
  44342. +
  44343. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  44344. + *kck, uint8_t *data, uint8_t *mic);
  44345. +
  44346. +#ifdef __cplusplus
  44347. +}
  44348. +#endif
  44349. +
  44350. +#endif /* _DWC_CRYPTO_H_ */
  44351. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_dh.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c
  44352. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_dh.c 1970-01-01 01:00:00.000000000 +0100
  44353. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c 2014-04-24 15:35:04.169565731 +0200
  44354. @@ -0,0 +1,291 @@
  44355. +/* =========================================================================
  44356. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  44357. + * $Revision: #3 $
  44358. + * $Date: 2010/09/28 $
  44359. + * $Change: 1596182 $
  44360. + *
  44361. + * Synopsys Portability Library Software and documentation
  44362. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44363. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44364. + * between Synopsys and you.
  44365. + *
  44366. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44367. + * under any End User Software License Agreement or Agreement for
  44368. + * Licensed Product with Synopsys or any supplement thereto. You are
  44369. + * permitted to use and redistribute this Software in source and binary
  44370. + * forms, with or without modification, provided that redistributions
  44371. + * of source code must retain this notice. You may not view, use,
  44372. + * disclose, copy or distribute this file or any information contained
  44373. + * herein except pursuant to this license grant from Synopsys. If you
  44374. + * do not agree with this notice, including the disclaimer below, then
  44375. + * you are not authorized to use the Software.
  44376. + *
  44377. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44378. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44379. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44380. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44381. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44382. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44383. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44384. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44385. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44386. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44387. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44388. + * DAMAGE.
  44389. + * ========================================================================= */
  44390. +#ifdef DWC_CRYPTOLIB
  44391. +
  44392. +#ifndef CONFIG_MACH_IPMATE
  44393. +
  44394. +#include "dwc_dh.h"
  44395. +#include "dwc_modpow.h"
  44396. +
  44397. +#ifdef DEBUG
  44398. +/* This function prints out a buffer in the format described in the Association
  44399. + * Model specification. */
  44400. +static void dh_dump(char *str, void *_num, int len)
  44401. +{
  44402. + uint8_t *num = _num;
  44403. + int i;
  44404. + DWC_PRINTF("%s\n", str);
  44405. + for (i = 0; i < len; i ++) {
  44406. + DWC_PRINTF("%02x", num[i]);
  44407. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  44408. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  44409. + }
  44410. +
  44411. + DWC_PRINTF("\n");
  44412. +}
  44413. +#else
  44414. +#define dh_dump(_x...) do {; } while(0)
  44415. +#endif
  44416. +
  44417. +/* Constant g value */
  44418. +static __u32 dh_g[] = {
  44419. + 0x02000000,
  44420. +};
  44421. +
  44422. +/* Constant p value */
  44423. +static __u32 dh_p[] = {
  44424. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  44425. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  44426. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  44427. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  44428. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  44429. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  44430. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  44431. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  44432. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  44433. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  44434. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  44435. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  44436. +};
  44437. +
  44438. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  44439. +{
  44440. + uint8_t *in = _in;
  44441. + uint8_t *out = _out;
  44442. + int i;
  44443. + for (i=0; i<len; i++) {
  44444. + out[i] = in[len-1-i];
  44445. + }
  44446. +}
  44447. +
  44448. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  44449. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  44450. + * of 4. */
  44451. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  44452. + void *exp, uint32_t exp_len,
  44453. + void *mod, uint32_t mod_len,
  44454. + void *out)
  44455. +{
  44456. + /* modpow() takes little endian numbers. AM uses big-endian. This
  44457. + * function swaps bytes of numbers before passing onto modpow. */
  44458. +
  44459. + int retval = 0;
  44460. + uint32_t *result;
  44461. +
  44462. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  44463. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  44464. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  44465. +
  44466. + dh_swap_bytes(num, &bignum_num[1], num_len);
  44467. + bignum_num[0] = num_len / 4;
  44468. +
  44469. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  44470. + bignum_exp[0] = exp_len / 4;
  44471. +
  44472. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  44473. + bignum_mod[0] = mod_len / 4;
  44474. +
  44475. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  44476. + if (!result) {
  44477. + retval = -1;
  44478. + goto dh_modpow_nomem;
  44479. + }
  44480. +
  44481. + dh_swap_bytes(&result[1], out, result[0] * 4);
  44482. + dwc_free(mem_ctx, result);
  44483. +
  44484. + dh_modpow_nomem:
  44485. + dwc_free(mem_ctx, bignum_num);
  44486. + dwc_free(mem_ctx, bignum_exp);
  44487. + dwc_free(mem_ctx, bignum_mod);
  44488. + return retval;
  44489. +}
  44490. +
  44491. +
  44492. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  44493. +{
  44494. + int retval;
  44495. + uint8_t m3[385];
  44496. +
  44497. +#ifndef DH_TEST_VECTORS
  44498. + DWC_RANDOM_BYTES(exp, 32);
  44499. +#endif
  44500. +
  44501. + /* Compute the pkd */
  44502. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  44503. + exp, 32,
  44504. + dh_p, 384, pk))) {
  44505. + return retval;
  44506. + }
  44507. +
  44508. + m3[384] = nd;
  44509. + DWC_MEMCPY(&m3[0], pk, 384);
  44510. + DWC_SHA256(m3, 385, hash);
  44511. +
  44512. + dh_dump("PK", pk, 384);
  44513. + dh_dump("SHA-256(M3)", hash, 32);
  44514. + return 0;
  44515. +}
  44516. +
  44517. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  44518. + uint8_t *exp, int is_host,
  44519. + char *dd, uint8_t *ck, uint8_t *kdk)
  44520. +{
  44521. + int retval;
  44522. + uint8_t mv[784];
  44523. + uint8_t sha_result[32];
  44524. + uint8_t dhkey[384];
  44525. + uint8_t shared_secret[384];
  44526. + char *message;
  44527. + uint32_t vd;
  44528. +
  44529. + uint8_t *pk;
  44530. +
  44531. + if (is_host) {
  44532. + pk = pkd;
  44533. + }
  44534. + else {
  44535. + pk = pkh;
  44536. + }
  44537. +
  44538. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  44539. + exp, 32,
  44540. + dh_p, 384, shared_secret))) {
  44541. + return retval;
  44542. + }
  44543. + dh_dump("Shared Secret", shared_secret, 384);
  44544. +
  44545. + DWC_SHA256(shared_secret, 384, dhkey);
  44546. + dh_dump("DHKEY", dhkey, 384);
  44547. +
  44548. + DWC_MEMCPY(&mv[0], pkd, 384);
  44549. + DWC_MEMCPY(&mv[384], pkh, 384);
  44550. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  44551. + dh_dump("MV", mv, 784);
  44552. +
  44553. + DWC_SHA256(mv, 784, sha_result);
  44554. + dh_dump("SHA-256(MV)", sha_result, 32);
  44555. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  44556. +
  44557. + dh_swap_bytes(sha_result, &vd, 4);
  44558. +#ifdef DEBUG
  44559. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  44560. +#endif
  44561. +
  44562. + switch (nd) {
  44563. + case 2:
  44564. + vd = vd % 100;
  44565. + DWC_SPRINTF(dd, "%02d", vd);
  44566. + break;
  44567. + case 3:
  44568. + vd = vd % 1000;
  44569. + DWC_SPRINTF(dd, "%03d", vd);
  44570. + break;
  44571. + case 4:
  44572. + vd = vd % 10000;
  44573. + DWC_SPRINTF(dd, "%04d", vd);
  44574. + break;
  44575. + }
  44576. +#ifdef DEBUG
  44577. + DWC_PRINTF("Display Digits: %s\n", dd);
  44578. +#endif
  44579. +
  44580. + message = "connection key";
  44581. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  44582. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  44583. + DWC_MEMCPY(ck, sha_result, 16);
  44584. +
  44585. + message = "key derivation key";
  44586. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  44587. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  44588. + DWC_MEMCPY(kdk, sha_result, 32);
  44589. +
  44590. + return 0;
  44591. +}
  44592. +
  44593. +
  44594. +#ifdef DH_TEST_VECTORS
  44595. +
  44596. +static __u8 dh_a[] = {
  44597. + 0x44, 0x00, 0x51, 0xd6,
  44598. + 0xf0, 0xb5, 0x5e, 0xa9,
  44599. + 0x67, 0xab, 0x31, 0xc6,
  44600. + 0x8a, 0x8b, 0x5e, 0x37,
  44601. + 0xd9, 0x10, 0xda, 0xe0,
  44602. + 0xe2, 0xd4, 0x59, 0xa4,
  44603. + 0x86, 0x45, 0x9c, 0xaa,
  44604. + 0xdf, 0x36, 0x75, 0x16,
  44605. +};
  44606. +
  44607. +static __u8 dh_b[] = {
  44608. + 0x5d, 0xae, 0xc7, 0x86,
  44609. + 0x79, 0x80, 0xa3, 0x24,
  44610. + 0x8c, 0xe3, 0x57, 0x8f,
  44611. + 0xc7, 0x5f, 0x1b, 0x0f,
  44612. + 0x2d, 0xf8, 0x9d, 0x30,
  44613. + 0x6f, 0xa4, 0x52, 0xcd,
  44614. + 0xe0, 0x7a, 0x04, 0x8a,
  44615. + 0xde, 0xd9, 0x26, 0x56,
  44616. +};
  44617. +
  44618. +void dwc_run_dh_test_vectors(void *mem_ctx)
  44619. +{
  44620. + uint8_t pkd[384];
  44621. + uint8_t pkh[384];
  44622. + uint8_t hashd[32];
  44623. + uint8_t hashh[32];
  44624. + uint8_t ck[16];
  44625. + uint8_t kdk[32];
  44626. + char dd[5];
  44627. +
  44628. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  44629. +
  44630. + /* compute the PKd and SHA-256(PKd || Nd) */
  44631. + DWC_PRINTF("Computing PKd\n");
  44632. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  44633. +
  44634. + /* compute the PKd and SHA-256(PKh || Nd) */
  44635. + DWC_PRINTF("Computing PKh\n");
  44636. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  44637. +
  44638. + /* compute the dhkey */
  44639. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  44640. +}
  44641. +#endif /* DH_TEST_VECTORS */
  44642. +
  44643. +#endif /* !CONFIG_MACH_IPMATE */
  44644. +
  44645. +#endif /* DWC_CRYPTOLIB */
  44646. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_dh.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h
  44647. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_dh.h 1970-01-01 01:00:00.000000000 +0100
  44648. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h 2014-04-24 15:35:04.169565731 +0200
  44649. @@ -0,0 +1,106 @@
  44650. +/* =========================================================================
  44651. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  44652. + * $Revision: #4 $
  44653. + * $Date: 2010/09/28 $
  44654. + * $Change: 1596182 $
  44655. + *
  44656. + * Synopsys Portability Library Software and documentation
  44657. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44658. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44659. + * between Synopsys and you.
  44660. + *
  44661. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44662. + * under any End User Software License Agreement or Agreement for
  44663. + * Licensed Product with Synopsys or any supplement thereto. You are
  44664. + * permitted to use and redistribute this Software in source and binary
  44665. + * forms, with or without modification, provided that redistributions
  44666. + * of source code must retain this notice. You may not view, use,
  44667. + * disclose, copy or distribute this file or any information contained
  44668. + * herein except pursuant to this license grant from Synopsys. If you
  44669. + * do not agree with this notice, including the disclaimer below, then
  44670. + * you are not authorized to use the Software.
  44671. + *
  44672. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44673. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44674. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44675. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44676. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44677. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44678. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44679. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44680. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44681. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44682. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44683. + * DAMAGE.
  44684. + * ========================================================================= */
  44685. +#ifndef _DWC_DH_H_
  44686. +#define _DWC_DH_H_
  44687. +
  44688. +#ifdef __cplusplus
  44689. +extern "C" {
  44690. +#endif
  44691. +
  44692. +#include "dwc_os.h"
  44693. +
  44694. +/** @file
  44695. + *
  44696. + * This file defines the common functions on device and host for performing
  44697. + * numeric association as defined in the WUSB spec. They are only to be
  44698. + * used internally by the DWC UWB modules. */
  44699. +
  44700. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  44701. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  44702. + uint8_t *key, uint32_t keylen,
  44703. + uint8_t *out);
  44704. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  44705. + void *exp, uint32_t exp_len,
  44706. + void *mod, uint32_t mod_len,
  44707. + void *out);
  44708. +
  44709. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  44710. + *
  44711. + * PK = g^exp mod p.
  44712. + *
  44713. + * Input:
  44714. + * Nd = Number of digits on the device.
  44715. + *
  44716. + * Output:
  44717. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  44718. + * used as either A or B.
  44719. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  44720. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  44721. + */
  44722. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  44723. +
  44724. +/** Computes the DHKEY, and VD.
  44725. + *
  44726. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  44727. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  44728. + *
  44729. + * Input:
  44730. + * pkd = The PKD value.
  44731. + * pkh = The PKH value.
  44732. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  44733. + * is_host = Set to non zero if a WUSB host is calling this function.
  44734. + *
  44735. + * Output:
  44736. +
  44737. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  44738. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  44739. + * null termination character. This buffer can be used directly for display.
  44740. + * ck = A 16-byte buffer to be filled with the CK.
  44741. + * kdk = A 32-byte buffer to be filled with the KDK.
  44742. + */
  44743. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  44744. + uint8_t *exp, int is_host,
  44745. + char *dd, uint8_t *ck, uint8_t *kdk);
  44746. +
  44747. +#ifdef DH_TEST_VECTORS
  44748. +extern void dwc_run_dh_test_vectors(void);
  44749. +#endif
  44750. +
  44751. +#ifdef __cplusplus
  44752. +}
  44753. +#endif
  44754. +
  44755. +#endif /* _DWC_DH_H_ */
  44756. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_list.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_list.h
  44757. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_list.h 1970-01-01 01:00:00.000000000 +0100
  44758. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_list.h 2014-04-24 15:35:04.169565731 +0200
  44759. @@ -0,0 +1,594 @@
  44760. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  44761. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  44762. +
  44763. +/*
  44764. + * Copyright (c) 1991, 1993
  44765. + * The Regents of the University of California. All rights reserved.
  44766. + *
  44767. + * Redistribution and use in source and binary forms, with or without
  44768. + * modification, are permitted provided that the following conditions
  44769. + * are met:
  44770. + * 1. Redistributions of source code must retain the above copyright
  44771. + * notice, this list of conditions and the following disclaimer.
  44772. + * 2. Redistributions in binary form must reproduce the above copyright
  44773. + * notice, this list of conditions and the following disclaimer in the
  44774. + * documentation and/or other materials provided with the distribution.
  44775. + * 3. Neither the name of the University nor the names of its contributors
  44776. + * may be used to endorse or promote products derived from this software
  44777. + * without specific prior written permission.
  44778. + *
  44779. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  44780. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44781. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44782. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  44783. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44784. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44785. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44786. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44787. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44788. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44789. + * SUCH DAMAGE.
  44790. + *
  44791. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  44792. + */
  44793. +
  44794. +#ifndef _DWC_LIST_H_
  44795. +#define _DWC_LIST_H_
  44796. +
  44797. +#ifdef __cplusplus
  44798. +extern "C" {
  44799. +#endif
  44800. +
  44801. +/** @file
  44802. + *
  44803. + * This file defines linked list operations. It is derived from BSD with
  44804. + * only the MACRO names being prefixed with DWC_. This is because a few of
  44805. + * these names conflict with those on Linux. For documentation on use, see the
  44806. + * inline comments in the source code. The original license for this source
  44807. + * code applies and is preserved in the dwc_list.h source file.
  44808. + */
  44809. +
  44810. +/*
  44811. + * This file defines five types of data structures: singly-linked lists,
  44812. + * lists, simple queues, tail queues, and circular queues.
  44813. + *
  44814. + *
  44815. + * A singly-linked list is headed by a single forward pointer. The elements
  44816. + * are singly linked for minimum space and pointer manipulation overhead at
  44817. + * the expense of O(n) removal for arbitrary elements. New elements can be
  44818. + * added to the list after an existing element or at the head of the list.
  44819. + * Elements being removed from the head of the list should use the explicit
  44820. + * macro for this purpose for optimum efficiency. A singly-linked list may
  44821. + * only be traversed in the forward direction. Singly-linked lists are ideal
  44822. + * for applications with large datasets and few or no removals or for
  44823. + * implementing a LIFO queue.
  44824. + *
  44825. + * A list is headed by a single forward pointer (or an array of forward
  44826. + * pointers for a hash table header). The elements are doubly linked
  44827. + * so that an arbitrary element can be removed without a need to
  44828. + * traverse the list. New elements can be added to the list before
  44829. + * or after an existing element or at the head of the list. A list
  44830. + * may only be traversed in the forward direction.
  44831. + *
  44832. + * A simple queue is headed by a pair of pointers, one the head of the
  44833. + * list and the other to the tail of the list. The elements are singly
  44834. + * linked to save space, so elements can only be removed from the
  44835. + * head of the list. New elements can be added to the list before or after
  44836. + * an existing element, at the head of the list, or at the end of the
  44837. + * list. A simple queue may only be traversed in the forward direction.
  44838. + *
  44839. + * A tail queue is headed by a pair of pointers, one to the head of the
  44840. + * list and the other to the tail of the list. The elements are doubly
  44841. + * linked so that an arbitrary element can be removed without a need to
  44842. + * traverse the list. New elements can be added to the list before or
  44843. + * after an existing element, at the head of the list, or at the end of
  44844. + * the list. A tail queue may be traversed in either direction.
  44845. + *
  44846. + * A circle queue is headed by a pair of pointers, one to the head of the
  44847. + * list and the other to the tail of the list. The elements are doubly
  44848. + * linked so that an arbitrary element can be removed without a need to
  44849. + * traverse the list. New elements can be added to the list before or after
  44850. + * an existing element, at the head of the list, or at the end of the list.
  44851. + * A circle queue may be traversed in either direction, but has a more
  44852. + * complex end of list detection.
  44853. + *
  44854. + * For details on the use of these macros, see the queue(3) manual page.
  44855. + */
  44856. +
  44857. +/*
  44858. + * Double-linked List.
  44859. + */
  44860. +
  44861. +typedef struct dwc_list_link {
  44862. + struct dwc_list_link *next;
  44863. + struct dwc_list_link *prev;
  44864. +} dwc_list_link_t;
  44865. +
  44866. +#define DWC_LIST_INIT(link) do { \
  44867. + (link)->next = (link); \
  44868. + (link)->prev = (link); \
  44869. +} while (0)
  44870. +
  44871. +#define DWC_LIST_FIRST(link) ((link)->next)
  44872. +#define DWC_LIST_LAST(link) ((link)->prev)
  44873. +#define DWC_LIST_END(link) (link)
  44874. +#define DWC_LIST_NEXT(link) ((link)->next)
  44875. +#define DWC_LIST_PREV(link) ((link)->prev)
  44876. +#define DWC_LIST_EMPTY(link) \
  44877. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  44878. +#define DWC_LIST_ENTRY(link, type, field) \
  44879. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  44880. +
  44881. +#if 0
  44882. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  44883. + (link)->next = (list)->next; \
  44884. + (link)->prev = (list); \
  44885. + (list)->next->prev = (link); \
  44886. + (list)->next = (link); \
  44887. +} while (0)
  44888. +
  44889. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  44890. + (link)->next = (list); \
  44891. + (link)->prev = (list)->prev; \
  44892. + (list)->prev->next = (link); \
  44893. + (list)->prev = (link); \
  44894. +} while (0)
  44895. +#else
  44896. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  44897. + dwc_list_link_t *__next__ = (list)->next; \
  44898. + __next__->prev = (link); \
  44899. + (link)->next = __next__; \
  44900. + (link)->prev = (list); \
  44901. + (list)->next = (link); \
  44902. +} while (0)
  44903. +
  44904. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  44905. + dwc_list_link_t *__prev__ = (list)->prev; \
  44906. + (list)->prev = (link); \
  44907. + (link)->next = (list); \
  44908. + (link)->prev = __prev__; \
  44909. + __prev__->next = (link); \
  44910. +} while (0)
  44911. +#endif
  44912. +
  44913. +#if 0
  44914. +static inline void __list_add(struct list_head *new,
  44915. + struct list_head *prev,
  44916. + struct list_head *next)
  44917. +{
  44918. + next->prev = new;
  44919. + new->next = next;
  44920. + new->prev = prev;
  44921. + prev->next = new;
  44922. +}
  44923. +
  44924. +static inline void list_add(struct list_head *new, struct list_head *head)
  44925. +{
  44926. + __list_add(new, head, head->next);
  44927. +}
  44928. +
  44929. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  44930. +{
  44931. + __list_add(new, head->prev, head);
  44932. +}
  44933. +
  44934. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  44935. +{
  44936. + next->prev = prev;
  44937. + prev->next = next;
  44938. +}
  44939. +
  44940. +static inline void list_del(struct list_head *entry)
  44941. +{
  44942. + __list_del(entry->prev, entry->next);
  44943. + entry->next = LIST_POISON1;
  44944. + entry->prev = LIST_POISON2;
  44945. +}
  44946. +#endif
  44947. +
  44948. +#define DWC_LIST_REMOVE(link) do { \
  44949. + (link)->next->prev = (link)->prev; \
  44950. + (link)->prev->next = (link)->next; \
  44951. +} while (0)
  44952. +
  44953. +#define DWC_LIST_REMOVE_INIT(link) do { \
  44954. + DWC_LIST_REMOVE(link); \
  44955. + DWC_LIST_INIT(link); \
  44956. +} while (0)
  44957. +
  44958. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  44959. + DWC_LIST_REMOVE(link); \
  44960. + DWC_LIST_INSERT_HEAD(list, link); \
  44961. +} while (0)
  44962. +
  44963. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  44964. + DWC_LIST_REMOVE(link); \
  44965. + DWC_LIST_INSERT_TAIL(list, link); \
  44966. +} while (0)
  44967. +
  44968. +#define DWC_LIST_FOREACH(var, list) \
  44969. + for((var) = DWC_LIST_FIRST(list); \
  44970. + (var) != DWC_LIST_END(list); \
  44971. + (var) = DWC_LIST_NEXT(var))
  44972. +
  44973. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  44974. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  44975. + (var) != DWC_LIST_END(list); \
  44976. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  44977. +
  44978. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  44979. + for((var) = DWC_LIST_LAST(list); \
  44980. + (var) != DWC_LIST_END(list); \
  44981. + (var) = DWC_LIST_PREV(var))
  44982. +
  44983. +/*
  44984. + * Singly-linked List definitions.
  44985. + */
  44986. +#define DWC_SLIST_HEAD(name, type) \
  44987. +struct name { \
  44988. + struct type *slh_first; /* first element */ \
  44989. +}
  44990. +
  44991. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  44992. + { NULL }
  44993. +
  44994. +#define DWC_SLIST_ENTRY(type) \
  44995. +struct { \
  44996. + struct type *sle_next; /* next element */ \
  44997. +}
  44998. +
  44999. +/*
  45000. + * Singly-linked List access methods.
  45001. + */
  45002. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  45003. +#define DWC_SLIST_END(head) NULL
  45004. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  45005. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  45006. +
  45007. +#define DWC_SLIST_FOREACH(var, head, field) \
  45008. + for((var) = SLIST_FIRST(head); \
  45009. + (var) != SLIST_END(head); \
  45010. + (var) = SLIST_NEXT(var, field))
  45011. +
  45012. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  45013. + for((varp) = &SLIST_FIRST((head)); \
  45014. + ((var) = *(varp)) != SLIST_END(head); \
  45015. + (varp) = &SLIST_NEXT((var), field))
  45016. +
  45017. +/*
  45018. + * Singly-linked List functions.
  45019. + */
  45020. +#define DWC_SLIST_INIT(head) { \
  45021. + SLIST_FIRST(head) = SLIST_END(head); \
  45022. +}
  45023. +
  45024. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  45025. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  45026. + (slistelm)->field.sle_next = (elm); \
  45027. +} while (0)
  45028. +
  45029. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  45030. + (elm)->field.sle_next = (head)->slh_first; \
  45031. + (head)->slh_first = (elm); \
  45032. +} while (0)
  45033. +
  45034. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  45035. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  45036. +} while (0)
  45037. +
  45038. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  45039. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  45040. +} while (0)
  45041. +
  45042. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  45043. + if ((head)->slh_first == (elm)) { \
  45044. + SLIST_REMOVE_HEAD((head), field); \
  45045. + } \
  45046. + else { \
  45047. + struct type *curelm = (head)->slh_first; \
  45048. + while( curelm->field.sle_next != (elm) ) \
  45049. + curelm = curelm->field.sle_next; \
  45050. + curelm->field.sle_next = \
  45051. + curelm->field.sle_next->field.sle_next; \
  45052. + } \
  45053. +} while (0)
  45054. +
  45055. +/*
  45056. + * Simple queue definitions.
  45057. + */
  45058. +#define DWC_SIMPLEQ_HEAD(name, type) \
  45059. +struct name { \
  45060. + struct type *sqh_first; /* first element */ \
  45061. + struct type **sqh_last; /* addr of last next element */ \
  45062. +}
  45063. +
  45064. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  45065. + { NULL, &(head).sqh_first }
  45066. +
  45067. +#define DWC_SIMPLEQ_ENTRY(type) \
  45068. +struct { \
  45069. + struct type *sqe_next; /* next element */ \
  45070. +}
  45071. +
  45072. +/*
  45073. + * Simple queue access methods.
  45074. + */
  45075. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  45076. +#define DWC_SIMPLEQ_END(head) NULL
  45077. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  45078. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  45079. +
  45080. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  45081. + for((var) = SIMPLEQ_FIRST(head); \
  45082. + (var) != SIMPLEQ_END(head); \
  45083. + (var) = SIMPLEQ_NEXT(var, field))
  45084. +
  45085. +/*
  45086. + * Simple queue functions.
  45087. + */
  45088. +#define DWC_SIMPLEQ_INIT(head) do { \
  45089. + (head)->sqh_first = NULL; \
  45090. + (head)->sqh_last = &(head)->sqh_first; \
  45091. +} while (0)
  45092. +
  45093. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  45094. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  45095. + (head)->sqh_last = &(elm)->field.sqe_next; \
  45096. + (head)->sqh_first = (elm); \
  45097. +} while (0)
  45098. +
  45099. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  45100. + (elm)->field.sqe_next = NULL; \
  45101. + *(head)->sqh_last = (elm); \
  45102. + (head)->sqh_last = &(elm)->field.sqe_next; \
  45103. +} while (0)
  45104. +
  45105. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  45106. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  45107. + (head)->sqh_last = &(elm)->field.sqe_next; \
  45108. + (listelm)->field.sqe_next = (elm); \
  45109. +} while (0)
  45110. +
  45111. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  45112. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  45113. + (head)->sqh_last = &(head)->sqh_first; \
  45114. +} while (0)
  45115. +
  45116. +/*
  45117. + * Tail queue definitions.
  45118. + */
  45119. +#define DWC_TAILQ_HEAD(name, type) \
  45120. +struct name { \
  45121. + struct type *tqh_first; /* first element */ \
  45122. + struct type **tqh_last; /* addr of last next element */ \
  45123. +}
  45124. +
  45125. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  45126. + { NULL, &(head).tqh_first }
  45127. +
  45128. +#define DWC_TAILQ_ENTRY(type) \
  45129. +struct { \
  45130. + struct type *tqe_next; /* next element */ \
  45131. + struct type **tqe_prev; /* address of previous next element */ \
  45132. +}
  45133. +
  45134. +/*
  45135. + * tail queue access methods
  45136. + */
  45137. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  45138. +#define DWC_TAILQ_END(head) NULL
  45139. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  45140. +#define DWC_TAILQ_LAST(head, headname) \
  45141. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  45142. +/* XXX */
  45143. +#define DWC_TAILQ_PREV(elm, headname, field) \
  45144. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  45145. +#define DWC_TAILQ_EMPTY(head) \
  45146. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  45147. +
  45148. +#define DWC_TAILQ_FOREACH(var, head, field) \
  45149. + for ((var) = DWC_TAILQ_FIRST(head); \
  45150. + (var) != DWC_TAILQ_END(head); \
  45151. + (var) = DWC_TAILQ_NEXT(var, field))
  45152. +
  45153. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  45154. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  45155. + (var) != DWC_TAILQ_END(head); \
  45156. + (var) = DWC_TAILQ_PREV(var, headname, field))
  45157. +
  45158. +/*
  45159. + * Tail queue functions.
  45160. + */
  45161. +#define DWC_TAILQ_INIT(head) do { \
  45162. + (head)->tqh_first = NULL; \
  45163. + (head)->tqh_last = &(head)->tqh_first; \
  45164. +} while (0)
  45165. +
  45166. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  45167. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  45168. + (head)->tqh_first->field.tqe_prev = \
  45169. + &(elm)->field.tqe_next; \
  45170. + else \
  45171. + (head)->tqh_last = &(elm)->field.tqe_next; \
  45172. + (head)->tqh_first = (elm); \
  45173. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  45174. +} while (0)
  45175. +
  45176. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  45177. + (elm)->field.tqe_next = NULL; \
  45178. + (elm)->field.tqe_prev = (head)->tqh_last; \
  45179. + *(head)->tqh_last = (elm); \
  45180. + (head)->tqh_last = &(elm)->field.tqe_next; \
  45181. +} while (0)
  45182. +
  45183. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  45184. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  45185. + (elm)->field.tqe_next->field.tqe_prev = \
  45186. + &(elm)->field.tqe_next; \
  45187. + else \
  45188. + (head)->tqh_last = &(elm)->field.tqe_next; \
  45189. + (listelm)->field.tqe_next = (elm); \
  45190. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  45191. +} while (0)
  45192. +
  45193. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  45194. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  45195. + (elm)->field.tqe_next = (listelm); \
  45196. + *(listelm)->field.tqe_prev = (elm); \
  45197. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  45198. +} while (0)
  45199. +
  45200. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  45201. + if (((elm)->field.tqe_next) != NULL) \
  45202. + (elm)->field.tqe_next->field.tqe_prev = \
  45203. + (elm)->field.tqe_prev; \
  45204. + else \
  45205. + (head)->tqh_last = (elm)->field.tqe_prev; \
  45206. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  45207. +} while (0)
  45208. +
  45209. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  45210. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  45211. + (elm2)->field.tqe_next->field.tqe_prev = \
  45212. + &(elm2)->field.tqe_next; \
  45213. + else \
  45214. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  45215. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  45216. + *(elm2)->field.tqe_prev = (elm2); \
  45217. +} while (0)
  45218. +
  45219. +/*
  45220. + * Circular queue definitions.
  45221. + */
  45222. +#define DWC_CIRCLEQ_HEAD(name, type) \
  45223. +struct name { \
  45224. + struct type *cqh_first; /* first element */ \
  45225. + struct type *cqh_last; /* last element */ \
  45226. +}
  45227. +
  45228. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  45229. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  45230. +
  45231. +#define DWC_CIRCLEQ_ENTRY(type) \
  45232. +struct { \
  45233. + struct type *cqe_next; /* next element */ \
  45234. + struct type *cqe_prev; /* previous element */ \
  45235. +}
  45236. +
  45237. +/*
  45238. + * Circular queue access methods
  45239. + */
  45240. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  45241. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  45242. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  45243. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  45244. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  45245. +#define DWC_CIRCLEQ_EMPTY(head) \
  45246. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  45247. +
  45248. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  45249. +
  45250. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  45251. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  45252. + (var) != DWC_CIRCLEQ_END(head); \
  45253. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  45254. +
  45255. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  45256. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  45257. + (var) != DWC_CIRCLEQ_END(head); \
  45258. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  45259. +
  45260. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  45261. + for((var) = DWC_CIRCLEQ_LAST(head); \
  45262. + (var) != DWC_CIRCLEQ_END(head); \
  45263. + (var) = DWC_CIRCLEQ_PREV(var, field))
  45264. +
  45265. +/*
  45266. + * Circular queue functions.
  45267. + */
  45268. +#define DWC_CIRCLEQ_INIT(head) do { \
  45269. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  45270. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  45271. +} while (0)
  45272. +
  45273. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  45274. + (elm)->field.cqe_next = NULL; \
  45275. + (elm)->field.cqe_prev = NULL; \
  45276. +} while (0)
  45277. +
  45278. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  45279. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  45280. + (elm)->field.cqe_prev = (listelm); \
  45281. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  45282. + (head)->cqh_last = (elm); \
  45283. + else \
  45284. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  45285. + (listelm)->field.cqe_next = (elm); \
  45286. +} while (0)
  45287. +
  45288. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  45289. + (elm)->field.cqe_next = (listelm); \
  45290. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  45291. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  45292. + (head)->cqh_first = (elm); \
  45293. + else \
  45294. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  45295. + (listelm)->field.cqe_prev = (elm); \
  45296. +} while (0)
  45297. +
  45298. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  45299. + (elm)->field.cqe_next = (head)->cqh_first; \
  45300. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  45301. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  45302. + (head)->cqh_last = (elm); \
  45303. + else \
  45304. + (head)->cqh_first->field.cqe_prev = (elm); \
  45305. + (head)->cqh_first = (elm); \
  45306. +} while (0)
  45307. +
  45308. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  45309. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  45310. + (elm)->field.cqe_prev = (head)->cqh_last; \
  45311. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  45312. + (head)->cqh_first = (elm); \
  45313. + else \
  45314. + (head)->cqh_last->field.cqe_next = (elm); \
  45315. + (head)->cqh_last = (elm); \
  45316. +} while (0)
  45317. +
  45318. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  45319. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  45320. + (head)->cqh_last = (elm)->field.cqe_prev; \
  45321. + else \
  45322. + (elm)->field.cqe_next->field.cqe_prev = \
  45323. + (elm)->field.cqe_prev; \
  45324. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  45325. + (head)->cqh_first = (elm)->field.cqe_next; \
  45326. + else \
  45327. + (elm)->field.cqe_prev->field.cqe_next = \
  45328. + (elm)->field.cqe_next; \
  45329. +} while (0)
  45330. +
  45331. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  45332. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  45333. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  45334. +} while (0)
  45335. +
  45336. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  45337. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  45338. + DWC_CIRCLEQ_END(head)) \
  45339. + (head).cqh_last = (elm2); \
  45340. + else \
  45341. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  45342. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  45343. + DWC_CIRCLEQ_END(head)) \
  45344. + (head).cqh_first = (elm2); \
  45345. + else \
  45346. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  45347. +} while (0)
  45348. +
  45349. +#ifdef __cplusplus
  45350. +}
  45351. +#endif
  45352. +
  45353. +#endif /* _DWC_LIST_H_ */
  45354. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_mem.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c
  45355. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_mem.c 1970-01-01 01:00:00.000000000 +0100
  45356. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c 2014-04-24 15:35:04.169565731 +0200
  45357. @@ -0,0 +1,245 @@
  45358. +/* Memory Debugging */
  45359. +#ifdef DWC_DEBUG_MEMORY
  45360. +
  45361. +#include "dwc_os.h"
  45362. +#include "dwc_list.h"
  45363. +
  45364. +struct allocation {
  45365. + void *addr;
  45366. + void *ctx;
  45367. + char *func;
  45368. + int line;
  45369. + uint32_t size;
  45370. + int dma;
  45371. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  45372. +};
  45373. +
  45374. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  45375. +
  45376. +struct allocation_manager {
  45377. + void *mem_ctx;
  45378. + struct allocation_queue allocations;
  45379. +
  45380. + /* statistics */
  45381. + int num;
  45382. + int num_freed;
  45383. + int num_active;
  45384. + uint32_t total;
  45385. + uint32_t cur;
  45386. + uint32_t max;
  45387. +};
  45388. +
  45389. +static struct allocation_manager *manager = NULL;
  45390. +
  45391. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  45392. + int dma)
  45393. +{
  45394. + struct allocation *a;
  45395. +
  45396. + DWC_ASSERT(manager != NULL, "manager not allocated");
  45397. +
  45398. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  45399. + if (!a) {
  45400. + return -DWC_E_NO_MEMORY;
  45401. + }
  45402. +
  45403. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  45404. + if (!a->func) {
  45405. + __DWC_FREE(manager->mem_ctx, a);
  45406. + return -DWC_E_NO_MEMORY;
  45407. + }
  45408. +
  45409. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  45410. + a->addr = addr;
  45411. + a->ctx = ctx;
  45412. + a->line = line;
  45413. + a->size = size;
  45414. + a->dma = dma;
  45415. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  45416. +
  45417. + /* Update stats */
  45418. + manager->num++;
  45419. + manager->num_active++;
  45420. + manager->total += size;
  45421. + manager->cur += size;
  45422. +
  45423. + if (manager->max < manager->cur) {
  45424. + manager->max = manager->cur;
  45425. + }
  45426. +
  45427. + return 0;
  45428. +}
  45429. +
  45430. +static struct allocation *find_allocation(void *ctx, void *addr)
  45431. +{
  45432. + struct allocation *a;
  45433. +
  45434. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  45435. + if (a->ctx == ctx && a->addr == addr) {
  45436. + return a;
  45437. + }
  45438. + }
  45439. +
  45440. + return NULL;
  45441. +}
  45442. +
  45443. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  45444. +{
  45445. + struct allocation *a = find_allocation(ctx, addr);
  45446. +
  45447. + if (!a) {
  45448. + DWC_ASSERT(0,
  45449. + "Free of address %p that was never allocated or already freed %s:%d",
  45450. + addr, func, line);
  45451. + return;
  45452. + }
  45453. +
  45454. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  45455. +
  45456. + manager->num_active--;
  45457. + manager->num_freed++;
  45458. + manager->cur -= a->size;
  45459. + __DWC_FREE(manager->mem_ctx, a->func);
  45460. + __DWC_FREE(manager->mem_ctx, a);
  45461. +}
  45462. +
  45463. +int dwc_memory_debug_start(void *mem_ctx)
  45464. +{
  45465. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  45466. +
  45467. + if (manager) {
  45468. + return -DWC_E_BUSY;
  45469. + }
  45470. +
  45471. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  45472. + if (!manager) {
  45473. + return -DWC_E_NO_MEMORY;
  45474. + }
  45475. +
  45476. + DWC_CIRCLEQ_INIT(&manager->allocations);
  45477. + manager->mem_ctx = mem_ctx;
  45478. + manager->num = 0;
  45479. + manager->num_freed = 0;
  45480. + manager->num_active = 0;
  45481. + manager->total = 0;
  45482. + manager->cur = 0;
  45483. + manager->max = 0;
  45484. +
  45485. + return 0;
  45486. +}
  45487. +
  45488. +void dwc_memory_debug_stop(void)
  45489. +{
  45490. + struct allocation *a;
  45491. +
  45492. + dwc_memory_debug_report();
  45493. +
  45494. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  45495. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  45496. + free_allocation(a->ctx, a->addr, NULL, -1);
  45497. + }
  45498. +
  45499. + __DWC_FREE(manager->mem_ctx, manager);
  45500. +}
  45501. +
  45502. +void dwc_memory_debug_report(void)
  45503. +{
  45504. + struct allocation *a;
  45505. +
  45506. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  45507. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  45508. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  45509. + DWC_PRINTF("Active = %d\n", manager->num_active);
  45510. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  45511. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  45512. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  45513. + DWC_PRINTF("Unfreed allocations:\n");
  45514. +
  45515. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  45516. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  45517. + a->addr, a->size, a->func, a->line, a->dma);
  45518. + }
  45519. +}
  45520. +
  45521. +/* The replacement functions */
  45522. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  45523. +{
  45524. + void *addr = __DWC_ALLOC(mem_ctx, size);
  45525. +
  45526. + if (!addr) {
  45527. + return NULL;
  45528. + }
  45529. +
  45530. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  45531. + __DWC_FREE(mem_ctx, addr);
  45532. + return NULL;
  45533. + }
  45534. +
  45535. + return addr;
  45536. +}
  45537. +
  45538. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  45539. + int line)
  45540. +{
  45541. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  45542. +
  45543. + if (!addr) {
  45544. + return NULL;
  45545. + }
  45546. +
  45547. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  45548. + __DWC_FREE(mem_ctx, addr);
  45549. + return NULL;
  45550. + }
  45551. +
  45552. + return addr;
  45553. +}
  45554. +
  45555. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  45556. +{
  45557. + free_allocation(mem_ctx, addr, func, line);
  45558. + __DWC_FREE(mem_ctx, addr);
  45559. +}
  45560. +
  45561. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  45562. + char const *func, int line)
  45563. +{
  45564. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  45565. +
  45566. + if (!addr) {
  45567. + return NULL;
  45568. + }
  45569. +
  45570. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  45571. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  45572. + return NULL;
  45573. + }
  45574. +
  45575. + return addr;
  45576. +}
  45577. +
  45578. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  45579. + dwc_dma_t *dma_addr, char const *func, int line)
  45580. +{
  45581. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  45582. +
  45583. + if (!addr) {
  45584. + return NULL;
  45585. + }
  45586. +
  45587. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  45588. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  45589. + return NULL;
  45590. + }
  45591. +
  45592. + return addr;
  45593. +}
  45594. +
  45595. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  45596. + dwc_dma_t dma_addr, char const *func, int line)
  45597. +{
  45598. + free_allocation(dma_ctx, virt_addr, func, line);
  45599. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  45600. +}
  45601. +
  45602. +#endif /* DWC_DEBUG_MEMORY */
  45603. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_modpow.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c
  45604. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_modpow.c 1970-01-01 01:00:00.000000000 +0100
  45605. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c 2014-04-24 15:35:04.169565731 +0200
  45606. @@ -0,0 +1,636 @@
  45607. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  45608. + *
  45609. + * PuTTY is copyright 1997-2007 Simon Tatham.
  45610. + *
  45611. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  45612. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  45613. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  45614. + * Kuhn, and CORE SDI S.A.
  45615. + *
  45616. + * Permission is hereby granted, free of charge, to any person
  45617. + * obtaining a copy of this software and associated documentation files
  45618. + * (the "Software"), to deal in the Software without restriction,
  45619. + * including without limitation the rights to use, copy, modify, merge,
  45620. + * publish, distribute, sublicense, and/or sell copies of the Software,
  45621. + * and to permit persons to whom the Software is furnished to do so,
  45622. + * subject to the following conditions:
  45623. + *
  45624. + * The above copyright notice and this permission notice shall be
  45625. + * included in all copies or substantial portions of the Software.
  45626. +
  45627. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  45628. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  45629. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  45630. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  45631. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  45632. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  45633. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  45634. + *
  45635. + */
  45636. +#ifdef DWC_CRYPTOLIB
  45637. +
  45638. +#ifndef CONFIG_MACH_IPMATE
  45639. +
  45640. +#include "dwc_modpow.h"
  45641. +
  45642. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  45643. +#define BIGNUM_TOP_BIT 0x80000000UL
  45644. +#define BIGNUM_INT_BITS 32
  45645. +
  45646. +
  45647. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  45648. +{
  45649. + void *p;
  45650. + size *= n;
  45651. + if (size == 0) size = 1;
  45652. + p = dwc_alloc(mem_ctx, size);
  45653. + return p;
  45654. +}
  45655. +
  45656. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  45657. +#define sfree dwc_free
  45658. +
  45659. +/*
  45660. + * Usage notes:
  45661. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  45662. + * subscripts, as some implementations object to this (see below).
  45663. + * * Note that none of the division methods below will cope if the
  45664. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  45665. + * to avoid this case.
  45666. + * If this condition occurs, in the case of the x86 DIV instruction,
  45667. + * an overflow exception will occur, which (according to a correspondent)
  45668. + * will manifest on Windows as something like
  45669. + * 0xC0000095: Integer overflow
  45670. + * The C variant won't give the right answer, either.
  45671. + */
  45672. +
  45673. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  45674. +
  45675. +#if defined __GNUC__ && defined __i386__
  45676. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  45677. + __asm__("div %2" : \
  45678. + "=d" (r), "=a" (q) : \
  45679. + "r" (w), "d" (hi), "a" (lo))
  45680. +#else
  45681. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  45682. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  45683. + q = n / w; \
  45684. + r = n % w; \
  45685. +} while (0)
  45686. +#endif
  45687. +
  45688. +// q = n / w;
  45689. +// r = n % w;
  45690. +
  45691. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  45692. +
  45693. +#define BIGNUM_INTERNAL
  45694. +
  45695. +static Bignum newbn(void *mem_ctx, int length)
  45696. +{
  45697. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  45698. + //if (!b)
  45699. + //abort(); /* FIXME */
  45700. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  45701. + b[0] = length;
  45702. + return b;
  45703. +}
  45704. +
  45705. +void freebn(void *mem_ctx, Bignum b)
  45706. +{
  45707. + /*
  45708. + * Burn the evidence, just in case.
  45709. + */
  45710. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  45711. + sfree(mem_ctx, b);
  45712. +}
  45713. +
  45714. +/*
  45715. + * Compute c = a * b.
  45716. + * Input is in the first len words of a and b.
  45717. + * Result is returned in the first 2*len words of c.
  45718. + */
  45719. +static void internal_mul(BignumInt *a, BignumInt *b,
  45720. + BignumInt *c, int len)
  45721. +{
  45722. + int i, j;
  45723. + BignumDblInt t;
  45724. +
  45725. + for (j = 0; j < 2 * len; j++)
  45726. + c[j] = 0;
  45727. +
  45728. + for (i = len - 1; i >= 0; i--) {
  45729. + t = 0;
  45730. + for (j = len - 1; j >= 0; j--) {
  45731. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  45732. + t += (BignumDblInt) c[i + j + 1];
  45733. + c[i + j + 1] = (BignumInt) t;
  45734. + t = t >> BIGNUM_INT_BITS;
  45735. + }
  45736. + c[i] = (BignumInt) t;
  45737. + }
  45738. +}
  45739. +
  45740. +static void internal_add_shifted(BignumInt *number,
  45741. + unsigned n, int shift)
  45742. +{
  45743. + int word = 1 + (shift / BIGNUM_INT_BITS);
  45744. + int bshift = shift % BIGNUM_INT_BITS;
  45745. + BignumDblInt addend;
  45746. +
  45747. + addend = (BignumDblInt)n << bshift;
  45748. +
  45749. + while (addend) {
  45750. + addend += number[word];
  45751. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  45752. + addend >>= BIGNUM_INT_BITS;
  45753. + word++;
  45754. + }
  45755. +}
  45756. +
  45757. +/*
  45758. + * Compute a = a % m.
  45759. + * Input in first alen words of a and first mlen words of m.
  45760. + * Output in first alen words of a
  45761. + * (of which first alen-mlen words will be zero).
  45762. + * The MSW of m MUST have its high bit set.
  45763. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  45764. + * rather than the internal bigendian format. Quotient parts are shifted
  45765. + * left by `qshift' before adding into quot.
  45766. + */
  45767. +static void internal_mod(BignumInt *a, int alen,
  45768. + BignumInt *m, int mlen,
  45769. + BignumInt *quot, int qshift)
  45770. +{
  45771. + BignumInt m0, m1;
  45772. + unsigned int h;
  45773. + int i, k;
  45774. +
  45775. + m0 = m[0];
  45776. + if (mlen > 1)
  45777. + m1 = m[1];
  45778. + else
  45779. + m1 = 0;
  45780. +
  45781. + for (i = 0; i <= alen - mlen; i++) {
  45782. + BignumDblInt t;
  45783. + unsigned int q, r, c, ai1;
  45784. +
  45785. + if (i == 0) {
  45786. + h = 0;
  45787. + } else {
  45788. + h = a[i - 1];
  45789. + a[i - 1] = 0;
  45790. + }
  45791. +
  45792. + if (i == alen - 1)
  45793. + ai1 = 0;
  45794. + else
  45795. + ai1 = a[i + 1];
  45796. +
  45797. + /* Find q = h:a[i] / m0 */
  45798. + if (h >= m0) {
  45799. + /*
  45800. + * Special case.
  45801. + *
  45802. + * To illustrate it, suppose a BignumInt is 8 bits, and
  45803. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  45804. + * our initial division will be 0xA123 / 0xA1, which
  45805. + * will give a quotient of 0x100 and a divide overflow.
  45806. + * However, the invariants in this division algorithm
  45807. + * are not violated, since the full number A1:23:... is
  45808. + * _less_ than the quotient prefix A1:B2:... and so the
  45809. + * following correction loop would have sorted it out.
  45810. + *
  45811. + * In this situation we set q to be the largest
  45812. + * quotient we _can_ stomach (0xFF, of course).
  45813. + */
  45814. + q = BIGNUM_INT_MASK;
  45815. + } else {
  45816. + /* Macro doesn't want an array subscript expression passed
  45817. + * into it (see definition), so use a temporary. */
  45818. + BignumInt tmplo = a[i];
  45819. + DIVMOD_WORD(q, r, h, tmplo, m0);
  45820. +
  45821. + /* Refine our estimate of q by looking at
  45822. + h:a[i]:a[i+1] / m0:m1 */
  45823. + t = MUL_WORD(m1, q);
  45824. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  45825. + q--;
  45826. + t -= m1;
  45827. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  45828. + if (r >= (BignumDblInt) m0 &&
  45829. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  45830. + }
  45831. + }
  45832. +
  45833. + /* Subtract q * m from a[i...] */
  45834. + c = 0;
  45835. + for (k = mlen - 1; k >= 0; k--) {
  45836. + t = MUL_WORD(q, m[k]);
  45837. + t += c;
  45838. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  45839. + if ((BignumInt) t > a[i + k])
  45840. + c++;
  45841. + a[i + k] -= (BignumInt) t;
  45842. + }
  45843. +
  45844. + /* Add back m in case of borrow */
  45845. + if (c != h) {
  45846. + t = 0;
  45847. + for (k = mlen - 1; k >= 0; k--) {
  45848. + t += m[k];
  45849. + t += a[i + k];
  45850. + a[i + k] = (BignumInt) t;
  45851. + t = t >> BIGNUM_INT_BITS;
  45852. + }
  45853. + q--;
  45854. + }
  45855. + if (quot)
  45856. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  45857. + }
  45858. +}
  45859. +
  45860. +/*
  45861. + * Compute p % mod.
  45862. + * The most significant word of mod MUST be non-zero.
  45863. + * We assume that the result array is the same size as the mod array.
  45864. + * We optionally write out a quotient if `quotient' is non-NULL.
  45865. + * We can avoid writing out the result if `result' is NULL.
  45866. + */
  45867. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  45868. +{
  45869. + BignumInt *n, *m;
  45870. + int mshift;
  45871. + int plen, mlen, i, j;
  45872. +
  45873. + /* Allocate m of size mlen, copy mod to m */
  45874. + /* We use big endian internally */
  45875. + mlen = mod[0];
  45876. + m = snewn(mem_ctx, mlen, BignumInt);
  45877. + //if (!m)
  45878. + //abort(); /* FIXME */
  45879. + for (j = 0; j < mlen; j++)
  45880. + m[j] = mod[mod[0] - j];
  45881. +
  45882. + /* Shift m left to make msb bit set */
  45883. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  45884. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  45885. + break;
  45886. + if (mshift) {
  45887. + for (i = 0; i < mlen - 1; i++)
  45888. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  45889. + m[mlen - 1] = m[mlen - 1] << mshift;
  45890. + }
  45891. +
  45892. + plen = p[0];
  45893. + /* Ensure plen > mlen */
  45894. + if (plen <= mlen)
  45895. + plen = mlen + 1;
  45896. +
  45897. + /* Allocate n of size plen, copy p to n */
  45898. + n = snewn(mem_ctx, plen, BignumInt);
  45899. + //if (!n)
  45900. + //abort(); /* FIXME */
  45901. + for (j = 0; j < plen; j++)
  45902. + n[j] = 0;
  45903. + for (j = 1; j <= (int)p[0]; j++)
  45904. + n[plen - j] = p[j];
  45905. +
  45906. + /* Main computation */
  45907. + internal_mod(n, plen, m, mlen, quotient, mshift);
  45908. +
  45909. + /* Fixup result in case the modulus was shifted */
  45910. + if (mshift) {
  45911. + for (i = plen - mlen - 1; i < plen - 1; i++)
  45912. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  45913. + n[plen - 1] = n[plen - 1] << mshift;
  45914. + internal_mod(n, plen, m, mlen, quotient, 0);
  45915. + for (i = plen - 1; i >= plen - mlen; i--)
  45916. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  45917. + }
  45918. +
  45919. + /* Copy result to buffer */
  45920. + if (result) {
  45921. + for (i = 1; i <= (int)result[0]; i++) {
  45922. + int j = plen - i;
  45923. + result[i] = j >= 0 ? n[j] : 0;
  45924. + }
  45925. + }
  45926. +
  45927. + /* Free temporary arrays */
  45928. + for (i = 0; i < mlen; i++)
  45929. + m[i] = 0;
  45930. + sfree(mem_ctx, m);
  45931. + for (i = 0; i < plen; i++)
  45932. + n[i] = 0;
  45933. + sfree(mem_ctx, n);
  45934. +}
  45935. +
  45936. +/*
  45937. + * Simple remainder.
  45938. + */
  45939. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  45940. +{
  45941. + Bignum r = newbn(mem_ctx, b[0]);
  45942. + bigdivmod(mem_ctx, a, b, r, NULL);
  45943. + return r;
  45944. +}
  45945. +
  45946. +/*
  45947. + * Compute (base ^ exp) % mod.
  45948. + */
  45949. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  45950. +{
  45951. + BignumInt *a, *b, *n, *m;
  45952. + int mshift;
  45953. + int mlen, i, j;
  45954. + Bignum base, result;
  45955. +
  45956. + /*
  45957. + * The most significant word of mod needs to be non-zero. It
  45958. + * should already be, but let's make sure.
  45959. + */
  45960. + //assert(mod[mod[0]] != 0);
  45961. +
  45962. + /*
  45963. + * Make sure the base is smaller than the modulus, by reducing
  45964. + * it modulo the modulus if not.
  45965. + */
  45966. + base = bigmod(mem_ctx, base_in, mod);
  45967. +
  45968. + /* Allocate m of size mlen, copy mod to m */
  45969. + /* We use big endian internally */
  45970. + mlen = mod[0];
  45971. + m = snewn(mem_ctx, mlen, BignumInt);
  45972. + //if (!m)
  45973. + //abort(); /* FIXME */
  45974. + for (j = 0; j < mlen; j++)
  45975. + m[j] = mod[mod[0] - j];
  45976. +
  45977. + /* Shift m left to make msb bit set */
  45978. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  45979. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  45980. + break;
  45981. + if (mshift) {
  45982. + for (i = 0; i < mlen - 1; i++)
  45983. + m[i] =
  45984. + (m[i] << mshift) | (m[i + 1] >>
  45985. + (BIGNUM_INT_BITS - mshift));
  45986. + m[mlen - 1] = m[mlen - 1] << mshift;
  45987. + }
  45988. +
  45989. + /* Allocate n of size mlen, copy base to n */
  45990. + n = snewn(mem_ctx, mlen, BignumInt);
  45991. + //if (!n)
  45992. + //abort(); /* FIXME */
  45993. + i = mlen - base[0];
  45994. + for (j = 0; j < i; j++)
  45995. + n[j] = 0;
  45996. + for (j = 0; j < base[0]; j++)
  45997. + n[i + j] = base[base[0] - j];
  45998. +
  45999. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  46000. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  46001. + //if (!a)
  46002. + //abort(); /* FIXME */
  46003. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  46004. + //if (!b)
  46005. + //abort(); /* FIXME */
  46006. + for (i = 0; i < 2 * mlen; i++)
  46007. + a[i] = 0;
  46008. + a[2 * mlen - 1] = 1;
  46009. +
  46010. + /* Skip leading zero bits of exp. */
  46011. + i = 0;
  46012. + j = BIGNUM_INT_BITS - 1;
  46013. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  46014. + j--;
  46015. + if (j < 0) {
  46016. + i++;
  46017. + j = BIGNUM_INT_BITS - 1;
  46018. + }
  46019. + }
  46020. +
  46021. + /* Main computation */
  46022. + while (i < exp[0]) {
  46023. + while (j >= 0) {
  46024. + internal_mul(a + mlen, a + mlen, b, mlen);
  46025. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  46026. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  46027. + internal_mul(b + mlen, n, a, mlen);
  46028. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  46029. + } else {
  46030. + BignumInt *t;
  46031. + t = a;
  46032. + a = b;
  46033. + b = t;
  46034. + }
  46035. + j--;
  46036. + }
  46037. + i++;
  46038. + j = BIGNUM_INT_BITS - 1;
  46039. + }
  46040. +
  46041. + /* Fixup result in case the modulus was shifted */
  46042. + if (mshift) {
  46043. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  46044. + a[i] =
  46045. + (a[i] << mshift) | (a[i + 1] >>
  46046. + (BIGNUM_INT_BITS - mshift));
  46047. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  46048. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  46049. + for (i = 2 * mlen - 1; i >= mlen; i--)
  46050. + a[i] =
  46051. + (a[i] >> mshift) | (a[i - 1] <<
  46052. + (BIGNUM_INT_BITS - mshift));
  46053. + }
  46054. +
  46055. + /* Copy result to buffer */
  46056. + result = newbn(mem_ctx, mod[0]);
  46057. + for (i = 0; i < mlen; i++)
  46058. + result[result[0] - i] = a[i + mlen];
  46059. + while (result[0] > 1 && result[result[0]] == 0)
  46060. + result[0]--;
  46061. +
  46062. + /* Free temporary arrays */
  46063. + for (i = 0; i < 2 * mlen; i++)
  46064. + a[i] = 0;
  46065. + sfree(mem_ctx, a);
  46066. + for (i = 0; i < 2 * mlen; i++)
  46067. + b[i] = 0;
  46068. + sfree(mem_ctx, b);
  46069. + for (i = 0; i < mlen; i++)
  46070. + m[i] = 0;
  46071. + sfree(mem_ctx, m);
  46072. + for (i = 0; i < mlen; i++)
  46073. + n[i] = 0;
  46074. + sfree(mem_ctx, n);
  46075. +
  46076. + freebn(mem_ctx, base);
  46077. +
  46078. + return result;
  46079. +}
  46080. +
  46081. +
  46082. +#ifdef UNITTEST
  46083. +
  46084. +static __u32 dh_p[] = {
  46085. + 96,
  46086. + 0xFFFFFFFF,
  46087. + 0xFFFFFFFF,
  46088. + 0xA93AD2CA,
  46089. + 0x4B82D120,
  46090. + 0xE0FD108E,
  46091. + 0x43DB5BFC,
  46092. + 0x74E5AB31,
  46093. + 0x08E24FA0,
  46094. + 0xBAD946E2,
  46095. + 0x770988C0,
  46096. + 0x7A615D6C,
  46097. + 0xBBE11757,
  46098. + 0x177B200C,
  46099. + 0x521F2B18,
  46100. + 0x3EC86A64,
  46101. + 0xD8760273,
  46102. + 0xD98A0864,
  46103. + 0xF12FFA06,
  46104. + 0x1AD2EE6B,
  46105. + 0xCEE3D226,
  46106. + 0x4A25619D,
  46107. + 0x1E8C94E0,
  46108. + 0xDB0933D7,
  46109. + 0xABF5AE8C,
  46110. + 0xA6E1E4C7,
  46111. + 0xB3970F85,
  46112. + 0x5D060C7D,
  46113. + 0x8AEA7157,
  46114. + 0x58DBEF0A,
  46115. + 0xECFB8504,
  46116. + 0xDF1CBA64,
  46117. + 0xA85521AB,
  46118. + 0x04507A33,
  46119. + 0xAD33170D,
  46120. + 0x8AAAC42D,
  46121. + 0x15728E5A,
  46122. + 0x98FA0510,
  46123. + 0x15D22618,
  46124. + 0xEA956AE5,
  46125. + 0x3995497C,
  46126. + 0x95581718,
  46127. + 0xDE2BCBF6,
  46128. + 0x6F4C52C9,
  46129. + 0xB5C55DF0,
  46130. + 0xEC07A28F,
  46131. + 0x9B2783A2,
  46132. + 0x180E8603,
  46133. + 0xE39E772C,
  46134. + 0x2E36CE3B,
  46135. + 0x32905E46,
  46136. + 0xCA18217C,
  46137. + 0xF1746C08,
  46138. + 0x4ABC9804,
  46139. + 0x670C354E,
  46140. + 0x7096966D,
  46141. + 0x9ED52907,
  46142. + 0x208552BB,
  46143. + 0x1C62F356,
  46144. + 0xDCA3AD96,
  46145. + 0x83655D23,
  46146. + 0xFD24CF5F,
  46147. + 0x69163FA8,
  46148. + 0x1C55D39A,
  46149. + 0x98DA4836,
  46150. + 0xA163BF05,
  46151. + 0xC2007CB8,
  46152. + 0xECE45B3D,
  46153. + 0x49286651,
  46154. + 0x7C4B1FE6,
  46155. + 0xAE9F2411,
  46156. + 0x5A899FA5,
  46157. + 0xEE386BFB,
  46158. + 0xF406B7ED,
  46159. + 0x0BFF5CB6,
  46160. + 0xA637ED6B,
  46161. + 0xF44C42E9,
  46162. + 0x625E7EC6,
  46163. + 0xE485B576,
  46164. + 0x6D51C245,
  46165. + 0x4FE1356D,
  46166. + 0xF25F1437,
  46167. + 0x302B0A6D,
  46168. + 0xCD3A431B,
  46169. + 0xEF9519B3,
  46170. + 0x8E3404DD,
  46171. + 0x514A0879,
  46172. + 0x3B139B22,
  46173. + 0x020BBEA6,
  46174. + 0x8A67CC74,
  46175. + 0x29024E08,
  46176. + 0x80DC1CD1,
  46177. + 0xC4C6628B,
  46178. + 0x2168C234,
  46179. + 0xC90FDAA2,
  46180. + 0xFFFFFFFF,
  46181. + 0xFFFFFFFF,
  46182. +};
  46183. +
  46184. +static __u32 dh_a[] = {
  46185. + 8,
  46186. + 0xdf367516,
  46187. + 0x86459caa,
  46188. + 0xe2d459a4,
  46189. + 0xd910dae0,
  46190. + 0x8a8b5e37,
  46191. + 0x67ab31c6,
  46192. + 0xf0b55ea9,
  46193. + 0x440051d6,
  46194. +};
  46195. +
  46196. +static __u32 dh_b[] = {
  46197. + 8,
  46198. + 0xded92656,
  46199. + 0xe07a048a,
  46200. + 0x6fa452cd,
  46201. + 0x2df89d30,
  46202. + 0xc75f1b0f,
  46203. + 0x8ce3578f,
  46204. + 0x7980a324,
  46205. + 0x5daec786,
  46206. +};
  46207. +
  46208. +static __u32 dh_g[] = {
  46209. + 1,
  46210. + 2,
  46211. +};
  46212. +
  46213. +int main(void)
  46214. +{
  46215. + int i;
  46216. + __u32 *k;
  46217. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  46218. +
  46219. + printf("\n\n");
  46220. + for (i=0; i<k[0]; i++) {
  46221. + __u32 word32 = k[k[0] - i];
  46222. + __u16 l = word32 & 0xffff;
  46223. + __u16 m = (word32 & 0xffff0000) >> 16;
  46224. + printf("%04x %04x ", m, l);
  46225. + if (!((i + 1)%13)) printf("\n");
  46226. + }
  46227. + printf("\n\n");
  46228. +
  46229. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  46230. + printf("PASS\n\n");
  46231. + }
  46232. + else {
  46233. + printf("FAIL\n\n");
  46234. + }
  46235. +
  46236. +}
  46237. +
  46238. +#endif /* UNITTEST */
  46239. +
  46240. +#endif /* CONFIG_MACH_IPMATE */
  46241. +
  46242. +#endif /*DWC_CRYPTOLIB */
  46243. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_modpow.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h
  46244. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_modpow.h 1970-01-01 01:00:00.000000000 +0100
  46245. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h 2014-04-24 15:35:04.169565731 +0200
  46246. @@ -0,0 +1,34 @@
  46247. +/*
  46248. + * dwc_modpow.h
  46249. + * See dwc_modpow.c for license and changes
  46250. + */
  46251. +#ifndef _DWC_MODPOW_H
  46252. +#define _DWC_MODPOW_H
  46253. +
  46254. +#ifdef __cplusplus
  46255. +extern "C" {
  46256. +#endif
  46257. +
  46258. +#include "dwc_os.h"
  46259. +
  46260. +/** @file
  46261. + *
  46262. + * This file defines the module exponentiation function which is only used
  46263. + * internally by the DWC UWB modules for calculation of PKs during numeric
  46264. + * association. The routine is taken from the PUTTY, an open source terminal
  46265. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  46266. + *
  46267. + */
  46268. +
  46269. +typedef uint32_t BignumInt;
  46270. +typedef uint64_t BignumDblInt;
  46271. +typedef BignumInt *Bignum;
  46272. +
  46273. +/* Compute modular exponentiaion */
  46274. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  46275. +
  46276. +#ifdef __cplusplus
  46277. +}
  46278. +#endif
  46279. +
  46280. +#endif /* _LINUX_BIGNUM_H */
  46281. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_notifier.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c
  46282. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_notifier.c 1970-01-01 01:00:00.000000000 +0100
  46283. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c 2014-04-24 15:35:04.169565731 +0200
  46284. @@ -0,0 +1,319 @@
  46285. +#ifdef DWC_NOTIFYLIB
  46286. +
  46287. +#include "dwc_notifier.h"
  46288. +#include "dwc_list.h"
  46289. +
  46290. +typedef struct dwc_observer {
  46291. + void *observer;
  46292. + dwc_notifier_callback_t callback;
  46293. + void *data;
  46294. + char *notification;
  46295. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  46296. +} observer_t;
  46297. +
  46298. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  46299. +
  46300. +typedef struct dwc_notifier {
  46301. + void *mem_ctx;
  46302. + void *object;
  46303. + struct observer_queue observers;
  46304. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  46305. +} notifier_t;
  46306. +
  46307. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  46308. +
  46309. +typedef struct manager {
  46310. + void *mem_ctx;
  46311. + void *wkq_ctx;
  46312. + dwc_workq_t *wq;
  46313. +// dwc_mutex_t *mutex;
  46314. + struct notifier_queue notifiers;
  46315. +} manager_t;
  46316. +
  46317. +static manager_t *manager = NULL;
  46318. +
  46319. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  46320. +{
  46321. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  46322. + if (!manager) {
  46323. + return -DWC_E_NO_MEMORY;
  46324. + }
  46325. +
  46326. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  46327. +
  46328. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  46329. + if (!manager->wq) {
  46330. + return -DWC_E_NO_MEMORY;
  46331. + }
  46332. +
  46333. + return 0;
  46334. +}
  46335. +
  46336. +static void free_manager(void)
  46337. +{
  46338. + dwc_workq_free(manager->wq);
  46339. +
  46340. + /* All notifiers must have unregistered themselves before this module
  46341. + * can be removed. Hitting this assertion indicates a programmer
  46342. + * error. */
  46343. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  46344. + "Notification manager being freed before all notifiers have been removed");
  46345. + dwc_free(manager->mem_ctx, manager);
  46346. +}
  46347. +
  46348. +#ifdef DEBUG
  46349. +static void dump_manager(void)
  46350. +{
  46351. + notifier_t *n;
  46352. + observer_t *o;
  46353. +
  46354. + DWC_ASSERT(manager, "Notification manager not found");
  46355. +
  46356. + DWC_DEBUG("List of all notifiers and observers:\n");
  46357. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  46358. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  46359. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  46360. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  46361. + }
  46362. + }
  46363. +}
  46364. +#else
  46365. +#define dump_manager(...)
  46366. +#endif
  46367. +
  46368. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  46369. + dwc_notifier_callback_t callback, void *data)
  46370. +{
  46371. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  46372. +
  46373. + if (!new_observer) {
  46374. + return NULL;
  46375. + }
  46376. +
  46377. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  46378. + new_observer->observer = observer;
  46379. + new_observer->notification = notification;
  46380. + new_observer->callback = callback;
  46381. + new_observer->data = data;
  46382. + return new_observer;
  46383. +}
  46384. +
  46385. +static void free_observer(void *mem_ctx, observer_t *observer)
  46386. +{
  46387. + dwc_free(mem_ctx, observer);
  46388. +}
  46389. +
  46390. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  46391. +{
  46392. + notifier_t *notifier;
  46393. +
  46394. + if (!object) {
  46395. + return NULL;
  46396. + }
  46397. +
  46398. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  46399. + if (!notifier) {
  46400. + return NULL;
  46401. + }
  46402. +
  46403. + DWC_CIRCLEQ_INIT(&notifier->observers);
  46404. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  46405. +
  46406. + notifier->mem_ctx = mem_ctx;
  46407. + notifier->object = object;
  46408. + return notifier;
  46409. +}
  46410. +
  46411. +static void free_notifier(notifier_t *notifier)
  46412. +{
  46413. + observer_t *observer;
  46414. +
  46415. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  46416. + free_observer(notifier->mem_ctx, observer);
  46417. + }
  46418. +
  46419. + dwc_free(notifier->mem_ctx, notifier);
  46420. +}
  46421. +
  46422. +static notifier_t *find_notifier(void *object)
  46423. +{
  46424. + notifier_t *notifier;
  46425. +
  46426. + DWC_ASSERT(manager, "Notification manager not found");
  46427. +
  46428. + if (!object) {
  46429. + return NULL;
  46430. + }
  46431. +
  46432. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  46433. + if (notifier->object == object) {
  46434. + return notifier;
  46435. + }
  46436. + }
  46437. +
  46438. + return NULL;
  46439. +}
  46440. +
  46441. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  46442. +{
  46443. + return create_manager(mem_ctx, wkq_ctx);
  46444. +}
  46445. +
  46446. +void dwc_free_notification_manager(void)
  46447. +{
  46448. + free_manager();
  46449. +}
  46450. +
  46451. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  46452. +{
  46453. + notifier_t *notifier;
  46454. +
  46455. + DWC_ASSERT(manager, "Notification manager not found");
  46456. +
  46457. + notifier = find_notifier(object);
  46458. + if (notifier) {
  46459. + DWC_ERROR("Notifier %p is already registered\n", object);
  46460. + return NULL;
  46461. + }
  46462. +
  46463. + notifier = alloc_notifier(mem_ctx, object);
  46464. + if (!notifier) {
  46465. + return NULL;
  46466. + }
  46467. +
  46468. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  46469. +
  46470. + DWC_INFO("Notifier %p registered", object);
  46471. + dump_manager();
  46472. +
  46473. + return notifier;
  46474. +}
  46475. +
  46476. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  46477. +{
  46478. + DWC_ASSERT(manager, "Notification manager not found");
  46479. +
  46480. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  46481. + observer_t *o;
  46482. +
  46483. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  46484. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  46485. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  46486. + }
  46487. +
  46488. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  46489. + "Notifier %p has active observers when removing", notifier);
  46490. + }
  46491. +
  46492. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  46493. + free_notifier(notifier);
  46494. +
  46495. + DWC_INFO("Notifier unregistered");
  46496. + dump_manager();
  46497. +}
  46498. +
  46499. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  46500. +int dwc_add_observer(void *observer, void *object, char *notification,
  46501. + dwc_notifier_callback_t callback, void *data)
  46502. +{
  46503. + notifier_t *notifier = find_notifier(object);
  46504. + observer_t *new_observer;
  46505. +
  46506. + if (!notifier) {
  46507. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  46508. + return -DWC_E_INVALID;
  46509. + }
  46510. +
  46511. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  46512. + if (!new_observer) {
  46513. + return -DWC_E_NO_MEMORY;
  46514. + }
  46515. +
  46516. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  46517. +
  46518. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  46519. + observer, object, notification, callback, data);
  46520. +
  46521. + dump_manager();
  46522. + return 0;
  46523. +}
  46524. +
  46525. +int dwc_remove_observer(void *observer)
  46526. +{
  46527. + notifier_t *n;
  46528. +
  46529. + DWC_ASSERT(manager, "Notification manager not found");
  46530. +
  46531. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  46532. + observer_t *o;
  46533. + observer_t *o2;
  46534. +
  46535. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  46536. + if (o->observer == observer) {
  46537. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  46538. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  46539. + o->observer, n->object, o->notification);
  46540. + free_observer(n->mem_ctx, o);
  46541. + }
  46542. + }
  46543. + }
  46544. +
  46545. + dump_manager();
  46546. + return 0;
  46547. +}
  46548. +
  46549. +typedef struct callback_data {
  46550. + void *mem_ctx;
  46551. + dwc_notifier_callback_t cb;
  46552. + void *observer;
  46553. + void *data;
  46554. + void *object;
  46555. + char *notification;
  46556. + void *notification_data;
  46557. +} cb_data_t;
  46558. +
  46559. +static void cb_task(void *data)
  46560. +{
  46561. + cb_data_t *cb = (cb_data_t *)data;
  46562. +
  46563. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  46564. + dwc_free(cb->mem_ctx, cb);
  46565. +}
  46566. +
  46567. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  46568. +{
  46569. + observer_t *o;
  46570. +
  46571. + DWC_ASSERT(manager, "Notification manager not found");
  46572. +
  46573. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  46574. + int len = DWC_STRLEN(notification);
  46575. +
  46576. + if (DWC_STRLEN(o->notification) != len) {
  46577. + continue;
  46578. + }
  46579. +
  46580. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  46581. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  46582. +
  46583. + if (!cb_data) {
  46584. + DWC_ERROR("Failed to allocate callback data\n");
  46585. + return;
  46586. + }
  46587. +
  46588. + cb_data->mem_ctx = notifier->mem_ctx;
  46589. + cb_data->cb = o->callback;
  46590. + cb_data->observer = o->observer;
  46591. + cb_data->data = o->data;
  46592. + cb_data->object = notifier->object;
  46593. + cb_data->notification = notification;
  46594. + cb_data->notification_data = notification_data;
  46595. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  46596. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  46597. + "Notify callback from %p for Notification %s, to observer %p",
  46598. + cb_data->object, notification, cb_data->observer);
  46599. + }
  46600. + }
  46601. +}
  46602. +
  46603. +#endif /* DWC_NOTIFYLIB */
  46604. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_notifier.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h
  46605. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_notifier.h 1970-01-01 01:00:00.000000000 +0100
  46606. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h 2014-04-24 15:35:04.169565731 +0200
  46607. @@ -0,0 +1,122 @@
  46608. +
  46609. +#ifndef __DWC_NOTIFIER_H__
  46610. +#define __DWC_NOTIFIER_H__
  46611. +
  46612. +#ifdef __cplusplus
  46613. +extern "C" {
  46614. +#endif
  46615. +
  46616. +#include "dwc_os.h"
  46617. +
  46618. +/** @file
  46619. + *
  46620. + * A simple implementation of the Observer pattern. Any "module" can
  46621. + * register as an observer or notifier. The notion of "module" is abstract and
  46622. + * can mean anything used to identify either an observer or notifier. Usually
  46623. + * it will be a pointer to a data structure which contains some state, ie an
  46624. + * object.
  46625. + *
  46626. + * Before any notifiers can be added, the global notification manager must be
  46627. + * brought up with dwc_alloc_notification_manager().
  46628. + * dwc_free_notification_manager() will bring it down and free all resources.
  46629. + * These would typically be called upon module load and unload. The
  46630. + * notification manager is a single global instance that handles all registered
  46631. + * observable modules and observers so this should be done only once.
  46632. + *
  46633. + * A module can be observable by using Notifications to publicize some general
  46634. + * information about it's state or operation. It does not care who listens, or
  46635. + * even if anyone listens, or what they do with the information. The observable
  46636. + * modules do not need to know any information about it's observers or their
  46637. + * interface, or their state or data.
  46638. + *
  46639. + * Any module can register to emit Notifications. It should publish a list of
  46640. + * notifications that it can emit and their behavior, such as when they will get
  46641. + * triggered, and what information will be provided to the observer. Then it
  46642. + * should register itself as an observable module. See dwc_register_notifier().
  46643. + *
  46644. + * Any module can observe any observable, registered module, provided it has a
  46645. + * handle to the other module and knows what notifications to observe. See
  46646. + * dwc_add_observer().
  46647. + *
  46648. + * A function of type dwc_notifier_callback_t is called whenever a notification
  46649. + * is triggered with one or more observers observing it. This function is
  46650. + * called in it's own process so it may sleep or block if needed. It is
  46651. + * guaranteed to be called sometime after the notification has occurred and will
  46652. + * be called once per each time the notification is triggered. It will NOT be
  46653. + * called in the same process context used to trigger the notification.
  46654. + *
  46655. + * @section Limitiations
  46656. + *
  46657. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  46658. + * schedule too many processes too handle. Be aware of this limitation when
  46659. + * designing to use notifications, and only add notifications for appropriate
  46660. + * observable information.
  46661. + *
  46662. + * Also Notification callbacks are not synchronous. If you need to synchronize
  46663. + * the behavior between module/observer you must use other means. And perhaps
  46664. + * that will mean Notifications are not the proper solution.
  46665. + */
  46666. +
  46667. +struct dwc_notifier;
  46668. +typedef struct dwc_notifier dwc_notifier_t;
  46669. +
  46670. +/** The callback function must be of this type.
  46671. + *
  46672. + * @param object This is the object that is being observed.
  46673. + * @param notification This is the notification that was triggered.
  46674. + * @param observer This is the observer
  46675. + * @param notification_data This is notification-specific data that the notifier
  46676. + * has included in this notification. The value of this should be published in
  46677. + * the documentation of the observable module with the notifications.
  46678. + * @param user_data This is any custom data that the observer provided when
  46679. + * adding itself as an observer to the notification. */
  46680. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  46681. + void *notification_data, void *user_data);
  46682. +
  46683. +/** Brings up the notification manager. */
  46684. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  46685. +/** Brings down the notification manager. */
  46686. +extern void dwc_free_notification_manager(void);
  46687. +
  46688. +/** This function registers an observable module. A dwc_notifier_t object is
  46689. + * returned to the observable module. This is an opaque object that is used by
  46690. + * the observable module to trigger notifications. This object should only be
  46691. + * accessible to functions that are authorized to trigger notifications for this
  46692. + * module. Observers do not need this object. */
  46693. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  46694. +
  46695. +/** This function unregisters an observable module. All observers have to be
  46696. + * removed prior to unregistration. */
  46697. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  46698. +
  46699. +/** Add a module as an observer to the observable module. The observable module
  46700. + * needs to have previously registered with the notification manager.
  46701. + *
  46702. + * @param observer The observer module
  46703. + * @param object The module to observe
  46704. + * @param notification The notification to observe
  46705. + * @param callback The callback function to call
  46706. + * @param user_data Any additional user data to pass into the callback function */
  46707. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  46708. + dwc_notifier_callback_t callback, void *user_data);
  46709. +
  46710. +/** Removes the specified observer from all notifications that it is currently
  46711. + * observing. */
  46712. +extern int dwc_remove_observer(void *observer);
  46713. +
  46714. +/** This function triggers a Notification. It should be called by the
  46715. + * observable module, or any module or library which the observable module
  46716. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  46717. + *
  46718. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  46719. + * their own process context for each trigger. Callbacks can be blocking.
  46720. + * dwc_notify can be called from interrupt context if needed.
  46721. + *
  46722. + */
  46723. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  46724. +
  46725. +#ifdef __cplusplus
  46726. +}
  46727. +#endif
  46728. +
  46729. +#endif /* __DWC_NOTIFIER_H__ */
  46730. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_os.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_os.h
  46731. --- linux-3.10.37/drivers/usb/host/dwc_common_port/dwc_os.h 1970-01-01 01:00:00.000000000 +0100
  46732. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_os.h 2014-04-24 15:35:04.169565731 +0200
  46733. @@ -0,0 +1,1262 @@
  46734. +/* =========================================================================
  46735. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  46736. + * $Revision: #14 $
  46737. + * $Date: 2010/11/04 $
  46738. + * $Change: 1621695 $
  46739. + *
  46740. + * Synopsys Portability Library Software and documentation
  46741. + * (hereinafter, "Software") is an Unsupported proprietary work of
  46742. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  46743. + * between Synopsys and you.
  46744. + *
  46745. + * The Software IS NOT an item of Licensed Software or Licensed Product
  46746. + * under any End User Software License Agreement or Agreement for
  46747. + * Licensed Product with Synopsys or any supplement thereto. You are
  46748. + * permitted to use and redistribute this Software in source and binary
  46749. + * forms, with or without modification, provided that redistributions
  46750. + * of source code must retain this notice. You may not view, use,
  46751. + * disclose, copy or distribute this file or any information contained
  46752. + * herein except pursuant to this license grant from Synopsys. If you
  46753. + * do not agree with this notice, including the disclaimer below, then
  46754. + * you are not authorized to use the Software.
  46755. + *
  46756. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  46757. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46758. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  46759. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  46760. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  46761. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  46762. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  46763. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  46764. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  46765. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  46766. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46767. + * DAMAGE.
  46768. + * ========================================================================= */
  46769. +#ifndef _DWC_OS_H_
  46770. +#define _DWC_OS_H_
  46771. +
  46772. +#ifdef __cplusplus
  46773. +extern "C" {
  46774. +#endif
  46775. +
  46776. +/** @file
  46777. + *
  46778. + * DWC portability library, low level os-wrapper functions
  46779. + *
  46780. + */
  46781. +
  46782. +/* These basic types need to be defined by some OS header file or custom header
  46783. + * file for your specific target architecture.
  46784. + *
  46785. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  46786. + *
  46787. + * Any custom or alternate header file must be added and enabled here.
  46788. + */
  46789. +
  46790. +#ifdef DWC_LINUX
  46791. +# include <linux/types.h>
  46792. +# ifdef CONFIG_DEBUG_MUTEXES
  46793. +# include <linux/mutex.h>
  46794. +# endif
  46795. +# include <linux/errno.h>
  46796. +# include <stdarg.h>
  46797. +#endif
  46798. +
  46799. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46800. +# include <os_dep.h>
  46801. +#endif
  46802. +
  46803. +
  46804. +/** @name Primitive Types and Values */
  46805. +
  46806. +/** We define a boolean type for consistency. Can be either YES or NO */
  46807. +typedef uint8_t dwc_bool_t;
  46808. +#define YES 1
  46809. +#define NO 0
  46810. +
  46811. +#ifdef DWC_LINUX
  46812. +
  46813. +/** @name Error Codes */
  46814. +#define DWC_E_INVALID EINVAL
  46815. +#define DWC_E_NO_MEMORY ENOMEM
  46816. +#define DWC_E_NO_DEVICE ENODEV
  46817. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  46818. +#define DWC_E_TIMEOUT ETIMEDOUT
  46819. +#define DWC_E_BUSY EBUSY
  46820. +#define DWC_E_AGAIN EAGAIN
  46821. +#define DWC_E_RESTART ERESTART
  46822. +#define DWC_E_ABORT ECONNABORTED
  46823. +#define DWC_E_SHUTDOWN ESHUTDOWN
  46824. +#define DWC_E_NO_DATA ENODATA
  46825. +#define DWC_E_DISCONNECT ECONNRESET
  46826. +#define DWC_E_UNKNOWN EINVAL
  46827. +#define DWC_E_NO_STREAM_RES ENOSR
  46828. +#define DWC_E_COMMUNICATION ECOMM
  46829. +#define DWC_E_OVERFLOW EOVERFLOW
  46830. +#define DWC_E_PROTOCOL EPROTO
  46831. +#define DWC_E_IN_PROGRESS EINPROGRESS
  46832. +#define DWC_E_PIPE EPIPE
  46833. +#define DWC_E_IO EIO
  46834. +#define DWC_E_NO_SPACE ENOSPC
  46835. +
  46836. +#else
  46837. +
  46838. +/** @name Error Codes */
  46839. +#define DWC_E_INVALID 1001
  46840. +#define DWC_E_NO_MEMORY 1002
  46841. +#define DWC_E_NO_DEVICE 1003
  46842. +#define DWC_E_NOT_SUPPORTED 1004
  46843. +#define DWC_E_TIMEOUT 1005
  46844. +#define DWC_E_BUSY 1006
  46845. +#define DWC_E_AGAIN 1007
  46846. +#define DWC_E_RESTART 1008
  46847. +#define DWC_E_ABORT 1009
  46848. +#define DWC_E_SHUTDOWN 1010
  46849. +#define DWC_E_NO_DATA 1011
  46850. +#define DWC_E_DISCONNECT 2000
  46851. +#define DWC_E_UNKNOWN 3000
  46852. +#define DWC_E_NO_STREAM_RES 4001
  46853. +#define DWC_E_COMMUNICATION 4002
  46854. +#define DWC_E_OVERFLOW 4003
  46855. +#define DWC_E_PROTOCOL 4004
  46856. +#define DWC_E_IN_PROGRESS 4005
  46857. +#define DWC_E_PIPE 4006
  46858. +#define DWC_E_IO 4007
  46859. +#define DWC_E_NO_SPACE 4008
  46860. +
  46861. +#endif
  46862. +
  46863. +
  46864. +/** @name Tracing/Logging Functions
  46865. + *
  46866. + * These function provide the capability to add tracing, debugging, and error
  46867. + * messages, as well exceptions as assertions. The WUDEV uses these
  46868. + * extensively. These could be logged to the main console, the serial port, an
  46869. + * internal buffer, etc. These functions could also be no-op if they are too
  46870. + * expensive on your system. By default undefining the DEBUG macro already
  46871. + * no-ops some of these functions. */
  46872. +
  46873. +/** Returns non-zero if in interrupt context. */
  46874. +extern dwc_bool_t DWC_IN_IRQ(void);
  46875. +#define dwc_in_irq DWC_IN_IRQ
  46876. +
  46877. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  46878. +static inline char *dwc_irq(void) {
  46879. + return DWC_IN_IRQ() ? "IRQ" : "";
  46880. +}
  46881. +
  46882. +/** Returns non-zero if in bottom-half context. */
  46883. +extern dwc_bool_t DWC_IN_BH(void);
  46884. +#define dwc_in_bh DWC_IN_BH
  46885. +
  46886. +/** Returns "BH" if DWC_IN_BH is true. */
  46887. +static inline char *dwc_bh(void) {
  46888. + return DWC_IN_BH() ? "BH" : "";
  46889. +}
  46890. +
  46891. +/**
  46892. + * A vprintf() clone. Just call vprintf if you've got it.
  46893. + */
  46894. +extern void DWC_VPRINTF(char *format, va_list args);
  46895. +#define dwc_vprintf DWC_VPRINTF
  46896. +
  46897. +/**
  46898. + * A vsnprintf() clone. Just call vprintf if you've got it.
  46899. + */
  46900. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  46901. +#define dwc_vsnprintf DWC_VSNPRINTF
  46902. +
  46903. +/**
  46904. + * printf() clone. Just call printf if you've go it.
  46905. + */
  46906. +extern void DWC_PRINTF(char *format, ...)
  46907. +/* This provides compiler level static checking of the parameters if you're
  46908. + * using GCC. */
  46909. +#ifdef __GNUC__
  46910. + __attribute__ ((format(printf, 1, 2)));
  46911. +#else
  46912. + ;
  46913. +#endif
  46914. +#define dwc_printf DWC_PRINTF
  46915. +
  46916. +/**
  46917. + * sprintf() clone. Just call sprintf if you've got it.
  46918. + */
  46919. +extern int DWC_SPRINTF(char *string, char *format, ...)
  46920. +#ifdef __GNUC__
  46921. + __attribute__ ((format(printf, 2, 3)));
  46922. +#else
  46923. + ;
  46924. +#endif
  46925. +#define dwc_sprintf DWC_SPRINTF
  46926. +
  46927. +/**
  46928. + * snprintf() clone. Just call snprintf if you've got it.
  46929. + */
  46930. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  46931. +#ifdef __GNUC__
  46932. + __attribute__ ((format(printf, 3, 4)));
  46933. +#else
  46934. + ;
  46935. +#endif
  46936. +#define dwc_snprintf DWC_SNPRINTF
  46937. +
  46938. +/**
  46939. + * Prints a WARNING message. On systems that don't differentiate between
  46940. + * warnings and regular log messages, just print it. Indicates that something
  46941. + * may be wrong with the driver. Works like printf().
  46942. + *
  46943. + * Use the DWC_WARN macro to call this function.
  46944. + */
  46945. +extern void __DWC_WARN(char *format, ...)
  46946. +#ifdef __GNUC__
  46947. + __attribute__ ((format(printf, 1, 2)));
  46948. +#else
  46949. + ;
  46950. +#endif
  46951. +
  46952. +/**
  46953. + * Prints an error message. On systems that don't differentiate between errors
  46954. + * and regular log messages, just print it. Indicates that something went wrong
  46955. + * with the driver. Works like printf().
  46956. + *
  46957. + * Use the DWC_ERROR macro to call this function.
  46958. + */
  46959. +extern void __DWC_ERROR(char *format, ...)
  46960. +#ifdef __GNUC__
  46961. + __attribute__ ((format(printf, 1, 2)));
  46962. +#else
  46963. + ;
  46964. +#endif
  46965. +
  46966. +/**
  46967. + * Prints an exception error message and takes some user-defined action such as
  46968. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  46969. + * abnormally wrong with the driver such as programmer error, or other
  46970. + * exceptional condition. It should not be ignored so even on systems without
  46971. + * printing capability, some action should be taken to notify the developer of
  46972. + * it. Works like printf().
  46973. + */
  46974. +extern void DWC_EXCEPTION(char *format, ...)
  46975. +#ifdef __GNUC__
  46976. + __attribute__ ((format(printf, 1, 2)));
  46977. +#else
  46978. + ;
  46979. +#endif
  46980. +#define dwc_exception DWC_EXCEPTION
  46981. +
  46982. +#ifndef DWC_OTG_DEBUG_LEV
  46983. +#define DWC_OTG_DEBUG_LEV 0
  46984. +#endif
  46985. +
  46986. +#ifdef DEBUG
  46987. +/**
  46988. + * Prints out a debug message. Used for logging/trace messages.
  46989. + *
  46990. + * Use the DWC_DEBUG macro to call this function
  46991. + */
  46992. +extern void __DWC_DEBUG(char *format, ...)
  46993. +#ifdef __GNUC__
  46994. + __attribute__ ((format(printf, 1, 2)));
  46995. +#else
  46996. + ;
  46997. +#endif
  46998. +#else
  46999. +#define __DWC_DEBUG printk
  47000. +#endif
  47001. +
  47002. +/**
  47003. + * Prints out a Debug message.
  47004. + */
  47005. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  47006. + __func__, dwc_irq(), ## _args)
  47007. +#define dwc_debug DWC_DEBUG
  47008. +/**
  47009. + * Prints out a Debug message if enabled at compile time.
  47010. + */
  47011. +#if DWC_OTG_DEBUG_LEV > 0
  47012. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  47013. +#else
  47014. +#define DWC_DEBUGC(_format, _args...)
  47015. +#endif
  47016. +#define dwc_debugc DWC_DEBUGC
  47017. +/**
  47018. + * Prints out an informative message.
  47019. + */
  47020. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  47021. + dwc_irq(), ## _args)
  47022. +#define dwc_info DWC_INFO
  47023. +/**
  47024. + * Prints out an informative message if enabled at compile time.
  47025. + */
  47026. +#if DWC_OTG_DEBUG_LEV > 1
  47027. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  47028. +#else
  47029. +#define DWC_INFOC(_format, _args...)
  47030. +#endif
  47031. +#define dwc_infoc DWC_INFOC
  47032. +/**
  47033. + * Prints out a warning message.
  47034. + */
  47035. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  47036. + dwc_irq(), __func__, __LINE__, ## _args)
  47037. +#define dwc_warn DWC_WARN
  47038. +/**
  47039. + * Prints out an error message.
  47040. + */
  47041. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  47042. + dwc_irq(), __func__, __LINE__, ## _args)
  47043. +#define dwc_error DWC_ERROR
  47044. +
  47045. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  47046. + dwc_irq(), __func__, __LINE__, ## _args)
  47047. +#define dwc_proto_error DWC_PROTO_ERROR
  47048. +
  47049. +#ifdef DEBUG
  47050. +/** Prints out a exception error message if the _expr expression fails. Disabled
  47051. + * if DEBUG is not enabled. */
  47052. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  47053. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  47054. + __FILE__, __LINE__, ## _args); } \
  47055. + } while (0)
  47056. +#else
  47057. +#define DWC_ASSERT(_x...)
  47058. +#endif
  47059. +#define dwc_assert DWC_ASSERT
  47060. +
  47061. +
  47062. +/** @name Byte Ordering
  47063. + * The following functions are for conversions between processor's byte ordering
  47064. + * and specific ordering you want.
  47065. + */
  47066. +
  47067. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  47068. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  47069. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  47070. +
  47071. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  47072. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  47073. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  47074. +
  47075. +/** Converts 32 bit little endian data to CPU byte ordering. */
  47076. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  47077. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  47078. +
  47079. +/** Converts 32 bit big endian data to CPU byte ordering. */
  47080. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  47081. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  47082. +
  47083. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  47084. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  47085. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  47086. +
  47087. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  47088. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  47089. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  47090. +
  47091. +/** Converts 16 bit little endian data to CPU byte ordering. */
  47092. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  47093. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  47094. +
  47095. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  47096. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  47097. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  47098. +
  47099. +
  47100. +/** @name Register Read/Write
  47101. + *
  47102. + * The following six functions should be implemented to read/write registers of
  47103. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  47104. + * The reg value is a pointer to the register calculated from the void *base
  47105. + * variable passed into the driver when it is started. */
  47106. +
  47107. +#ifdef DWC_LINUX
  47108. +/* Linux doesn't need any extra parameters for register read/write, so we
  47109. + * just throw away the IO context parameter.
  47110. + */
  47111. +/** Reads the content of a 32-bit register. */
  47112. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  47113. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  47114. +
  47115. +/** Reads the content of a 64-bit register. */
  47116. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  47117. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  47118. +
  47119. +/** Writes to a 32-bit register. */
  47120. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  47121. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  47122. +
  47123. +/** Writes to a 64-bit register. */
  47124. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  47125. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  47126. +
  47127. +/**
  47128. + * Modify bit values in a register. Using the
  47129. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  47130. + */
  47131. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  47132. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  47133. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  47134. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  47135. +
  47136. +#endif /* DWC_LINUX */
  47137. +
  47138. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47139. +typedef struct dwc_ioctx {
  47140. + struct device *dev;
  47141. + bus_space_tag_t iot;
  47142. + bus_space_handle_t ioh;
  47143. +} dwc_ioctx_t;
  47144. +
  47145. +/** BSD needs two extra parameters for register read/write, so we pass
  47146. + * them in using the IO context parameter.
  47147. + */
  47148. +/** Reads the content of a 32-bit register. */
  47149. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  47150. +#define dwc_read_reg32 DWC_READ_REG32
  47151. +
  47152. +/** Reads the content of a 64-bit register. */
  47153. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  47154. +#define dwc_read_reg64 DWC_READ_REG64
  47155. +
  47156. +/** Writes to a 32-bit register. */
  47157. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  47158. +#define dwc_write_reg32 DWC_WRITE_REG32
  47159. +
  47160. +/** Writes to a 64-bit register. */
  47161. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  47162. +#define dwc_write_reg64 DWC_WRITE_REG64
  47163. +
  47164. +/**
  47165. + * Modify bit values in a register. Using the
  47166. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  47167. + */
  47168. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  47169. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  47170. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  47171. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  47172. +
  47173. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  47174. +
  47175. +/** @cond */
  47176. +
  47177. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  47178. + * register writes. */
  47179. +
  47180. +#ifdef DWC_LINUX
  47181. +
  47182. +# ifdef DWC_DEBUG_REGS
  47183. +
  47184. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47185. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  47186. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  47187. +} \
  47188. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  47189. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  47190. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  47191. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  47192. +}
  47193. +
  47194. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47195. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  47196. + return DWC_READ_REG32(&container->regs->_reg); \
  47197. +} \
  47198. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  47199. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  47200. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  47201. +}
  47202. +
  47203. +# else /* DWC_DEBUG_REGS */
  47204. +
  47205. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47206. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  47207. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  47208. +} \
  47209. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  47210. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  47211. +}
  47212. +
  47213. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47214. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  47215. + return DWC_READ_REG32(&container->regs->_reg); \
  47216. +} \
  47217. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  47218. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  47219. +}
  47220. +
  47221. +# endif /* DWC_DEBUG_REGS */
  47222. +
  47223. +#endif /* DWC_LINUX */
  47224. +
  47225. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47226. +
  47227. +# ifdef DWC_DEBUG_REGS
  47228. +
  47229. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47230. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  47231. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  47232. +} \
  47233. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  47234. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  47235. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  47236. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  47237. +}
  47238. +
  47239. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47240. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  47241. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  47242. +} \
  47243. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  47244. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  47245. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  47246. +}
  47247. +
  47248. +# else /* DWC_DEBUG_REGS */
  47249. +
  47250. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  47251. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  47252. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  47253. +} \
  47254. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  47255. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  47256. +}
  47257. +
  47258. +#define dwc_define_read_write_reg(_reg,_container_type) \
  47259. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  47260. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  47261. +} \
  47262. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  47263. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  47264. +}
  47265. +
  47266. +# endif /* DWC_DEBUG_REGS */
  47267. +
  47268. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  47269. +
  47270. +/** @endcond */
  47271. +
  47272. +
  47273. +#ifdef DWC_CRYPTOLIB
  47274. +/** @name Crypto Functions
  47275. + *
  47276. + * These are the low-level cryptographic functions used by the driver. */
  47277. +
  47278. +/** Perform AES CBC */
  47279. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  47280. +#define dwc_aes_cbc DWC_AES_CBC
  47281. +
  47282. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  47283. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  47284. +#define dwc_random_bytes DWC_RANDOM_BYTES
  47285. +
  47286. +/** Perform the SHA-256 hash function */
  47287. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  47288. +#define dwc_sha256 DWC_SHA256
  47289. +
  47290. +/** Calculated the HMAC-SHA256 */
  47291. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  47292. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  47293. +
  47294. +#endif /* DWC_CRYPTOLIB */
  47295. +
  47296. +
  47297. +/** @name Memory Allocation
  47298. + *
  47299. + * These function provide access to memory allocation. There are only 2 DMA
  47300. + * functions and 3 Regular memory functions that need to be implemented. None
  47301. + * of the memory debugging routines need to be implemented. The allocation
  47302. + * routines all ZERO the contents of the memory.
  47303. + *
  47304. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  47305. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  47306. + * keeps track of how much memory the driver is using at any given time. */
  47307. +
  47308. +#define DWC_PAGE_SIZE 4096
  47309. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  47310. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  47311. +
  47312. +#define DWC_INVALID_DMA_ADDR 0x0
  47313. +
  47314. +#ifdef DWC_LINUX
  47315. +/** Type for a DMA address */
  47316. +typedef dma_addr_t dwc_dma_t;
  47317. +#endif
  47318. +
  47319. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47320. +typedef bus_addr_t dwc_dma_t;
  47321. +#endif
  47322. +
  47323. +#ifdef DWC_FREEBSD
  47324. +typedef struct dwc_dmactx {
  47325. + struct device *dev;
  47326. + bus_dma_tag_t dma_tag;
  47327. + bus_dmamap_t dma_map;
  47328. + bus_addr_t dma_paddr;
  47329. + void *dma_vaddr;
  47330. +} dwc_dmactx_t;
  47331. +#endif
  47332. +
  47333. +#ifdef DWC_NETBSD
  47334. +typedef struct dwc_dmactx {
  47335. + struct device *dev;
  47336. + bus_dma_tag_t dma_tag;
  47337. + bus_dmamap_t dma_map;
  47338. + bus_dma_segment_t segs[1];
  47339. + int nsegs;
  47340. + bus_addr_t dma_paddr;
  47341. + void *dma_vaddr;
  47342. +} dwc_dmactx_t;
  47343. +#endif
  47344. +
  47345. +/* @todo these functions will be added in the future */
  47346. +#if 0
  47347. +/**
  47348. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  47349. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  47350. + * boundary requirements specified.
  47351. + *
  47352. + * @param[in] size Specifies the size of the buffers that will be allocated from
  47353. + * this pool.
  47354. + * @param[in] align Specifies the byte alignment requirements of the buffers
  47355. + * allocated from this pool. Must be a power of 2.
  47356. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  47357. + * this pool must not cross.
  47358. + *
  47359. + * @returns A pointer to an internal opaque structure which is not to be
  47360. + * accessed outside of these library functions. Use this handle to specify
  47361. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  47362. + * when you are done with it.
  47363. + */
  47364. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  47365. +
  47366. +/**
  47367. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  47368. + */
  47369. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  47370. +
  47371. +/**
  47372. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  47373. + */
  47374. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  47375. +
  47376. +/**
  47377. + * Free a previously allocated buffer from the DMA pool.
  47378. + */
  47379. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  47380. +#endif
  47381. +
  47382. +/** Allocates a DMA capable buffer and zeroes its contents. */
  47383. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  47384. +
  47385. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  47386. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  47387. +
  47388. +/** Frees a previously allocated buffer. */
  47389. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  47390. +
  47391. +/** Allocates a block of memory and zeroes its contents. */
  47392. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  47393. +
  47394. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  47395. + * which can be used inside interrupt context. The size should be sufficiently
  47396. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  47397. + * __DWC_ALLOC if it is atomic. */
  47398. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  47399. +
  47400. +/** Frees a previously allocated buffer. */
  47401. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  47402. +
  47403. +#ifndef DWC_DEBUG_MEMORY
  47404. +
  47405. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  47406. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  47407. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  47408. +
  47409. +# ifdef DWC_LINUX
  47410. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  47411. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  47412. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  47413. +# endif
  47414. +
  47415. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47416. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  47417. +#define DWC_DMA_FREE __DWC_DMA_FREE
  47418. +# endif
  47419. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  47420. +
  47421. +#else /* DWC_DEBUG_MEMORY */
  47422. +
  47423. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  47424. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  47425. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  47426. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  47427. + char const *func, int line);
  47428. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  47429. + char const *func, int line);
  47430. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  47431. + dwc_dma_t dma_addr, char const *func, int line);
  47432. +
  47433. +extern int dwc_memory_debug_start(void *mem_ctx);
  47434. +extern void dwc_memory_debug_stop(void);
  47435. +extern void dwc_memory_debug_report(void);
  47436. +
  47437. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  47438. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  47439. + __func__, __LINE__)
  47440. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  47441. +
  47442. +# ifdef DWC_LINUX
  47443. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  47444. + _dma_, __func__, __LINE__)
  47445. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  47446. + _dma_, __func__, __LINE__)
  47447. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  47448. + _virt_, _dma_, __func__, __LINE__)
  47449. +# endif
  47450. +
  47451. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47452. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  47453. + _dma_, __func__, __LINE__)
  47454. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  47455. + _virt_, _dma_, __func__, __LINE__)
  47456. +# endif
  47457. +
  47458. +#endif /* DWC_DEBUG_MEMORY */
  47459. +
  47460. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  47461. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  47462. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  47463. +
  47464. +#ifdef DWC_LINUX
  47465. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  47466. + * just throw away the DMA context parameter.
  47467. + */
  47468. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  47469. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  47470. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  47471. +#endif
  47472. +
  47473. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47474. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  47475. + * them in using the DMA context parameter.
  47476. + */
  47477. +#define dwc_dma_alloc DWC_DMA_ALLOC
  47478. +#define dwc_dma_free DWC_DMA_FREE
  47479. +#endif
  47480. +
  47481. +
  47482. +/** @name Memory and String Processing */
  47483. +
  47484. +/** memset() clone */
  47485. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  47486. +#define dwc_memset DWC_MEMSET
  47487. +
  47488. +/** memcpy() clone */
  47489. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  47490. +#define dwc_memcpy DWC_MEMCPY
  47491. +
  47492. +/** memmove() clone */
  47493. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  47494. +#define dwc_memmove DWC_MEMMOVE
  47495. +
  47496. +/** memcmp() clone */
  47497. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  47498. +#define dwc_memcmp DWC_MEMCMP
  47499. +
  47500. +/** strcmp() clone */
  47501. +extern int DWC_STRCMP(void *s1, void *s2);
  47502. +#define dwc_strcmp DWC_STRCMP
  47503. +
  47504. +/** strncmp() clone */
  47505. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  47506. +#define dwc_strncmp DWC_STRNCMP
  47507. +
  47508. +/** strlen() clone, for NULL terminated ASCII strings */
  47509. +extern int DWC_STRLEN(char const *str);
  47510. +#define dwc_strlen DWC_STRLEN
  47511. +
  47512. +/** strcpy() clone, for NULL terminated ASCII strings */
  47513. +extern char *DWC_STRCPY(char *to, const char *from);
  47514. +#define dwc_strcpy DWC_STRCPY
  47515. +
  47516. +/** strdup() clone. If you wish to use memory allocation debugging, this
  47517. + * implementation of strdup should use the DWC_* memory routines instead of
  47518. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  47519. + * will not be seen by the debugging routines. */
  47520. +extern char *DWC_STRDUP(char const *str);
  47521. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  47522. +
  47523. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  47524. + * converted from the string str in base 10 unless the string begins with a "0x"
  47525. + * in which case it is base 16. String must be a NULL terminated sequence of
  47526. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  47527. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  47528. + * the number and end with a NULL character. If any invalid characters are
  47529. + * encountered or it returns with a negative error code and the results of the
  47530. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  47531. + * undefined. An example implementation using atoi() can be referenced from the
  47532. + * Linux implementation. */
  47533. +extern int DWC_ATOI(const char *str, int32_t *value);
  47534. +#define dwc_atoi DWC_ATOI
  47535. +
  47536. +/** Same as above but for unsigned. */
  47537. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  47538. +#define dwc_atoui DWC_ATOUI
  47539. +
  47540. +#ifdef DWC_UTFLIB
  47541. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  47542. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  47543. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  47544. +#endif
  47545. +
  47546. +
  47547. +/** @name Wait queues
  47548. + *
  47549. + * Wait queues provide a means of synchronizing between threads or processes. A
  47550. + * process can block on a waitq if some condition is not true, waiting for it to
  47551. + * become true. When the waitq is triggered all waiting process will get
  47552. + * unblocked and the condition will be check again. Waitqs should be triggered
  47553. + * every time a condition can potentially change.*/
  47554. +struct dwc_waitq;
  47555. +
  47556. +/** Type for a waitq */
  47557. +typedef struct dwc_waitq dwc_waitq_t;
  47558. +
  47559. +/** The type of waitq condition callback function. This is called every time
  47560. + * condition is evaluated. */
  47561. +typedef int (*dwc_waitq_condition_t)(void *data);
  47562. +
  47563. +/** Allocate a waitq */
  47564. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  47565. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  47566. +
  47567. +/** Free a waitq */
  47568. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  47569. +#define dwc_waitq_free DWC_WAITQ_FREE
  47570. +
  47571. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  47572. + * condition again. The function returns when the condition becomes true. The return value
  47573. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  47574. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  47575. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  47576. +
  47577. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  47578. + * check the condition again. The function returns when the condition become
  47579. + * true or the timeout has passed. The return value is 0 on condition true or
  47580. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  47581. + * error. */
  47582. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  47583. + void *data, int32_t msecs);
  47584. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  47585. +
  47586. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  47587. + * has potentially changed. */
  47588. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  47589. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  47590. +
  47591. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  47592. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  47593. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  47594. +
  47595. +
  47596. +/** @name Threads
  47597. + *
  47598. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  47599. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  47600. + * returns the value from the thread.
  47601. + */
  47602. +
  47603. +struct dwc_thread;
  47604. +
  47605. +/** Type for a thread */
  47606. +typedef struct dwc_thread dwc_thread_t;
  47607. +
  47608. +/** The thread function */
  47609. +typedef int (*dwc_thread_function_t)(void *data);
  47610. +
  47611. +/** Create a thread and start it running the thread_function. Returns a handle
  47612. + * to the thread */
  47613. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  47614. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  47615. +
  47616. +/** Stops a thread. Return the value returned by the thread. Or will return
  47617. + * DWC_ABORT if the thread never started. */
  47618. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  47619. +#define dwc_thread_stop DWC_THREAD_STOP
  47620. +
  47621. +/** Signifies to the thread that it must stop. */
  47622. +#ifdef DWC_LINUX
  47623. +/* Linux doesn't need any parameters for kthread_should_stop() */
  47624. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  47625. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  47626. +
  47627. +/* No thread_exit function in Linux */
  47628. +#define dwc_thread_exit(_thrd_)
  47629. +#endif
  47630. +
  47631. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  47632. +/** BSD needs the thread pointer for kthread_suspend_check() */
  47633. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  47634. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  47635. +
  47636. +/** The thread must call this to exit. */
  47637. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  47638. +#define dwc_thread_exit DWC_THREAD_EXIT
  47639. +#endif
  47640. +
  47641. +
  47642. +/** @name Work queues
  47643. + *
  47644. + * Workqs are used to queue a callback function to be called at some later time,
  47645. + * in another thread. */
  47646. +struct dwc_workq;
  47647. +
  47648. +/** Type for a workq */
  47649. +typedef struct dwc_workq dwc_workq_t;
  47650. +
  47651. +/** The type of the callback function to be called. */
  47652. +typedef void (*dwc_work_callback_t)(void *data);
  47653. +
  47654. +/** Allocate a workq */
  47655. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  47656. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  47657. +
  47658. +/** Free a workq. All work must be completed before being freed. */
  47659. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  47660. +#define dwc_workq_free DWC_WORKQ_FREE
  47661. +
  47662. +/** Schedule a callback on the workq, passing in data. The function will be
  47663. + * scheduled at some later time. */
  47664. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  47665. + void *data, char *format, ...)
  47666. +#ifdef __GNUC__
  47667. + __attribute__ ((format(printf, 4, 5)));
  47668. +#else
  47669. + ;
  47670. +#endif
  47671. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  47672. +
  47673. +/** Schedule a callback on the workq, that will be called until at least
  47674. + * given number miliseconds have passed. */
  47675. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  47676. + void *data, uint32_t time, char *format, ...)
  47677. +#ifdef __GNUC__
  47678. + __attribute__ ((format(printf, 5, 6)));
  47679. +#else
  47680. + ;
  47681. +#endif
  47682. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  47683. +
  47684. +/** The number of processes in the workq */
  47685. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  47686. +#define dwc_workq_pending DWC_WORKQ_PENDING
  47687. +
  47688. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  47689. + * 0 on timeout. */
  47690. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  47691. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  47692. +
  47693. +
  47694. +/** @name Tasklets
  47695. + *
  47696. + */
  47697. +struct dwc_tasklet;
  47698. +
  47699. +/** Type for a tasklet */
  47700. +typedef struct dwc_tasklet dwc_tasklet_t;
  47701. +
  47702. +/** The type of the callback function to be called */
  47703. +typedef void (*dwc_tasklet_callback_t)(void *data);
  47704. +
  47705. +/** Allocates a tasklet */
  47706. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  47707. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  47708. +
  47709. +/** Frees a tasklet */
  47710. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  47711. +#define dwc_task_free DWC_TASK_FREE
  47712. +
  47713. +/** Schedules a tasklet to run */
  47714. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  47715. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  47716. +
  47717. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  47718. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  47719. +
  47720. +/** @name Timer
  47721. + *
  47722. + * Callbacks must be small and atomic.
  47723. + */
  47724. +struct dwc_timer;
  47725. +
  47726. +/** Type for a timer */
  47727. +typedef struct dwc_timer dwc_timer_t;
  47728. +
  47729. +/** The type of the callback function to be called */
  47730. +typedef void (*dwc_timer_callback_t)(void *data);
  47731. +
  47732. +/** Allocates a timer */
  47733. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  47734. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  47735. +
  47736. +/** Frees a timer */
  47737. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  47738. +#define dwc_timer_free DWC_TIMER_FREE
  47739. +
  47740. +/** Schedules the timer to run at time ms from now. And will repeat at every
  47741. + * repeat_interval msec therafter
  47742. + *
  47743. + * Modifies a timer that is still awaiting execution to a new expiration time.
  47744. + * The mod_time is added to the old time. */
  47745. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  47746. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  47747. +
  47748. +/** Disables the timer from execution. */
  47749. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  47750. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  47751. +
  47752. +
  47753. +/** @name Spinlocks
  47754. + *
  47755. + * These locks are used when the work between the lock/unlock is atomic and
  47756. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  47757. + * suitable to lock between interrupt/non-interrupt context. They also lock
  47758. + * between processes if you have multiple CPUs or Preemption. If you don't have
  47759. + * multiple CPUS or Preemption, then the you can simply implement the
  47760. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  47761. + * the work between the lock/unlock is atomic, the process context will never
  47762. + * change, and so you never have to lock between processes. */
  47763. +
  47764. +struct dwc_spinlock;
  47765. +
  47766. +/** Type for a spinlock */
  47767. +typedef struct dwc_spinlock dwc_spinlock_t;
  47768. +
  47769. +/** Type for the 'flags' argument to spinlock funtions */
  47770. +typedef unsigned long dwc_irqflags_t;
  47771. +
  47772. +/** Returns an initialized lock variable. This function should allocate and
  47773. + * initialize the OS-specific data structure used for locking. This data
  47774. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  47775. + * be freed by the DWC_FREE_LOCK when it is no longer used. */
  47776. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  47777. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  47778. +
  47779. +/** Frees an initialized lock variable. */
  47780. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  47781. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  47782. +
  47783. +/** Disables interrupts and blocks until it acquires the lock.
  47784. + *
  47785. + * @param lock Pointer to the spinlock.
  47786. + * @param flags Unsigned long for irq flags storage.
  47787. + */
  47788. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  47789. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  47790. +
  47791. +/** Re-enables the interrupt and releases the lock.
  47792. + *
  47793. + * @param lock Pointer to the spinlock.
  47794. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  47795. + * passed into DWC_LOCK.
  47796. + */
  47797. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  47798. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  47799. +
  47800. +/** Blocks until it acquires the lock.
  47801. + *
  47802. + * @param lock Pointer to the spinlock.
  47803. + */
  47804. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  47805. +#define dwc_spinlock DWC_SPINLOCK
  47806. +
  47807. +/** Releases the lock.
  47808. + *
  47809. + * @param lock Pointer to the spinlock.
  47810. + */
  47811. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  47812. +#define dwc_spinunlock DWC_SPINUNLOCK
  47813. +
  47814. +
  47815. +/** @name Mutexes
  47816. + *
  47817. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  47818. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  47819. + */
  47820. +
  47821. +struct dwc_mutex;
  47822. +
  47823. +/** Type for a mutex */
  47824. +typedef struct dwc_mutex dwc_mutex_t;
  47825. +
  47826. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  47827. + * the symbol to determine recursive locking. This makes it falsely think
  47828. + * recursive locking occurs. */
  47829. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  47830. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  47831. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  47832. + mutex_init((struct mutex *)__mutexp); \
  47833. +})
  47834. +#endif
  47835. +
  47836. +/** Allocate a mutex */
  47837. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  47838. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  47839. +
  47840. +/* For memory leak debugging when using Linux Mutex Debugging */
  47841. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  47842. +#define DWC_MUTEX_FREE(__mutexp) do { \
  47843. + mutex_destroy((struct mutex *)__mutexp); \
  47844. + DWC_FREE(__mutexp); \
  47845. +} while(0)
  47846. +#else
  47847. +/** Free a mutex */
  47848. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  47849. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  47850. +#endif
  47851. +
  47852. +/** Lock a mutex */
  47853. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  47854. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  47855. +
  47856. +/** Non-blocking lock returns 1 on successful lock. */
  47857. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  47858. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  47859. +
  47860. +/** Unlock a mutex */
  47861. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  47862. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  47863. +
  47864. +
  47865. +/** @name Time */
  47866. +
  47867. +/** Microsecond delay.
  47868. + *
  47869. + * @param usecs Microseconds to delay.
  47870. + */
  47871. +extern void DWC_UDELAY(uint32_t usecs);
  47872. +#define dwc_udelay DWC_UDELAY
  47873. +
  47874. +/** Millisecond delay.
  47875. + *
  47876. + * @param msecs Milliseconds to delay.
  47877. + */
  47878. +extern void DWC_MDELAY(uint32_t msecs);
  47879. +#define dwc_mdelay DWC_MDELAY
  47880. +
  47881. +/** Non-busy waiting.
  47882. + * Sleeps for specified number of milliseconds.
  47883. + *
  47884. + * @param msecs Milliseconds to sleep.
  47885. + */
  47886. +extern void DWC_MSLEEP(uint32_t msecs);
  47887. +#define dwc_msleep DWC_MSLEEP
  47888. +
  47889. +/**
  47890. + * Returns number of milliseconds since boot.
  47891. + */
  47892. +extern uint32_t DWC_TIME(void);
  47893. +#define dwc_time DWC_TIME
  47894. +
  47895. +
  47896. +
  47897. +
  47898. +/* @mainpage DWC Portability and Common Library
  47899. + *
  47900. + * This is the documentation for the DWC Portability and Common Library.
  47901. + *
  47902. + * @section intro Introduction
  47903. + *
  47904. + * The DWC Portability library consists of wrapper calls and data structures to
  47905. + * all low-level functions which are typically provided by the OS. The WUDEV
  47906. + * driver uses only these functions. In order to port the WUDEV driver, only
  47907. + * the functions in this library need to be re-implemented, with the same
  47908. + * behavior as documented here.
  47909. + *
  47910. + * The Common library consists of higher level functions, which rely only on
  47911. + * calling the functions from the DWC Portability library. These common
  47912. + * routines are shared across modules. Some of the common libraries need to be
  47913. + * used directly by the driver programmer when porting WUDEV. Such as the
  47914. + * parameter and notification libraries.
  47915. + *
  47916. + * @section low Portability Library OS Wrapper Functions
  47917. + *
  47918. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  47919. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  47920. + * these functions are included in the dwc_os.h file.
  47921. + *
  47922. + * There are many functions here covering a wide array of OS services. Please
  47923. + * see dwc_os.h for details, and implementation notes for each function.
  47924. + *
  47925. + * @section common Common Library Functions
  47926. + *
  47927. + * Any function starting with dwc and in all lowercase is a common library
  47928. + * routine. These functions have a portable implementation and do not need to
  47929. + * be reimplemented when porting. The common routines can be used by any
  47930. + * driver, and some must be used by the end user to control the drivers. For
  47931. + * example, you must use the Parameter common library in order to set the
  47932. + * parameters in the WUDEV module.
  47933. + *
  47934. + * The common libraries consist of the following:
  47935. + *
  47936. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  47937. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  47938. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  47939. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  47940. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  47941. + * - Modpow - Used internally only. See dwc_modpow.h
  47942. + * - DH - Used internally only. See dwc_dh.h
  47943. + * - Crypto - Used internally only. See dwc_crypto.h
  47944. + *
  47945. + *
  47946. + * @section prereq Prerequistes For dwc_os.h
  47947. + * @subsection types Data Types
  47948. + *
  47949. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  47950. + * compilation environment. These data types are:
  47951. + *
  47952. + * - uint8_t - unsigned 8-bit data type
  47953. + * - int8_t - signed 8-bit data type
  47954. + * - uint16_t - unsigned 16-bit data type
  47955. + * - int16_t - signed 16-bit data type
  47956. + * - uint32_t - unsigned 32-bit data type
  47957. + * - int32_t - signed 32-bit data type
  47958. + * - uint64_t - unsigned 64-bit data type
  47959. + * - int64_t - signed 64-bit data type
  47960. + *
  47961. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  47962. + * that is to modify the top of the file to include the appropriate header.
  47963. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  47964. + * defined, the correct header will be added. A standard header <stdint.h> is
  47965. + * also used for environments where standard C headers are available.
  47966. + *
  47967. + * @subsection stdarg Variable Arguments
  47968. + *
  47969. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  47970. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  47971. + * provided in your enviornment in order to use dwc_os.h with the debug and
  47972. + * tracing message functionality.
  47973. + *
  47974. + * @subsection thread Threading
  47975. + *
  47976. + * WUDEV Core must be run on an operating system that provides for multiple
  47977. + * threads/processes. Threading can be implemented in many ways, even in
  47978. + * embedded systems without an operating system. At the bare minimum, the
  47979. + * system should be able to start any number of processes at any time to handle
  47980. + * special work. It need not be a pre-emptive system. Process context can
  47981. + * change upon a call to a blocking function. The hardware interrupt context
  47982. + * that calls the module's ISR() function must be differentiable from process
  47983. + * context, even if your processes are impemented via a hardware interrupt.
  47984. + * Further locking mechanism between process must exist (or be implemented), and
  47985. + * process context must have a way to disable interrupts for a period of time to
  47986. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  47987. + * threading should be able to be implemented with the defined behavior.
  47988. + *
  47989. + */
  47990. +
  47991. +#ifdef __cplusplus
  47992. +}
  47993. +#endif
  47994. +
  47995. +#endif /* _DWC_OS_H_ */
  47996. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/Makefile linux-rpi/drivers/usb/host/dwc_common_port/Makefile
  47997. --- linux-3.10.37/drivers/usb/host/dwc_common_port/Makefile 1970-01-01 01:00:00.000000000 +0100
  47998. +++ linux-rpi/drivers/usb/host/dwc_common_port/Makefile 2014-04-24 15:35:04.169565731 +0200
  47999. @@ -0,0 +1,58 @@
  48000. +#
  48001. +# Makefile for DWC_common library
  48002. +#
  48003. +
  48004. +ifneq ($(KERNELRELEASE),)
  48005. +
  48006. +ccflags-y += -DDWC_LINUX
  48007. +#ccflags-y += -DDEBUG
  48008. +#ccflags-y += -DDWC_DEBUG_REGS
  48009. +#ccflags-y += -DDWC_DEBUG_MEMORY
  48010. +
  48011. +ccflags-y += -DDWC_LIBMODULE
  48012. +ccflags-y += -DDWC_CCLIB
  48013. +#ccflags-y += -DDWC_CRYPTOLIB
  48014. +ccflags-y += -DDWC_NOTIFYLIB
  48015. +ccflags-y += -DDWC_UTFLIB
  48016. +
  48017. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  48018. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  48019. + dwc_crypto.o dwc_notifier.o \
  48020. + dwc_common_linux.o dwc_mem.o
  48021. +
  48022. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  48023. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  48024. +
  48025. +ifneq ($(kernrel3),2.6.20)
  48026. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  48027. +ccflags-y += $(CPPFLAGS)
  48028. +endif
  48029. +
  48030. +else
  48031. +
  48032. +#ifeq ($(KDIR),)
  48033. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  48034. +#endif
  48035. +
  48036. +ifeq ($(ARCH),)
  48037. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  48038. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  48039. +endif
  48040. +
  48041. +ifeq ($(DOXYGEN),)
  48042. +DOXYGEN := doxygen
  48043. +endif
  48044. +
  48045. +default:
  48046. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  48047. +
  48048. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  48049. + $(DOXYGEN) doc/doxygen.cfg
  48050. +
  48051. +tags: $(wildcard *.[hc])
  48052. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  48053. +
  48054. +endif
  48055. +
  48056. +clean:
  48057. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  48058. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/Makefile.fbsd linux-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd
  48059. --- linux-3.10.37/drivers/usb/host/dwc_common_port/Makefile.fbsd 1970-01-01 01:00:00.000000000 +0100
  48060. +++ linux-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd 2014-04-24 15:35:04.169565731 +0200
  48061. @@ -0,0 +1,17 @@
  48062. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  48063. +CFLAGS += -DDWC_FREEBSD
  48064. +CFLAGS += -DDEBUG
  48065. +#CFLAGS += -DDWC_DEBUG_REGS
  48066. +#CFLAGS += -DDWC_DEBUG_MEMORY
  48067. +
  48068. +#CFLAGS += -DDWC_LIBMODULE
  48069. +#CFLAGS += -DDWC_CCLIB
  48070. +#CFLAGS += -DDWC_CRYPTOLIB
  48071. +#CFLAGS += -DDWC_NOTIFYLIB
  48072. +#CFLAGS += -DDWC_UTFLIB
  48073. +
  48074. +KMOD = dwc_common_port_lib
  48075. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  48076. + dwc_common_fbsd.c dwc_mem.c
  48077. +
  48078. +.include <bsd.kmod.mk>
  48079. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/Makefile.linux linux-rpi/drivers/usb/host/dwc_common_port/Makefile.linux
  48080. --- linux-3.10.37/drivers/usb/host/dwc_common_port/Makefile.linux 1970-01-01 01:00:00.000000000 +0100
  48081. +++ linux-rpi/drivers/usb/host/dwc_common_port/Makefile.linux 2014-04-24 15:35:04.169565731 +0200
  48082. @@ -0,0 +1,49 @@
  48083. +#
  48084. +# Makefile for DWC_common library
  48085. +#
  48086. +ifneq ($(KERNELRELEASE),)
  48087. +
  48088. +ccflags-y += -DDWC_LINUX
  48089. +#ccflags-y += -DDEBUG
  48090. +#ccflags-y += -DDWC_DEBUG_REGS
  48091. +#ccflags-y += -DDWC_DEBUG_MEMORY
  48092. +
  48093. +ccflags-y += -DDWC_LIBMODULE
  48094. +ccflags-y += -DDWC_CCLIB
  48095. +ccflags-y += -DDWC_CRYPTOLIB
  48096. +ccflags-y += -DDWC_NOTIFYLIB
  48097. +ccflags-y += -DDWC_UTFLIB
  48098. +
  48099. +obj-m := dwc_common_port_lib.o
  48100. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  48101. + dwc_crypto.o dwc_notifier.o \
  48102. + dwc_common_linux.o dwc_mem.o
  48103. +
  48104. +else
  48105. +
  48106. +ifeq ($(KDIR),)
  48107. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  48108. +endif
  48109. +
  48110. +ifeq ($(ARCH),)
  48111. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  48112. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  48113. +endif
  48114. +
  48115. +ifeq ($(DOXYGEN),)
  48116. +DOXYGEN := doxygen
  48117. +endif
  48118. +
  48119. +default:
  48120. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  48121. +
  48122. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  48123. + $(DOXYGEN) doc/doxygen.cfg
  48124. +
  48125. +tags: $(wildcard *.[hc])
  48126. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  48127. +
  48128. +endif
  48129. +
  48130. +clean:
  48131. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  48132. diff -Nur linux-3.10.37/drivers/usb/host/dwc_common_port/usb.h linux-rpi/drivers/usb/host/dwc_common_port/usb.h
  48133. --- linux-3.10.37/drivers/usb/host/dwc_common_port/usb.h 1970-01-01 01:00:00.000000000 +0100
  48134. +++ linux-rpi/drivers/usb/host/dwc_common_port/usb.h 2014-04-24 15:35:04.169565731 +0200
  48135. @@ -0,0 +1,946 @@
  48136. +/*
  48137. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  48138. + * All rights reserved.
  48139. + *
  48140. + * This code is derived from software contributed to The NetBSD Foundation
  48141. + * by Lennart Augustsson (lennart@augustsson.net) at
  48142. + * Carlstedt Research & Technology.
  48143. + *
  48144. + * Redistribution and use in source and binary forms, with or without
  48145. + * modification, are permitted provided that the following conditions
  48146. + * are met:
  48147. + * 1. Redistributions of source code must retain the above copyright
  48148. + * notice, this list of conditions and the following disclaimer.
  48149. + * 2. Redistributions in binary form must reproduce the above copyright
  48150. + * notice, this list of conditions and the following disclaimer in the
  48151. + * documentation and/or other materials provided with the distribution.
  48152. + * 3. All advertising materials mentioning features or use of this software
  48153. + * must display the following acknowledgement:
  48154. + * This product includes software developed by the NetBSD
  48155. + * Foundation, Inc. and its contributors.
  48156. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  48157. + * contributors may be used to endorse or promote products derived
  48158. + * from this software without specific prior written permission.
  48159. + *
  48160. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  48161. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  48162. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48163. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  48164. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  48165. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  48166. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48167. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  48168. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48169. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48170. + * POSSIBILITY OF SUCH DAMAGE.
  48171. + */
  48172. +
  48173. +/* Modified by Synopsys, Inc, 12/12/2007 */
  48174. +
  48175. +
  48176. +#ifndef _USB_H_
  48177. +#define _USB_H_
  48178. +
  48179. +#ifdef __cplusplus
  48180. +extern "C" {
  48181. +#endif
  48182. +
  48183. +/*
  48184. + * The USB records contain some unaligned little-endian word
  48185. + * components. The U[SG]ETW macros take care of both the alignment
  48186. + * and endian problem and should always be used to access non-byte
  48187. + * values.
  48188. + */
  48189. +typedef u_int8_t uByte;
  48190. +typedef u_int8_t uWord[2];
  48191. +typedef u_int8_t uDWord[4];
  48192. +
  48193. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  48194. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  48195. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  48196. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  48197. +
  48198. +#if 1
  48199. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  48200. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  48201. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  48202. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  48203. + (w)[1] = (u_int8_t)((v) >> 8), \
  48204. + (w)[2] = (u_int8_t)((v) >> 16), \
  48205. + (w)[3] = (u_int8_t)((v) >> 24))
  48206. +#else
  48207. +/*
  48208. + * On little-endian machines that can handle unanliged accesses
  48209. + * (e.g. i386) these macros can be replaced by the following.
  48210. + */
  48211. +#define UGETW(w) (*(u_int16_t *)(w))
  48212. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  48213. +#define UGETDW(w) (*(u_int32_t *)(w))
  48214. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  48215. +#endif
  48216. +
  48217. +/*
  48218. + * Macros for accessing UAS IU fields, which are big-endian
  48219. + */
  48220. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  48221. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  48222. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  48223. + ((x) >> 8) & 0xff, (x) & 0xff }
  48224. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  48225. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  48226. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  48227. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  48228. + (w)[1] = (u_int8_t)((v) >> 16), \
  48229. + (w)[2] = (u_int8_t)((v) >> 8), \
  48230. + (w)[3] = (u_int8_t)(v))
  48231. +
  48232. +#define UPACKED __attribute__((__packed__))
  48233. +
  48234. +typedef struct {
  48235. + uByte bmRequestType;
  48236. + uByte bRequest;
  48237. + uWord wValue;
  48238. + uWord wIndex;
  48239. + uWord wLength;
  48240. +} UPACKED usb_device_request_t;
  48241. +
  48242. +#define UT_GET_DIR(a) ((a) & 0x80)
  48243. +#define UT_WRITE 0x00
  48244. +#define UT_READ 0x80
  48245. +
  48246. +#define UT_GET_TYPE(a) ((a) & 0x60)
  48247. +#define UT_STANDARD 0x00
  48248. +#define UT_CLASS 0x20
  48249. +#define UT_VENDOR 0x40
  48250. +
  48251. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  48252. +#define UT_DEVICE 0x00
  48253. +#define UT_INTERFACE 0x01
  48254. +#define UT_ENDPOINT 0x02
  48255. +#define UT_OTHER 0x03
  48256. +
  48257. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  48258. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  48259. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  48260. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  48261. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  48262. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  48263. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  48264. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  48265. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  48266. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  48267. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  48268. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  48269. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  48270. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  48271. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  48272. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  48273. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  48274. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  48275. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  48276. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  48277. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  48278. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  48279. +
  48280. +/* Requests */
  48281. +#define UR_GET_STATUS 0x00
  48282. +#define USTAT_STANDARD_STATUS 0x00
  48283. +#define WUSTAT_WUSB_FEATURE 0x01
  48284. +#define WUSTAT_CHANNEL_INFO 0x02
  48285. +#define WUSTAT_RECEIVED_DATA 0x03
  48286. +#define WUSTAT_MAS_AVAILABILITY 0x04
  48287. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  48288. +#define UR_CLEAR_FEATURE 0x01
  48289. +#define UR_SET_FEATURE 0x03
  48290. +#define UR_SET_AND_TEST_FEATURE 0x0c
  48291. +#define UR_SET_ADDRESS 0x05
  48292. +#define UR_GET_DESCRIPTOR 0x06
  48293. +#define UDESC_DEVICE 0x01
  48294. +#define UDESC_CONFIG 0x02
  48295. +#define UDESC_STRING 0x03
  48296. +#define UDESC_INTERFACE 0x04
  48297. +#define UDESC_ENDPOINT 0x05
  48298. +#define UDESC_SS_USB_COMPANION 0x30
  48299. +#define UDESC_DEVICE_QUALIFIER 0x06
  48300. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  48301. +#define UDESC_INTERFACE_POWER 0x08
  48302. +#define UDESC_OTG 0x09
  48303. +#define WUDESC_SECURITY 0x0c
  48304. +#define WUDESC_KEY 0x0d
  48305. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  48306. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  48307. +#define WUD_KEY_TYPE_ASSOC 0x01
  48308. +#define WUD_KEY_TYPE_GTK 0x02
  48309. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  48310. +#define WUD_KEY_ORIGIN_HOST 0x00
  48311. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  48312. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  48313. +#define WUDESC_BOS 0x0f
  48314. +#define WUDESC_DEVICE_CAPABILITY 0x10
  48315. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  48316. +#define UDESC_BOS 0x0f
  48317. +#define UDESC_DEVICE_CAPABILITY 0x10
  48318. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  48319. +#define UDESC_CS_CONFIG 0x22
  48320. +#define UDESC_CS_STRING 0x23
  48321. +#define UDESC_CS_INTERFACE 0x24
  48322. +#define UDESC_CS_ENDPOINT 0x25
  48323. +#define UDESC_HUB 0x29
  48324. +#define UR_SET_DESCRIPTOR 0x07
  48325. +#define UR_GET_CONFIG 0x08
  48326. +#define UR_SET_CONFIG 0x09
  48327. +#define UR_GET_INTERFACE 0x0a
  48328. +#define UR_SET_INTERFACE 0x0b
  48329. +#define UR_SYNCH_FRAME 0x0c
  48330. +#define WUR_SET_ENCRYPTION 0x0d
  48331. +#define WUR_GET_ENCRYPTION 0x0e
  48332. +#define WUR_SET_HANDSHAKE 0x0f
  48333. +#define WUR_GET_HANDSHAKE 0x10
  48334. +#define WUR_SET_CONNECTION 0x11
  48335. +#define WUR_SET_SECURITY_DATA 0x12
  48336. +#define WUR_GET_SECURITY_DATA 0x13
  48337. +#define WUR_SET_WUSB_DATA 0x14
  48338. +#define WUDATA_DRPIE_INFO 0x01
  48339. +#define WUDATA_TRANSMIT_DATA 0x02
  48340. +#define WUDATA_TRANSMIT_PARAMS 0x03
  48341. +#define WUDATA_RECEIVE_PARAMS 0x04
  48342. +#define WUDATA_TRANSMIT_POWER 0x05
  48343. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  48344. +#define WUR_LOOPBACK_DATA_READ 0x16
  48345. +#define WUR_SET_INTERFACE_DS 0x17
  48346. +
  48347. +/* Feature numbers */
  48348. +#define UF_ENDPOINT_HALT 0
  48349. +#define UF_DEVICE_REMOTE_WAKEUP 1
  48350. +#define UF_TEST_MODE 2
  48351. +#define UF_DEVICE_B_HNP_ENABLE 3
  48352. +#define UF_DEVICE_A_HNP_SUPPORT 4
  48353. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  48354. +#define WUF_WUSB 3
  48355. +#define WUF_TX_DRPIE 0x0
  48356. +#define WUF_DEV_XMIT_PACKET 0x1
  48357. +#define WUF_COUNT_PACKETS 0x2
  48358. +#define WUF_CAPTURE_PACKETS 0x3
  48359. +#define UF_FUNCTION_SUSPEND 0
  48360. +#define UF_U1_ENABLE 48
  48361. +#define UF_U2_ENABLE 49
  48362. +#define UF_LTM_ENABLE 50
  48363. +
  48364. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  48365. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  48366. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  48367. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  48368. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  48369. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  48370. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  48371. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  48372. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  48373. +
  48374. +#ifdef _MSC_VER
  48375. +#include <pshpack1.h>
  48376. +#endif
  48377. +
  48378. +typedef struct {
  48379. + uByte bLength;
  48380. + uByte bDescriptorType;
  48381. + uByte bDescriptorSubtype;
  48382. +} UPACKED usb_descriptor_t;
  48383. +
  48384. +typedef struct {
  48385. + uByte bLength;
  48386. + uByte bDescriptorType;
  48387. +} UPACKED usb_descriptor_header_t;
  48388. +
  48389. +typedef struct {
  48390. + uByte bLength;
  48391. + uByte bDescriptorType;
  48392. + uWord bcdUSB;
  48393. +#define UD_USB_2_0 0x0200
  48394. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  48395. + uByte bDeviceClass;
  48396. + uByte bDeviceSubClass;
  48397. + uByte bDeviceProtocol;
  48398. + uByte bMaxPacketSize;
  48399. + /* The fields below are not part of the initial descriptor. */
  48400. + uWord idVendor;
  48401. + uWord idProduct;
  48402. + uWord bcdDevice;
  48403. + uByte iManufacturer;
  48404. + uByte iProduct;
  48405. + uByte iSerialNumber;
  48406. + uByte bNumConfigurations;
  48407. +} UPACKED usb_device_descriptor_t;
  48408. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  48409. +
  48410. +typedef struct {
  48411. + uByte bLength;
  48412. + uByte bDescriptorType;
  48413. + uWord wTotalLength;
  48414. + uByte bNumInterface;
  48415. + uByte bConfigurationValue;
  48416. + uByte iConfiguration;
  48417. +#define UC_ATT_ONE (1 << 7) /* must be set */
  48418. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  48419. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  48420. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  48421. + uByte bmAttributes;
  48422. +#define UC_BUS_POWERED 0x80
  48423. +#define UC_SELF_POWERED 0x40
  48424. +#define UC_REMOTE_WAKEUP 0x20
  48425. + uByte bMaxPower; /* max current in 2 mA units */
  48426. +#define UC_POWER_FACTOR 2
  48427. +} UPACKED usb_config_descriptor_t;
  48428. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  48429. +
  48430. +typedef struct {
  48431. + uByte bLength;
  48432. + uByte bDescriptorType;
  48433. + uByte bInterfaceNumber;
  48434. + uByte bAlternateSetting;
  48435. + uByte bNumEndpoints;
  48436. + uByte bInterfaceClass;
  48437. + uByte bInterfaceSubClass;
  48438. + uByte bInterfaceProtocol;
  48439. + uByte iInterface;
  48440. +} UPACKED usb_interface_descriptor_t;
  48441. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  48442. +
  48443. +typedef struct {
  48444. + uByte bLength;
  48445. + uByte bDescriptorType;
  48446. + uByte bEndpointAddress;
  48447. +#define UE_GET_DIR(a) ((a) & 0x80)
  48448. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  48449. +#define UE_DIR_IN 0x80
  48450. +#define UE_DIR_OUT 0x00
  48451. +#define UE_ADDR 0x0f
  48452. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  48453. + uByte bmAttributes;
  48454. +#define UE_XFERTYPE 0x03
  48455. +#define UE_CONTROL 0x00
  48456. +#define UE_ISOCHRONOUS 0x01
  48457. +#define UE_BULK 0x02
  48458. +#define UE_INTERRUPT 0x03
  48459. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  48460. +#define UE_ISO_TYPE 0x0c
  48461. +#define UE_ISO_ASYNC 0x04
  48462. +#define UE_ISO_ADAPT 0x08
  48463. +#define UE_ISO_SYNC 0x0c
  48464. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  48465. + uWord wMaxPacketSize;
  48466. + uByte bInterval;
  48467. +} UPACKED usb_endpoint_descriptor_t;
  48468. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  48469. +
  48470. +typedef struct ss_endpoint_companion_descriptor {
  48471. + uByte bLength;
  48472. + uByte bDescriptorType;
  48473. + uByte bMaxBurst;
  48474. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  48475. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  48476. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  48477. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  48478. + uByte bmAttributes;
  48479. + uWord wBytesPerInterval;
  48480. +} UPACKED ss_endpoint_companion_descriptor_t;
  48481. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  48482. +
  48483. +typedef struct {
  48484. + uByte bLength;
  48485. + uByte bDescriptorType;
  48486. + uWord bString[127];
  48487. +} UPACKED usb_string_descriptor_t;
  48488. +#define USB_MAX_STRING_LEN 128
  48489. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  48490. +
  48491. +/* Hub specific request */
  48492. +#define UR_GET_BUS_STATE 0x02
  48493. +#define UR_CLEAR_TT_BUFFER 0x08
  48494. +#define UR_RESET_TT 0x09
  48495. +#define UR_GET_TT_STATE 0x0a
  48496. +#define UR_STOP_TT 0x0b
  48497. +
  48498. +/* Hub features */
  48499. +#define UHF_C_HUB_LOCAL_POWER 0
  48500. +#define UHF_C_HUB_OVER_CURRENT 1
  48501. +#define UHF_PORT_CONNECTION 0
  48502. +#define UHF_PORT_ENABLE 1
  48503. +#define UHF_PORT_SUSPEND 2
  48504. +#define UHF_PORT_OVER_CURRENT 3
  48505. +#define UHF_PORT_RESET 4
  48506. +#define UHF_PORT_L1 5
  48507. +#define UHF_PORT_POWER 8
  48508. +#define UHF_PORT_LOW_SPEED 9
  48509. +#define UHF_PORT_HIGH_SPEED 10
  48510. +#define UHF_C_PORT_CONNECTION 16
  48511. +#define UHF_C_PORT_ENABLE 17
  48512. +#define UHF_C_PORT_SUSPEND 18
  48513. +#define UHF_C_PORT_OVER_CURRENT 19
  48514. +#define UHF_C_PORT_RESET 20
  48515. +#define UHF_C_PORT_L1 23
  48516. +#define UHF_PORT_TEST 21
  48517. +#define UHF_PORT_INDICATOR 22
  48518. +
  48519. +typedef struct {
  48520. + uByte bDescLength;
  48521. + uByte bDescriptorType;
  48522. + uByte bNbrPorts;
  48523. + uWord wHubCharacteristics;
  48524. +#define UHD_PWR 0x0003
  48525. +#define UHD_PWR_GANGED 0x0000
  48526. +#define UHD_PWR_INDIVIDUAL 0x0001
  48527. +#define UHD_PWR_NO_SWITCH 0x0002
  48528. +#define UHD_COMPOUND 0x0004
  48529. +#define UHD_OC 0x0018
  48530. +#define UHD_OC_GLOBAL 0x0000
  48531. +#define UHD_OC_INDIVIDUAL 0x0008
  48532. +#define UHD_OC_NONE 0x0010
  48533. +#define UHD_TT_THINK 0x0060
  48534. +#define UHD_TT_THINK_8 0x0000
  48535. +#define UHD_TT_THINK_16 0x0020
  48536. +#define UHD_TT_THINK_24 0x0040
  48537. +#define UHD_TT_THINK_32 0x0060
  48538. +#define UHD_PORT_IND 0x0080
  48539. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  48540. +#define UHD_PWRON_FACTOR 2
  48541. + uByte bHubContrCurrent;
  48542. + uByte DeviceRemovable[32]; /* max 255 ports */
  48543. +#define UHD_NOT_REMOV(desc, i) \
  48544. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  48545. + /* deprecated */ uByte PortPowerCtrlMask[1];
  48546. +} UPACKED usb_hub_descriptor_t;
  48547. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  48548. +
  48549. +typedef struct {
  48550. + uByte bLength;
  48551. + uByte bDescriptorType;
  48552. + uWord bcdUSB;
  48553. + uByte bDeviceClass;
  48554. + uByte bDeviceSubClass;
  48555. + uByte bDeviceProtocol;
  48556. + uByte bMaxPacketSize0;
  48557. + uByte bNumConfigurations;
  48558. + uByte bReserved;
  48559. +} UPACKED usb_device_qualifier_t;
  48560. +#define USB_DEVICE_QUALIFIER_SIZE 10
  48561. +
  48562. +typedef struct {
  48563. + uByte bLength;
  48564. + uByte bDescriptorType;
  48565. + uByte bmAttributes;
  48566. +#define UOTG_SRP 0x01
  48567. +#define UOTG_HNP 0x02
  48568. +} UPACKED usb_otg_descriptor_t;
  48569. +
  48570. +/* OTG feature selectors */
  48571. +#define UOTG_B_HNP_ENABLE 3
  48572. +#define UOTG_A_HNP_SUPPORT 4
  48573. +#define UOTG_A_ALT_HNP_SUPPORT 5
  48574. +
  48575. +typedef struct {
  48576. + uWord wStatus;
  48577. +/* Device status flags */
  48578. +#define UDS_SELF_POWERED 0x0001
  48579. +#define UDS_REMOTE_WAKEUP 0x0002
  48580. +/* Endpoint status flags */
  48581. +#define UES_HALT 0x0001
  48582. +} UPACKED usb_status_t;
  48583. +
  48584. +typedef struct {
  48585. + uWord wHubStatus;
  48586. +#define UHS_LOCAL_POWER 0x0001
  48587. +#define UHS_OVER_CURRENT 0x0002
  48588. + uWord wHubChange;
  48589. +} UPACKED usb_hub_status_t;
  48590. +
  48591. +typedef struct {
  48592. + uWord wPortStatus;
  48593. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  48594. +#define UPS_PORT_ENABLED 0x0002
  48595. +#define UPS_SUSPEND 0x0004
  48596. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  48597. +#define UPS_RESET 0x0010
  48598. +#define UPS_PORT_POWER 0x0100
  48599. +#define UPS_LOW_SPEED 0x0200
  48600. +#define UPS_HIGH_SPEED 0x0400
  48601. +#define UPS_PORT_TEST 0x0800
  48602. +#define UPS_PORT_INDICATOR 0x1000
  48603. + uWord wPortChange;
  48604. +#define UPS_C_CONNECT_STATUS 0x0001
  48605. +#define UPS_C_PORT_ENABLED 0x0002
  48606. +#define UPS_C_SUSPEND 0x0004
  48607. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  48608. +#define UPS_C_PORT_RESET 0x0010
  48609. +} UPACKED usb_port_status_t;
  48610. +
  48611. +#ifdef _MSC_VER
  48612. +#include <poppack.h>
  48613. +#endif
  48614. +
  48615. +/* Device class codes */
  48616. +#define UDCLASS_IN_INTERFACE 0x00
  48617. +#define UDCLASS_COMM 0x02
  48618. +#define UDCLASS_HUB 0x09
  48619. +#define UDSUBCLASS_HUB 0x00
  48620. +#define UDPROTO_FSHUB 0x00
  48621. +#define UDPROTO_HSHUBSTT 0x01
  48622. +#define UDPROTO_HSHUBMTT 0x02
  48623. +#define UDCLASS_DIAGNOSTIC 0xdc
  48624. +#define UDCLASS_WIRELESS 0xe0
  48625. +#define UDSUBCLASS_RF 0x01
  48626. +#define UDPROTO_BLUETOOTH 0x01
  48627. +#define UDCLASS_VENDOR 0xff
  48628. +
  48629. +/* Interface class codes */
  48630. +#define UICLASS_UNSPEC 0x00
  48631. +
  48632. +#define UICLASS_AUDIO 0x01
  48633. +#define UISUBCLASS_AUDIOCONTROL 1
  48634. +#define UISUBCLASS_AUDIOSTREAM 2
  48635. +#define UISUBCLASS_MIDISTREAM 3
  48636. +
  48637. +#define UICLASS_CDC 0x02 /* communication */
  48638. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  48639. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  48640. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  48641. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  48642. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  48643. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  48644. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  48645. +#define UIPROTO_CDC_AT 1
  48646. +
  48647. +#define UICLASS_HID 0x03
  48648. +#define UISUBCLASS_BOOT 1
  48649. +#define UIPROTO_BOOT_KEYBOARD 1
  48650. +
  48651. +#define UICLASS_PHYSICAL 0x05
  48652. +
  48653. +#define UICLASS_IMAGE 0x06
  48654. +
  48655. +#define UICLASS_PRINTER 0x07
  48656. +#define UISUBCLASS_PRINTER 1
  48657. +#define UIPROTO_PRINTER_UNI 1
  48658. +#define UIPROTO_PRINTER_BI 2
  48659. +#define UIPROTO_PRINTER_1284 3
  48660. +
  48661. +#define UICLASS_MASS 0x08
  48662. +#define UISUBCLASS_RBC 1
  48663. +#define UISUBCLASS_SFF8020I 2
  48664. +#define UISUBCLASS_QIC157 3
  48665. +#define UISUBCLASS_UFI 4
  48666. +#define UISUBCLASS_SFF8070I 5
  48667. +#define UISUBCLASS_SCSI 6
  48668. +#define UIPROTO_MASS_CBI_I 0
  48669. +#define UIPROTO_MASS_CBI 1
  48670. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  48671. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  48672. +
  48673. +#define UICLASS_HUB 0x09
  48674. +#define UISUBCLASS_HUB 0
  48675. +#define UIPROTO_FSHUB 0
  48676. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  48677. +#define UIPROTO_HSHUBMTT 1
  48678. +
  48679. +#define UICLASS_CDC_DATA 0x0a
  48680. +#define UISUBCLASS_DATA 0
  48681. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  48682. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  48683. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  48684. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  48685. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  48686. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  48687. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  48688. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  48689. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  48690. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  48691. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  48692. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  48693. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  48694. +
  48695. +#define UICLASS_SMARTCARD 0x0b
  48696. +
  48697. +/*#define UICLASS_FIRM_UPD 0x0c*/
  48698. +
  48699. +#define UICLASS_SECURITY 0x0d
  48700. +
  48701. +#define UICLASS_DIAGNOSTIC 0xdc
  48702. +
  48703. +#define UICLASS_WIRELESS 0xe0
  48704. +#define UISUBCLASS_RF 0x01
  48705. +#define UIPROTO_BLUETOOTH 0x01
  48706. +
  48707. +#define UICLASS_APPL_SPEC 0xfe
  48708. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  48709. +#define UISUBCLASS_IRDA 2
  48710. +#define UIPROTO_IRDA 0
  48711. +
  48712. +#define UICLASS_VENDOR 0xff
  48713. +
  48714. +#define USB_HUB_MAX_DEPTH 5
  48715. +
  48716. +/*
  48717. + * Minimum time a device needs to be powered down to go through
  48718. + * a power cycle. XXX Are these time in the spec?
  48719. + */
  48720. +#define USB_POWER_DOWN_TIME 200 /* ms */
  48721. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  48722. +
  48723. +#if 0
  48724. +/* These are the values from the spec. */
  48725. +#define USB_PORT_RESET_DELAY 10 /* ms */
  48726. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  48727. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  48728. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  48729. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  48730. +#define USB_RESUME_DELAY (20*5) /* ms */
  48731. +#define USB_RESUME_WAIT 10 /* ms */
  48732. +#define USB_RESUME_RECOVERY 10 /* ms */
  48733. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  48734. +#else
  48735. +/* Allow for marginal (i.e. non-conforming) devices. */
  48736. +#define USB_PORT_RESET_DELAY 50 /* ms */
  48737. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  48738. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  48739. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  48740. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  48741. +#define USB_RESUME_DELAY (50*5) /* ms */
  48742. +#define USB_RESUME_WAIT 50 /* ms */
  48743. +#define USB_RESUME_RECOVERY 50 /* ms */
  48744. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  48745. +#endif
  48746. +
  48747. +#define USB_MIN_POWER 100 /* mA */
  48748. +#define USB_MAX_POWER 500 /* mA */
  48749. +
  48750. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  48751. +
  48752. +#define USB_UNCONFIG_NO 0
  48753. +#define USB_UNCONFIG_INDEX (-1)
  48754. +
  48755. +/*** ioctl() related stuff ***/
  48756. +
  48757. +struct usb_ctl_request {
  48758. + int ucr_addr;
  48759. + usb_device_request_t ucr_request;
  48760. + void *ucr_data;
  48761. + int ucr_flags;
  48762. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  48763. + int ucr_actlen; /* actual length transferred */
  48764. +};
  48765. +
  48766. +struct usb_alt_interface {
  48767. + int uai_config_index;
  48768. + int uai_interface_index;
  48769. + int uai_alt_no;
  48770. +};
  48771. +
  48772. +#define USB_CURRENT_CONFIG_INDEX (-1)
  48773. +#define USB_CURRENT_ALT_INDEX (-1)
  48774. +
  48775. +struct usb_config_desc {
  48776. + int ucd_config_index;
  48777. + usb_config_descriptor_t ucd_desc;
  48778. +};
  48779. +
  48780. +struct usb_interface_desc {
  48781. + int uid_config_index;
  48782. + int uid_interface_index;
  48783. + int uid_alt_index;
  48784. + usb_interface_descriptor_t uid_desc;
  48785. +};
  48786. +
  48787. +struct usb_endpoint_desc {
  48788. + int ued_config_index;
  48789. + int ued_interface_index;
  48790. + int ued_alt_index;
  48791. + int ued_endpoint_index;
  48792. + usb_endpoint_descriptor_t ued_desc;
  48793. +};
  48794. +
  48795. +struct usb_full_desc {
  48796. + int ufd_config_index;
  48797. + u_int ufd_size;
  48798. + u_char *ufd_data;
  48799. +};
  48800. +
  48801. +struct usb_string_desc {
  48802. + int usd_string_index;
  48803. + int usd_language_id;
  48804. + usb_string_descriptor_t usd_desc;
  48805. +};
  48806. +
  48807. +struct usb_ctl_report_desc {
  48808. + int ucrd_size;
  48809. + u_char ucrd_data[1024]; /* filled data size will vary */
  48810. +};
  48811. +
  48812. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  48813. +
  48814. +#define USB_MAX_DEVNAMES 4
  48815. +#define USB_MAX_DEVNAMELEN 16
  48816. +struct usb_device_info {
  48817. + u_int8_t udi_bus;
  48818. + u_int8_t udi_addr; /* device address */
  48819. + usb_event_cookie_t udi_cookie;
  48820. + char udi_product[USB_MAX_STRING_LEN];
  48821. + char udi_vendor[USB_MAX_STRING_LEN];
  48822. + char udi_release[8];
  48823. + u_int16_t udi_productNo;
  48824. + u_int16_t udi_vendorNo;
  48825. + u_int16_t udi_releaseNo;
  48826. + u_int8_t udi_class;
  48827. + u_int8_t udi_subclass;
  48828. + u_int8_t udi_protocol;
  48829. + u_int8_t udi_config;
  48830. + u_int8_t udi_speed;
  48831. +#define USB_SPEED_UNKNOWN 0
  48832. +#define USB_SPEED_LOW 1
  48833. +#define USB_SPEED_FULL 2
  48834. +#define USB_SPEED_HIGH 3
  48835. +#define USB_SPEED_VARIABLE 4
  48836. +#define USB_SPEED_SUPER 5
  48837. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  48838. + int udi_nports;
  48839. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  48840. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  48841. +#define USB_PORT_ENABLED 0xff
  48842. +#define USB_PORT_SUSPENDED 0xfe
  48843. +#define USB_PORT_POWERED 0xfd
  48844. +#define USB_PORT_DISABLED 0xfc
  48845. +};
  48846. +
  48847. +struct usb_ctl_report {
  48848. + int ucr_report;
  48849. + u_char ucr_data[1024]; /* filled data size will vary */
  48850. +};
  48851. +
  48852. +struct usb_device_stats {
  48853. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  48854. +};
  48855. +
  48856. +#define WUSB_MIN_IE 0x80
  48857. +#define WUSB_WCTA_IE 0x80
  48858. +#define WUSB_WCONNECTACK_IE 0x81
  48859. +#define WUSB_WHOSTINFO_IE 0x82
  48860. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  48861. +#define WUHI_CA_RECONN 0x00
  48862. +#define WUHI_CA_LIMITED 0x01
  48863. +#define WUHI_CA_ALL 0x03
  48864. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  48865. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  48866. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  48867. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  48868. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  48869. +#define WUSB_WWORK_IE 0x87
  48870. +#define WUSB_WCHANNEL_STOP_IE 0x88
  48871. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  48872. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  48873. +#define WUSB_WRESETDEVICE_IE 0x8B
  48874. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  48875. +#define WUSB_MAX_IE 0x8C
  48876. +
  48877. +/* Device Notification Types */
  48878. +
  48879. +#define WUSB_DN_MIN 0x01
  48880. +#define WUSB_DN_CONNECT 0x01
  48881. +# define WUSB_DA_OLDCONN 0x00
  48882. +# define WUSB_DA_NEWCONN 0x01
  48883. +# define WUSB_DA_SELF_BEACON 0x02
  48884. +# define WUSB_DA_DIR_BEACON 0x04
  48885. +# define WUSB_DA_NO_BEACON 0x06
  48886. +#define WUSB_DN_DISCONNECT 0x02
  48887. +#define WUSB_DN_EPRDY 0x03
  48888. +#define WUSB_DN_MASAVAILCHANGED 0x04
  48889. +#define WUSB_DN_REMOTEWAKEUP 0x05
  48890. +#define WUSB_DN_SLEEP 0x06
  48891. +#define WUSB_DN_ALIVE 0x07
  48892. +#define WUSB_DN_MAX 0x07
  48893. +
  48894. +#ifdef _MSC_VER
  48895. +#include <pshpack1.h>
  48896. +#endif
  48897. +
  48898. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  48899. +typedef struct wusb_hndshk_data {
  48900. + uByte bMessageNumber;
  48901. + uByte bStatus;
  48902. + uByte tTKID[3];
  48903. + uByte bReserved;
  48904. + uByte CDID[16];
  48905. + uByte Nonce[16];
  48906. + uByte MIC[8];
  48907. +} UPACKED wusb_hndshk_data_t;
  48908. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  48909. +
  48910. +/* WUSB Connection Context */
  48911. +typedef struct wusb_conn_context {
  48912. + uByte CHID [16];
  48913. + uByte CDID [16];
  48914. + uByte CK [16];
  48915. +} UPACKED wusb_conn_context_t;
  48916. +
  48917. +/* WUSB Security Descriptor */
  48918. +typedef struct wusb_security_desc {
  48919. + uByte bLength;
  48920. + uByte bDescriptorType;
  48921. + uWord wTotalLength;
  48922. + uByte bNumEncryptionTypes;
  48923. +} UPACKED wusb_security_desc_t;
  48924. +
  48925. +/* WUSB Encryption Type Descriptor */
  48926. +typedef struct wusb_encrypt_type_desc {
  48927. + uByte bLength;
  48928. + uByte bDescriptorType;
  48929. +
  48930. + uByte bEncryptionType;
  48931. +#define WUETD_UNSECURE 0
  48932. +#define WUETD_WIRED 1
  48933. +#define WUETD_CCM_1 2
  48934. +#define WUETD_RSA_1 3
  48935. +
  48936. + uByte bEncryptionValue;
  48937. + uByte bAuthKeyIndex;
  48938. +} UPACKED wusb_encrypt_type_desc_t;
  48939. +
  48940. +/* WUSB Key Descriptor */
  48941. +typedef struct wusb_key_desc {
  48942. + uByte bLength;
  48943. + uByte bDescriptorType;
  48944. + uByte tTKID[3];
  48945. + uByte bReserved;
  48946. + uByte KeyData[1]; /* variable length */
  48947. +} UPACKED wusb_key_desc_t;
  48948. +
  48949. +/* WUSB BOS Descriptor (Binary device Object Store) */
  48950. +typedef struct wusb_bos_desc {
  48951. + uByte bLength;
  48952. + uByte bDescriptorType;
  48953. + uWord wTotalLength;
  48954. + uByte bNumDeviceCaps;
  48955. +} UPACKED wusb_bos_desc_t;
  48956. +
  48957. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  48958. +typedef struct usb_dev_cap_20_ext_desc {
  48959. + uByte bLength;
  48960. + uByte bDescriptorType;
  48961. + uByte bDevCapabilityType;
  48962. +#define USB_20_EXT_LPM 0x02
  48963. + uDWord bmAttributes;
  48964. +} UPACKED usb_dev_cap_20_ext_desc_t;
  48965. +
  48966. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  48967. +typedef struct usb_dev_cap_ss_usb {
  48968. + uByte bLength;
  48969. + uByte bDescriptorType;
  48970. + uByte bDevCapabilityType;
  48971. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  48972. + uByte bmAttributes;
  48973. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  48974. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  48975. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  48976. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  48977. + uWord wSpeedsSupported;
  48978. + uByte bFunctionalitySupport;
  48979. + uByte bU1DevExitLat;
  48980. + uWord wU2DevExitLat;
  48981. +} UPACKED usb_dev_cap_ss_usb_t;
  48982. +
  48983. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  48984. +typedef struct usb_dev_cap_container_id {
  48985. + uByte bLength;
  48986. + uByte bDescriptorType;
  48987. + uByte bDevCapabilityType;
  48988. + uByte bReserved;
  48989. + uByte containerID[16];
  48990. +} UPACKED usb_dev_cap_container_id_t;
  48991. +
  48992. +/* Device Capability Type Codes */
  48993. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  48994. +
  48995. +/* Device Capability Descriptor */
  48996. +typedef struct wusb_dev_cap_desc {
  48997. + uByte bLength;
  48998. + uByte bDescriptorType;
  48999. + uByte bDevCapabilityType;
  49000. + uByte caps[1]; /* Variable length */
  49001. +} UPACKED wusb_dev_cap_desc_t;
  49002. +
  49003. +/* Device Capability Descriptor */
  49004. +typedef struct wusb_dev_cap_uwb_desc {
  49005. + uByte bLength;
  49006. + uByte bDescriptorType;
  49007. + uByte bDevCapabilityType;
  49008. + uByte bmAttributes;
  49009. + uWord wPHYRates; /* Bitmap */
  49010. + uByte bmTFITXPowerInfo;
  49011. + uByte bmFFITXPowerInfo;
  49012. + uWord bmBandGroup;
  49013. + uByte bReserved;
  49014. +} UPACKED wusb_dev_cap_uwb_desc_t;
  49015. +
  49016. +/* Wireless USB Endpoint Companion Descriptor */
  49017. +typedef struct wusb_endpoint_companion_desc {
  49018. + uByte bLength;
  49019. + uByte bDescriptorType;
  49020. + uByte bMaxBurst;
  49021. + uByte bMaxSequence;
  49022. + uWord wMaxStreamDelay;
  49023. + uWord wOverTheAirPacketSize;
  49024. + uByte bOverTheAirInterval;
  49025. + uByte bmCompAttributes;
  49026. +} UPACKED wusb_endpoint_companion_desc_t;
  49027. +
  49028. +/* Wireless USB Numeric Association M1 Data Structure */
  49029. +typedef struct wusb_m1_data {
  49030. + uByte version;
  49031. + uWord langId;
  49032. + uByte deviceFriendlyNameLength;
  49033. + uByte sha_256_m3[32];
  49034. + uByte deviceFriendlyName[256];
  49035. +} UPACKED wusb_m1_data_t;
  49036. +
  49037. +typedef struct wusb_m2_data {
  49038. + uByte version;
  49039. + uWord langId;
  49040. + uByte hostFriendlyNameLength;
  49041. + uByte pkh[384];
  49042. + uByte hostFriendlyName[256];
  49043. +} UPACKED wusb_m2_data_t;
  49044. +
  49045. +typedef struct wusb_m3_data {
  49046. + uByte pkd[384];
  49047. + uByte nd;
  49048. +} UPACKED wusb_m3_data_t;
  49049. +
  49050. +typedef struct wusb_m4_data {
  49051. + uDWord _attributeTypeIdAndLength_1;
  49052. + uWord associationTypeId;
  49053. +
  49054. + uDWord _attributeTypeIdAndLength_2;
  49055. + uWord associationSubTypeId;
  49056. +
  49057. + uDWord _attributeTypeIdAndLength_3;
  49058. + uDWord length;
  49059. +
  49060. + uDWord _attributeTypeIdAndLength_4;
  49061. + uDWord associationStatus;
  49062. +
  49063. + uDWord _attributeTypeIdAndLength_5;
  49064. + uByte chid[16];
  49065. +
  49066. + uDWord _attributeTypeIdAndLength_6;
  49067. + uByte cdid[16];
  49068. +
  49069. + uDWord _attributeTypeIdAndLength_7;
  49070. + uByte bandGroups[2];
  49071. +} UPACKED wusb_m4_data_t;
  49072. +
  49073. +#ifdef _MSC_VER
  49074. +#include <poppack.h>
  49075. +#endif
  49076. +
  49077. +#ifdef __cplusplus
  49078. +}
  49079. +#endif
  49080. +
  49081. +#endif /* _USB_H_ */
  49082. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/doc/doxygen.cfg linux-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  49083. --- linux-3.10.37/drivers/usb/host/dwc_otg/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  49084. +++ linux-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg 2014-04-24 15:35:04.169565731 +0200
  49085. @@ -0,0 +1,224 @@
  49086. +# Doxyfile 1.3.9.1
  49087. +
  49088. +#---------------------------------------------------------------------------
  49089. +# Project related configuration options
  49090. +#---------------------------------------------------------------------------
  49091. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  49092. +PROJECT_NUMBER = v3.00a
  49093. +OUTPUT_DIRECTORY = ./doc/
  49094. +CREATE_SUBDIRS = NO
  49095. +OUTPUT_LANGUAGE = English
  49096. +BRIEF_MEMBER_DESC = YES
  49097. +REPEAT_BRIEF = YES
  49098. +ABBREVIATE_BRIEF = "The $name class" \
  49099. + "The $name widget" \
  49100. + "The $name file" \
  49101. + is \
  49102. + provides \
  49103. + specifies \
  49104. + contains \
  49105. + represents \
  49106. + a \
  49107. + an \
  49108. + the
  49109. +ALWAYS_DETAILED_SEC = NO
  49110. +INLINE_INHERITED_MEMB = NO
  49111. +FULL_PATH_NAMES = NO
  49112. +STRIP_FROM_PATH =
  49113. +STRIP_FROM_INC_PATH =
  49114. +SHORT_NAMES = NO
  49115. +JAVADOC_AUTOBRIEF = YES
  49116. +MULTILINE_CPP_IS_BRIEF = NO
  49117. +INHERIT_DOCS = YES
  49118. +DISTRIBUTE_GROUP_DOC = NO
  49119. +TAB_SIZE = 8
  49120. +ALIASES =
  49121. +OPTIMIZE_OUTPUT_FOR_C = YES
  49122. +OPTIMIZE_OUTPUT_JAVA = NO
  49123. +SUBGROUPING = YES
  49124. +#---------------------------------------------------------------------------
  49125. +# Build related configuration options
  49126. +#---------------------------------------------------------------------------
  49127. +EXTRACT_ALL = NO
  49128. +EXTRACT_PRIVATE = YES
  49129. +EXTRACT_STATIC = YES
  49130. +EXTRACT_LOCAL_CLASSES = YES
  49131. +EXTRACT_LOCAL_METHODS = NO
  49132. +HIDE_UNDOC_MEMBERS = NO
  49133. +HIDE_UNDOC_CLASSES = NO
  49134. +HIDE_FRIEND_COMPOUNDS = NO
  49135. +HIDE_IN_BODY_DOCS = NO
  49136. +INTERNAL_DOCS = NO
  49137. +CASE_SENSE_NAMES = NO
  49138. +HIDE_SCOPE_NAMES = NO
  49139. +SHOW_INCLUDE_FILES = YES
  49140. +INLINE_INFO = YES
  49141. +SORT_MEMBER_DOCS = NO
  49142. +SORT_BRIEF_DOCS = NO
  49143. +SORT_BY_SCOPE_NAME = NO
  49144. +GENERATE_TODOLIST = YES
  49145. +GENERATE_TESTLIST = YES
  49146. +GENERATE_BUGLIST = YES
  49147. +GENERATE_DEPRECATEDLIST= YES
  49148. +ENABLED_SECTIONS =
  49149. +MAX_INITIALIZER_LINES = 30
  49150. +SHOW_USED_FILES = YES
  49151. +SHOW_DIRECTORIES = YES
  49152. +#---------------------------------------------------------------------------
  49153. +# configuration options related to warning and progress messages
  49154. +#---------------------------------------------------------------------------
  49155. +QUIET = YES
  49156. +WARNINGS = YES
  49157. +WARN_IF_UNDOCUMENTED = NO
  49158. +WARN_IF_DOC_ERROR = YES
  49159. +WARN_FORMAT = "$file:$line: $text"
  49160. +WARN_LOGFILE =
  49161. +#---------------------------------------------------------------------------
  49162. +# configuration options related to the input files
  49163. +#---------------------------------------------------------------------------
  49164. +INPUT = .
  49165. +FILE_PATTERNS = *.c \
  49166. + *.h \
  49167. + ./linux/*.c \
  49168. + ./linux/*.h
  49169. +RECURSIVE = NO
  49170. +EXCLUDE = ./test/ \
  49171. + ./dwc_otg/.AppleDouble/
  49172. +EXCLUDE_SYMLINKS = YES
  49173. +EXCLUDE_PATTERNS = *.mod.*
  49174. +EXAMPLE_PATH =
  49175. +EXAMPLE_PATTERNS = *
  49176. +EXAMPLE_RECURSIVE = NO
  49177. +IMAGE_PATH =
  49178. +INPUT_FILTER =
  49179. +FILTER_PATTERNS =
  49180. +FILTER_SOURCE_FILES = NO
  49181. +#---------------------------------------------------------------------------
  49182. +# configuration options related to source browsing
  49183. +#---------------------------------------------------------------------------
  49184. +SOURCE_BROWSER = YES
  49185. +INLINE_SOURCES = NO
  49186. +STRIP_CODE_COMMENTS = YES
  49187. +REFERENCED_BY_RELATION = NO
  49188. +REFERENCES_RELATION = NO
  49189. +VERBATIM_HEADERS = NO
  49190. +#---------------------------------------------------------------------------
  49191. +# configuration options related to the alphabetical class index
  49192. +#---------------------------------------------------------------------------
  49193. +ALPHABETICAL_INDEX = NO
  49194. +COLS_IN_ALPHA_INDEX = 5
  49195. +IGNORE_PREFIX =
  49196. +#---------------------------------------------------------------------------
  49197. +# configuration options related to the HTML output
  49198. +#---------------------------------------------------------------------------
  49199. +GENERATE_HTML = YES
  49200. +HTML_OUTPUT = html
  49201. +HTML_FILE_EXTENSION = .html
  49202. +HTML_HEADER =
  49203. +HTML_FOOTER =
  49204. +HTML_STYLESHEET =
  49205. +HTML_ALIGN_MEMBERS = YES
  49206. +GENERATE_HTMLHELP = NO
  49207. +CHM_FILE =
  49208. +HHC_LOCATION =
  49209. +GENERATE_CHI = NO
  49210. +BINARY_TOC = NO
  49211. +TOC_EXPAND = NO
  49212. +DISABLE_INDEX = NO
  49213. +ENUM_VALUES_PER_LINE = 4
  49214. +GENERATE_TREEVIEW = YES
  49215. +TREEVIEW_WIDTH = 250
  49216. +#---------------------------------------------------------------------------
  49217. +# configuration options related to the LaTeX output
  49218. +#---------------------------------------------------------------------------
  49219. +GENERATE_LATEX = NO
  49220. +LATEX_OUTPUT = latex
  49221. +LATEX_CMD_NAME = latex
  49222. +MAKEINDEX_CMD_NAME = makeindex
  49223. +COMPACT_LATEX = NO
  49224. +PAPER_TYPE = a4wide
  49225. +EXTRA_PACKAGES =
  49226. +LATEX_HEADER =
  49227. +PDF_HYPERLINKS = NO
  49228. +USE_PDFLATEX = NO
  49229. +LATEX_BATCHMODE = NO
  49230. +LATEX_HIDE_INDICES = NO
  49231. +#---------------------------------------------------------------------------
  49232. +# configuration options related to the RTF output
  49233. +#---------------------------------------------------------------------------
  49234. +GENERATE_RTF = NO
  49235. +RTF_OUTPUT = rtf
  49236. +COMPACT_RTF = NO
  49237. +RTF_HYPERLINKS = NO
  49238. +RTF_STYLESHEET_FILE =
  49239. +RTF_EXTENSIONS_FILE =
  49240. +#---------------------------------------------------------------------------
  49241. +# configuration options related to the man page output
  49242. +#---------------------------------------------------------------------------
  49243. +GENERATE_MAN = NO
  49244. +MAN_OUTPUT = man
  49245. +MAN_EXTENSION = .3
  49246. +MAN_LINKS = NO
  49247. +#---------------------------------------------------------------------------
  49248. +# configuration options related to the XML output
  49249. +#---------------------------------------------------------------------------
  49250. +GENERATE_XML = NO
  49251. +XML_OUTPUT = xml
  49252. +XML_SCHEMA =
  49253. +XML_DTD =
  49254. +XML_PROGRAMLISTING = YES
  49255. +#---------------------------------------------------------------------------
  49256. +# configuration options for the AutoGen Definitions output
  49257. +#---------------------------------------------------------------------------
  49258. +GENERATE_AUTOGEN_DEF = NO
  49259. +#---------------------------------------------------------------------------
  49260. +# configuration options related to the Perl module output
  49261. +#---------------------------------------------------------------------------
  49262. +GENERATE_PERLMOD = NO
  49263. +PERLMOD_LATEX = NO
  49264. +PERLMOD_PRETTY = YES
  49265. +PERLMOD_MAKEVAR_PREFIX =
  49266. +#---------------------------------------------------------------------------
  49267. +# Configuration options related to the preprocessor
  49268. +#---------------------------------------------------------------------------
  49269. +ENABLE_PREPROCESSING = YES
  49270. +MACRO_EXPANSION = YES
  49271. +EXPAND_ONLY_PREDEF = YES
  49272. +SEARCH_INCLUDES = YES
  49273. +INCLUDE_PATH =
  49274. +INCLUDE_FILE_PATTERNS =
  49275. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  49276. +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
  49277. +SKIP_FUNCTION_MACROS = NO
  49278. +#---------------------------------------------------------------------------
  49279. +# Configuration::additions related to external references
  49280. +#---------------------------------------------------------------------------
  49281. +TAGFILES =
  49282. +GENERATE_TAGFILE =
  49283. +ALLEXTERNALS = NO
  49284. +EXTERNAL_GROUPS = YES
  49285. +PERL_PATH = /usr/bin/perl
  49286. +#---------------------------------------------------------------------------
  49287. +# Configuration options related to the dot tool
  49288. +#---------------------------------------------------------------------------
  49289. +CLASS_DIAGRAMS = YES
  49290. +HIDE_UNDOC_RELATIONS = YES
  49291. +HAVE_DOT = NO
  49292. +CLASS_GRAPH = YES
  49293. +COLLABORATION_GRAPH = YES
  49294. +UML_LOOK = NO
  49295. +TEMPLATE_RELATIONS = NO
  49296. +INCLUDE_GRAPH = YES
  49297. +INCLUDED_BY_GRAPH = YES
  49298. +CALL_GRAPH = NO
  49299. +GRAPHICAL_HIERARCHY = YES
  49300. +DOT_IMAGE_FORMAT = png
  49301. +DOT_PATH =
  49302. +DOTFILE_DIRS =
  49303. +MAX_DOT_GRAPH_DEPTH = 1000
  49304. +GENERATE_LEGEND = YES
  49305. +DOT_CLEANUP = YES
  49306. +#---------------------------------------------------------------------------
  49307. +# Configuration::additions related to the search engine
  49308. +#---------------------------------------------------------------------------
  49309. +SEARCHENGINE = NO
  49310. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dummy_audio.c linux-rpi/drivers/usb/host/dwc_otg/dummy_audio.c
  49311. --- linux-3.10.37/drivers/usb/host/dwc_otg/dummy_audio.c 1970-01-01 01:00:00.000000000 +0100
  49312. +++ linux-rpi/drivers/usb/host/dwc_otg/dummy_audio.c 2014-04-24 15:35:04.169565731 +0200
  49313. @@ -0,0 +1,1575 @@
  49314. +/*
  49315. + * zero.c -- Gadget Zero, for USB development
  49316. + *
  49317. + * Copyright (C) 2003-2004 David Brownell
  49318. + * All rights reserved.
  49319. + *
  49320. + * Redistribution and use in source and binary forms, with or without
  49321. + * modification, are permitted provided that the following conditions
  49322. + * are met:
  49323. + * 1. Redistributions of source code must retain the above copyright
  49324. + * notice, this list of conditions, and the following disclaimer,
  49325. + * without modification.
  49326. + * 2. Redistributions in binary form must reproduce the above copyright
  49327. + * notice, this list of conditions and the following disclaimer in the
  49328. + * documentation and/or other materials provided with the distribution.
  49329. + * 3. The names of the above-listed copyright holders may not be used
  49330. + * to endorse or promote products derived from this software without
  49331. + * specific prior written permission.
  49332. + *
  49333. + * ALTERNATIVELY, this software may be distributed under the terms of the
  49334. + * GNU General Public License ("GPL") as published by the Free Software
  49335. + * Foundation, either version 2 of that License or (at your option) any
  49336. + * later version.
  49337. + *
  49338. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  49339. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  49340. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  49341. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49342. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  49343. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  49344. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  49345. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  49346. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  49347. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  49348. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49349. + */
  49350. +
  49351. +
  49352. +/*
  49353. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  49354. + * can write a hardware-agnostic gadget driver running inside a USB device.
  49355. + *
  49356. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  49357. + * affect most of the driver.
  49358. + *
  49359. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  49360. + * functional test of your device-side usb stack, or with "usb-skeleton".
  49361. + *
  49362. + * It supports two similar configurations. One sinks whatever the usb host
  49363. + * writes, and in return sources zeroes. The other loops whatever the host
  49364. + * writes back, so the host can read it. Module options include:
  49365. + *
  49366. + * buflen=N default N=4096, buffer size used
  49367. + * qlen=N default N=32, how many buffers in the loopback queue
  49368. + * loopdefault default false, list loopback config first
  49369. + *
  49370. + * Many drivers will only have one configuration, letting them be much
  49371. + * simpler if they also don't support high speed operation (like this
  49372. + * driver does).
  49373. + */
  49374. +
  49375. +#include <linux/config.h>
  49376. +#include <linux/module.h>
  49377. +#include <linux/kernel.h>
  49378. +#include <linux/delay.h>
  49379. +#include <linux/ioport.h>
  49380. +#include <linux/sched.h>
  49381. +#include <linux/slab.h>
  49382. +#include <linux/smp_lock.h>
  49383. +#include <linux/errno.h>
  49384. +#include <linux/init.h>
  49385. +#include <linux/timer.h>
  49386. +#include <linux/list.h>
  49387. +#include <linux/interrupt.h>
  49388. +#include <linux/uts.h>
  49389. +#include <linux/version.h>
  49390. +#include <linux/device.h>
  49391. +#include <linux/moduleparam.h>
  49392. +#include <linux/proc_fs.h>
  49393. +
  49394. +#include <asm/byteorder.h>
  49395. +#include <asm/io.h>
  49396. +#include <asm/irq.h>
  49397. +#include <asm/system.h>
  49398. +#include <asm/unaligned.h>
  49399. +
  49400. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  49401. +# include <linux/usb/ch9.h>
  49402. +#else
  49403. +# include <linux/usb_ch9.h>
  49404. +#endif
  49405. +
  49406. +#include <linux/usb_gadget.h>
  49407. +
  49408. +
  49409. +/*-------------------------------------------------------------------------*/
  49410. +/*-------------------------------------------------------------------------*/
  49411. +
  49412. +
  49413. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  49414. +{
  49415. + int count = 0;
  49416. + u8 c;
  49417. + u16 uchar;
  49418. +
  49419. + /* this insists on correct encodings, though not minimal ones.
  49420. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  49421. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  49422. + */
  49423. + while (len != 0 && (c = (u8) *s++) != 0) {
  49424. + if (unlikely(c & 0x80)) {
  49425. + // 2-byte sequence:
  49426. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  49427. + if ((c & 0xe0) == 0xc0) {
  49428. + uchar = (c & 0x1f) << 6;
  49429. +
  49430. + c = (u8) *s++;
  49431. + if ((c & 0xc0) != 0xc0)
  49432. + goto fail;
  49433. + c &= 0x3f;
  49434. + uchar |= c;
  49435. +
  49436. + // 3-byte sequence (most CJKV characters):
  49437. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  49438. + } else if ((c & 0xf0) == 0xe0) {
  49439. + uchar = (c & 0x0f) << 12;
  49440. +
  49441. + c = (u8) *s++;
  49442. + if ((c & 0xc0) != 0xc0)
  49443. + goto fail;
  49444. + c &= 0x3f;
  49445. + uchar |= c << 6;
  49446. +
  49447. + c = (u8) *s++;
  49448. + if ((c & 0xc0) != 0xc0)
  49449. + goto fail;
  49450. + c &= 0x3f;
  49451. + uchar |= c;
  49452. +
  49453. + /* no bogus surrogates */
  49454. + if (0xd800 <= uchar && uchar <= 0xdfff)
  49455. + goto fail;
  49456. +
  49457. + // 4-byte sequence (surrogate pairs, currently rare):
  49458. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  49459. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  49460. + // (uuuuu = wwww + 1)
  49461. + // FIXME accept the surrogate code points (only)
  49462. +
  49463. + } else
  49464. + goto fail;
  49465. + } else
  49466. + uchar = c;
  49467. + put_unaligned (cpu_to_le16 (uchar), cp++);
  49468. + count++;
  49469. + len--;
  49470. + }
  49471. + return count;
  49472. +fail:
  49473. + return -1;
  49474. +}
  49475. +
  49476. +
  49477. +/**
  49478. + * usb_gadget_get_string - fill out a string descriptor
  49479. + * @table: of c strings encoded using UTF-8
  49480. + * @id: string id, from low byte of wValue in get string descriptor
  49481. + * @buf: at least 256 bytes
  49482. + *
  49483. + * Finds the UTF-8 string matching the ID, and converts it into a
  49484. + * string descriptor in utf16-le.
  49485. + * Returns length of descriptor (always even) or negative errno
  49486. + *
  49487. + * If your driver needs stings in multiple languages, you'll probably
  49488. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  49489. + * using this routine after choosing which set of UTF-8 strings to use.
  49490. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  49491. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  49492. + * characters (which are also widely used in C strings).
  49493. + */
  49494. +int
  49495. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  49496. +{
  49497. + struct usb_string *s;
  49498. + int len;
  49499. +
  49500. + /* descriptor 0 has the language id */
  49501. + if (id == 0) {
  49502. + buf [0] = 4;
  49503. + buf [1] = USB_DT_STRING;
  49504. + buf [2] = (u8) table->language;
  49505. + buf [3] = (u8) (table->language >> 8);
  49506. + return 4;
  49507. + }
  49508. + for (s = table->strings; s && s->s; s++)
  49509. + if (s->id == id)
  49510. + break;
  49511. +
  49512. + /* unrecognized: stall. */
  49513. + if (!s || !s->s)
  49514. + return -EINVAL;
  49515. +
  49516. + /* string descriptors have length, tag, then UTF16-LE text */
  49517. + len = min ((size_t) 126, strlen (s->s));
  49518. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  49519. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  49520. + if (len < 0)
  49521. + return -EINVAL;
  49522. + buf [0] = (len + 1) * 2;
  49523. + buf [1] = USB_DT_STRING;
  49524. + return buf [0];
  49525. +}
  49526. +
  49527. +
  49528. +/*-------------------------------------------------------------------------*/
  49529. +/*-------------------------------------------------------------------------*/
  49530. +
  49531. +
  49532. +/**
  49533. + * usb_descriptor_fillbuf - fill buffer with descriptors
  49534. + * @buf: Buffer to be filled
  49535. + * @buflen: Size of buf
  49536. + * @src: Array of descriptor pointers, terminated by null pointer.
  49537. + *
  49538. + * Copies descriptors into the buffer, returning the length or a
  49539. + * negative error code if they can't all be copied. Useful when
  49540. + * assembling descriptors for an associated set of interfaces used
  49541. + * as part of configuring a composite device; or in other cases where
  49542. + * sets of descriptors need to be marshaled.
  49543. + */
  49544. +int
  49545. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  49546. + const struct usb_descriptor_header **src)
  49547. +{
  49548. + u8 *dest = buf;
  49549. +
  49550. + if (!src)
  49551. + return -EINVAL;
  49552. +
  49553. + /* fill buffer from src[] until null descriptor ptr */
  49554. + for (; 0 != *src; src++) {
  49555. + unsigned len = (*src)->bLength;
  49556. +
  49557. + if (len > buflen)
  49558. + return -EINVAL;
  49559. + memcpy(dest, *src, len);
  49560. + buflen -= len;
  49561. + dest += len;
  49562. + }
  49563. + return dest - (u8 *)buf;
  49564. +}
  49565. +
  49566. +
  49567. +/**
  49568. + * usb_gadget_config_buf - builts a complete configuration descriptor
  49569. + * @config: Header for the descriptor, including characteristics such
  49570. + * as power requirements and number of interfaces.
  49571. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  49572. + * endpoint, etc) defining all functions in this device configuration.
  49573. + * @buf: Buffer for the resulting configuration descriptor.
  49574. + * @length: Length of buffer. If this is not big enough to hold the
  49575. + * entire configuration descriptor, an error code will be returned.
  49576. + *
  49577. + * This copies descriptors into the response buffer, building a descriptor
  49578. + * for that configuration. It returns the buffer length or a negative
  49579. + * status code. The config.wTotalLength field is set to match the length
  49580. + * of the result, but other descriptor fields (including power usage and
  49581. + * interface count) must be set by the caller.
  49582. + *
  49583. + * Gadget drivers could use this when constructing a config descriptor
  49584. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  49585. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  49586. + */
  49587. +int usb_gadget_config_buf(
  49588. + const struct usb_config_descriptor *config,
  49589. + void *buf,
  49590. + unsigned length,
  49591. + const struct usb_descriptor_header **desc
  49592. +)
  49593. +{
  49594. + struct usb_config_descriptor *cp = buf;
  49595. + int len;
  49596. +
  49597. + /* config descriptor first */
  49598. + if (length < USB_DT_CONFIG_SIZE || !desc)
  49599. + return -EINVAL;
  49600. + *cp = *config;
  49601. +
  49602. + /* then interface/endpoint/class/vendor/... */
  49603. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  49604. + length - USB_DT_CONFIG_SIZE, desc);
  49605. + if (len < 0)
  49606. + return len;
  49607. + len += USB_DT_CONFIG_SIZE;
  49608. + if (len > 0xffff)
  49609. + return -EINVAL;
  49610. +
  49611. + /* patch up the config descriptor */
  49612. + cp->bLength = USB_DT_CONFIG_SIZE;
  49613. + cp->bDescriptorType = USB_DT_CONFIG;
  49614. + cp->wTotalLength = cpu_to_le16(len);
  49615. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  49616. + return len;
  49617. +}
  49618. +
  49619. +/*-------------------------------------------------------------------------*/
  49620. +/*-------------------------------------------------------------------------*/
  49621. +
  49622. +
  49623. +#define RBUF_LEN (1024*1024)
  49624. +static int rbuf_start;
  49625. +static int rbuf_len;
  49626. +static __u8 rbuf[RBUF_LEN];
  49627. +
  49628. +/*-------------------------------------------------------------------------*/
  49629. +
  49630. +#define DRIVER_VERSION "St Patrick's Day 2004"
  49631. +
  49632. +static const char shortname [] = "zero";
  49633. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  49634. +
  49635. +static const char source_sink [] = "source and sink data";
  49636. +static const char loopback [] = "loop input to output";
  49637. +
  49638. +/*-------------------------------------------------------------------------*/
  49639. +
  49640. +/*
  49641. + * driver assumes self-powered hardware, and
  49642. + * has no way for users to trigger remote wakeup.
  49643. + *
  49644. + * this version autoconfigures as much as possible,
  49645. + * which is reasonable for most "bulk-only" drivers.
  49646. + */
  49647. +static const char *EP_IN_NAME; /* source */
  49648. +static const char *EP_OUT_NAME; /* sink */
  49649. +
  49650. +/*-------------------------------------------------------------------------*/
  49651. +
  49652. +/* big enough to hold our biggest descriptor */
  49653. +#define USB_BUFSIZ 512
  49654. +
  49655. +struct zero_dev {
  49656. + spinlock_t lock;
  49657. + struct usb_gadget *gadget;
  49658. + struct usb_request *req; /* for control responses */
  49659. +
  49660. + /* when configured, we have one of two configs:
  49661. + * - source data (in to host) and sink it (out from host)
  49662. + * - or loop it back (out from host back in to host)
  49663. + */
  49664. + u8 config;
  49665. + struct usb_ep *in_ep, *out_ep;
  49666. +
  49667. + /* autoresume timer */
  49668. + struct timer_list resume;
  49669. +};
  49670. +
  49671. +#define xprintk(d,level,fmt,args...) \
  49672. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  49673. +
  49674. +#ifdef DEBUG
  49675. +#define DBG(dev,fmt,args...) \
  49676. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  49677. +#else
  49678. +#define DBG(dev,fmt,args...) \
  49679. + do { } while (0)
  49680. +#endif /* DEBUG */
  49681. +
  49682. +#ifdef VERBOSE
  49683. +#define VDBG DBG
  49684. +#else
  49685. +#define VDBG(dev,fmt,args...) \
  49686. + do { } while (0)
  49687. +#endif /* VERBOSE */
  49688. +
  49689. +#define ERROR(dev,fmt,args...) \
  49690. + xprintk(dev , KERN_ERR , fmt , ## args)
  49691. +#define WARN(dev,fmt,args...) \
  49692. + xprintk(dev , KERN_WARNING , fmt , ## args)
  49693. +#define INFO(dev,fmt,args...) \
  49694. + xprintk(dev , KERN_INFO , fmt , ## args)
  49695. +
  49696. +/*-------------------------------------------------------------------------*/
  49697. +
  49698. +static unsigned buflen = 4096;
  49699. +static unsigned qlen = 32;
  49700. +static unsigned pattern = 0;
  49701. +
  49702. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  49703. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  49704. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  49705. +
  49706. +/*
  49707. + * if it's nonzero, autoresume says how many seconds to wait
  49708. + * before trying to wake up the host after suspend.
  49709. + */
  49710. +static unsigned autoresume = 0;
  49711. +module_param (autoresume, uint, 0);
  49712. +
  49713. +/*
  49714. + * Normally the "loopback" configuration is second (index 1) so
  49715. + * it's not the default. Here's where to change that order, to
  49716. + * work better with hosts where config changes are problematic.
  49717. + * Or controllers (like superh) that only support one config.
  49718. + */
  49719. +static int loopdefault = 0;
  49720. +
  49721. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  49722. +
  49723. +/*-------------------------------------------------------------------------*/
  49724. +
  49725. +/* Thanks to NetChip Technologies for donating this product ID.
  49726. + *
  49727. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  49728. + * Instead: allocate your own, using normal USB-IF procedures.
  49729. + */
  49730. +#ifndef CONFIG_USB_ZERO_HNPTEST
  49731. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  49732. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  49733. +#else
  49734. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  49735. +#define DRIVER_PRODUCT_NUM 0xbadd
  49736. +#endif
  49737. +
  49738. +/*-------------------------------------------------------------------------*/
  49739. +
  49740. +/*
  49741. + * DESCRIPTORS ... most are static, but strings and (full)
  49742. + * configuration descriptors are built on demand.
  49743. + */
  49744. +
  49745. +/*
  49746. +#define STRING_MANUFACTURER 25
  49747. +#define STRING_PRODUCT 42
  49748. +#define STRING_SERIAL 101
  49749. +*/
  49750. +#define STRING_MANUFACTURER 1
  49751. +#define STRING_PRODUCT 2
  49752. +#define STRING_SERIAL 3
  49753. +
  49754. +#define STRING_SOURCE_SINK 250
  49755. +#define STRING_LOOPBACK 251
  49756. +
  49757. +/*
  49758. + * This device advertises two configurations; these numbers work
  49759. + * on a pxa250 as well as more flexible hardware.
  49760. + */
  49761. +#define CONFIG_SOURCE_SINK 3
  49762. +#define CONFIG_LOOPBACK 2
  49763. +
  49764. +/*
  49765. +static struct usb_device_descriptor
  49766. +device_desc = {
  49767. + .bLength = sizeof device_desc,
  49768. + .bDescriptorType = USB_DT_DEVICE,
  49769. +
  49770. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  49771. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  49772. +
  49773. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  49774. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  49775. + .iManufacturer = STRING_MANUFACTURER,
  49776. + .iProduct = STRING_PRODUCT,
  49777. + .iSerialNumber = STRING_SERIAL,
  49778. + .bNumConfigurations = 2,
  49779. +};
  49780. +*/
  49781. +static struct usb_device_descriptor
  49782. +device_desc = {
  49783. + .bLength = sizeof device_desc,
  49784. + .bDescriptorType = USB_DT_DEVICE,
  49785. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  49786. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  49787. + .bDeviceSubClass = 0,
  49788. + .bDeviceProtocol = 0,
  49789. + .bMaxPacketSize0 = 64,
  49790. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  49791. + .idVendor = __constant_cpu_to_le16 (0x0499),
  49792. + .idProduct = __constant_cpu_to_le16 (0x3002),
  49793. + .iManufacturer = STRING_MANUFACTURER,
  49794. + .iProduct = STRING_PRODUCT,
  49795. + .iSerialNumber = STRING_SERIAL,
  49796. + .bNumConfigurations = 1,
  49797. +};
  49798. +
  49799. +static struct usb_config_descriptor
  49800. +z_config = {
  49801. + .bLength = sizeof z_config,
  49802. + .bDescriptorType = USB_DT_CONFIG,
  49803. +
  49804. + /* compute wTotalLength on the fly */
  49805. + .bNumInterfaces = 2,
  49806. + .bConfigurationValue = 1,
  49807. + .iConfiguration = 0,
  49808. + .bmAttributes = 0x40,
  49809. + .bMaxPower = 0, /* self-powered */
  49810. +};
  49811. +
  49812. +
  49813. +static struct usb_otg_descriptor
  49814. +otg_descriptor = {
  49815. + .bLength = sizeof otg_descriptor,
  49816. + .bDescriptorType = USB_DT_OTG,
  49817. +
  49818. + .bmAttributes = USB_OTG_SRP,
  49819. +};
  49820. +
  49821. +/* one interface in each configuration */
  49822. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  49823. +
  49824. +/*
  49825. + * usb 2.0 devices need to expose both high speed and full speed
  49826. + * descriptors, unless they only run at full speed.
  49827. + *
  49828. + * that means alternate endpoint descriptors (bigger packets)
  49829. + * and a "device qualifier" ... plus more construction options
  49830. + * for the config descriptor.
  49831. + */
  49832. +
  49833. +static struct usb_qualifier_descriptor
  49834. +dev_qualifier = {
  49835. + .bLength = sizeof dev_qualifier,
  49836. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  49837. +
  49838. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  49839. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  49840. +
  49841. + .bNumConfigurations = 2,
  49842. +};
  49843. +
  49844. +
  49845. +struct usb_cs_as_general_descriptor {
  49846. + __u8 bLength;
  49847. + __u8 bDescriptorType;
  49848. +
  49849. + __u8 bDescriptorSubType;
  49850. + __u8 bTerminalLink;
  49851. + __u8 bDelay;
  49852. + __u16 wFormatTag;
  49853. +} __attribute__ ((packed));
  49854. +
  49855. +struct usb_cs_as_format_descriptor {
  49856. + __u8 bLength;
  49857. + __u8 bDescriptorType;
  49858. +
  49859. + __u8 bDescriptorSubType;
  49860. + __u8 bFormatType;
  49861. + __u8 bNrChannels;
  49862. + __u8 bSubframeSize;
  49863. + __u8 bBitResolution;
  49864. + __u8 bSamfreqType;
  49865. + __u8 tLowerSamFreq[3];
  49866. + __u8 tUpperSamFreq[3];
  49867. +} __attribute__ ((packed));
  49868. +
  49869. +static const struct usb_interface_descriptor
  49870. +z_audio_control_if_desc = {
  49871. + .bLength = sizeof z_audio_control_if_desc,
  49872. + .bDescriptorType = USB_DT_INTERFACE,
  49873. + .bInterfaceNumber = 0,
  49874. + .bAlternateSetting = 0,
  49875. + .bNumEndpoints = 0,
  49876. + .bInterfaceClass = USB_CLASS_AUDIO,
  49877. + .bInterfaceSubClass = 0x1,
  49878. + .bInterfaceProtocol = 0,
  49879. + .iInterface = 0,
  49880. +};
  49881. +
  49882. +static const struct usb_interface_descriptor
  49883. +z_audio_if_desc = {
  49884. + .bLength = sizeof z_audio_if_desc,
  49885. + .bDescriptorType = USB_DT_INTERFACE,
  49886. + .bInterfaceNumber = 1,
  49887. + .bAlternateSetting = 0,
  49888. + .bNumEndpoints = 0,
  49889. + .bInterfaceClass = USB_CLASS_AUDIO,
  49890. + .bInterfaceSubClass = 0x2,
  49891. + .bInterfaceProtocol = 0,
  49892. + .iInterface = 0,
  49893. +};
  49894. +
  49895. +static const struct usb_interface_descriptor
  49896. +z_audio_if_desc2 = {
  49897. + .bLength = sizeof z_audio_if_desc,
  49898. + .bDescriptorType = USB_DT_INTERFACE,
  49899. + .bInterfaceNumber = 1,
  49900. + .bAlternateSetting = 1,
  49901. + .bNumEndpoints = 1,
  49902. + .bInterfaceClass = USB_CLASS_AUDIO,
  49903. + .bInterfaceSubClass = 0x2,
  49904. + .bInterfaceProtocol = 0,
  49905. + .iInterface = 0,
  49906. +};
  49907. +
  49908. +static const struct usb_cs_as_general_descriptor
  49909. +z_audio_cs_as_if_desc = {
  49910. + .bLength = 7,
  49911. + .bDescriptorType = 0x24,
  49912. +
  49913. + .bDescriptorSubType = 0x01,
  49914. + .bTerminalLink = 0x01,
  49915. + .bDelay = 0x0,
  49916. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  49917. +};
  49918. +
  49919. +
  49920. +static const struct usb_cs_as_format_descriptor
  49921. +z_audio_cs_as_format_desc = {
  49922. + .bLength = 0xe,
  49923. + .bDescriptorType = 0x24,
  49924. +
  49925. + .bDescriptorSubType = 2,
  49926. + .bFormatType = 1,
  49927. + .bNrChannels = 1,
  49928. + .bSubframeSize = 1,
  49929. + .bBitResolution = 8,
  49930. + .bSamfreqType = 0,
  49931. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  49932. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  49933. +};
  49934. +
  49935. +static const struct usb_endpoint_descriptor
  49936. +z_iso_ep = {
  49937. + .bLength = 0x09,
  49938. + .bDescriptorType = 0x05,
  49939. + .bEndpointAddress = 0x04,
  49940. + .bmAttributes = 0x09,
  49941. + .wMaxPacketSize = 0x0038,
  49942. + .bInterval = 0x01,
  49943. + .bRefresh = 0x00,
  49944. + .bSynchAddress = 0x00,
  49945. +};
  49946. +
  49947. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49948. +
  49949. +// 9 bytes
  49950. +static char z_ac_interface_header_desc[] =
  49951. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  49952. +
  49953. +// 12 bytes
  49954. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  49955. + 0x03, 0x00, 0x00, 0x00};
  49956. +// 13 bytes
  49957. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  49958. + 0x02, 0x00, 0x02, 0x00, 0x00};
  49959. +// 9 bytes
  49960. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  49961. + 0x00};
  49962. +
  49963. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  49964. + 0x00};
  49965. +
  49966. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49967. +
  49968. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  49969. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49970. +
  49971. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  49972. + 0x00};
  49973. +
  49974. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49975. +
  49976. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  49977. + 0x00};
  49978. +
  49979. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49980. +
  49981. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  49982. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49983. +
  49984. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  49985. + 0x00};
  49986. +
  49987. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49988. +
  49989. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  49990. + 0x00};
  49991. +
  49992. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49993. +
  49994. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  49995. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49996. +
  49997. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  49998. + 0x00};
  49999. +
  50000. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  50001. +
  50002. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  50003. + 0x00};
  50004. +
  50005. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  50006. +
  50007. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  50008. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  50009. +
  50010. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  50011. + 0x00};
  50012. +
  50013. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  50014. +
  50015. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  50016. + 0x00};
  50017. +
  50018. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  50019. +
  50020. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  50021. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  50022. +
  50023. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  50024. + 0x00};
  50025. +
  50026. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  50027. +
  50028. +
  50029. +
  50030. +static const struct usb_descriptor_header *z_function [] = {
  50031. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  50032. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  50033. + (struct usb_descriptor_header *) &z_0,
  50034. + (struct usb_descriptor_header *) &z_1,
  50035. + (struct usb_descriptor_header *) &z_2,
  50036. + (struct usb_descriptor_header *) &z_audio_if_desc,
  50037. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  50038. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  50039. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  50040. + (struct usb_descriptor_header *) &z_iso_ep,
  50041. + (struct usb_descriptor_header *) &z_iso_ep2,
  50042. + (struct usb_descriptor_header *) &za_0,
  50043. + (struct usb_descriptor_header *) &za_1,
  50044. + (struct usb_descriptor_header *) &za_2,
  50045. + (struct usb_descriptor_header *) &za_3,
  50046. + (struct usb_descriptor_header *) &za_4,
  50047. + (struct usb_descriptor_header *) &za_5,
  50048. + (struct usb_descriptor_header *) &za_6,
  50049. + (struct usb_descriptor_header *) &za_7,
  50050. + (struct usb_descriptor_header *) &za_8,
  50051. + (struct usb_descriptor_header *) &za_9,
  50052. + (struct usb_descriptor_header *) &za_10,
  50053. + (struct usb_descriptor_header *) &za_11,
  50054. + (struct usb_descriptor_header *) &za_12,
  50055. + (struct usb_descriptor_header *) &za_13,
  50056. + (struct usb_descriptor_header *) &za_14,
  50057. + (struct usb_descriptor_header *) &za_15,
  50058. + (struct usb_descriptor_header *) &za_16,
  50059. + (struct usb_descriptor_header *) &za_17,
  50060. + (struct usb_descriptor_header *) &za_18,
  50061. + (struct usb_descriptor_header *) &za_19,
  50062. + (struct usb_descriptor_header *) &za_20,
  50063. + (struct usb_descriptor_header *) &za_21,
  50064. + (struct usb_descriptor_header *) &za_22,
  50065. + (struct usb_descriptor_header *) &za_23,
  50066. + (struct usb_descriptor_header *) &za_24,
  50067. + NULL,
  50068. +};
  50069. +
  50070. +/* maxpacket and other transfer characteristics vary by speed. */
  50071. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  50072. +
  50073. +#else
  50074. +
  50075. +/* if there's no high speed support, maxpacket doesn't change. */
  50076. +#define ep_desc(g,hs,fs) fs
  50077. +
  50078. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  50079. +
  50080. +static char manufacturer [40];
  50081. +//static char serial [40];
  50082. +static char serial [] = "Ser 00 em";
  50083. +
  50084. +/* static strings, in UTF-8 */
  50085. +static struct usb_string strings [] = {
  50086. + { STRING_MANUFACTURER, manufacturer, },
  50087. + { STRING_PRODUCT, longname, },
  50088. + { STRING_SERIAL, serial, },
  50089. + { STRING_LOOPBACK, loopback, },
  50090. + { STRING_SOURCE_SINK, source_sink, },
  50091. + { } /* end of list */
  50092. +};
  50093. +
  50094. +static struct usb_gadget_strings stringtab = {
  50095. + .language = 0x0409, /* en-us */
  50096. + .strings = strings,
  50097. +};
  50098. +
  50099. +/*
  50100. + * config descriptors are also handcrafted. these must agree with code
  50101. + * that sets configurations, and with code managing interfaces and their
  50102. + * altsettings. other complexity may come from:
  50103. + *
  50104. + * - high speed support, including "other speed config" rules
  50105. + * - multiple configurations
  50106. + * - interfaces with alternate settings
  50107. + * - embedded class or vendor-specific descriptors
  50108. + *
  50109. + * this handles high speed, and has a second config that could as easily
  50110. + * have been an alternate interface setting (on most hardware).
  50111. + *
  50112. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  50113. + * should include an altsetting to test interrupt transfers, including
  50114. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  50115. + * device?)
  50116. + */
  50117. +static int
  50118. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  50119. +{
  50120. + int len;
  50121. + const struct usb_descriptor_header **function;
  50122. +
  50123. + function = z_function;
  50124. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  50125. + if (len < 0)
  50126. + return len;
  50127. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  50128. + return len;
  50129. +}
  50130. +
  50131. +/*-------------------------------------------------------------------------*/
  50132. +
  50133. +static struct usb_request *
  50134. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  50135. +{
  50136. + struct usb_request *req;
  50137. +
  50138. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  50139. + if (req) {
  50140. + req->length = length;
  50141. + req->buf = usb_ep_alloc_buffer (ep, length,
  50142. + &req->dma, GFP_ATOMIC);
  50143. + if (!req->buf) {
  50144. + usb_ep_free_request (ep, req);
  50145. + req = NULL;
  50146. + }
  50147. + }
  50148. + return req;
  50149. +}
  50150. +
  50151. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  50152. +{
  50153. + if (req->buf)
  50154. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  50155. + usb_ep_free_request (ep, req);
  50156. +}
  50157. +
  50158. +/*-------------------------------------------------------------------------*/
  50159. +
  50160. +/* optionally require specific source/sink data patterns */
  50161. +
  50162. +static int
  50163. +check_read_data (
  50164. + struct zero_dev *dev,
  50165. + struct usb_ep *ep,
  50166. + struct usb_request *req
  50167. +)
  50168. +{
  50169. + unsigned i;
  50170. + u8 *buf = req->buf;
  50171. +
  50172. + for (i = 0; i < req->actual; i++, buf++) {
  50173. + switch (pattern) {
  50174. + /* all-zeroes has no synchronization issues */
  50175. + case 0:
  50176. + if (*buf == 0)
  50177. + continue;
  50178. + break;
  50179. + /* mod63 stays in sync with short-terminated transfers,
  50180. + * or otherwise when host and gadget agree on how large
  50181. + * each usb transfer request should be. resync is done
  50182. + * with set_interface or set_config.
  50183. + */
  50184. + case 1:
  50185. + if (*buf == (u8)(i % 63))
  50186. + continue;
  50187. + break;
  50188. + }
  50189. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  50190. + usb_ep_set_halt (ep);
  50191. + return -EINVAL;
  50192. + }
  50193. + return 0;
  50194. +}
  50195. +
  50196. +/*-------------------------------------------------------------------------*/
  50197. +
  50198. +static void zero_reset_config (struct zero_dev *dev)
  50199. +{
  50200. + if (dev->config == 0)
  50201. + return;
  50202. +
  50203. + DBG (dev, "reset config\n");
  50204. +
  50205. + /* just disable endpoints, forcing completion of pending i/o.
  50206. + * all our completion handlers free their requests in this case.
  50207. + */
  50208. + if (dev->in_ep) {
  50209. + usb_ep_disable (dev->in_ep);
  50210. + dev->in_ep = NULL;
  50211. + }
  50212. + if (dev->out_ep) {
  50213. + usb_ep_disable (dev->out_ep);
  50214. + dev->out_ep = NULL;
  50215. + }
  50216. + dev->config = 0;
  50217. + del_timer (&dev->resume);
  50218. +}
  50219. +
  50220. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  50221. +
  50222. +static void
  50223. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  50224. +{
  50225. + struct zero_dev *dev = ep->driver_data;
  50226. + int status = req->status;
  50227. + int i, j;
  50228. +
  50229. + switch (status) {
  50230. +
  50231. + case 0: /* normal completion? */
  50232. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  50233. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  50234. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  50235. + rbuf[j] = ((__u8*)req->buf)[i];
  50236. + j++;
  50237. + if (j >= RBUF_LEN) j=0;
  50238. + }
  50239. + rbuf_start = j;
  50240. + //printk ("\n\n");
  50241. +
  50242. + if (rbuf_len < RBUF_LEN) {
  50243. + rbuf_len += req->actual;
  50244. + if (rbuf_len > RBUF_LEN) {
  50245. + rbuf_len = RBUF_LEN;
  50246. + }
  50247. + }
  50248. +
  50249. + break;
  50250. +
  50251. + /* this endpoint is normally active while we're configured */
  50252. + case -ECONNABORTED: /* hardware forced ep reset */
  50253. + case -ECONNRESET: /* request dequeued */
  50254. + case -ESHUTDOWN: /* disconnect from host */
  50255. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  50256. + req->actual, req->length);
  50257. + if (ep == dev->out_ep)
  50258. + check_read_data (dev, ep, req);
  50259. + free_ep_req (ep, req);
  50260. + return;
  50261. +
  50262. + case -EOVERFLOW: /* buffer overrun on read means that
  50263. + * we didn't provide a big enough
  50264. + * buffer.
  50265. + */
  50266. + default:
  50267. +#if 1
  50268. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  50269. + status, req->actual, req->length);
  50270. +#endif
  50271. + case -EREMOTEIO: /* short read */
  50272. + break;
  50273. + }
  50274. +
  50275. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  50276. + if (status) {
  50277. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  50278. + ep->name, req->length, status);
  50279. + usb_ep_set_halt (ep);
  50280. + /* FIXME recover later ... somehow */
  50281. + }
  50282. +}
  50283. +
  50284. +static struct usb_request *
  50285. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  50286. +{
  50287. + struct usb_request *req;
  50288. + int status;
  50289. +
  50290. + req = alloc_ep_req (ep, 512);
  50291. + if (!req)
  50292. + return NULL;
  50293. +
  50294. + req->complete = zero_isoc_complete;
  50295. +
  50296. + status = usb_ep_queue (ep, req, gfp_flags);
  50297. + if (status) {
  50298. + struct zero_dev *dev = ep->driver_data;
  50299. +
  50300. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  50301. + free_ep_req (ep, req);
  50302. + req = NULL;
  50303. + }
  50304. +
  50305. + return req;
  50306. +}
  50307. +
  50308. +/* change our operational config. this code must agree with the code
  50309. + * that returns config descriptors, and altsetting code.
  50310. + *
  50311. + * it's also responsible for power management interactions. some
  50312. + * configurations might not work with our current power sources.
  50313. + *
  50314. + * note that some device controller hardware will constrain what this
  50315. + * code can do, perhaps by disallowing more than one configuration or
  50316. + * by limiting configuration choices (like the pxa2xx).
  50317. + */
  50318. +static int
  50319. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  50320. +{
  50321. + int result = 0;
  50322. + struct usb_gadget *gadget = dev->gadget;
  50323. + const struct usb_endpoint_descriptor *d;
  50324. + struct usb_ep *ep;
  50325. +
  50326. + if (number == dev->config)
  50327. + return 0;
  50328. +
  50329. + zero_reset_config (dev);
  50330. +
  50331. + gadget_for_each_ep (ep, gadget) {
  50332. +
  50333. + if (strcmp (ep->name, "ep4") == 0) {
  50334. +
  50335. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  50336. + result = usb_ep_enable (ep, d);
  50337. +
  50338. + if (result == 0) {
  50339. + ep->driver_data = dev;
  50340. + dev->in_ep = ep;
  50341. +
  50342. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  50343. +
  50344. + dev->in_ep = ep;
  50345. + continue;
  50346. + }
  50347. +
  50348. + usb_ep_disable (ep);
  50349. + result = -EIO;
  50350. + }
  50351. + }
  50352. +
  50353. + }
  50354. +
  50355. + dev->config = number;
  50356. + return result;
  50357. +}
  50358. +
  50359. +/*-------------------------------------------------------------------------*/
  50360. +
  50361. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  50362. +{
  50363. + if (req->status || req->actual != req->length)
  50364. + DBG ((struct zero_dev *) ep->driver_data,
  50365. + "setup complete --> %d, %d/%d\n",
  50366. + req->status, req->actual, req->length);
  50367. +}
  50368. +
  50369. +/*
  50370. + * The setup() callback implements all the ep0 functionality that's
  50371. + * not handled lower down, in hardware or the hardware driver (like
  50372. + * device and endpoint feature flags, and their status). It's all
  50373. + * housekeeping for the gadget function we're implementing. Most of
  50374. + * the work is in config-specific setup.
  50375. + */
  50376. +static int
  50377. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  50378. +{
  50379. + struct zero_dev *dev = get_gadget_data (gadget);
  50380. + struct usb_request *req = dev->req;
  50381. + int value = -EOPNOTSUPP;
  50382. +
  50383. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  50384. + * but config change events will reconfigure hardware.
  50385. + */
  50386. + req->zero = 0;
  50387. + switch (ctrl->bRequest) {
  50388. +
  50389. + case USB_REQ_GET_DESCRIPTOR:
  50390. +
  50391. + switch (ctrl->wValue >> 8) {
  50392. +
  50393. + case USB_DT_DEVICE:
  50394. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  50395. + memcpy (req->buf, &device_desc, value);
  50396. + break;
  50397. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50398. + case USB_DT_DEVICE_QUALIFIER:
  50399. + if (!gadget->is_dualspeed)
  50400. + break;
  50401. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  50402. + memcpy (req->buf, &dev_qualifier, value);
  50403. + break;
  50404. +
  50405. + case USB_DT_OTHER_SPEED_CONFIG:
  50406. + if (!gadget->is_dualspeed)
  50407. + break;
  50408. + // FALLTHROUGH
  50409. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  50410. + case USB_DT_CONFIG:
  50411. + value = config_buf (gadget, req->buf,
  50412. + ctrl->wValue >> 8,
  50413. + ctrl->wValue & 0xff);
  50414. + if (value >= 0)
  50415. + value = min (ctrl->wLength, (u16) value);
  50416. + break;
  50417. +
  50418. + case USB_DT_STRING:
  50419. + /* wIndex == language code.
  50420. + * this driver only handles one language, you can
  50421. + * add string tables for other languages, using
  50422. + * any UTF-8 characters
  50423. + */
  50424. + value = usb_gadget_get_string (&stringtab,
  50425. + ctrl->wValue & 0xff, req->buf);
  50426. + if (value >= 0) {
  50427. + value = min (ctrl->wLength, (u16) value);
  50428. + }
  50429. + break;
  50430. + }
  50431. + break;
  50432. +
  50433. + /* currently two configs, two speeds */
  50434. + case USB_REQ_SET_CONFIGURATION:
  50435. + if (ctrl->bRequestType != 0)
  50436. + goto unknown;
  50437. +
  50438. + spin_lock (&dev->lock);
  50439. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  50440. + spin_unlock (&dev->lock);
  50441. + break;
  50442. + case USB_REQ_GET_CONFIGURATION:
  50443. + if (ctrl->bRequestType != USB_DIR_IN)
  50444. + goto unknown;
  50445. + *(u8 *)req->buf = dev->config;
  50446. + value = min (ctrl->wLength, (u16) 1);
  50447. + break;
  50448. +
  50449. + /* until we add altsetting support, or other interfaces,
  50450. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  50451. + * and already killed pending endpoint I/O.
  50452. + */
  50453. + case USB_REQ_SET_INTERFACE:
  50454. +
  50455. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  50456. + goto unknown;
  50457. + spin_lock (&dev->lock);
  50458. + if (dev->config) {
  50459. + u8 config = dev->config;
  50460. +
  50461. + /* resets interface configuration, forgets about
  50462. + * previous transaction state (queued bufs, etc)
  50463. + * and re-inits endpoint state (toggle etc)
  50464. + * no response queued, just zero status == success.
  50465. + * if we had more than one interface we couldn't
  50466. + * use this "reset the config" shortcut.
  50467. + */
  50468. + zero_reset_config (dev);
  50469. + zero_set_config (dev, config, GFP_ATOMIC);
  50470. + value = 0;
  50471. + }
  50472. + spin_unlock (&dev->lock);
  50473. + break;
  50474. + case USB_REQ_GET_INTERFACE:
  50475. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  50476. + value = ctrl->wLength;
  50477. + break;
  50478. + }
  50479. + else {
  50480. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  50481. + goto unknown;
  50482. + if (!dev->config)
  50483. + break;
  50484. + if (ctrl->wIndex != 0) {
  50485. + value = -EDOM;
  50486. + break;
  50487. + }
  50488. + *(u8 *)req->buf = 0;
  50489. + value = min (ctrl->wLength, (u16) 1);
  50490. + }
  50491. + break;
  50492. +
  50493. + /*
  50494. + * These are the same vendor-specific requests supported by
  50495. + * Intel's USB 2.0 compliance test devices. We exceed that
  50496. + * device spec by allowing multiple-packet requests.
  50497. + */
  50498. + case 0x5b: /* control WRITE test -- fill the buffer */
  50499. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  50500. + goto unknown;
  50501. + if (ctrl->wValue || ctrl->wIndex)
  50502. + break;
  50503. + /* just read that many bytes into the buffer */
  50504. + if (ctrl->wLength > USB_BUFSIZ)
  50505. + break;
  50506. + value = ctrl->wLength;
  50507. + break;
  50508. + case 0x5c: /* control READ test -- return the buffer */
  50509. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  50510. + goto unknown;
  50511. + if (ctrl->wValue || ctrl->wIndex)
  50512. + break;
  50513. + /* expect those bytes are still in the buffer; send back */
  50514. + if (ctrl->wLength > USB_BUFSIZ
  50515. + || ctrl->wLength != req->length)
  50516. + break;
  50517. + value = ctrl->wLength;
  50518. + break;
  50519. +
  50520. + case 0x01: // SET_CUR
  50521. + case 0x02:
  50522. + case 0x03:
  50523. + case 0x04:
  50524. + case 0x05:
  50525. + value = ctrl->wLength;
  50526. + break;
  50527. + case 0x81:
  50528. + switch (ctrl->wValue) {
  50529. + case 0x0201:
  50530. + case 0x0202:
  50531. + ((u8*)req->buf)[0] = 0x00;
  50532. + ((u8*)req->buf)[1] = 0xe3;
  50533. + break;
  50534. + case 0x0300:
  50535. + case 0x0500:
  50536. + ((u8*)req->buf)[0] = 0x00;
  50537. + break;
  50538. + }
  50539. + //((u8*)req->buf)[0] = 0x81;
  50540. + //((u8*)req->buf)[1] = 0x81;
  50541. + value = ctrl->wLength;
  50542. + break;
  50543. + case 0x82:
  50544. + switch (ctrl->wValue) {
  50545. + case 0x0201:
  50546. + case 0x0202:
  50547. + ((u8*)req->buf)[0] = 0x00;
  50548. + ((u8*)req->buf)[1] = 0xc3;
  50549. + break;
  50550. + case 0x0300:
  50551. + case 0x0500:
  50552. + ((u8*)req->buf)[0] = 0x00;
  50553. + break;
  50554. + }
  50555. + //((u8*)req->buf)[0] = 0x82;
  50556. + //((u8*)req->buf)[1] = 0x82;
  50557. + value = ctrl->wLength;
  50558. + break;
  50559. + case 0x83:
  50560. + switch (ctrl->wValue) {
  50561. + case 0x0201:
  50562. + case 0x0202:
  50563. + ((u8*)req->buf)[0] = 0x00;
  50564. + ((u8*)req->buf)[1] = 0x00;
  50565. + break;
  50566. + case 0x0300:
  50567. + ((u8*)req->buf)[0] = 0x60;
  50568. + break;
  50569. + case 0x0500:
  50570. + ((u8*)req->buf)[0] = 0x18;
  50571. + break;
  50572. + }
  50573. + //((u8*)req->buf)[0] = 0x83;
  50574. + //((u8*)req->buf)[1] = 0x83;
  50575. + value = ctrl->wLength;
  50576. + break;
  50577. + case 0x84:
  50578. + switch (ctrl->wValue) {
  50579. + case 0x0201:
  50580. + case 0x0202:
  50581. + ((u8*)req->buf)[0] = 0x00;
  50582. + ((u8*)req->buf)[1] = 0x01;
  50583. + break;
  50584. + case 0x0300:
  50585. + case 0x0500:
  50586. + ((u8*)req->buf)[0] = 0x08;
  50587. + break;
  50588. + }
  50589. + //((u8*)req->buf)[0] = 0x84;
  50590. + //((u8*)req->buf)[1] = 0x84;
  50591. + value = ctrl->wLength;
  50592. + break;
  50593. + case 0x85:
  50594. + ((u8*)req->buf)[0] = 0x85;
  50595. + ((u8*)req->buf)[1] = 0x85;
  50596. + value = ctrl->wLength;
  50597. + break;
  50598. +
  50599. +
  50600. + default:
  50601. +unknown:
  50602. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  50603. + ctrl->bRequestType, ctrl->bRequest,
  50604. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  50605. + }
  50606. +
  50607. + /* respond with data transfer before status phase? */
  50608. + if (value >= 0) {
  50609. + req->length = value;
  50610. + req->zero = value < ctrl->wLength
  50611. + && (value % gadget->ep0->maxpacket) == 0;
  50612. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  50613. + if (value < 0) {
  50614. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  50615. + req->status = 0;
  50616. + zero_setup_complete (gadget->ep0, req);
  50617. + }
  50618. + }
  50619. +
  50620. + /* device either stalls (value < 0) or reports success */
  50621. + return value;
  50622. +}
  50623. +
  50624. +static void
  50625. +zero_disconnect (struct usb_gadget *gadget)
  50626. +{
  50627. + struct zero_dev *dev = get_gadget_data (gadget);
  50628. + unsigned long flags;
  50629. +
  50630. + spin_lock_irqsave (&dev->lock, flags);
  50631. + zero_reset_config (dev);
  50632. +
  50633. + /* a more significant application might have some non-usb
  50634. + * activities to quiesce here, saving resources like power
  50635. + * or pushing the notification up a network stack.
  50636. + */
  50637. + spin_unlock_irqrestore (&dev->lock, flags);
  50638. +
  50639. + /* next we may get setup() calls to enumerate new connections;
  50640. + * or an unbind() during shutdown (including removing module).
  50641. + */
  50642. +}
  50643. +
  50644. +static void
  50645. +zero_autoresume (unsigned long _dev)
  50646. +{
  50647. + struct zero_dev *dev = (struct zero_dev *) _dev;
  50648. + int status;
  50649. +
  50650. + /* normally the host would be woken up for something
  50651. + * more significant than just a timer firing...
  50652. + */
  50653. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  50654. + status = usb_gadget_wakeup (dev->gadget);
  50655. + DBG (dev, "wakeup --> %d\n", status);
  50656. + }
  50657. +}
  50658. +
  50659. +/*-------------------------------------------------------------------------*/
  50660. +
  50661. +static void
  50662. +zero_unbind (struct usb_gadget *gadget)
  50663. +{
  50664. + struct zero_dev *dev = get_gadget_data (gadget);
  50665. +
  50666. + DBG (dev, "unbind\n");
  50667. +
  50668. + /* we've already been disconnected ... no i/o is active */
  50669. + if (dev->req)
  50670. + free_ep_req (gadget->ep0, dev->req);
  50671. + del_timer_sync (&dev->resume);
  50672. + kfree (dev);
  50673. + set_gadget_data (gadget, NULL);
  50674. +}
  50675. +
  50676. +static int
  50677. +zero_bind (struct usb_gadget *gadget)
  50678. +{
  50679. + struct zero_dev *dev;
  50680. + //struct usb_ep *ep;
  50681. +
  50682. + printk("binding\n");
  50683. + /*
  50684. + * DRIVER POLICY CHOICE: you may want to do this differently.
  50685. + * One thing to avoid is reusing a bcdDevice revision code
  50686. + * with different host-visible configurations or behavior
  50687. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  50688. + */
  50689. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  50690. +
  50691. +
  50692. + /* ok, we made sense of the hardware ... */
  50693. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  50694. + if (!dev)
  50695. + return -ENOMEM;
  50696. + memset (dev, 0, sizeof *dev);
  50697. + spin_lock_init (&dev->lock);
  50698. + dev->gadget = gadget;
  50699. + set_gadget_data (gadget, dev);
  50700. +
  50701. + /* preallocate control response and buffer */
  50702. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  50703. + if (!dev->req)
  50704. + goto enomem;
  50705. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  50706. + &dev->req->dma, GFP_KERNEL);
  50707. + if (!dev->req->buf)
  50708. + goto enomem;
  50709. +
  50710. + dev->req->complete = zero_setup_complete;
  50711. +
  50712. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  50713. +
  50714. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50715. + /* assume ep0 uses the same value for both speeds ... */
  50716. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  50717. +
  50718. + /* and that all endpoints are dual-speed */
  50719. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  50720. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  50721. +#endif
  50722. +
  50723. + usb_gadget_set_selfpowered (gadget);
  50724. +
  50725. + init_timer (&dev->resume);
  50726. + dev->resume.function = zero_autoresume;
  50727. + dev->resume.data = (unsigned long) dev;
  50728. +
  50729. + gadget->ep0->driver_data = dev;
  50730. +
  50731. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  50732. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  50733. + EP_OUT_NAME, EP_IN_NAME);
  50734. +
  50735. + snprintf (manufacturer, sizeof manufacturer,
  50736. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  50737. + gadget->name);
  50738. +
  50739. + return 0;
  50740. +
  50741. +enomem:
  50742. + zero_unbind (gadget);
  50743. + return -ENOMEM;
  50744. +}
  50745. +
  50746. +/*-------------------------------------------------------------------------*/
  50747. +
  50748. +static void
  50749. +zero_suspend (struct usb_gadget *gadget)
  50750. +{
  50751. + struct zero_dev *dev = get_gadget_data (gadget);
  50752. +
  50753. + if (gadget->speed == USB_SPEED_UNKNOWN)
  50754. + return;
  50755. +
  50756. + if (autoresume) {
  50757. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  50758. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  50759. + } else
  50760. + DBG (dev, "suspend\n");
  50761. +}
  50762. +
  50763. +static void
  50764. +zero_resume (struct usb_gadget *gadget)
  50765. +{
  50766. + struct zero_dev *dev = get_gadget_data (gadget);
  50767. +
  50768. + DBG (dev, "resume\n");
  50769. + del_timer (&dev->resume);
  50770. +}
  50771. +
  50772. +
  50773. +/*-------------------------------------------------------------------------*/
  50774. +
  50775. +static struct usb_gadget_driver zero_driver = {
  50776. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50777. + .speed = USB_SPEED_HIGH,
  50778. +#else
  50779. + .speed = USB_SPEED_FULL,
  50780. +#endif
  50781. + .function = (char *) longname,
  50782. + .bind = zero_bind,
  50783. + .unbind = zero_unbind,
  50784. +
  50785. + .setup = zero_setup,
  50786. + .disconnect = zero_disconnect,
  50787. +
  50788. + .suspend = zero_suspend,
  50789. + .resume = zero_resume,
  50790. +
  50791. + .driver = {
  50792. + .name = (char *) shortname,
  50793. + // .shutdown = ...
  50794. + // .suspend = ...
  50795. + // .resume = ...
  50796. + },
  50797. +};
  50798. +
  50799. +MODULE_AUTHOR ("David Brownell");
  50800. +MODULE_LICENSE ("Dual BSD/GPL");
  50801. +
  50802. +static struct proc_dir_entry *pdir, *pfile;
  50803. +
  50804. +static int isoc_read_data (char *page, char **start,
  50805. + off_t off, int count,
  50806. + int *eof, void *data)
  50807. +{
  50808. + int i;
  50809. + static int c = 0;
  50810. + static int done = 0;
  50811. + static int s = 0;
  50812. +
  50813. +/*
  50814. + printk ("\ncount: %d\n", count);
  50815. + printk ("rbuf_start: %d\n", rbuf_start);
  50816. + printk ("rbuf_len: %d\n", rbuf_len);
  50817. + printk ("off: %d\n", off);
  50818. + printk ("start: %p\n\n", *start);
  50819. +*/
  50820. + if (done) {
  50821. + c = 0;
  50822. + done = 0;
  50823. + *eof = 1;
  50824. + return 0;
  50825. + }
  50826. +
  50827. + if (c == 0) {
  50828. + if (rbuf_len == RBUF_LEN)
  50829. + s = rbuf_start;
  50830. + else s = 0;
  50831. + }
  50832. +
  50833. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  50834. + page[i] = rbuf[(c+s) % RBUF_LEN];
  50835. + }
  50836. + *start = page;
  50837. +
  50838. + if (c >= rbuf_len) {
  50839. + *eof = 1;
  50840. + done = 1;
  50841. + }
  50842. +
  50843. +
  50844. + return i;
  50845. +}
  50846. +
  50847. +static int __init init (void)
  50848. +{
  50849. +
  50850. + int retval = 0;
  50851. +
  50852. + pdir = proc_mkdir("isoc_test", NULL);
  50853. + if(pdir == NULL) {
  50854. + retval = -ENOMEM;
  50855. + printk("Error creating dir\n");
  50856. + goto done;
  50857. + }
  50858. + pdir->owner = THIS_MODULE;
  50859. +
  50860. + pfile = create_proc_read_entry("isoc_data",
  50861. + 0444, pdir,
  50862. + isoc_read_data,
  50863. + NULL);
  50864. + if (pfile == NULL) {
  50865. + retval = -ENOMEM;
  50866. + printk("Error creating file\n");
  50867. + goto no_file;
  50868. + }
  50869. + pfile->owner = THIS_MODULE;
  50870. +
  50871. + return usb_gadget_register_driver (&zero_driver);
  50872. +
  50873. + no_file:
  50874. + remove_proc_entry("isoc_data", NULL);
  50875. + done:
  50876. + return retval;
  50877. +}
  50878. +module_init (init);
  50879. +
  50880. +static void __exit cleanup (void)
  50881. +{
  50882. +
  50883. + usb_gadget_unregister_driver (&zero_driver);
  50884. +
  50885. + remove_proc_entry("isoc_data", pdir);
  50886. + remove_proc_entry("isoc_test", NULL);
  50887. +}
  50888. +module_exit (cleanup);
  50889. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_cfi_common.h linux-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  50890. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_cfi_common.h 1970-01-01 01:00:00.000000000 +0100
  50891. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h 2014-04-24 15:35:04.169565731 +0200
  50892. @@ -0,0 +1,142 @@
  50893. +/* ==========================================================================
  50894. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50895. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50896. + * otherwise expressly agreed to in writing between Synopsys and you.
  50897. + *
  50898. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50899. + * any End User Software License Agreement or Agreement for Licensed Product
  50900. + * with Synopsys or any supplement thereto. You are permitted to use and
  50901. + * redistribute this Software in source and binary forms, with or without
  50902. + * modification, provided that redistributions of source code must retain this
  50903. + * notice. You may not view, use, disclose, copy or distribute this file or
  50904. + * any information contained herein except pursuant to this license grant from
  50905. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50906. + * below, then you are not authorized to use the Software.
  50907. + *
  50908. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50909. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50910. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50911. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50912. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50913. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50914. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50915. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50916. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50917. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50918. + * DAMAGE.
  50919. + * ========================================================================== */
  50920. +
  50921. +#if !defined(__DWC_CFI_COMMON_H__)
  50922. +#define __DWC_CFI_COMMON_H__
  50923. +
  50924. +//#include <linux/types.h>
  50925. +
  50926. +/**
  50927. + * @file
  50928. + *
  50929. + * This file contains the CFI specific common constants, interfaces
  50930. + * (functions and macros) and structures for Linux. No PCD specific
  50931. + * data structure or definition is to be included in this file.
  50932. + *
  50933. + */
  50934. +
  50935. +/** This is a request for all Core Features */
  50936. +#define VEN_CORE_GET_FEATURES 0xB1
  50937. +
  50938. +/** This is a request to get the value of a specific Core Feature */
  50939. +#define VEN_CORE_GET_FEATURE 0xB2
  50940. +
  50941. +/** This command allows the host to set the value of a specific Core Feature */
  50942. +#define VEN_CORE_SET_FEATURE 0xB3
  50943. +
  50944. +/** This command allows the host to set the default values of
  50945. + * either all or any specific Core Feature
  50946. + */
  50947. +#define VEN_CORE_RESET_FEATURES 0xB4
  50948. +
  50949. +/** This command forces the PCD to write the deferred values of a Core Features */
  50950. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  50951. +
  50952. +/** This request reads a DWORD value from a register at the specified offset */
  50953. +#define VEN_CORE_READ_REGISTER 0xB6
  50954. +
  50955. +/** This request writes a DWORD value into a register at the specified offset */
  50956. +#define VEN_CORE_WRITE_REGISTER 0xB7
  50957. +
  50958. +/** This structure is the header of the Core Features dataset returned to
  50959. + * the Host
  50960. + */
  50961. +struct cfi_all_features_header {
  50962. +/** The features header structure length is */
  50963. +#define CFI_ALL_FEATURES_HDR_LEN 8
  50964. + /**
  50965. + * The total length of the features dataset returned to the Host
  50966. + */
  50967. + uint16_t wTotalLen;
  50968. +
  50969. + /**
  50970. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  50971. + * This field identifies the version of the CFI Specification with which
  50972. + * the device is compliant.
  50973. + */
  50974. + uint16_t wVersion;
  50975. +
  50976. + /** The ID of the Core */
  50977. + uint16_t wCoreID;
  50978. +#define CFI_CORE_ID_UDC 1
  50979. +#define CFI_CORE_ID_OTG 2
  50980. +#define CFI_CORE_ID_WUDEV 3
  50981. +
  50982. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  50983. + uint16_t wNumFeatures;
  50984. +} UPACKED;
  50985. +
  50986. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  50987. +
  50988. +/** This structure is a header of the Core Feature descriptor dataset returned to
  50989. + * the Host after the VEN_CORE_GET_FEATURES request
  50990. + */
  50991. +struct cfi_feature_desc_header {
  50992. +#define CFI_FEATURE_DESC_HDR_LEN 8
  50993. +
  50994. + /** The feature ID */
  50995. + uint16_t wFeatureID;
  50996. +
  50997. + /** Length of this feature descriptor in bytes - including the
  50998. + * length of the feature name string
  50999. + */
  51000. + uint16_t wLength;
  51001. +
  51002. + /** The data length of this feature in bytes */
  51003. + uint16_t wDataLength;
  51004. +
  51005. + /**
  51006. + * Attributes of this features
  51007. + * D0: Access rights
  51008. + * 0 - Read/Write
  51009. + * 1 - Read only
  51010. + */
  51011. + uint8_t bmAttributes;
  51012. +#define CFI_FEATURE_ATTR_RO 1
  51013. +#define CFI_FEATURE_ATTR_RW 0
  51014. +
  51015. + /** Length of the feature name in bytes */
  51016. + uint8_t bNameLen;
  51017. +
  51018. + /** The feature name buffer */
  51019. + //uint8_t *name;
  51020. +} UPACKED;
  51021. +
  51022. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  51023. +
  51024. +/**
  51025. + * This structure describes a NULL terminated string referenced by its id field.
  51026. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  51027. + */
  51028. +struct cfi_string {
  51029. + uint16_t id;
  51030. + const uint8_t *s;
  51031. +};
  51032. +typedef struct cfi_string cfi_string_t;
  51033. +
  51034. +#endif
  51035. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_adp.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  51036. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_adp.c 1970-01-01 01:00:00.000000000 +0100
  51037. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c 2014-04-24 15:35:04.169565731 +0200
  51038. @@ -0,0 +1,854 @@
  51039. +/* ==========================================================================
  51040. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  51041. + * $Revision: #12 $
  51042. + * $Date: 2011/10/26 $
  51043. + * $Change: 1873028 $
  51044. + *
  51045. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51046. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51047. + * otherwise expressly agreed to in writing between Synopsys and you.
  51048. + *
  51049. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51050. + * any End User Software License Agreement or Agreement for Licensed Product
  51051. + * with Synopsys or any supplement thereto. You are permitted to use and
  51052. + * redistribute this Software in source and binary forms, with or without
  51053. + * modification, provided that redistributions of source code must retain this
  51054. + * notice. You may not view, use, disclose, copy or distribute this file or
  51055. + * any information contained herein except pursuant to this license grant from
  51056. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51057. + * below, then you are not authorized to use the Software.
  51058. + *
  51059. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51060. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51061. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51062. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51063. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51064. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51065. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51066. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51067. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51068. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51069. + * DAMAGE.
  51070. + * ========================================================================== */
  51071. +
  51072. +#include "dwc_os.h"
  51073. +#include "dwc_otg_regs.h"
  51074. +#include "dwc_otg_cil.h"
  51075. +#include "dwc_otg_adp.h"
  51076. +
  51077. +/** @file
  51078. + *
  51079. + * This file contains the most of the Attach Detect Protocol implementation for
  51080. + * the driver to support OTG Rev2.0.
  51081. + *
  51082. + */
  51083. +
  51084. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  51085. +{
  51086. + adpctl_data_t adpctl;
  51087. +
  51088. + adpctl.d32 = value;
  51089. + adpctl.b.ar = 0x2;
  51090. +
  51091. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  51092. +
  51093. + while (adpctl.b.ar) {
  51094. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  51095. + }
  51096. +
  51097. +}
  51098. +
  51099. +/**
  51100. + * Function is called to read ADP registers
  51101. + */
  51102. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  51103. +{
  51104. + adpctl_data_t adpctl;
  51105. +
  51106. + adpctl.d32 = 0;
  51107. + adpctl.b.ar = 0x1;
  51108. +
  51109. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  51110. +
  51111. + while (adpctl.b.ar) {
  51112. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  51113. + }
  51114. +
  51115. + return adpctl.d32;
  51116. +}
  51117. +
  51118. +/**
  51119. + * Function is called to read ADPCTL register and filter Write-clear bits
  51120. + */
  51121. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  51122. +{
  51123. + adpctl_data_t adpctl;
  51124. +
  51125. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51126. + adpctl.b.adp_tmout_int = 0;
  51127. + adpctl.b.adp_prb_int = 0;
  51128. + adpctl.b.adp_tmout_int = 0;
  51129. +
  51130. + return adpctl.d32;
  51131. +}
  51132. +
  51133. +/**
  51134. + * Function is called to write ADP registers
  51135. + */
  51136. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  51137. + uint32_t set)
  51138. +{
  51139. + dwc_otg_adp_write_reg(core_if,
  51140. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  51141. +}
  51142. +
  51143. +static void adp_sense_timeout(void *ptr)
  51144. +{
  51145. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  51146. + core_if->adp.sense_timer_started = 0;
  51147. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  51148. + if (core_if->adp_enable) {
  51149. + dwc_otg_adp_sense_stop(core_if);
  51150. + dwc_otg_adp_probe_start(core_if);
  51151. + }
  51152. +}
  51153. +
  51154. +/**
  51155. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  51156. + */
  51157. +static void adp_vbuson_timeout(void *ptr)
  51158. +{
  51159. + gpwrdn_data_t gpwrdn;
  51160. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  51161. + hprt0_data_t hprt0 = {.d32 = 0 };
  51162. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  51163. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  51164. + if (core_if) {
  51165. + core_if->adp.vbuson_timer_started = 0;
  51166. + /* Turn off vbus */
  51167. + hprt0.b.prtpwr = 1;
  51168. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  51169. + gpwrdn.d32 = 0;
  51170. +
  51171. + /* Power off the core */
  51172. + if (core_if->power_down == 2) {
  51173. + /* Enable Wakeup Logic */
  51174. +// gpwrdn.b.wkupactiv = 1;
  51175. + gpwrdn.b.pmuactv = 0;
  51176. + gpwrdn.b.pwrdnrstn = 1;
  51177. + gpwrdn.b.pwrdnclmp = 1;
  51178. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51179. + gpwrdn.d32);
  51180. +
  51181. + /* Suspend the Phy Clock */
  51182. + pcgcctl.b.stoppclk = 1;
  51183. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  51184. +
  51185. + /* Switch on VDD */
  51186. +// gpwrdn.b.wkupactiv = 1;
  51187. + gpwrdn.b.pmuactv = 1;
  51188. + gpwrdn.b.pwrdnrstn = 1;
  51189. + gpwrdn.b.pwrdnclmp = 1;
  51190. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51191. + gpwrdn.d32);
  51192. + } else {
  51193. + /* Enable Power Down Logic */
  51194. + gpwrdn.b.pmuintsel = 1;
  51195. + gpwrdn.b.pmuactv = 1;
  51196. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51197. + }
  51198. +
  51199. + /* Power off the core */
  51200. + if (core_if->power_down == 2) {
  51201. + gpwrdn.d32 = 0;
  51202. + gpwrdn.b.pwrdnswtch = 1;
  51203. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  51204. + gpwrdn.d32, 0);
  51205. + }
  51206. +
  51207. + /* Unmask SRP detected interrupt from Power Down Logic */
  51208. + gpwrdn.d32 = 0;
  51209. + gpwrdn.b.srp_det_msk = 1;
  51210. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51211. +
  51212. + dwc_otg_adp_probe_start(core_if);
  51213. + dwc_otg_dump_global_registers(core_if);
  51214. + dwc_otg_dump_host_registers(core_if);
  51215. + }
  51216. +
  51217. +}
  51218. +
  51219. +/**
  51220. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  51221. + * not asserted within 1.1 seconds.
  51222. + *
  51223. + * @param core_if the pointer to core_if strucure.
  51224. + */
  51225. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  51226. +{
  51227. + core_if->adp.vbuson_timer_started = 1;
  51228. + if (core_if->adp.vbuson_timer)
  51229. + {
  51230. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  51231. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  51232. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  51233. + } else {
  51234. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  51235. + }
  51236. +}
  51237. +
  51238. +#if 0
  51239. +/**
  51240. + * Masks all DWC OTG core interrupts
  51241. + *
  51242. + */
  51243. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  51244. +{
  51245. + int i;
  51246. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  51247. +
  51248. + /* Mask Host Interrupts */
  51249. +
  51250. + /* Clear and disable HCINTs */
  51251. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  51252. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  51253. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  51254. +
  51255. + }
  51256. +
  51257. + /* Clear and disable HAINT */
  51258. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  51259. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  51260. +
  51261. + /* Mask Device Interrupts */
  51262. + if (!core_if->multiproc_int_enable) {
  51263. + /* Clear and disable IN Endpoint interrupts */
  51264. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  51265. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  51266. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  51267. + diepint, 0xFFFFFFFF);
  51268. + }
  51269. +
  51270. + /* Clear and disable OUT Endpoint interrupts */
  51271. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  51272. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  51273. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  51274. + doepint, 0xFFFFFFFF);
  51275. + }
  51276. +
  51277. + /* Clear and disable DAINT */
  51278. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  51279. + 0xFFFFFFFF);
  51280. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  51281. + } else {
  51282. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  51283. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  51284. + diepeachintmsk[i], 0);
  51285. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  51286. + diepint, 0xFFFFFFFF);
  51287. + }
  51288. +
  51289. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  51290. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  51291. + doepeachintmsk[i], 0);
  51292. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  51293. + doepint, 0xFFFFFFFF);
  51294. + }
  51295. +
  51296. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  51297. + 0);
  51298. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  51299. + 0xFFFFFFFF);
  51300. +
  51301. + }
  51302. +
  51303. + /* Disable interrupts */
  51304. + ahbcfg.b.glblintrmsk = 1;
  51305. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  51306. +
  51307. + /* Disable all interrupts. */
  51308. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  51309. +
  51310. + /* Clear any pending interrupts */
  51311. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  51312. +
  51313. + /* Clear any pending OTG Interrupts */
  51314. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  51315. +}
  51316. +
  51317. +/**
  51318. + * Unmask Port Connection Detected interrupt
  51319. + *
  51320. + */
  51321. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  51322. +{
  51323. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  51324. +
  51325. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  51326. +}
  51327. +#endif
  51328. +
  51329. +/**
  51330. + * Starts the ADP Probing
  51331. + *
  51332. + * @param core_if the pointer to core_if structure.
  51333. + */
  51334. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  51335. +{
  51336. +
  51337. + adpctl_data_t adpctl = {.d32 = 0};
  51338. + gpwrdn_data_t gpwrdn;
  51339. +#if 0
  51340. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  51341. + .b.adp_sns_int = 1, b.adp_tmout_int};
  51342. +#endif
  51343. + dwc_otg_disable_global_interrupts(core_if);
  51344. + DWC_PRINTF("ADP Probe Start\n");
  51345. + core_if->adp.probe_enabled = 1;
  51346. +
  51347. + adpctl.b.adpres = 1;
  51348. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51349. +
  51350. + while (adpctl.b.adpres) {
  51351. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51352. + }
  51353. +
  51354. + adpctl.d32 = 0;
  51355. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51356. +
  51357. + /* In Host mode unmask SRP detected interrupt */
  51358. + gpwrdn.d32 = 0;
  51359. + gpwrdn.b.sts_chngint_msk = 1;
  51360. + if (!gpwrdn.b.idsts) {
  51361. + gpwrdn.b.srp_det_msk = 1;
  51362. + }
  51363. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51364. +
  51365. + adpctl.b.adp_tmout_int_msk = 1;
  51366. + adpctl.b.adp_prb_int_msk = 1;
  51367. + adpctl.b.prb_dschg = 1;
  51368. + adpctl.b.prb_delta = 1;
  51369. + adpctl.b.prb_per = 1;
  51370. + adpctl.b.adpen = 1;
  51371. + adpctl.b.enaprb = 1;
  51372. +
  51373. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51374. + DWC_PRINTF("ADP Probe Finish\n");
  51375. + return 0;
  51376. +}
  51377. +
  51378. +/**
  51379. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  51380. + * within 3 seconds.
  51381. + *
  51382. + * @param core_if the pointer to core_if strucure.
  51383. + */
  51384. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  51385. +{
  51386. + core_if->adp.sense_timer_started = 1;
  51387. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  51388. +}
  51389. +
  51390. +/**
  51391. + * Starts the ADP Sense
  51392. + *
  51393. + * @param core_if the pointer to core_if strucure.
  51394. + */
  51395. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  51396. +{
  51397. + adpctl_data_t adpctl;
  51398. +
  51399. + DWC_PRINTF("ADP Sense Start\n");
  51400. +
  51401. + /* Unmask ADP sense interrupt and mask all other from the core */
  51402. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  51403. + adpctl.b.adp_sns_int_msk = 1;
  51404. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51405. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  51406. +
  51407. + /* Set ADP reset bit*/
  51408. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  51409. + adpctl.b.adpres = 1;
  51410. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51411. +
  51412. + while (adpctl.b.adpres) {
  51413. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51414. + }
  51415. +
  51416. + adpctl.b.adpres = 0;
  51417. + adpctl.b.adpen = 1;
  51418. + adpctl.b.enasns = 1;
  51419. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51420. +
  51421. + dwc_otg_adp_sense_timer_start(core_if);
  51422. +
  51423. + return 0;
  51424. +}
  51425. +
  51426. +/**
  51427. + * Stops the ADP Probing
  51428. + *
  51429. + * @param core_if the pointer to core_if strucure.
  51430. + */
  51431. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  51432. +{
  51433. +
  51434. + adpctl_data_t adpctl;
  51435. + DWC_PRINTF("Stop ADP probe\n");
  51436. + core_if->adp.probe_enabled = 0;
  51437. + core_if->adp.probe_counter = 0;
  51438. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51439. +
  51440. + adpctl.b.adpen = 0;
  51441. + adpctl.b.adp_prb_int = 1;
  51442. + adpctl.b.adp_tmout_int = 1;
  51443. + adpctl.b.adp_sns_int = 1;
  51444. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51445. +
  51446. + return 0;
  51447. +}
  51448. +
  51449. +/**
  51450. + * Stops the ADP Sensing
  51451. + *
  51452. + * @param core_if the pointer to core_if strucure.
  51453. + */
  51454. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  51455. +{
  51456. + adpctl_data_t adpctl;
  51457. +
  51458. + core_if->adp.sense_enabled = 0;
  51459. +
  51460. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  51461. + adpctl.b.enasns = 0;
  51462. + adpctl.b.adp_sns_int = 1;
  51463. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51464. +
  51465. + return 0;
  51466. +}
  51467. +
  51468. +/**
  51469. + * Called to turn on the VBUS after initial ADP probe in host mode.
  51470. + * If port power was already enabled in cil_hcd_start function then
  51471. + * only schedule a timer.
  51472. + *
  51473. + * @param core_if the pointer to core_if structure.
  51474. + */
  51475. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  51476. +{
  51477. + hprt0_data_t hprt0 = {.d32 = 0 };
  51478. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  51479. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  51480. +
  51481. + if (hprt0.b.prtpwr == 0) {
  51482. + hprt0.b.prtpwr = 1;
  51483. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  51484. + }
  51485. +
  51486. + dwc_otg_adp_vbuson_timer_start(core_if);
  51487. +}
  51488. +
  51489. +/**
  51490. + * Called right after driver is loaded
  51491. + * to perform initial actions for ADP
  51492. + *
  51493. + * @param core_if the pointer to core_if structure.
  51494. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  51495. + */
  51496. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  51497. +{
  51498. + gpwrdn_data_t gpwrdn;
  51499. +
  51500. + DWC_PRINTF("ADP Initial Start\n");
  51501. + core_if->adp.adp_started = 1;
  51502. +
  51503. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  51504. + dwc_otg_disable_global_interrupts(core_if);
  51505. + if (is_host) {
  51506. + DWC_PRINTF("HOST MODE\n");
  51507. + /* Enable Power Down Logic Interrupt*/
  51508. + gpwrdn.d32 = 0;
  51509. + gpwrdn.b.pmuintsel = 1;
  51510. + gpwrdn.b.pmuactv = 1;
  51511. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51512. + /* Initialize first ADP probe to obtain Ramp Time value */
  51513. + core_if->adp.initial_probe = 1;
  51514. + dwc_otg_adp_probe_start(core_if);
  51515. + } else {
  51516. + gotgctl_data_t gotgctl;
  51517. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  51518. + DWC_PRINTF("DEVICE MODE\n");
  51519. + if (gotgctl.b.bsesvld == 0) {
  51520. + /* Enable Power Down Logic Interrupt*/
  51521. + gpwrdn.d32 = 0;
  51522. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  51523. + gpwrdn.b.pmuintsel = 1;
  51524. + gpwrdn.b.pmuactv = 1;
  51525. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  51526. + core_if->adp.initial_probe = 1;
  51527. + dwc_otg_adp_probe_start(core_if);
  51528. + } else {
  51529. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  51530. + core_if->op_state = B_PERIPHERAL;
  51531. + dwc_otg_core_init(core_if);
  51532. + dwc_otg_enable_global_interrupts(core_if);
  51533. + cil_pcd_start(core_if);
  51534. + dwc_otg_dump_global_registers(core_if);
  51535. + dwc_otg_dump_dev_registers(core_if);
  51536. + }
  51537. + }
  51538. +}
  51539. +
  51540. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  51541. +{
  51542. + core_if->adp.adp_started = 0;
  51543. + core_if->adp.initial_probe = 0;
  51544. + core_if->adp.probe_timer_values[0] = -1;
  51545. + core_if->adp.probe_timer_values[1] = -1;
  51546. + core_if->adp.probe_enabled = 0;
  51547. + core_if->adp.sense_enabled = 0;
  51548. + core_if->adp.sense_timer_started = 0;
  51549. + core_if->adp.vbuson_timer_started = 0;
  51550. + core_if->adp.probe_counter = 0;
  51551. + core_if->adp.gpwrdn = 0;
  51552. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  51553. + /* Initialize timers */
  51554. + core_if->adp.sense_timer =
  51555. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  51556. + core_if->adp.vbuson_timer =
  51557. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  51558. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  51559. + {
  51560. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  51561. + }
  51562. +}
  51563. +
  51564. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  51565. +{
  51566. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  51567. + gpwrdn.b.pmuintsel = 1;
  51568. + gpwrdn.b.pmuactv = 1;
  51569. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51570. +
  51571. + if (core_if->adp.probe_enabled)
  51572. + dwc_otg_adp_probe_stop(core_if);
  51573. + if (core_if->adp.sense_enabled)
  51574. + dwc_otg_adp_sense_stop(core_if);
  51575. + if (core_if->adp.sense_timer_started)
  51576. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  51577. + if (core_if->adp.vbuson_timer_started)
  51578. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  51579. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  51580. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  51581. +}
  51582. +
  51583. +/////////////////////////////////////////////////////////////////////
  51584. +////////////// ADP Interrupt Handlers ///////////////////////////////
  51585. +/////////////////////////////////////////////////////////////////////
  51586. +/**
  51587. + * This function sets Ramp Timer values
  51588. + */
  51589. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  51590. +{
  51591. + if (core_if->adp.probe_timer_values[0] == -1) {
  51592. + core_if->adp.probe_timer_values[0] = val;
  51593. + core_if->adp.probe_timer_values[1] = -1;
  51594. + return 1;
  51595. + } else {
  51596. + core_if->adp.probe_timer_values[1] =
  51597. + core_if->adp.probe_timer_values[0];
  51598. + core_if->adp.probe_timer_values[0] = val;
  51599. + return 0;
  51600. + }
  51601. +}
  51602. +
  51603. +/**
  51604. + * This function compares Ramp Timer values
  51605. + */
  51606. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  51607. +{
  51608. + uint32_t diff;
  51609. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  51610. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  51611. + else
  51612. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  51613. + if(diff < 2) {
  51614. + return 0;
  51615. + } else {
  51616. + return 1;
  51617. + }
  51618. +}
  51619. +
  51620. +/**
  51621. + * This function handles ADP Probe Interrupts
  51622. + */
  51623. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  51624. + uint32_t val)
  51625. +{
  51626. + adpctl_data_t adpctl = {.d32 = 0 };
  51627. + gpwrdn_data_t gpwrdn, temp;
  51628. + adpctl.d32 = val;
  51629. +
  51630. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51631. + core_if->adp.probe_counter++;
  51632. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51633. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  51634. + DWC_PRINTF("RTIM value is 0\n");
  51635. + goto exit;
  51636. + }
  51637. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  51638. + core_if->adp.initial_probe) {
  51639. + core_if->adp.initial_probe = 0;
  51640. + dwc_otg_adp_probe_stop(core_if);
  51641. + gpwrdn.d32 = 0;
  51642. + gpwrdn.b.pmuactv = 1;
  51643. + gpwrdn.b.pmuintsel = 1;
  51644. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51645. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  51646. +
  51647. + /* check which value is for device mode and which for Host mode */
  51648. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  51649. + /*
  51650. + * Turn on VBUS after initial ADP probe.
  51651. + */
  51652. + core_if->op_state = A_HOST;
  51653. + dwc_otg_enable_global_interrupts(core_if);
  51654. + DWC_SPINUNLOCK(core_if->lock);
  51655. + cil_hcd_start(core_if);
  51656. + dwc_otg_adp_turnon_vbus(core_if);
  51657. + DWC_SPINLOCK(core_if->lock);
  51658. + } else {
  51659. + /*
  51660. + * Initiate SRP after initial ADP probe.
  51661. + */
  51662. + dwc_otg_enable_global_interrupts(core_if);
  51663. + dwc_otg_initiate_srp(core_if);
  51664. + }
  51665. + } else if (core_if->adp.probe_counter > 2){
  51666. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51667. + if (compare_timer_values(core_if)) {
  51668. + DWC_PRINTF("Difference in timer values !!! \n");
  51669. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  51670. + dwc_otg_adp_probe_stop(core_if);
  51671. +
  51672. + /* Power on the core */
  51673. + if (core_if->power_down == 2) {
  51674. + gpwrdn.b.pwrdnswtch = 1;
  51675. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51676. + gpwrdn, 0, gpwrdn.d32);
  51677. + }
  51678. +
  51679. + /* check which value is for device mode and which for Host mode */
  51680. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  51681. + /* Disable Interrupt from Power Down Logic */
  51682. + gpwrdn.d32 = 0;
  51683. + gpwrdn.b.pmuintsel = 1;
  51684. + gpwrdn.b.pmuactv = 1;
  51685. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51686. + gpwrdn, gpwrdn.d32, 0);
  51687. +
  51688. + /*
  51689. + * Initialize the Core for Host mode.
  51690. + */
  51691. + core_if->op_state = A_HOST;
  51692. + dwc_otg_core_init(core_if);
  51693. + dwc_otg_enable_global_interrupts(core_if);
  51694. + cil_hcd_start(core_if);
  51695. + } else {
  51696. + gotgctl_data_t gotgctl;
  51697. + /* Mask SRP detected interrupt from Power Down Logic */
  51698. + gpwrdn.d32 = 0;
  51699. + gpwrdn.b.srp_det_msk = 1;
  51700. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51701. + gpwrdn, gpwrdn.d32, 0);
  51702. +
  51703. + /* Disable Power Down Logic */
  51704. + gpwrdn.d32 = 0;
  51705. + gpwrdn.b.pmuintsel = 1;
  51706. + gpwrdn.b.pmuactv = 1;
  51707. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51708. + gpwrdn, gpwrdn.d32, 0);
  51709. +
  51710. + /*
  51711. + * Initialize the Core for Device mode.
  51712. + */
  51713. + core_if->op_state = B_PERIPHERAL;
  51714. + dwc_otg_core_init(core_if);
  51715. + dwc_otg_enable_global_interrupts(core_if);
  51716. + cil_pcd_start(core_if);
  51717. +
  51718. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  51719. + if (!gotgctl.b.bsesvld) {
  51720. + dwc_otg_initiate_srp(core_if);
  51721. + }
  51722. + }
  51723. + }
  51724. + if (core_if->power_down == 2) {
  51725. + if (gpwrdn.b.bsessvld) {
  51726. + /* Mask SRP detected interrupt from Power Down Logic */
  51727. + gpwrdn.d32 = 0;
  51728. + gpwrdn.b.srp_det_msk = 1;
  51729. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51730. +
  51731. + /* Disable Power Down Logic */
  51732. + gpwrdn.d32 = 0;
  51733. + gpwrdn.b.pmuactv = 1;
  51734. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51735. +
  51736. + /*
  51737. + * Initialize the Core for Device mode.
  51738. + */
  51739. + core_if->op_state = B_PERIPHERAL;
  51740. + dwc_otg_core_init(core_if);
  51741. + dwc_otg_enable_global_interrupts(core_if);
  51742. + cil_pcd_start(core_if);
  51743. + }
  51744. + }
  51745. + }
  51746. +exit:
  51747. + /* Clear interrupt */
  51748. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51749. + adpctl.b.adp_prb_int = 1;
  51750. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51751. +
  51752. + return 0;
  51753. +}
  51754. +
  51755. +/**
  51756. + * This function hadles ADP Sense Interrupt
  51757. + */
  51758. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  51759. +{
  51760. + adpctl_data_t adpctl;
  51761. + /* Stop ADP Sense timer */
  51762. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  51763. +
  51764. + /* Restart ADP Sense timer */
  51765. + dwc_otg_adp_sense_timer_start(core_if);
  51766. +
  51767. + /* Clear interrupt */
  51768. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51769. + adpctl.b.adp_sns_int = 1;
  51770. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51771. +
  51772. + return 0;
  51773. +}
  51774. +
  51775. +/**
  51776. + * This function handles ADP Probe Interrupts
  51777. + */
  51778. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  51779. + uint32_t val)
  51780. +{
  51781. + adpctl_data_t adpctl = {.d32 = 0 };
  51782. + adpctl.d32 = val;
  51783. + set_timer_value(core_if, adpctl.b.rtim);
  51784. +
  51785. + /* Clear interrupt */
  51786. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51787. + adpctl.b.adp_tmout_int = 1;
  51788. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51789. +
  51790. + return 0;
  51791. +}
  51792. +
  51793. +/**
  51794. + * ADP Interrupt handler.
  51795. + *
  51796. + */
  51797. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  51798. +{
  51799. + int retval = 0;
  51800. + adpctl_data_t adpctl = {.d32 = 0};
  51801. +
  51802. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51803. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  51804. +
  51805. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  51806. + DWC_PRINTF("ADP Sense interrupt\n");
  51807. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  51808. + }
  51809. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  51810. + DWC_PRINTF("ADP timeout interrupt\n");
  51811. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  51812. + }
  51813. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  51814. + DWC_PRINTF("ADP Probe interrupt\n");
  51815. + adpctl.b.adp_prb_int = 1;
  51816. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  51817. + }
  51818. +
  51819. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  51820. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51821. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  51822. +
  51823. + return retval;
  51824. +}
  51825. +
  51826. +/**
  51827. + *
  51828. + * @param core_if Programming view of DWC_otg controller.
  51829. + */
  51830. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  51831. +{
  51832. +
  51833. +#ifndef DWC_HOST_ONLY
  51834. + hprt0_data_t hprt0;
  51835. + gpwrdn_data_t gpwrdn;
  51836. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  51837. +
  51838. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51839. + /* check which value is for device mode and which for Host mode */
  51840. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  51841. + DWC_PRINTF("SRP: Host mode\n");
  51842. +
  51843. + if (core_if->adp_enable) {
  51844. + dwc_otg_adp_probe_stop(core_if);
  51845. +
  51846. + /* Power on the core */
  51847. + if (core_if->power_down == 2) {
  51848. + gpwrdn.b.pwrdnswtch = 1;
  51849. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51850. + gpwrdn, 0, gpwrdn.d32);
  51851. + }
  51852. +
  51853. + core_if->op_state = A_HOST;
  51854. + dwc_otg_core_init(core_if);
  51855. + dwc_otg_enable_global_interrupts(core_if);
  51856. + cil_hcd_start(core_if);
  51857. + }
  51858. +
  51859. + /* Turn on the port power bit. */
  51860. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  51861. + hprt0.b.prtpwr = 1;
  51862. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  51863. +
  51864. + /* Start the Connection timer. So a message can be displayed
  51865. + * if connect does not occur within 10 seconds. */
  51866. + cil_hcd_session_start(core_if);
  51867. + } else {
  51868. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  51869. + if (core_if->adp_enable) {
  51870. + dwc_otg_adp_probe_stop(core_if);
  51871. +
  51872. + /* Power on the core */
  51873. + if (core_if->power_down == 2) {
  51874. + gpwrdn.b.pwrdnswtch = 1;
  51875. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51876. + gpwrdn, 0, gpwrdn.d32);
  51877. + }
  51878. +
  51879. + gpwrdn.d32 = 0;
  51880. + gpwrdn.b.pmuactv = 0;
  51881. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51882. + gpwrdn.d32);
  51883. +
  51884. + core_if->op_state = B_PERIPHERAL;
  51885. + dwc_otg_core_init(core_if);
  51886. + dwc_otg_enable_global_interrupts(core_if);
  51887. + cil_pcd_start(core_if);
  51888. + }
  51889. + }
  51890. +#endif
  51891. + return 1;
  51892. +}
  51893. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_adp.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  51894. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_adp.h 1970-01-01 01:00:00.000000000 +0100
  51895. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h 2014-04-24 15:35:04.169565731 +0200
  51896. @@ -0,0 +1,80 @@
  51897. +/* ==========================================================================
  51898. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  51899. + * $Revision: #7 $
  51900. + * $Date: 2011/10/24 $
  51901. + * $Change: 1871159 $
  51902. + *
  51903. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51904. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51905. + * otherwise expressly agreed to in writing between Synopsys and you.
  51906. + *
  51907. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51908. + * any End User Software License Agreement or Agreement for Licensed Product
  51909. + * with Synopsys or any supplement thereto. You are permitted to use and
  51910. + * redistribute this Software in source and binary forms, with or without
  51911. + * modification, provided that redistributions of source code must retain this
  51912. + * notice. You may not view, use, disclose, copy or distribute this file or
  51913. + * any information contained herein except pursuant to this license grant from
  51914. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51915. + * below, then you are not authorized to use the Software.
  51916. + *
  51917. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51918. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51919. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51920. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51921. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51922. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51923. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51924. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51925. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51926. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51927. + * DAMAGE.
  51928. + * ========================================================================== */
  51929. +
  51930. +#ifndef __DWC_OTG_ADP_H__
  51931. +#define __DWC_OTG_ADP_H__
  51932. +
  51933. +/**
  51934. + * @file
  51935. + *
  51936. + * This file contains the Attach Detect Protocol interfaces and defines
  51937. + * (functions) and structures for Linux.
  51938. + *
  51939. + */
  51940. +
  51941. +#define DWC_OTG_ADP_UNATTACHED 0
  51942. +#define DWC_OTG_ADP_ATTACHED 1
  51943. +#define DWC_OTG_ADP_UNKOWN 2
  51944. +
  51945. +typedef struct dwc_otg_adp {
  51946. + uint32_t adp_started;
  51947. + uint32_t initial_probe;
  51948. + int32_t probe_timer_values[2];
  51949. + uint32_t probe_enabled;
  51950. + uint32_t sense_enabled;
  51951. + dwc_timer_t *sense_timer;
  51952. + uint32_t sense_timer_started;
  51953. + dwc_timer_t *vbuson_timer;
  51954. + uint32_t vbuson_timer_started;
  51955. + uint32_t attached;
  51956. + uint32_t probe_counter;
  51957. + uint32_t gpwrdn;
  51958. +} dwc_otg_adp_t;
  51959. +
  51960. +/**
  51961. + * Attach Detect Protocol functions
  51962. + */
  51963. +
  51964. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  51965. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  51966. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  51967. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  51968. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  51969. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  51970. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  51971. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  51972. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  51973. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  51974. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  51975. +
  51976. +#endif //__DWC_OTG_ADP_H__
  51977. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_attr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  51978. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_attr.c 1970-01-01 01:00:00.000000000 +0100
  51979. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c 2014-04-24 15:35:04.169565731 +0200
  51980. @@ -0,0 +1,1210 @@
  51981. +/* ==========================================================================
  51982. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  51983. + * $Revision: #44 $
  51984. + * $Date: 2010/11/29 $
  51985. + * $Change: 1636033 $
  51986. + *
  51987. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51988. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51989. + * otherwise expressly agreed to in writing between Synopsys and you.
  51990. + *
  51991. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51992. + * any End User Software License Agreement or Agreement for Licensed Product
  51993. + * with Synopsys or any supplement thereto. You are permitted to use and
  51994. + * redistribute this Software in source and binary forms, with or without
  51995. + * modification, provided that redistributions of source code must retain this
  51996. + * notice. You may not view, use, disclose, copy or distribute this file or
  51997. + * any information contained herein except pursuant to this license grant from
  51998. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51999. + * below, then you are not authorized to use the Software.
  52000. + *
  52001. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  52002. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52003. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52004. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  52005. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  52006. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  52007. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  52008. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52009. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52010. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52011. + * DAMAGE.
  52012. + * ========================================================================== */
  52013. +
  52014. +/** @file
  52015. + *
  52016. + * The diagnostic interface will provide access to the controller for
  52017. + * bringing up the hardware and testing. The Linux driver attributes
  52018. + * feature will be used to provide the Linux Diagnostic
  52019. + * Interface. These attributes are accessed through sysfs.
  52020. + */
  52021. +
  52022. +/** @page "Linux Module Attributes"
  52023. + *
  52024. + * The Linux module attributes feature is used to provide the Linux
  52025. + * Diagnostic Interface. These attributes are accessed through sysfs.
  52026. + * The diagnostic interface will provide access to the controller for
  52027. + * bringing up the hardware and testing.
  52028. +
  52029. + The following table shows the attributes.
  52030. + <table>
  52031. + <tr>
  52032. + <td><b> Name</b></td>
  52033. + <td><b> Description</b></td>
  52034. + <td><b> Access</b></td>
  52035. + </tr>
  52036. +
  52037. + <tr>
  52038. + <td> mode </td>
  52039. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  52040. + <td> Read</td>
  52041. + </tr>
  52042. +
  52043. + <tr>
  52044. + <td> hnpcapable </td>
  52045. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  52046. + Read returns the current value.</td>
  52047. + <td> Read/Write</td>
  52048. + </tr>
  52049. +
  52050. + <tr>
  52051. + <td> srpcapable </td>
  52052. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  52053. + Read returns the current value.</td>
  52054. + <td> Read/Write</td>
  52055. + </tr>
  52056. +
  52057. + <tr>
  52058. + <td> hsic_connect </td>
  52059. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  52060. + Read returns the current value.</td>
  52061. + <td> Read/Write</td>
  52062. + </tr>
  52063. +
  52064. + <tr>
  52065. + <td> inv_sel_hsic </td>
  52066. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  52067. + Read returns the current value.</td>
  52068. + <td> Read/Write</td>
  52069. + </tr>
  52070. +
  52071. + <tr>
  52072. + <td> hnp </td>
  52073. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  52074. + <td> Read/Write</td>
  52075. + </tr>
  52076. +
  52077. + <tr>
  52078. + <td> srp </td>
  52079. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  52080. + <td> Read/Write</td>
  52081. + </tr>
  52082. +
  52083. + <tr>
  52084. + <td> buspower </td>
  52085. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  52086. + <td> Read/Write</td>
  52087. + </tr>
  52088. +
  52089. + <tr>
  52090. + <td> bussuspend </td>
  52091. + <td> Suspends the USB bus.</td>
  52092. + <td> Read/Write</td>
  52093. + </tr>
  52094. +
  52095. + <tr>
  52096. + <td> busconnected </td>
  52097. + <td> Gets the connection status of the bus</td>
  52098. + <td> Read</td>
  52099. + </tr>
  52100. +
  52101. + <tr>
  52102. + <td> gotgctl </td>
  52103. + <td> Gets or sets the Core Control Status Register.</td>
  52104. + <td> Read/Write</td>
  52105. + </tr>
  52106. +
  52107. + <tr>
  52108. + <td> gusbcfg </td>
  52109. + <td> Gets or sets the Core USB Configuration Register</td>
  52110. + <td> Read/Write</td>
  52111. + </tr>
  52112. +
  52113. + <tr>
  52114. + <td> grxfsiz </td>
  52115. + <td> Gets or sets the Receive FIFO Size Register</td>
  52116. + <td> Read/Write</td>
  52117. + </tr>
  52118. +
  52119. + <tr>
  52120. + <td> gnptxfsiz </td>
  52121. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  52122. + <td> Read/Write</td>
  52123. + </tr>
  52124. +
  52125. + <tr>
  52126. + <td> gpvndctl </td>
  52127. + <td> Gets or sets the PHY Vendor Control Register</td>
  52128. + <td> Read/Write</td>
  52129. + </tr>
  52130. +
  52131. + <tr>
  52132. + <td> ggpio </td>
  52133. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  52134. + or sets the upper 16 bits.</td>
  52135. + <td> Read/Write</td>
  52136. + </tr>
  52137. +
  52138. + <tr>
  52139. + <td> guid </td>
  52140. + <td> Gets or sets the value of the User ID Register</td>
  52141. + <td> Read/Write</td>
  52142. + </tr>
  52143. +
  52144. + <tr>
  52145. + <td> gsnpsid </td>
  52146. + <td> Gets the value of the Synopsys ID Regester</td>
  52147. + <td> Read</td>
  52148. + </tr>
  52149. +
  52150. + <tr>
  52151. + <td> devspeed </td>
  52152. + <td> Gets or sets the device speed setting in the DCFG register</td>
  52153. + <td> Read/Write</td>
  52154. + </tr>
  52155. +
  52156. + <tr>
  52157. + <td> enumspeed </td>
  52158. + <td> Gets the device enumeration Speed.</td>
  52159. + <td> Read</td>
  52160. + </tr>
  52161. +
  52162. + <tr>
  52163. + <td> hptxfsiz </td>
  52164. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  52165. + <td> Read</td>
  52166. + </tr>
  52167. +
  52168. + <tr>
  52169. + <td> hprt0 </td>
  52170. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  52171. + <td> Read/Write</td>
  52172. + </tr>
  52173. +
  52174. + <tr>
  52175. + <td> regoffset </td>
  52176. + <td> Sets the register offset for the next Register Access</td>
  52177. + <td> Read/Write</td>
  52178. + </tr>
  52179. +
  52180. + <tr>
  52181. + <td> regvalue </td>
  52182. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  52183. + <td> Read/Write</td>
  52184. + </tr>
  52185. +
  52186. + <tr>
  52187. + <td> remote_wakeup </td>
  52188. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  52189. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  52190. + Wakeup signalling bit in the Device Control Register is set for 1
  52191. + milli-second.</td>
  52192. + <td> Read/Write</td>
  52193. + </tr>
  52194. +
  52195. + <tr>
  52196. + <td> rem_wakeup_pwrdn </td>
  52197. + <td> On read, shows the status core - hibernated or not. On write, initiates
  52198. + a remote wakeup of the device from Hibernation. </td>
  52199. + <td> Read/Write</td>
  52200. + </tr>
  52201. +
  52202. + <tr>
  52203. + <td> mode_ch_tim_en </td>
  52204. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  52205. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  52206. + after Suspend or LPM. </td>
  52207. + <td> Read/Write</td>
  52208. + </tr>
  52209. +
  52210. + <tr>
  52211. + <td> fr_interval </td>
  52212. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  52213. + reload HFIR register during runtime. The application can write a value to this
  52214. + register only after the Port Enable bit of the Host Port Control and Status
  52215. + register (HPRT.PrtEnaPort) has been set </td>
  52216. + <td> Read/Write</td>
  52217. + </tr>
  52218. +
  52219. + <tr>
  52220. + <td> disconnect_us </td>
  52221. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  52222. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  52223. + <td> Read/Write</td>
  52224. + </tr>
  52225. +
  52226. + <tr>
  52227. + <td> regdump </td>
  52228. + <td> Dumps the contents of core registers.</td>
  52229. + <td> Read</td>
  52230. + </tr>
  52231. +
  52232. + <tr>
  52233. + <td> spramdump </td>
  52234. + <td> Dumps the contents of core registers.</td>
  52235. + <td> Read</td>
  52236. + </tr>
  52237. +
  52238. + <tr>
  52239. + <td> hcddump </td>
  52240. + <td> Dumps the current HCD state.</td>
  52241. + <td> Read</td>
  52242. + </tr>
  52243. +
  52244. + <tr>
  52245. + <td> hcd_frrem </td>
  52246. + <td> Shows the average value of the Frame Remaining
  52247. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  52248. + occurs. This can be used to determine the average interrupt latency. Also
  52249. + shows the average Frame Remaining value for start_transfer and the "a" and
  52250. + "b" sample points. The "a" and "b" sample points may be used during debugging
  52251. + bto determine how long it takes to execute a section of the HCD code.</td>
  52252. + <td> Read</td>
  52253. + </tr>
  52254. +
  52255. + <tr>
  52256. + <td> rd_reg_test </td>
  52257. + <td> Displays the time required to read the GNPTXFSIZ register many times
  52258. + (the output shows the number of times the register is read).
  52259. + <td> Read</td>
  52260. + </tr>
  52261. +
  52262. + <tr>
  52263. + <td> wr_reg_test </td>
  52264. + <td> Displays the time required to write the GNPTXFSIZ register many times
  52265. + (the output shows the number of times the register is written).
  52266. + <td> Read</td>
  52267. + </tr>
  52268. +
  52269. + <tr>
  52270. + <td> lpm_response </td>
  52271. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  52272. + <td> Write</td>
  52273. + </tr>
  52274. +
  52275. + <tr>
  52276. + <td> sleep_status </td>
  52277. + <td> Shows sleep status of device.
  52278. + <td> Read</td>
  52279. + </tr>
  52280. +
  52281. + </table>
  52282. +
  52283. + Example usage:
  52284. + To get the current mode:
  52285. + cat /sys/devices/lm0/mode
  52286. +
  52287. + To power down the USB:
  52288. + echo 0 > /sys/devices/lm0/buspower
  52289. + */
  52290. +
  52291. +#include "dwc_otg_os_dep.h"
  52292. +#include "dwc_os.h"
  52293. +#include "dwc_otg_driver.h"
  52294. +#include "dwc_otg_attr.h"
  52295. +#include "dwc_otg_core_if.h"
  52296. +#include "dwc_otg_pcd_if.h"
  52297. +#include "dwc_otg_hcd_if.h"
  52298. +
  52299. +/*
  52300. + * MACROs for defining sysfs attribute
  52301. + */
  52302. +#ifdef LM_INTERFACE
  52303. +
  52304. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52305. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52306. +{ \
  52307. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52308. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52309. + uint32_t val; \
  52310. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52311. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  52312. +}
  52313. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52314. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52315. + const char *buf, size_t count) \
  52316. +{ \
  52317. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52318. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52319. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  52320. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  52321. + return count; \
  52322. +}
  52323. +
  52324. +#elif defined(PCI_INTERFACE)
  52325. +
  52326. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52327. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52328. +{ \
  52329. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52330. + uint32_t val; \
  52331. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52332. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  52333. +}
  52334. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52335. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52336. + const char *buf, size_t count) \
  52337. +{ \
  52338. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52339. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  52340. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  52341. + return count; \
  52342. +}
  52343. +
  52344. +#elif defined(PLATFORM_INTERFACE)
  52345. +
  52346. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52347. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52348. +{ \
  52349. + struct platform_device *platform_dev = \
  52350. + container_of(_dev, struct platform_device, dev); \
  52351. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52352. + uint32_t val; \
  52353. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  52354. + __func__, _dev, platform_dev, otg_dev); \
  52355. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52356. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  52357. +}
  52358. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52359. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52360. + const char *buf, size_t count) \
  52361. +{ \
  52362. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  52363. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52364. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  52365. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  52366. + return count; \
  52367. +}
  52368. +#endif
  52369. +
  52370. +/*
  52371. + * MACROs for defining sysfs attribute for 32-bit registers
  52372. + */
  52373. +#ifdef LM_INTERFACE
  52374. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52375. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52376. +{ \
  52377. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52378. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52379. + uint32_t val; \
  52380. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52381. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  52382. +}
  52383. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52384. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52385. + const char *buf, size_t count) \
  52386. +{ \
  52387. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  52388. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  52389. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  52390. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  52391. + return count; \
  52392. +}
  52393. +#elif defined(PCI_INTERFACE)
  52394. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52395. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52396. +{ \
  52397. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52398. + uint32_t val; \
  52399. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52400. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  52401. +}
  52402. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52403. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52404. + const char *buf, size_t count) \
  52405. +{ \
  52406. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  52407. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  52408. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  52409. + return count; \
  52410. +}
  52411. +
  52412. +#elif defined(PLATFORM_INTERFACE)
  52413. +#include "dwc_otg_dbg.h"
  52414. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52415. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  52416. +{ \
  52417. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  52418. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52419. + uint32_t val; \
  52420. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  52421. + __func__, _dev, platform_dev, otg_dev); \
  52422. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  52423. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  52424. +}
  52425. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52426. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  52427. + const char *buf, size_t count) \
  52428. +{ \
  52429. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  52430. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  52431. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  52432. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  52433. + return count; \
  52434. +}
  52435. +
  52436. +#endif
  52437. +
  52438. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  52439. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52440. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  52441. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  52442. +
  52443. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  52444. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  52445. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  52446. +
  52447. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  52448. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52449. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  52450. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  52451. +
  52452. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  52453. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  52454. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  52455. +
  52456. +/** @name Functions for Show/Store of Attributes */
  52457. +/**@{*/
  52458. +
  52459. +/**
  52460. + * Helper function returning the otg_device structure of the given device
  52461. + */
  52462. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  52463. +{
  52464. + dwc_otg_device_t *otg_dev;
  52465. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  52466. + return otg_dev;
  52467. +}
  52468. +
  52469. +/**
  52470. + * Show the register offset of the Register Access.
  52471. + */
  52472. +static ssize_t regoffset_show(struct device *_dev,
  52473. + struct device_attribute *attr, char *buf)
  52474. +{
  52475. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52476. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  52477. + otg_dev->os_dep.reg_offset);
  52478. +}
  52479. +
  52480. +/**
  52481. + * Set the register offset for the next Register Access Read/Write
  52482. + */
  52483. +static ssize_t regoffset_store(struct device *_dev,
  52484. + struct device_attribute *attr,
  52485. + const char *buf, size_t count)
  52486. +{
  52487. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52488. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  52489. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  52490. + if (offset < SZ_256K) {
  52491. +#elif defined(PCI_INTERFACE)
  52492. + if (offset < 0x00040000) {
  52493. +#endif
  52494. + otg_dev->os_dep.reg_offset = offset;
  52495. + } else {
  52496. + dev_err(_dev, "invalid offset\n");
  52497. + }
  52498. +
  52499. + return count;
  52500. +}
  52501. +
  52502. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  52503. +
  52504. +/**
  52505. + * Show the value of the register at the offset in the reg_offset
  52506. + * attribute.
  52507. + */
  52508. +static ssize_t regvalue_show(struct device *_dev,
  52509. + struct device_attribute *attr, char *buf)
  52510. +{
  52511. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52512. + uint32_t val;
  52513. + volatile uint32_t *addr;
  52514. +
  52515. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  52516. + /* Calculate the address */
  52517. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  52518. + (uint8_t *) otg_dev->os_dep.base);
  52519. + val = DWC_READ_REG32(addr);
  52520. + return snprintf(buf,
  52521. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  52522. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  52523. + val);
  52524. + } else {
  52525. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  52526. + return sprintf(buf, "invalid offset\n");
  52527. + }
  52528. +}
  52529. +
  52530. +/**
  52531. + * Store the value in the register at the offset in the reg_offset
  52532. + * attribute.
  52533. + *
  52534. + */
  52535. +static ssize_t regvalue_store(struct device *_dev,
  52536. + struct device_attribute *attr,
  52537. + const char *buf, size_t count)
  52538. +{
  52539. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52540. + volatile uint32_t *addr;
  52541. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52542. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  52543. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  52544. + /* Calculate the address */
  52545. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  52546. + (uint8_t *) otg_dev->os_dep.base);
  52547. + DWC_WRITE_REG32(addr, val);
  52548. + } else {
  52549. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  52550. + otg_dev->os_dep.reg_offset);
  52551. + }
  52552. + return count;
  52553. +}
  52554. +
  52555. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  52556. +
  52557. +/*
  52558. + * Attributes
  52559. + */
  52560. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  52561. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  52562. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  52563. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  52564. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  52565. +
  52566. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  52567. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  52568. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  52569. +
  52570. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  52571. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  52572. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  52573. + "GUSBCFG");
  52574. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  52575. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  52576. + "GRXFSIZ");
  52577. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  52578. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  52579. + "GNPTXFSIZ");
  52580. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  52581. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  52582. + "GPVNDCTL");
  52583. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  52584. + &(otg_dev->core_if->core_global_regs->ggpio),
  52585. + "GGPIO");
  52586. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  52587. + "GUID");
  52588. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  52589. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  52590. + "GSNPSID");
  52591. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  52592. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  52593. +
  52594. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  52595. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  52596. + "HPTXFSIZ");
  52597. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  52598. +
  52599. +/**
  52600. + * @todo Add code to initiate the HNP.
  52601. + */
  52602. +/**
  52603. + * Show the HNP status bit
  52604. + */
  52605. +static ssize_t hnp_show(struct device *_dev,
  52606. + struct device_attribute *attr, char *buf)
  52607. +{
  52608. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52609. + return sprintf(buf, "HstNegScs = 0x%x\n",
  52610. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  52611. +}
  52612. +
  52613. +/**
  52614. + * Set the HNP Request bit
  52615. + */
  52616. +static ssize_t hnp_store(struct device *_dev,
  52617. + struct device_attribute *attr,
  52618. + const char *buf, size_t count)
  52619. +{
  52620. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52621. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52622. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  52623. + return count;
  52624. +}
  52625. +
  52626. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  52627. +
  52628. +/**
  52629. + * @todo Add code to initiate the SRP.
  52630. + */
  52631. +/**
  52632. + * Show the SRP status bit
  52633. + */
  52634. +static ssize_t srp_show(struct device *_dev,
  52635. + struct device_attribute *attr, char *buf)
  52636. +{
  52637. +#ifndef DWC_HOST_ONLY
  52638. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52639. + return sprintf(buf, "SesReqScs = 0x%x\n",
  52640. + dwc_otg_get_srpstatus(otg_dev->core_if));
  52641. +#else
  52642. + return sprintf(buf, "Host Only Mode!\n");
  52643. +#endif
  52644. +}
  52645. +
  52646. +/**
  52647. + * Set the SRP Request bit
  52648. + */
  52649. +static ssize_t srp_store(struct device *_dev,
  52650. + struct device_attribute *attr,
  52651. + const char *buf, size_t count)
  52652. +{
  52653. +#ifndef DWC_HOST_ONLY
  52654. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52655. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  52656. +#endif
  52657. + return count;
  52658. +}
  52659. +
  52660. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  52661. +
  52662. +/**
  52663. + * @todo Need to do more for power on/off?
  52664. + */
  52665. +/**
  52666. + * Show the Bus Power status
  52667. + */
  52668. +static ssize_t buspower_show(struct device *_dev,
  52669. + struct device_attribute *attr, char *buf)
  52670. +{
  52671. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52672. + return sprintf(buf, "Bus Power = 0x%x\n",
  52673. + dwc_otg_get_prtpower(otg_dev->core_if));
  52674. +}
  52675. +
  52676. +/**
  52677. + * Set the Bus Power status
  52678. + */
  52679. +static ssize_t buspower_store(struct device *_dev,
  52680. + struct device_attribute *attr,
  52681. + const char *buf, size_t count)
  52682. +{
  52683. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52684. + uint32_t on = simple_strtoul(buf, NULL, 16);
  52685. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  52686. + return count;
  52687. +}
  52688. +
  52689. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  52690. +
  52691. +/**
  52692. + * @todo Need to do more for suspend?
  52693. + */
  52694. +/**
  52695. + * Show the Bus Suspend status
  52696. + */
  52697. +static ssize_t bussuspend_show(struct device *_dev,
  52698. + struct device_attribute *attr, char *buf)
  52699. +{
  52700. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52701. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  52702. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  52703. +}
  52704. +
  52705. +/**
  52706. + * Set the Bus Suspend status
  52707. + */
  52708. +static ssize_t bussuspend_store(struct device *_dev,
  52709. + struct device_attribute *attr,
  52710. + const char *buf, size_t count)
  52711. +{
  52712. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52713. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52714. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  52715. + return count;
  52716. +}
  52717. +
  52718. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  52719. +
  52720. +/**
  52721. + * Show the Mode Change Ready Timer status
  52722. + */
  52723. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  52724. + struct device_attribute *attr, char *buf)
  52725. +{
  52726. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52727. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  52728. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  52729. +}
  52730. +
  52731. +/**
  52732. + * Set the Mode Change Ready Timer status
  52733. + */
  52734. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  52735. + struct device_attribute *attr,
  52736. + const char *buf, size_t count)
  52737. +{
  52738. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52739. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52740. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  52741. + return count;
  52742. +}
  52743. +
  52744. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  52745. +
  52746. +/**
  52747. + * Show the value of HFIR Frame Interval bitfield
  52748. + */
  52749. +static ssize_t fr_interval_show(struct device *_dev,
  52750. + struct device_attribute *attr, char *buf)
  52751. +{
  52752. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52753. + return sprintf(buf, "Frame Interval = 0x%x\n",
  52754. + dwc_otg_get_fr_interval(otg_dev->core_if));
  52755. +}
  52756. +
  52757. +/**
  52758. + * Set the HFIR Frame Interval value
  52759. + */
  52760. +static ssize_t fr_interval_store(struct device *_dev,
  52761. + struct device_attribute *attr,
  52762. + const char *buf, size_t count)
  52763. +{
  52764. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52765. + uint32_t in = simple_strtoul(buf, NULL, 10);
  52766. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  52767. + return count;
  52768. +}
  52769. +
  52770. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  52771. +
  52772. +/**
  52773. + * Show the status of Remote Wakeup.
  52774. + */
  52775. +static ssize_t remote_wakeup_show(struct device *_dev,
  52776. + struct device_attribute *attr, char *buf)
  52777. +{
  52778. +#ifndef DWC_HOST_ONLY
  52779. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52780. +
  52781. + return sprintf(buf,
  52782. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  52783. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  52784. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  52785. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  52786. +#else
  52787. + return sprintf(buf, "Host Only Mode!\n");
  52788. +#endif /* DWC_HOST_ONLY */
  52789. +}
  52790. +
  52791. +/**
  52792. + * Initiate a remote wakeup of the host. The Device control register
  52793. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  52794. + * flag is set.
  52795. + *
  52796. + */
  52797. +static ssize_t remote_wakeup_store(struct device *_dev,
  52798. + struct device_attribute *attr,
  52799. + const char *buf, size_t count)
  52800. +{
  52801. +#ifndef DWC_HOST_ONLY
  52802. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52803. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52804. +
  52805. + if (val & 1) {
  52806. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  52807. + } else {
  52808. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  52809. + }
  52810. +#endif /* DWC_HOST_ONLY */
  52811. + return count;
  52812. +}
  52813. +
  52814. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  52815. + remote_wakeup_store);
  52816. +
  52817. +/**
  52818. + * Show the whether core is hibernated or not.
  52819. + */
  52820. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  52821. + struct device_attribute *attr, char *buf)
  52822. +{
  52823. +#ifndef DWC_HOST_ONLY
  52824. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52825. +
  52826. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  52827. + DWC_PRINTF("Core is in hibernation\n");
  52828. + } else {
  52829. + DWC_PRINTF("Core is not in hibernation\n");
  52830. + }
  52831. +#endif /* DWC_HOST_ONLY */
  52832. + return 0;
  52833. +}
  52834. +
  52835. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  52836. + int rem_wakeup, int reset);
  52837. +
  52838. +/**
  52839. + * Initiate a remote wakeup of the device to exit from hibernation.
  52840. + */
  52841. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  52842. + struct device_attribute *attr,
  52843. + const char *buf, size_t count)
  52844. +{
  52845. +#ifndef DWC_HOST_ONLY
  52846. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52847. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  52848. +#endif
  52849. + return count;
  52850. +}
  52851. +
  52852. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  52853. + rem_wakeup_pwrdn_store);
  52854. +
  52855. +static ssize_t disconnect_us(struct device *_dev,
  52856. + struct device_attribute *attr,
  52857. + const char *buf, size_t count)
  52858. +{
  52859. +
  52860. +#ifndef DWC_HOST_ONLY
  52861. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52862. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52863. + DWC_PRINTF("The Passed value is %04x\n", val);
  52864. +
  52865. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  52866. +
  52867. +#endif /* DWC_HOST_ONLY */
  52868. + return count;
  52869. +}
  52870. +
  52871. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  52872. +
  52873. +/**
  52874. + * Dump global registers and either host or device registers (depending on the
  52875. + * current mode of the core).
  52876. + */
  52877. +static ssize_t regdump_show(struct device *_dev,
  52878. + struct device_attribute *attr, char *buf)
  52879. +{
  52880. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52881. +
  52882. + dwc_otg_dump_global_registers(otg_dev->core_if);
  52883. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  52884. + dwc_otg_dump_host_registers(otg_dev->core_if);
  52885. + } else {
  52886. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  52887. +
  52888. + }
  52889. + return sprintf(buf, "Register Dump\n");
  52890. +}
  52891. +
  52892. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  52893. +
  52894. +/**
  52895. + * Dump global registers and either host or device registers (depending on the
  52896. + * current mode of the core).
  52897. + */
  52898. +static ssize_t spramdump_show(struct device *_dev,
  52899. + struct device_attribute *attr, char *buf)
  52900. +{
  52901. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52902. +
  52903. + //dwc_otg_dump_spram(otg_dev->core_if);
  52904. +
  52905. + return sprintf(buf, "SPRAM Dump\n");
  52906. +}
  52907. +
  52908. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  52909. +
  52910. +/**
  52911. + * Dump the current hcd state.
  52912. + */
  52913. +static ssize_t hcddump_show(struct device *_dev,
  52914. + struct device_attribute *attr, char *buf)
  52915. +{
  52916. +#ifndef DWC_DEVICE_ONLY
  52917. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52918. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  52919. +#endif /* DWC_DEVICE_ONLY */
  52920. + return sprintf(buf, "HCD Dump\n");
  52921. +}
  52922. +
  52923. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  52924. +
  52925. +/**
  52926. + * Dump the average frame remaining at SOF. This can be used to
  52927. + * determine average interrupt latency. Frame remaining is also shown for
  52928. + * start transfer and two additional sample points.
  52929. + */
  52930. +static ssize_t hcd_frrem_show(struct device *_dev,
  52931. + struct device_attribute *attr, char *buf)
  52932. +{
  52933. +#ifndef DWC_DEVICE_ONLY
  52934. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52935. +
  52936. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  52937. +#endif /* DWC_DEVICE_ONLY */
  52938. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  52939. +}
  52940. +
  52941. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  52942. +
  52943. +/**
  52944. + * Displays the time required to read the GNPTXFSIZ register many times (the
  52945. + * output shows the number of times the register is read).
  52946. + */
  52947. +#define RW_REG_COUNT 10000000
  52948. +#define MSEC_PER_JIFFIE 1000/HZ
  52949. +static ssize_t rd_reg_test_show(struct device *_dev,
  52950. + struct device_attribute *attr, char *buf)
  52951. +{
  52952. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52953. + int i;
  52954. + int time;
  52955. + int start_jiffies;
  52956. +
  52957. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  52958. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  52959. + start_jiffies = jiffies;
  52960. + for (i = 0; i < RW_REG_COUNT; i++) {
  52961. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  52962. + }
  52963. + time = jiffies - start_jiffies;
  52964. + return sprintf(buf,
  52965. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  52966. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  52967. +}
  52968. +
  52969. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  52970. +
  52971. +/**
  52972. + * Displays the time required to write the GNPTXFSIZ register many times (the
  52973. + * output shows the number of times the register is written).
  52974. + */
  52975. +static ssize_t wr_reg_test_show(struct device *_dev,
  52976. + struct device_attribute *attr, char *buf)
  52977. +{
  52978. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52979. + uint32_t reg_val;
  52980. + int i;
  52981. + int time;
  52982. + int start_jiffies;
  52983. +
  52984. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  52985. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  52986. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  52987. + start_jiffies = jiffies;
  52988. + for (i = 0; i < RW_REG_COUNT; i++) {
  52989. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  52990. + }
  52991. + time = jiffies - start_jiffies;
  52992. + return sprintf(buf,
  52993. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  52994. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  52995. +}
  52996. +
  52997. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  52998. +
  52999. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53000. +
  53001. +/**
  53002. +* Show the lpm_response attribute.
  53003. +*/
  53004. +static ssize_t lpmresp_show(struct device *_dev,
  53005. + struct device_attribute *attr, char *buf)
  53006. +{
  53007. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53008. +
  53009. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  53010. + return sprintf(buf, "** LPM is DISABLED **\n");
  53011. +
  53012. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  53013. + return sprintf(buf, "** Current mode is not device mode\n");
  53014. + }
  53015. + return sprintf(buf, "lpm_response = %d\n",
  53016. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  53017. +}
  53018. +
  53019. +/**
  53020. +* Store the lpm_response attribute.
  53021. +*/
  53022. +static ssize_t lpmresp_store(struct device *_dev,
  53023. + struct device_attribute *attr,
  53024. + const char *buf, size_t count)
  53025. +{
  53026. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53027. + uint32_t val = simple_strtoul(buf, NULL, 16);
  53028. +
  53029. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  53030. + return 0;
  53031. + }
  53032. +
  53033. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  53034. + return 0;
  53035. + }
  53036. +
  53037. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  53038. + return count;
  53039. +}
  53040. +
  53041. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  53042. +
  53043. +/**
  53044. +* Show the sleep_status attribute.
  53045. +*/
  53046. +static ssize_t sleepstatus_show(struct device *_dev,
  53047. + struct device_attribute *attr, char *buf)
  53048. +{
  53049. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53050. + return sprintf(buf, "Sleep Status = %d\n",
  53051. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  53052. +}
  53053. +
  53054. +/**
  53055. + * Store the sleep_status attribure.
  53056. + */
  53057. +static ssize_t sleepstatus_store(struct device *_dev,
  53058. + struct device_attribute *attr,
  53059. + const char *buf, size_t count)
  53060. +{
  53061. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  53062. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  53063. +
  53064. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  53065. + if (dwc_otg_is_host_mode(core_if)) {
  53066. +
  53067. + DWC_PRINTF("Host initiated resume\n");
  53068. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  53069. + }
  53070. + }
  53071. +
  53072. + return count;
  53073. +}
  53074. +
  53075. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  53076. + sleepstatus_store);
  53077. +
  53078. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  53079. +
  53080. +/**@}*/
  53081. +
  53082. +/**
  53083. + * Create the device files
  53084. + */
  53085. +void dwc_otg_attr_create(
  53086. +#ifdef LM_INTERFACE
  53087. + struct lm_device *dev
  53088. +#elif defined(PCI_INTERFACE)
  53089. + struct pci_dev *dev
  53090. +#elif defined(PLATFORM_INTERFACE)
  53091. + struct platform_device *dev
  53092. +#endif
  53093. + )
  53094. +{
  53095. + int error;
  53096. +
  53097. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  53098. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  53099. + error = device_create_file(&dev->dev, &dev_attr_mode);
  53100. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  53101. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  53102. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  53103. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  53104. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  53105. + error = device_create_file(&dev->dev, &dev_attr_srp);
  53106. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  53107. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  53108. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  53109. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  53110. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  53111. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  53112. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  53113. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  53114. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  53115. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  53116. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  53117. + error = device_create_file(&dev->dev, &dev_attr_guid);
  53118. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  53119. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  53120. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  53121. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  53122. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  53123. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  53124. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  53125. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  53126. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  53127. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  53128. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  53129. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  53130. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  53131. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  53132. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53133. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  53134. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  53135. +#endif
  53136. +}
  53137. +
  53138. +/**
  53139. + * Remove the device files
  53140. + */
  53141. +void dwc_otg_attr_remove(
  53142. +#ifdef LM_INTERFACE
  53143. + struct lm_device *dev
  53144. +#elif defined(PCI_INTERFACE)
  53145. + struct pci_dev *dev
  53146. +#elif defined(PLATFORM_INTERFACE)
  53147. + struct platform_device *dev
  53148. +#endif
  53149. + )
  53150. +{
  53151. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  53152. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  53153. + device_remove_file(&dev->dev, &dev_attr_mode);
  53154. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  53155. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  53156. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  53157. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  53158. + device_remove_file(&dev->dev, &dev_attr_hnp);
  53159. + device_remove_file(&dev->dev, &dev_attr_srp);
  53160. + device_remove_file(&dev->dev, &dev_attr_buspower);
  53161. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  53162. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  53163. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  53164. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  53165. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  53166. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  53167. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  53168. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  53169. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  53170. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  53171. + device_remove_file(&dev->dev, &dev_attr_guid);
  53172. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  53173. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  53174. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  53175. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  53176. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  53177. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  53178. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  53179. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  53180. + device_remove_file(&dev->dev, &dev_attr_regdump);
  53181. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  53182. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  53183. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  53184. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  53185. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  53186. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53187. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  53188. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  53189. +#endif
  53190. +}
  53191. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_attr.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  53192. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_attr.h 1970-01-01 01:00:00.000000000 +0100
  53193. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h 2014-04-24 15:35:04.173565776 +0200
  53194. @@ -0,0 +1,89 @@
  53195. +/* ==========================================================================
  53196. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  53197. + * $Revision: #13 $
  53198. + * $Date: 2010/06/21 $
  53199. + * $Change: 1532021 $
  53200. + *
  53201. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  53202. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  53203. + * otherwise expressly agreed to in writing between Synopsys and you.
  53204. + *
  53205. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  53206. + * any End User Software License Agreement or Agreement for Licensed Product
  53207. + * with Synopsys or any supplement thereto. You are permitted to use and
  53208. + * redistribute this Software in source and binary forms, with or without
  53209. + * modification, provided that redistributions of source code must retain this
  53210. + * notice. You may not view, use, disclose, copy or distribute this file or
  53211. + * any information contained herein except pursuant to this license grant from
  53212. + * Synopsys. If you do not agree with this notice, including the disclaimer
  53213. + * below, then you are not authorized to use the Software.
  53214. + *
  53215. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  53216. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53217. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53218. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  53219. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53220. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53221. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  53222. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53223. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53224. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  53225. + * DAMAGE.
  53226. + * ========================================================================== */
  53227. +
  53228. +#if !defined(__DWC_OTG_ATTR_H__)
  53229. +#define __DWC_OTG_ATTR_H__
  53230. +
  53231. +/** @file
  53232. + * This file contains the interface to the Linux device attributes.
  53233. + */
  53234. +extern struct device_attribute dev_attr_regoffset;
  53235. +extern struct device_attribute dev_attr_regvalue;
  53236. +
  53237. +extern struct device_attribute dev_attr_mode;
  53238. +extern struct device_attribute dev_attr_hnpcapable;
  53239. +extern struct device_attribute dev_attr_srpcapable;
  53240. +extern struct device_attribute dev_attr_hnp;
  53241. +extern struct device_attribute dev_attr_srp;
  53242. +extern struct device_attribute dev_attr_buspower;
  53243. +extern struct device_attribute dev_attr_bussuspend;
  53244. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  53245. +extern struct device_attribute dev_attr_fr_interval;
  53246. +extern struct device_attribute dev_attr_busconnected;
  53247. +extern struct device_attribute dev_attr_gotgctl;
  53248. +extern struct device_attribute dev_attr_gusbcfg;
  53249. +extern struct device_attribute dev_attr_grxfsiz;
  53250. +extern struct device_attribute dev_attr_gnptxfsiz;
  53251. +extern struct device_attribute dev_attr_gpvndctl;
  53252. +extern struct device_attribute dev_attr_ggpio;
  53253. +extern struct device_attribute dev_attr_guid;
  53254. +extern struct device_attribute dev_attr_gsnpsid;
  53255. +extern struct device_attribute dev_attr_devspeed;
  53256. +extern struct device_attribute dev_attr_enumspeed;
  53257. +extern struct device_attribute dev_attr_hptxfsiz;
  53258. +extern struct device_attribute dev_attr_hprt0;
  53259. +#ifdef CONFIG_USB_DWC_OTG_LPM
  53260. +extern struct device_attribute dev_attr_lpm_response;
  53261. +extern struct device_attribute devi_attr_sleep_status;
  53262. +#endif
  53263. +
  53264. +void dwc_otg_attr_create(
  53265. +#ifdef LM_INTERFACE
  53266. + struct lm_device *dev
  53267. +#elif defined(PCI_INTERFACE)
  53268. + struct pci_dev *dev
  53269. +#elif defined(PLATFORM_INTERFACE)
  53270. + struct platform_device *dev
  53271. +#endif
  53272. + );
  53273. +
  53274. +void dwc_otg_attr_remove(
  53275. +#ifdef LM_INTERFACE
  53276. + struct lm_device *dev
  53277. +#elif defined(PCI_INTERFACE)
  53278. + struct pci_dev *dev
  53279. +#elif defined(PLATFORM_INTERFACE)
  53280. + struct platform_device *dev
  53281. +#endif
  53282. + );
  53283. +#endif
  53284. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cfi.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  53285. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 1970-01-01 01:00:00.000000000 +0100
  53286. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 2014-04-24 15:35:04.173565776 +0200
  53287. @@ -0,0 +1,1876 @@
  53288. +/* ==========================================================================
  53289. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  53290. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  53291. + * otherwise expressly agreed to in writing between Synopsys and you.
  53292. + *
  53293. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  53294. + * any End User Software License Agreement or Agreement for Licensed Product
  53295. + * with Synopsys or any supplement thereto. You are permitted to use and
  53296. + * redistribute this Software in source and binary forms, with or without
  53297. + * modification, provided that redistributions of source code must retain this
  53298. + * notice. You may not view, use, disclose, copy or distribute this file or
  53299. + * any information contained herein except pursuant to this license grant from
  53300. + * Synopsys. If you do not agree with this notice, including the disclaimer
  53301. + * below, then you are not authorized to use the Software.
  53302. + *
  53303. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  53304. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53305. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53306. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  53307. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53308. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53309. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  53310. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53311. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53312. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  53313. + * DAMAGE.
  53314. + * ========================================================================== */
  53315. +
  53316. +/** @file
  53317. + *
  53318. + * This file contains the most of the CFI(Core Feature Interface)
  53319. + * implementation for the OTG.
  53320. + */
  53321. +
  53322. +#ifdef DWC_UTE_CFI
  53323. +
  53324. +#include "dwc_otg_pcd.h"
  53325. +#include "dwc_otg_cfi.h"
  53326. +
  53327. +/** This definition should actually migrate to the Portability Library */
  53328. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  53329. +
  53330. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  53331. +
  53332. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  53333. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  53334. + struct dwc_otg_pcd *pcd,
  53335. + struct cfi_usb_ctrlrequest *ctrl_req);
  53336. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  53337. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  53338. + struct cfi_usb_ctrlrequest *req);
  53339. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  53340. + struct cfi_usb_ctrlrequest *req);
  53341. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  53342. + struct cfi_usb_ctrlrequest *req);
  53343. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  53344. + struct cfi_usb_ctrlrequest *req);
  53345. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  53346. +
  53347. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  53348. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  53349. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  53350. +
  53351. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  53352. +
  53353. +/** This is the header of the all features descriptor */
  53354. +static cfi_all_features_header_t all_props_desc_header = {
  53355. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  53356. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  53357. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  53358. +};
  53359. +
  53360. +/** This is an array of statically allocated feature descriptors */
  53361. +static cfi_feature_desc_header_t prop_descs[] = {
  53362. +
  53363. + /* FT_ID_DMA_MODE */
  53364. + {
  53365. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  53366. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53367. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  53368. + },
  53369. +
  53370. + /* FT_ID_DMA_BUFFER_SETUP */
  53371. + {
  53372. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  53373. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53374. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53375. + },
  53376. +
  53377. + /* FT_ID_DMA_BUFF_ALIGN */
  53378. + {
  53379. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  53380. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53381. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53382. + },
  53383. +
  53384. + /* FT_ID_DMA_CONCAT_SETUP */
  53385. + {
  53386. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  53387. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53388. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53389. + },
  53390. +
  53391. + /* FT_ID_DMA_CIRCULAR */
  53392. + {
  53393. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  53394. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53395. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53396. + },
  53397. +
  53398. + /* FT_ID_THRESHOLD_SETUP */
  53399. + {
  53400. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  53401. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53402. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  53403. + },
  53404. +
  53405. + /* FT_ID_DFIFO_DEPTH */
  53406. + {
  53407. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  53408. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  53409. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53410. + },
  53411. +
  53412. + /* FT_ID_TX_FIFO_DEPTH */
  53413. + {
  53414. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  53415. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53416. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53417. + },
  53418. +
  53419. + /* FT_ID_RX_FIFO_DEPTH */
  53420. + {
  53421. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  53422. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  53423. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  53424. + }
  53425. +};
  53426. +
  53427. +/** The table of feature names */
  53428. +cfi_string_t prop_name_table[] = {
  53429. + {FT_ID_DMA_MODE, "dma_mode"},
  53430. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  53431. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  53432. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  53433. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  53434. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  53435. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  53436. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  53437. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  53438. + {}
  53439. +};
  53440. +
  53441. +/************************************************************************/
  53442. +
  53443. +/**
  53444. + * Returns the name of the feature by its ID
  53445. + * or NULL if no featute ID matches.
  53446. + *
  53447. + */
  53448. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  53449. +{
  53450. + cfi_string_t *pstr;
  53451. + *len = 0;
  53452. +
  53453. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  53454. + if (pstr->id == prop_id) {
  53455. + *len = DWC_STRLEN(pstr->s);
  53456. + return pstr->s;
  53457. + }
  53458. + }
  53459. + return NULL;
  53460. +}
  53461. +
  53462. +/**
  53463. + * This function handles all CFI specific control requests.
  53464. + *
  53465. + * Return a negative value to stall the DCE.
  53466. + */
  53467. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  53468. +{
  53469. + int retval = 0;
  53470. + dwc_otg_pcd_ep_t *ep = NULL;
  53471. + cfiobject_t *cfi = pcd->cfi;
  53472. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  53473. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  53474. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  53475. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  53476. + uint32_t regaddr = 0;
  53477. + uint32_t regval = 0;
  53478. +
  53479. + /* Save this Control Request in the CFI object.
  53480. + * The data field will be assigned in the data stage completion CB function.
  53481. + */
  53482. + cfi->ctrl_req = *ctrl;
  53483. + cfi->ctrl_req.data = NULL;
  53484. +
  53485. + cfi->need_gadget_att = 0;
  53486. + cfi->need_status_in_complete = 0;
  53487. +
  53488. + switch (ctrl->bRequest) {
  53489. + case VEN_CORE_GET_FEATURES:
  53490. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  53491. + if (retval >= 0) {
  53492. + //dump_msg(cfi->buf_in.buf, retval);
  53493. + ep = &pcd->ep0;
  53494. +
  53495. + retval = min((uint16_t) retval, wLen);
  53496. + /* Transfer this buffer to the host through the EP0-IN EP */
  53497. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  53498. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  53499. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  53500. + ep->dwc_ep.xfer_len = retval;
  53501. + ep->dwc_ep.xfer_count = 0;
  53502. + ep->dwc_ep.sent_zlp = 0;
  53503. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53504. +
  53505. + pcd->ep0_pending = 1;
  53506. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53507. + }
  53508. + retval = 0;
  53509. + break;
  53510. +
  53511. + case VEN_CORE_GET_FEATURE:
  53512. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  53513. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  53514. + pcd, ctrl);
  53515. + if (retval >= 0) {
  53516. + ep = &pcd->ep0;
  53517. +
  53518. + retval = min((uint16_t) retval, wLen);
  53519. + /* Transfer this buffer to the host through the EP0-IN EP */
  53520. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  53521. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  53522. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  53523. + ep->dwc_ep.xfer_len = retval;
  53524. + ep->dwc_ep.xfer_count = 0;
  53525. + ep->dwc_ep.sent_zlp = 0;
  53526. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53527. +
  53528. + pcd->ep0_pending = 1;
  53529. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53530. + }
  53531. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  53532. + dump_msg(cfi->buf_in.buf, retval);
  53533. + break;
  53534. +
  53535. + case VEN_CORE_SET_FEATURE:
  53536. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  53537. + /* Set up an XFER to get the data stage of the control request,
  53538. + * which is the new value of the feature to be modified.
  53539. + */
  53540. + ep = &pcd->ep0;
  53541. + ep->dwc_ep.is_in = 0;
  53542. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  53543. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  53544. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  53545. + ep->dwc_ep.xfer_len = wLen;
  53546. + ep->dwc_ep.xfer_count = 0;
  53547. + ep->dwc_ep.sent_zlp = 0;
  53548. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53549. +
  53550. + pcd->ep0_pending = 1;
  53551. + /* Read the control write's data stage */
  53552. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53553. + retval = 0;
  53554. + break;
  53555. +
  53556. + case VEN_CORE_RESET_FEATURES:
  53557. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  53558. + cfi->need_gadget_att = 1;
  53559. + cfi->need_status_in_complete = 1;
  53560. + retval = cfi_preproc_reset(pcd, ctrl);
  53561. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  53562. + break;
  53563. +
  53564. + case VEN_CORE_ACTIVATE_FEATURES:
  53565. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  53566. + break;
  53567. +
  53568. + case VEN_CORE_READ_REGISTER:
  53569. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  53570. + /* wValue optionally contains the HI WORD of the register offset and
  53571. + * wIndex contains the LOW WORD of the register offset
  53572. + */
  53573. + if (wValue == 0) {
  53574. + /* @TODO - MAS - fix the access to the base field */
  53575. + regaddr = 0;
  53576. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  53577. + //GET_CORE_IF(pcd)->co
  53578. + regaddr |= wIndex;
  53579. + } else {
  53580. + regaddr = (wValue << 16) | wIndex;
  53581. + }
  53582. +
  53583. + /* Read a 32-bit value of the memory at the regaddr */
  53584. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  53585. +
  53586. + ep = &pcd->ep0;
  53587. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  53588. + ep->dwc_ep.is_in = 1;
  53589. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  53590. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  53591. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  53592. + ep->dwc_ep.xfer_len = wLen;
  53593. + ep->dwc_ep.xfer_count = 0;
  53594. + ep->dwc_ep.sent_zlp = 0;
  53595. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53596. +
  53597. + pcd->ep0_pending = 1;
  53598. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53599. + cfi->need_gadget_att = 0;
  53600. + retval = 0;
  53601. + break;
  53602. +
  53603. + case VEN_CORE_WRITE_REGISTER:
  53604. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  53605. + /* Set up an XFER to get the data stage of the control request,
  53606. + * which is the new value of the register to be modified.
  53607. + */
  53608. + ep = &pcd->ep0;
  53609. + ep->dwc_ep.is_in = 0;
  53610. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  53611. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  53612. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  53613. + ep->dwc_ep.xfer_len = wLen;
  53614. + ep->dwc_ep.xfer_count = 0;
  53615. + ep->dwc_ep.sent_zlp = 0;
  53616. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  53617. +
  53618. + pcd->ep0_pending = 1;
  53619. + /* Read the control write's data stage */
  53620. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  53621. + retval = 0;
  53622. + break;
  53623. +
  53624. + default:
  53625. + retval = -DWC_E_NOT_SUPPORTED;
  53626. + break;
  53627. + }
  53628. +
  53629. + return retval;
  53630. +}
  53631. +
  53632. +/**
  53633. + * This function prepares the core features descriptors and copies its
  53634. + * raw representation into the buffer <buf>.
  53635. + *
  53636. + * The buffer structure is as follows:
  53637. + * all_features_header (8 bytes)
  53638. + * features_#1 (8 bytes + feature name string length)
  53639. + * features_#2 (8 bytes + feature name string length)
  53640. + * .....
  53641. + * features_#n - where n=the total count of feature descriptors
  53642. + */
  53643. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  53644. +{
  53645. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  53646. + cfi_feature_desc_header_t *prop;
  53647. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  53648. + cfi_all_features_header_t *tmp;
  53649. + uint8_t *tmpbuf = buf;
  53650. + const uint8_t *pname = NULL;
  53651. + int i, j, namelen = 0, totlen;
  53652. +
  53653. + /* Prepare and copy the core features into the buffer */
  53654. + CFI_INFO("%s:\n", __func__);
  53655. +
  53656. + tmp = (cfi_all_features_header_t *) tmpbuf;
  53657. + *tmp = *all_props_hdr;
  53658. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  53659. +
  53660. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  53661. + for (i = 0; i < j; i++, prop_hdr++) {
  53662. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  53663. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  53664. + *prop = *prop_hdr;
  53665. +
  53666. + prop->bNameLen = namelen;
  53667. + prop->wLength =
  53668. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  53669. + namelen);
  53670. +
  53671. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  53672. + dwc_memcpy(tmpbuf, pname, namelen);
  53673. + tmpbuf += namelen;
  53674. + }
  53675. +
  53676. + totlen = tmpbuf - buf;
  53677. +
  53678. + if (totlen > 0) {
  53679. + tmp = (cfi_all_features_header_t *) buf;
  53680. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  53681. + }
  53682. +
  53683. + return totlen;
  53684. +}
  53685. +
  53686. +/**
  53687. + * This function releases all the dynamic memory in the CFI object.
  53688. + */
  53689. +static void cfi_release(cfiobject_t * cfiobj)
  53690. +{
  53691. + cfi_ep_t *cfiep;
  53692. + dwc_list_link_t *tmp;
  53693. +
  53694. + CFI_INFO("%s\n", __func__);
  53695. +
  53696. + if (cfiobj->buf_in.buf) {
  53697. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  53698. + cfiobj->buf_in.addr);
  53699. + cfiobj->buf_in.buf = NULL;
  53700. + }
  53701. +
  53702. + if (cfiobj->buf_out.buf) {
  53703. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  53704. + cfiobj->buf_out.addr);
  53705. + cfiobj->buf_out.buf = NULL;
  53706. + }
  53707. +
  53708. + /* Free the Buffer Setup values for each EP */
  53709. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  53710. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  53711. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53712. + cfi_free_ep_bs_dyn_data(cfiep);
  53713. + }
  53714. +}
  53715. +
  53716. +/**
  53717. + * This function frees the dynamically allocated EP buffer setup data.
  53718. + */
  53719. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  53720. +{
  53721. + if (cfiep->bm_sg) {
  53722. + DWC_FREE(cfiep->bm_sg);
  53723. + cfiep->bm_sg = NULL;
  53724. + }
  53725. +
  53726. + if (cfiep->bm_align) {
  53727. + DWC_FREE(cfiep->bm_align);
  53728. + cfiep->bm_align = NULL;
  53729. + }
  53730. +
  53731. + if (cfiep->bm_concat) {
  53732. + if (NULL != cfiep->bm_concat->wTxBytes) {
  53733. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  53734. + cfiep->bm_concat->wTxBytes = NULL;
  53735. + }
  53736. + DWC_FREE(cfiep->bm_concat);
  53737. + cfiep->bm_concat = NULL;
  53738. + }
  53739. +}
  53740. +
  53741. +/**
  53742. + * This function initializes the default values of the features
  53743. + * for a specific endpoint and should be called only once when
  53744. + * the EP is enabled first time.
  53745. + */
  53746. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  53747. +{
  53748. + int retval = 0;
  53749. +
  53750. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  53751. + if (NULL == cfiep->bm_sg) {
  53752. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  53753. + return -DWC_E_NO_MEMORY;
  53754. + }
  53755. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  53756. +
  53757. + /* For the Concatenation feature's default value we do not allocate
  53758. + * memory for the wTxBytes field - it will be done in the set_feature_value
  53759. + * request handler.
  53760. + */
  53761. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  53762. + if (NULL == cfiep->bm_concat) {
  53763. + CFI_INFO
  53764. + ("Failed to allocate memory for CONCATENATION feature value\n");
  53765. + DWC_FREE(cfiep->bm_sg);
  53766. + return -DWC_E_NO_MEMORY;
  53767. + }
  53768. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  53769. +
  53770. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  53771. + if (NULL == cfiep->bm_align) {
  53772. + CFI_INFO
  53773. + ("Failed to allocate memory for Alignment feature value\n");
  53774. + DWC_FREE(cfiep->bm_sg);
  53775. + DWC_FREE(cfiep->bm_concat);
  53776. + return -DWC_E_NO_MEMORY;
  53777. + }
  53778. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  53779. +
  53780. + return retval;
  53781. +}
  53782. +
  53783. +/**
  53784. + * The callback function that notifies the CFI on the activation of
  53785. + * an endpoint in the PCD. The following steps are done in this function:
  53786. + *
  53787. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  53788. + * active endpoint)
  53789. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  53790. + * Set the Buffer Mode to standard
  53791. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  53792. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  53793. + */
  53794. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  53795. + struct dwc_otg_pcd_ep *ep)
  53796. +{
  53797. + cfi_ep_t *cfiep;
  53798. + int retval = -DWC_E_NOT_SUPPORTED;
  53799. +
  53800. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  53801. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  53802. + /* MAS - Check whether this endpoint already is in the list */
  53803. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  53804. +
  53805. + if (NULL == cfiep) {
  53806. + /* Allocate a cfi_ep_t object */
  53807. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  53808. + if (NULL == cfiep) {
  53809. + CFI_INFO
  53810. + ("Unable to allocate memory for <cfiep> in function %s\n",
  53811. + __func__);
  53812. + return -DWC_E_NO_MEMORY;
  53813. + }
  53814. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  53815. +
  53816. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  53817. + cfiep->ep = ep;
  53818. +
  53819. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  53820. + ep->dwc_ep.descs =
  53821. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  53822. + sizeof(dwc_otg_dma_desc_t),
  53823. + &ep->dwc_ep.descs_dma_addr);
  53824. +
  53825. + if (NULL == ep->dwc_ep.descs) {
  53826. + DWC_FREE(cfiep);
  53827. + return -DWC_E_NO_MEMORY;
  53828. + }
  53829. +
  53830. + DWC_LIST_INIT(&cfiep->lh);
  53831. +
  53832. + /* Set the buffer mode to BM_STANDARD. It will be modified
  53833. + * when building descriptors for a specific buffer mode */
  53834. + ep->dwc_ep.buff_mode = BM_STANDARD;
  53835. +
  53836. + /* Create and initialize the default values for this EP's Buffer modes */
  53837. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  53838. + return retval;
  53839. +
  53840. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  53841. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  53842. + retval = 0;
  53843. + } else { /* The sought EP already is in the list */
  53844. + CFI_INFO("%s: The sought EP already is in the list\n",
  53845. + __func__);
  53846. + }
  53847. +
  53848. + return retval;
  53849. +}
  53850. +
  53851. +/**
  53852. + * This function is called when the data stage of a 3-stage Control Write request
  53853. + * is complete.
  53854. + *
  53855. + */
  53856. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  53857. + struct dwc_otg_pcd *pcd)
  53858. +{
  53859. + uint32_t addr, reg_value;
  53860. + uint16_t wIndex, wValue;
  53861. + uint8_t bRequest;
  53862. + uint8_t *buf = cfi->buf_out.buf;
  53863. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  53864. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  53865. + int retval = -DWC_E_NOT_SUPPORTED;
  53866. +
  53867. + CFI_INFO("%s\n", __func__);
  53868. +
  53869. + bRequest = ctrl_req->bRequest;
  53870. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  53871. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  53872. +
  53873. + /*
  53874. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  53875. + * The request should be already saved in the command stage by now.
  53876. + */
  53877. + ctrl_req->data = cfi->buf_out.buf;
  53878. + cfi->need_status_in_complete = 0;
  53879. + cfi->need_gadget_att = 0;
  53880. +
  53881. + switch (bRequest) {
  53882. + case VEN_CORE_WRITE_REGISTER:
  53883. + /* The buffer contains raw data of the new value for the register */
  53884. + reg_value = *((uint32_t *) buf);
  53885. + if (wValue == 0) {
  53886. + addr = 0;
  53887. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  53888. + addr += wIndex;
  53889. + } else {
  53890. + addr = (wValue << 16) | wIndex;
  53891. + }
  53892. +
  53893. + //writel(reg_value, addr);
  53894. +
  53895. + retval = 0;
  53896. + cfi->need_status_in_complete = 1;
  53897. + break;
  53898. +
  53899. + case VEN_CORE_SET_FEATURE:
  53900. + /* The buffer contains raw data of the new value of the feature */
  53901. + retval = cfi_set_feature_value(pcd);
  53902. + if (retval < 0)
  53903. + return retval;
  53904. +
  53905. + cfi->need_status_in_complete = 1;
  53906. + break;
  53907. +
  53908. + default:
  53909. + break;
  53910. + }
  53911. +
  53912. + return retval;
  53913. +}
  53914. +
  53915. +/**
  53916. + * This function builds the DMA descriptors for the SG buffer mode.
  53917. + */
  53918. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53919. + dwc_otg_pcd_request_t * req)
  53920. +{
  53921. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53922. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  53923. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53924. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  53925. + dma_addr_t buff_addr = req->dma;
  53926. + int i;
  53927. + uint32_t txsize, off;
  53928. +
  53929. + txsize = sgval->wSize;
  53930. + off = sgval->bOffset;
  53931. +
  53932. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  53933. +// __func__, cfiep->ep->ep.name, txsize, off);
  53934. +
  53935. + for (i = 0; i < sgval->bCount; i++) {
  53936. + desc->status.b.bs = BS_HOST_BUSY;
  53937. + desc->buf = buff_addr;
  53938. + desc->status.b.l = 0;
  53939. + desc->status.b.ioc = 0;
  53940. + desc->status.b.sp = 0;
  53941. + desc->status.b.bytes = txsize;
  53942. + desc->status.b.bs = BS_HOST_READY;
  53943. +
  53944. + /* Set the next address of the buffer */
  53945. + buff_addr += txsize + off;
  53946. + desc_last = desc;
  53947. + desc++;
  53948. + }
  53949. +
  53950. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  53951. + desc_last->status.b.l = 1;
  53952. + desc_last->status.b.ioc = 1;
  53953. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  53954. + /* Save the last DMA descriptor pointer */
  53955. + cfiep->dma_desc_last = desc_last;
  53956. + cfiep->desc_count = sgval->bCount;
  53957. +}
  53958. +
  53959. +/**
  53960. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  53961. + */
  53962. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53963. + dwc_otg_pcd_request_t * req)
  53964. +{
  53965. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53966. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  53967. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53968. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  53969. + dma_addr_t buff_addr = req->dma;
  53970. + int i;
  53971. + uint16_t *txsize;
  53972. +
  53973. + txsize = concatval->wTxBytes;
  53974. +
  53975. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  53976. + desc->buf = buff_addr;
  53977. + desc->status.b.bs = BS_HOST_BUSY;
  53978. + desc->status.b.l = 0;
  53979. + desc->status.b.ioc = 0;
  53980. + desc->status.b.sp = 0;
  53981. + desc->status.b.bytes = *txsize;
  53982. + desc->status.b.bs = BS_HOST_READY;
  53983. +
  53984. + txsize++;
  53985. + /* Set the next address of the buffer */
  53986. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  53987. + desc_last = desc;
  53988. + desc++;
  53989. + }
  53990. +
  53991. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  53992. + desc_last->status.b.l = 1;
  53993. + desc_last->status.b.ioc = 1;
  53994. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  53995. + cfiep->dma_desc_last = desc_last;
  53996. + cfiep->desc_count = concatval->hdr.bDescCount;
  53997. +}
  53998. +
  53999. +/**
  54000. + * This function builds the DMA descriptors for the Circular buffer mode
  54001. + */
  54002. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  54003. + dwc_otg_pcd_request_t * req)
  54004. +{
  54005. + /* @todo: MAS - add implementation when this feature needs to be tested */
  54006. +}
  54007. +
  54008. +/**
  54009. + * This function builds the DMA descriptors for the Alignment buffer mode
  54010. + */
  54011. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  54012. + dwc_otg_pcd_request_t * req)
  54013. +{
  54014. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  54015. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  54016. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  54017. + dma_addr_t buff_addr = req->dma;
  54018. +
  54019. + desc->status.b.bs = BS_HOST_BUSY;
  54020. + desc->status.b.l = 1;
  54021. + desc->status.b.ioc = 1;
  54022. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  54023. + desc->status.b.bytes = req->length;
  54024. + /* Adjust the buffer alignment */
  54025. + desc->buf = (buff_addr + alignval->bAlign);
  54026. + desc->status.b.bs = BS_HOST_READY;
  54027. + cfiep->dma_desc_last = desc;
  54028. + cfiep->desc_count = 1;
  54029. +}
  54030. +
  54031. +/**
  54032. + * This function builds the DMA descriptors chain for different modes of the
  54033. + * buffer setup of an endpoint.
  54034. + */
  54035. +static void cfi_build_descriptors(struct cfiobject *cfi,
  54036. + struct dwc_otg_pcd *pcd,
  54037. + struct dwc_otg_pcd_ep *ep,
  54038. + dwc_otg_pcd_request_t * req)
  54039. +{
  54040. + cfi_ep_t *cfiep;
  54041. +
  54042. + /* Get the cfiep by the dwc_otg_pcd_ep */
  54043. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  54044. + if (NULL == cfiep) {
  54045. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  54046. + __func__);
  54047. + return;
  54048. + }
  54049. +
  54050. + cfiep->xfer_len = req->length;
  54051. +
  54052. + /* Iterate through all the DMA descriptors */
  54053. + switch (cfiep->ep->dwc_ep.buff_mode) {
  54054. + case BM_SG:
  54055. + cfi_build_sg_descs(cfi, cfiep, req);
  54056. + break;
  54057. +
  54058. + case BM_CONCAT:
  54059. + cfi_build_concat_descs(cfi, cfiep, req);
  54060. + break;
  54061. +
  54062. + case BM_CIRCULAR:
  54063. + cfi_build_circ_descs(cfi, cfiep, req);
  54064. + break;
  54065. +
  54066. + case BM_ALIGN:
  54067. + cfi_build_align_descs(cfi, cfiep, req);
  54068. + break;
  54069. +
  54070. + default:
  54071. + break;
  54072. + }
  54073. +}
  54074. +
  54075. +/**
  54076. + * Allocate DMA buffer for different Buffer modes.
  54077. + */
  54078. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  54079. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  54080. + unsigned size, gfp_t flags)
  54081. +{
  54082. + return DWC_DMA_ALLOC(size, dma);
  54083. +}
  54084. +
  54085. +/**
  54086. + * This function initializes the CFI object.
  54087. + */
  54088. +int init_cfi(cfiobject_t * cfiobj)
  54089. +{
  54090. + CFI_INFO("%s\n", __func__);
  54091. +
  54092. + /* Allocate a buffer for IN XFERs */
  54093. + cfiobj->buf_in.buf =
  54094. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  54095. + if (NULL == cfiobj->buf_in.buf) {
  54096. + CFI_INFO("Unable to allocate buffer for INs\n");
  54097. + return -DWC_E_NO_MEMORY;
  54098. + }
  54099. +
  54100. + /* Allocate a buffer for OUT XFERs */
  54101. + cfiobj->buf_out.buf =
  54102. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  54103. + if (NULL == cfiobj->buf_out.buf) {
  54104. + CFI_INFO("Unable to allocate buffer for OUT\n");
  54105. + return -DWC_E_NO_MEMORY;
  54106. + }
  54107. +
  54108. + /* Initialize the callback function pointers */
  54109. + cfiobj->ops.release = cfi_release;
  54110. + cfiobj->ops.ep_enable = cfi_ep_enable;
  54111. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  54112. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  54113. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  54114. +
  54115. + /* Initialize the list of active endpoints in the CFI object */
  54116. + DWC_LIST_INIT(&cfiobj->active_eps);
  54117. +
  54118. + return 0;
  54119. +}
  54120. +
  54121. +/**
  54122. + * This function reads the required feature's current value into the buffer
  54123. + *
  54124. + * @retval: Returns negative as error, or the data length of the feature
  54125. + */
  54126. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  54127. + struct dwc_otg_pcd *pcd,
  54128. + struct cfi_usb_ctrlrequest *ctrl_req)
  54129. +{
  54130. + int retval = -DWC_E_NOT_SUPPORTED;
  54131. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  54132. + uint16_t dfifo, rxfifo, txfifo;
  54133. +
  54134. + switch (ctrl_req->wIndex) {
  54135. + /* Whether the DDMA is enabled or not */
  54136. + case FT_ID_DMA_MODE:
  54137. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  54138. + retval = 1;
  54139. + break;
  54140. +
  54141. + case FT_ID_DMA_BUFFER_SETUP:
  54142. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  54143. + break;
  54144. +
  54145. + case FT_ID_DMA_BUFF_ALIGN:
  54146. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  54147. + break;
  54148. +
  54149. + case FT_ID_DMA_CONCAT_SETUP:
  54150. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  54151. + break;
  54152. +
  54153. + case FT_ID_DMA_CIRCULAR:
  54154. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  54155. + break;
  54156. +
  54157. + case FT_ID_THRESHOLD_SETUP:
  54158. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  54159. + break;
  54160. +
  54161. + case FT_ID_DFIFO_DEPTH:
  54162. + dfifo = get_dfifo_size(coreif);
  54163. + *((uint16_t *) buf) = dfifo;
  54164. + retval = sizeof(uint16_t);
  54165. + break;
  54166. +
  54167. + case FT_ID_TX_FIFO_DEPTH:
  54168. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  54169. + if (retval >= 0) {
  54170. + txfifo = retval;
  54171. + *((uint16_t *) buf) = txfifo;
  54172. + retval = sizeof(uint16_t);
  54173. + }
  54174. + break;
  54175. +
  54176. + case FT_ID_RX_FIFO_DEPTH:
  54177. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  54178. + if (retval >= 0) {
  54179. + rxfifo = retval;
  54180. + *((uint16_t *) buf) = rxfifo;
  54181. + retval = sizeof(uint16_t);
  54182. + }
  54183. + break;
  54184. + }
  54185. +
  54186. + return retval;
  54187. +}
  54188. +
  54189. +/**
  54190. + * This function resets the SG for the specified EP to its default value
  54191. + */
  54192. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  54193. +{
  54194. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  54195. + return 0;
  54196. +}
  54197. +
  54198. +/**
  54199. + * This function resets the Alignment for the specified EP to its default value
  54200. + */
  54201. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  54202. +{
  54203. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  54204. + return 0;
  54205. +}
  54206. +
  54207. +/**
  54208. + * This function resets the Concatenation for the specified EP to its default value
  54209. + * This function will also set the value of the wTxBytes field to NULL after
  54210. + * freeing the memory previously allocated for this field.
  54211. + */
  54212. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  54213. +{
  54214. + /* First we need to free the wTxBytes field */
  54215. + if (cfiep->bm_concat->wTxBytes) {
  54216. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  54217. + cfiep->bm_concat->wTxBytes = NULL;
  54218. + }
  54219. +
  54220. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  54221. + return 0;
  54222. +}
  54223. +
  54224. +/**
  54225. + * This function resets all the buffer setups of the specified endpoint
  54226. + */
  54227. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  54228. +{
  54229. + cfi_reset_sg_val(cfiep);
  54230. + cfi_reset_align_val(cfiep);
  54231. + cfi_reset_concat_val(cfiep);
  54232. + return 0;
  54233. +}
  54234. +
  54235. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  54236. + uint8_t rx_rst, uint8_t tx_rst)
  54237. +{
  54238. + int retval = -DWC_E_INVALID;
  54239. + uint16_t tx_siz[15];
  54240. + uint16_t rx_siz = 0;
  54241. + dwc_otg_pcd_ep_t *ep = NULL;
  54242. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54243. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54244. +
  54245. + if (rx_rst) {
  54246. + rx_siz = params->dev_rx_fifo_size;
  54247. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  54248. + }
  54249. +
  54250. + if (tx_rst) {
  54251. + if (ep_addr == 0) {
  54252. + int i;
  54253. +
  54254. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54255. + tx_siz[i] =
  54256. + core_if->core_params->dev_tx_fifo_size[i];
  54257. + core_if->core_params->dev_tx_fifo_size[i] =
  54258. + core_if->init_txfsiz[i];
  54259. + }
  54260. + } else {
  54261. +
  54262. + ep = get_ep_by_addr(pcd, ep_addr);
  54263. +
  54264. + if (NULL == ep) {
  54265. + CFI_INFO
  54266. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  54267. + __func__, ep_addr);
  54268. + return -DWC_E_INVALID;
  54269. + }
  54270. +
  54271. + tx_siz[0] =
  54272. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  54273. + 1];
  54274. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  54275. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  54276. + dwc_ep.tx_fifo_num -
  54277. + 1];
  54278. + }
  54279. + }
  54280. +
  54281. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54282. + retval = 0;
  54283. + } else {
  54284. + CFI_INFO
  54285. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  54286. + __func__);
  54287. + if (rx_rst) {
  54288. + params->dev_rx_fifo_size = rx_siz;
  54289. + }
  54290. +
  54291. + if (tx_rst) {
  54292. + if (ep_addr == 0) {
  54293. + int i;
  54294. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  54295. + i++) {
  54296. + core_if->
  54297. + core_params->dev_tx_fifo_size[i] =
  54298. + tx_siz[i];
  54299. + }
  54300. + } else {
  54301. + params->dev_tx_fifo_size[ep->
  54302. + dwc_ep.tx_fifo_num -
  54303. + 1] = tx_siz[0];
  54304. + }
  54305. + }
  54306. + retval = -DWC_E_INVALID;
  54307. + }
  54308. + return retval;
  54309. +}
  54310. +
  54311. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  54312. +{
  54313. + int retval = 0;
  54314. + cfi_ep_t *cfiep;
  54315. + cfiobject_t *cfi = pcd->cfi;
  54316. + dwc_list_link_t *tmp;
  54317. +
  54318. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  54319. + if (retval < 0) {
  54320. + return retval;
  54321. + }
  54322. +
  54323. + /* If the EP address is known then reset the features for only that EP */
  54324. + if (addr) {
  54325. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54326. + if (NULL == cfiep) {
  54327. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54328. + __func__, addr);
  54329. + return -DWC_E_INVALID;
  54330. + }
  54331. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  54332. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  54333. + }
  54334. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54335. + else {
  54336. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54337. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54338. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54339. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54340. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  54341. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  54342. + if (retval < 0) {
  54343. + CFI_INFO
  54344. + ("%s: Error resetting the feature Reset All\n",
  54345. + __func__);
  54346. + return retval;
  54347. + }
  54348. + }
  54349. + }
  54350. + return retval;
  54351. +}
  54352. +
  54353. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  54354. + uint8_t addr)
  54355. +{
  54356. + int retval = 0;
  54357. + cfi_ep_t *cfiep;
  54358. + cfiobject_t *cfi = pcd->cfi;
  54359. + dwc_list_link_t *tmp;
  54360. +
  54361. + /* If the EP address is known then reset the features for only that EP */
  54362. + if (addr) {
  54363. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54364. + if (NULL == cfiep) {
  54365. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54366. + __func__, addr);
  54367. + return -DWC_E_INVALID;
  54368. + }
  54369. + retval = cfi_reset_sg_val(cfiep);
  54370. + }
  54371. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54372. + else {
  54373. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54374. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54375. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54376. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54377. + retval = cfi_reset_sg_val(cfiep);
  54378. + if (retval < 0) {
  54379. + CFI_INFO
  54380. + ("%s: Error resetting the feature Buffer Setup\n",
  54381. + __func__);
  54382. + return retval;
  54383. + }
  54384. + }
  54385. + }
  54386. + return retval;
  54387. +}
  54388. +
  54389. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  54390. +{
  54391. + int retval = 0;
  54392. + cfi_ep_t *cfiep;
  54393. + cfiobject_t *cfi = pcd->cfi;
  54394. + dwc_list_link_t *tmp;
  54395. +
  54396. + /* If the EP address is known then reset the features for only that EP */
  54397. + if (addr) {
  54398. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54399. + if (NULL == cfiep) {
  54400. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54401. + __func__, addr);
  54402. + return -DWC_E_INVALID;
  54403. + }
  54404. + retval = cfi_reset_concat_val(cfiep);
  54405. + }
  54406. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54407. + else {
  54408. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54409. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54410. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54411. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54412. + retval = cfi_reset_concat_val(cfiep);
  54413. + if (retval < 0) {
  54414. + CFI_INFO
  54415. + ("%s: Error resetting the feature Concatenation Value\n",
  54416. + __func__);
  54417. + return retval;
  54418. + }
  54419. + }
  54420. + }
  54421. + return retval;
  54422. +}
  54423. +
  54424. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  54425. +{
  54426. + int retval = 0;
  54427. + cfi_ep_t *cfiep;
  54428. + cfiobject_t *cfi = pcd->cfi;
  54429. + dwc_list_link_t *tmp;
  54430. +
  54431. + /* If the EP address is known then reset the features for only that EP */
  54432. + if (addr) {
  54433. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54434. + if (NULL == cfiep) {
  54435. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  54436. + __func__, addr);
  54437. + return -DWC_E_INVALID;
  54438. + }
  54439. + retval = cfi_reset_align_val(cfiep);
  54440. + }
  54441. + /* Otherwise (wValue == 0), reset all features of all EP's */
  54442. + else {
  54443. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  54444. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  54445. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54446. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54447. + retval = cfi_reset_align_val(cfiep);
  54448. + if (retval < 0) {
  54449. + CFI_INFO
  54450. + ("%s: Error resetting the feature Aliignment Value\n",
  54451. + __func__);
  54452. + return retval;
  54453. + }
  54454. + }
  54455. + }
  54456. + return retval;
  54457. +
  54458. +}
  54459. +
  54460. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  54461. + struct cfi_usb_ctrlrequest *req)
  54462. +{
  54463. + int retval = 0;
  54464. +
  54465. + switch (req->wIndex) {
  54466. + case 0:
  54467. + /* Reset all features */
  54468. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  54469. + break;
  54470. +
  54471. + case FT_ID_DMA_BUFFER_SETUP:
  54472. + /* Reset the SG buffer setup */
  54473. + retval =
  54474. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  54475. + break;
  54476. +
  54477. + case FT_ID_DMA_CONCAT_SETUP:
  54478. + /* Reset the Concatenation buffer setup */
  54479. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  54480. + break;
  54481. +
  54482. + case FT_ID_DMA_BUFF_ALIGN:
  54483. + /* Reset the Alignment buffer setup */
  54484. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  54485. + break;
  54486. +
  54487. + case FT_ID_TX_FIFO_DEPTH:
  54488. + retval =
  54489. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  54490. + pcd->cfi->need_gadget_att = 0;
  54491. + break;
  54492. +
  54493. + case FT_ID_RX_FIFO_DEPTH:
  54494. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  54495. + pcd->cfi->need_gadget_att = 0;
  54496. + break;
  54497. + default:
  54498. + break;
  54499. + }
  54500. + return retval;
  54501. +}
  54502. +
  54503. +/**
  54504. + * This function sets a new value for the SG buffer setup.
  54505. + */
  54506. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  54507. +{
  54508. + uint8_t inaddr, outaddr;
  54509. + cfi_ep_t *epin, *epout;
  54510. + ddma_sg_buffer_setup_t *psgval;
  54511. + uint32_t desccount, size;
  54512. +
  54513. + CFI_INFO("%s\n", __func__);
  54514. +
  54515. + psgval = (ddma_sg_buffer_setup_t *) buf;
  54516. + desccount = (uint32_t) psgval->bCount;
  54517. + size = (uint32_t) psgval->wSize;
  54518. +
  54519. + /* Check the DMA descriptor count */
  54520. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  54521. + CFI_INFO
  54522. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  54523. + __func__, MAX_DMA_DESCS_PER_EP);
  54524. + return -DWC_E_INVALID;
  54525. + }
  54526. +
  54527. + /* Check the DMA descriptor count */
  54528. +
  54529. + if (size == 0) {
  54530. +
  54531. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  54532. + __func__);
  54533. +
  54534. + return -DWC_E_INVALID;
  54535. +
  54536. + }
  54537. +
  54538. + inaddr = psgval->bInEndpointAddress;
  54539. + outaddr = psgval->bOutEndpointAddress;
  54540. +
  54541. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  54542. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  54543. +
  54544. + if (NULL == epin || NULL == epout) {
  54545. + CFI_INFO
  54546. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  54547. + __func__, inaddr, outaddr);
  54548. + return -DWC_E_INVALID;
  54549. + }
  54550. +
  54551. + epin->ep->dwc_ep.buff_mode = BM_SG;
  54552. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  54553. +
  54554. + epout->ep->dwc_ep.buff_mode = BM_SG;
  54555. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  54556. +
  54557. + return 0;
  54558. +}
  54559. +
  54560. +/**
  54561. + * This function sets a new value for the buffer Alignment setup.
  54562. + */
  54563. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  54564. +{
  54565. + cfi_ep_t *ep;
  54566. + uint8_t addr;
  54567. + ddma_align_buffer_setup_t *palignval;
  54568. +
  54569. + palignval = (ddma_align_buffer_setup_t *) buf;
  54570. + addr = palignval->bEndpointAddress;
  54571. +
  54572. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54573. +
  54574. + if (NULL == ep) {
  54575. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54576. + __func__, addr);
  54577. + return -DWC_E_INVALID;
  54578. + }
  54579. +
  54580. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  54581. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  54582. +
  54583. + return 0;
  54584. +}
  54585. +
  54586. +/**
  54587. + * This function sets a new value for the Concatenation buffer setup.
  54588. + */
  54589. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  54590. +{
  54591. + uint8_t addr;
  54592. + cfi_ep_t *ep;
  54593. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  54594. + uint16_t *pVals;
  54595. + uint32_t desccount;
  54596. + int i;
  54597. + uint16_t mps;
  54598. +
  54599. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  54600. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  54601. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  54602. +
  54603. + /* Check the DMA descriptor count */
  54604. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  54605. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  54606. + __func__, MAX_DMA_DESCS_PER_EP);
  54607. + return -DWC_E_INVALID;
  54608. + }
  54609. +
  54610. + addr = pConcatValHdr->bEndpointAddress;
  54611. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54612. + if (NULL == ep) {
  54613. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54614. + __func__, addr);
  54615. + return -DWC_E_INVALID;
  54616. + }
  54617. +
  54618. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  54619. +
  54620. +#if 0
  54621. + for (i = 0; i < desccount; i++) {
  54622. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  54623. + }
  54624. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  54625. +#endif
  54626. +
  54627. + /* Check the wTxSizes to be less than or equal to the mps */
  54628. + for (i = 0; i < desccount; i++) {
  54629. + if (pVals[i] > mps) {
  54630. + CFI_INFO
  54631. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  54632. + __func__, i, pVals[i]);
  54633. + return -DWC_E_INVALID;
  54634. + }
  54635. + }
  54636. +
  54637. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  54638. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  54639. +
  54640. + /* Free the previously allocated storage for the wTxBytes */
  54641. + if (ep->bm_concat->wTxBytes) {
  54642. + DWC_FREE(ep->bm_concat->wTxBytes);
  54643. + }
  54644. +
  54645. + /* Allocate a new storage for the wTxBytes field */
  54646. + ep->bm_concat->wTxBytes =
  54647. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  54648. + if (NULL == ep->bm_concat->wTxBytes) {
  54649. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  54650. + return -DWC_E_NO_MEMORY;
  54651. + }
  54652. +
  54653. + /* Copy the new values into the wTxBytes filed */
  54654. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  54655. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  54656. +
  54657. + return 0;
  54658. +}
  54659. +
  54660. +/**
  54661. + * This function calculates the total of all FIFO sizes
  54662. + *
  54663. + * @param core_if Programming view of DWC_otg controller
  54664. + *
  54665. + * @return The total of data FIFO sizes.
  54666. + *
  54667. + */
  54668. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  54669. +{
  54670. + dwc_otg_core_params_t *params = core_if->core_params;
  54671. + uint16_t dfifo_total = 0;
  54672. + int i;
  54673. +
  54674. + /* The shared RxFIFO size */
  54675. + dfifo_total =
  54676. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  54677. +
  54678. + /* Add up each TxFIFO size to the total */
  54679. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54680. + dfifo_total += params->dev_tx_fifo_size[i];
  54681. + }
  54682. +
  54683. + return dfifo_total;
  54684. +}
  54685. +
  54686. +/**
  54687. + * This function returns Rx FIFO size
  54688. + *
  54689. + * @param core_if Programming view of DWC_otg controller
  54690. + *
  54691. + * @return The total of data FIFO sizes.
  54692. + *
  54693. + */
  54694. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  54695. +{
  54696. + switch (wValue >> 8) {
  54697. + case 0:
  54698. + return (core_if->pwron_rxfsiz <
  54699. + 32768) ? core_if->pwron_rxfsiz : 32768;
  54700. + break;
  54701. + case 1:
  54702. + return core_if->core_params->dev_rx_fifo_size;
  54703. + break;
  54704. + default:
  54705. + return -DWC_E_INVALID;
  54706. + break;
  54707. + }
  54708. +}
  54709. +
  54710. +/**
  54711. + * This function returns Tx FIFO size for IN EP
  54712. + *
  54713. + * @param core_if Programming view of DWC_otg controller
  54714. + *
  54715. + * @return The total of data FIFO sizes.
  54716. + *
  54717. + */
  54718. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  54719. +{
  54720. + dwc_otg_pcd_ep_t *ep;
  54721. +
  54722. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  54723. +
  54724. + if (NULL == ep) {
  54725. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54726. + __func__, wValue & 0xff);
  54727. + return -DWC_E_INVALID;
  54728. + }
  54729. +
  54730. + if (!ep->dwc_ep.is_in) {
  54731. + CFI_INFO
  54732. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  54733. + __func__, wValue & 0xff);
  54734. + return -DWC_E_INVALID;
  54735. + }
  54736. +
  54737. + switch (wValue >> 8) {
  54738. + case 0:
  54739. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  54740. + [ep->dwc_ep.tx_fifo_num - 1] <
  54741. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  54742. + dwc_ep.tx_fifo_num
  54743. + - 1] : 32768;
  54744. + break;
  54745. + case 1:
  54746. + return GET_CORE_IF(pcd)->core_params->
  54747. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  54748. + break;
  54749. + default:
  54750. + return -DWC_E_INVALID;
  54751. + break;
  54752. + }
  54753. +}
  54754. +
  54755. +/**
  54756. + * This function checks if the submitted combination of
  54757. + * device mode FIFO sizes is possible or not.
  54758. + *
  54759. + * @param core_if Programming view of DWC_otg controller
  54760. + *
  54761. + * @return 1 if possible, 0 otherwise.
  54762. + *
  54763. + */
  54764. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  54765. +{
  54766. + uint16_t dfifo_actual = 0;
  54767. + dwc_otg_core_params_t *params = core_if->core_params;
  54768. + uint16_t start_addr = 0;
  54769. + int i;
  54770. +
  54771. + dfifo_actual =
  54772. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  54773. +
  54774. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54775. + dfifo_actual += params->dev_tx_fifo_size[i];
  54776. + }
  54777. +
  54778. + if (dfifo_actual > core_if->total_fifo_size) {
  54779. + return 0;
  54780. + }
  54781. +
  54782. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  54783. + return 0;
  54784. +
  54785. + if (params->dev_nperio_tx_fifo_size > 32768
  54786. + || params->dev_nperio_tx_fifo_size < 16)
  54787. + return 0;
  54788. +
  54789. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54790. +
  54791. + if (params->dev_tx_fifo_size[i] > 768
  54792. + || params->dev_tx_fifo_size[i] < 4)
  54793. + return 0;
  54794. + }
  54795. +
  54796. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  54797. + return 0;
  54798. + start_addr = params->dev_rx_fifo_size;
  54799. +
  54800. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  54801. + return 0;
  54802. + start_addr += params->dev_nperio_tx_fifo_size;
  54803. +
  54804. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54805. +
  54806. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  54807. + return 0;
  54808. + start_addr += params->dev_tx_fifo_size[i];
  54809. + }
  54810. +
  54811. + return 1;
  54812. +}
  54813. +
  54814. +/**
  54815. + * This function resizes Device mode FIFOs
  54816. + *
  54817. + * @param core_if Programming view of DWC_otg controller
  54818. + *
  54819. + * @return 1 if successful, 0 otherwise
  54820. + *
  54821. + */
  54822. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  54823. +{
  54824. + int i = 0;
  54825. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  54826. + dwc_otg_core_params_t *params = core_if->core_params;
  54827. + uint32_t rx_fifo_size;
  54828. + fifosize_data_t nptxfifosize;
  54829. + fifosize_data_t txfifosize[15];
  54830. +
  54831. + uint32_t rx_fsz_bak;
  54832. + uint32_t nptxfsz_bak;
  54833. + uint32_t txfsz_bak[15];
  54834. +
  54835. + uint16_t start_address;
  54836. + uint8_t retval = 1;
  54837. +
  54838. + if (!check_fifo_sizes(core_if)) {
  54839. + return 0;
  54840. + }
  54841. +
  54842. + /* Configure data FIFO sizes */
  54843. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  54844. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  54845. + rx_fifo_size = params->dev_rx_fifo_size;
  54846. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  54847. +
  54848. + /*
  54849. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  54850. + * Indexes of the FIFO size module parameters in the
  54851. + * dev_tx_fifo_size array and the FIFO size registers in
  54852. + * the dtxfsiz array run from 0 to 14.
  54853. + */
  54854. +
  54855. + /* Non-periodic Tx FIFO */
  54856. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  54857. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  54858. + start_address = params->dev_rx_fifo_size;
  54859. + nptxfifosize.b.startaddr = start_address;
  54860. +
  54861. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  54862. +
  54863. + start_address += nptxfifosize.b.depth;
  54864. +
  54865. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54866. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  54867. +
  54868. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  54869. + txfifosize[i].b.startaddr = start_address;
  54870. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  54871. + txfifosize[i].d32);
  54872. +
  54873. + start_address += txfifosize[i].b.depth;
  54874. + }
  54875. +
  54876. + /** Check if register values are set correctly */
  54877. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  54878. + retval = 0;
  54879. + }
  54880. +
  54881. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  54882. + retval = 0;
  54883. + }
  54884. +
  54885. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54886. + if (txfifosize[i].d32 !=
  54887. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  54888. + retval = 0;
  54889. + }
  54890. + }
  54891. +
  54892. + /** If register values are not set correctly, reset old values */
  54893. + if (retval == 0) {
  54894. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  54895. +
  54896. + /* Non-periodic Tx FIFO */
  54897. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  54898. +
  54899. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54900. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  54901. + txfsz_bak[i]);
  54902. + }
  54903. + }
  54904. + } else {
  54905. + return 0;
  54906. + }
  54907. +
  54908. + /* Flush the FIFOs */
  54909. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  54910. + dwc_otg_flush_rx_fifo(core_if);
  54911. +
  54912. + return retval;
  54913. +}
  54914. +
  54915. +/**
  54916. + * This function sets a new value for the buffer Alignment setup.
  54917. + */
  54918. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  54919. +{
  54920. + int retval;
  54921. + uint32_t fsiz;
  54922. + uint16_t size;
  54923. + uint16_t ep_addr;
  54924. + dwc_otg_pcd_ep_t *ep;
  54925. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54926. + tx_fifo_size_setup_t *ptxfifoval;
  54927. +
  54928. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  54929. + ep_addr = ptxfifoval->bEndpointAddress;
  54930. + size = ptxfifoval->wDepth;
  54931. +
  54932. + ep = get_ep_by_addr(pcd, ep_addr);
  54933. +
  54934. + CFI_INFO
  54935. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  54936. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  54937. +
  54938. + if (NULL == ep) {
  54939. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54940. + __func__, ep_addr);
  54941. + return -DWC_E_INVALID;
  54942. + }
  54943. +
  54944. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  54945. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  54946. +
  54947. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54948. + retval = 0;
  54949. + } else {
  54950. + CFI_INFO
  54951. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  54952. + __func__, ep_addr);
  54953. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  54954. + retval = -DWC_E_INVALID;
  54955. + }
  54956. +
  54957. + return retval;
  54958. +}
  54959. +
  54960. +/**
  54961. + * This function sets a new value for the buffer Alignment setup.
  54962. + */
  54963. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  54964. +{
  54965. + int retval;
  54966. + uint32_t fsiz;
  54967. + uint16_t size;
  54968. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54969. + rx_fifo_size_setup_t *prxfifoval;
  54970. +
  54971. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  54972. + size = prxfifoval->wDepth;
  54973. +
  54974. + fsiz = params->dev_rx_fifo_size;
  54975. + params->dev_rx_fifo_size = size;
  54976. +
  54977. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54978. + retval = 0;
  54979. + } else {
  54980. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  54981. + __func__);
  54982. + params->dev_rx_fifo_size = fsiz;
  54983. + retval = -DWC_E_INVALID;
  54984. + }
  54985. +
  54986. + return retval;
  54987. +}
  54988. +
  54989. +/**
  54990. + * This function reads the SG of an EP's buffer setup into the buffer buf
  54991. + */
  54992. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  54993. + struct cfi_usb_ctrlrequest *req)
  54994. +{
  54995. + int retval = -DWC_E_INVALID;
  54996. + uint8_t addr;
  54997. + cfi_ep_t *ep;
  54998. +
  54999. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  55000. + addr = req->wValue & 0xFF;
  55001. + if (addr == 0) /* The address should be non-zero */
  55002. + return retval;
  55003. +
  55004. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  55005. + if (NULL == ep) {
  55006. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  55007. + __func__, addr);
  55008. + return retval;
  55009. + }
  55010. +
  55011. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  55012. + retval = BS_SG_VAL_DESC_LEN;
  55013. + return retval;
  55014. +}
  55015. +
  55016. +/**
  55017. + * This function reads the Concatenation value of an EP's buffer mode into
  55018. + * the buffer buf
  55019. + */
  55020. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  55021. + struct cfi_usb_ctrlrequest *req)
  55022. +{
  55023. + int retval = -DWC_E_INVALID;
  55024. + uint8_t addr;
  55025. + cfi_ep_t *ep;
  55026. + uint8_t desc_count;
  55027. +
  55028. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  55029. + addr = req->wValue & 0xFF;
  55030. + if (addr == 0) /* The address should be non-zero */
  55031. + return retval;
  55032. +
  55033. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  55034. + if (NULL == ep) {
  55035. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  55036. + __func__, addr);
  55037. + return retval;
  55038. + }
  55039. +
  55040. + /* Copy the header to the buffer */
  55041. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  55042. + /* Advance the buffer pointer by the header size */
  55043. + buf += BS_CONCAT_VAL_HDR_LEN;
  55044. +
  55045. + desc_count = ep->bm_concat->hdr.bDescCount;
  55046. + /* Copy alll the wTxBytes to the buffer */
  55047. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  55048. +
  55049. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  55050. + return retval;
  55051. +}
  55052. +
  55053. +/**
  55054. + * This function reads the buffer Alignment value of an EP's buffer mode into
  55055. + * the buffer buf
  55056. + *
  55057. + * @return The total number of bytes copied to the buffer or negative error code.
  55058. + */
  55059. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  55060. + struct cfi_usb_ctrlrequest *req)
  55061. +{
  55062. + int retval = -DWC_E_INVALID;
  55063. + uint8_t addr;
  55064. + cfi_ep_t *ep;
  55065. +
  55066. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  55067. + addr = req->wValue & 0xFF;
  55068. + if (addr == 0) /* The address should be non-zero */
  55069. + return retval;
  55070. +
  55071. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  55072. + if (NULL == ep) {
  55073. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  55074. + __func__, addr);
  55075. + return retval;
  55076. + }
  55077. +
  55078. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  55079. + retval = BS_ALIGN_VAL_HDR_LEN;
  55080. +
  55081. + return retval;
  55082. +}
  55083. +
  55084. +/**
  55085. + * This function sets a new value for the specified feature
  55086. + *
  55087. + * @param pcd A pointer to the PCD object
  55088. + *
  55089. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  55090. + */
  55091. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  55092. +{
  55093. + int retval = -DWC_E_NOT_SUPPORTED;
  55094. + uint16_t wIndex, wValue;
  55095. + uint8_t bRequest;
  55096. + struct dwc_otg_core_if *coreif;
  55097. + cfiobject_t *cfi = pcd->cfi;
  55098. + struct cfi_usb_ctrlrequest *ctrl_req;
  55099. + uint8_t *buf;
  55100. + ctrl_req = &cfi->ctrl_req;
  55101. +
  55102. + buf = pcd->cfi->ctrl_req.data;
  55103. +
  55104. + coreif = GET_CORE_IF(pcd);
  55105. + bRequest = ctrl_req->bRequest;
  55106. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  55107. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  55108. +
  55109. + /* See which feature is to be modified */
  55110. + switch (wIndex) {
  55111. + case FT_ID_DMA_BUFFER_SETUP:
  55112. + /* Modify the feature */
  55113. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  55114. + return retval;
  55115. +
  55116. + /* And send this request to the gadget */
  55117. + cfi->need_gadget_att = 1;
  55118. + break;
  55119. +
  55120. + case FT_ID_DMA_BUFF_ALIGN:
  55121. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  55122. + return retval;
  55123. + cfi->need_gadget_att = 1;
  55124. + break;
  55125. +
  55126. + case FT_ID_DMA_CONCAT_SETUP:
  55127. + /* Modify the feature */
  55128. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  55129. + return retval;
  55130. + cfi->need_gadget_att = 1;
  55131. + break;
  55132. +
  55133. + case FT_ID_DMA_CIRCULAR:
  55134. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  55135. + break;
  55136. +
  55137. + case FT_ID_THRESHOLD_SETUP:
  55138. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  55139. + break;
  55140. +
  55141. + case FT_ID_DFIFO_DEPTH:
  55142. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  55143. + break;
  55144. +
  55145. + case FT_ID_TX_FIFO_DEPTH:
  55146. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  55147. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  55148. + return retval;
  55149. + cfi->need_gadget_att = 0;
  55150. + break;
  55151. +
  55152. + case FT_ID_RX_FIFO_DEPTH:
  55153. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  55154. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  55155. + return retval;
  55156. + cfi->need_gadget_att = 0;
  55157. + break;
  55158. + }
  55159. +
  55160. + return retval;
  55161. +}
  55162. +
  55163. +#endif //DWC_UTE_CFI
  55164. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cfi.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  55165. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 1970-01-01 01:00:00.000000000 +0100
  55166. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 2014-04-24 15:35:04.173565776 +0200
  55167. @@ -0,0 +1,320 @@
  55168. +/* ==========================================================================
  55169. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  55170. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  55171. + * otherwise expressly agreed to in writing between Synopsys and you.
  55172. + *
  55173. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  55174. + * any End User Software License Agreement or Agreement for Licensed Product
  55175. + * with Synopsys or any supplement thereto. You are permitted to use and
  55176. + * redistribute this Software in source and binary forms, with or without
  55177. + * modification, provided that redistributions of source code must retain this
  55178. + * notice. You may not view, use, disclose, copy or distribute this file or
  55179. + * any information contained herein except pursuant to this license grant from
  55180. + * Synopsys. If you do not agree with this notice, including the disclaimer
  55181. + * below, then you are not authorized to use the Software.
  55182. + *
  55183. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  55184. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55185. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55186. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  55187. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  55188. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  55189. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  55190. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55191. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55192. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  55193. + * DAMAGE.
  55194. + * ========================================================================== */
  55195. +
  55196. +#if !defined(__DWC_OTG_CFI_H__)
  55197. +#define __DWC_OTG_CFI_H__
  55198. +
  55199. +#include "dwc_otg_pcd.h"
  55200. +#include "dwc_cfi_common.h"
  55201. +
  55202. +/**
  55203. + * @file
  55204. + * This file contains the CFI related OTG PCD specific common constants,
  55205. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  55206. + * optional interface for internal testing purposes that a DUT may implement to
  55207. + * support testing of configurable features.
  55208. + *
  55209. + */
  55210. +
  55211. +struct dwc_otg_pcd;
  55212. +struct dwc_otg_pcd_ep;
  55213. +
  55214. +/** OTG CFI Features (properties) ID constants */
  55215. +/** This is a request for all Core Features */
  55216. +#define FT_ID_DMA_MODE 0x0001
  55217. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  55218. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  55219. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  55220. +#define FT_ID_DMA_CIRCULAR 0x0005
  55221. +#define FT_ID_THRESHOLD_SETUP 0x0006
  55222. +#define FT_ID_DFIFO_DEPTH 0x0007
  55223. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  55224. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  55225. +
  55226. +/**********************************************************/
  55227. +#define CFI_INFO_DEF
  55228. +
  55229. +#ifdef CFI_INFO_DEF
  55230. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  55231. +#else
  55232. +#define CFI_INFO(fmt...)
  55233. +#endif
  55234. +
  55235. +#define min(x,y) ({ \
  55236. + x < y ? x : y; })
  55237. +
  55238. +#define max(x,y) ({ \
  55239. + x > y ? x : y; })
  55240. +
  55241. +/**
  55242. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  55243. + * also used for setting up a buffer for Circular DDMA.
  55244. + */
  55245. +struct _ddma_sg_buffer_setup {
  55246. +#define BS_SG_VAL_DESC_LEN 6
  55247. + /* The OUT EP address */
  55248. + uint8_t bOutEndpointAddress;
  55249. + /* The IN EP address */
  55250. + uint8_t bInEndpointAddress;
  55251. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  55252. + uint8_t bOffset;
  55253. + /* The number of transfer segments (a DMA descriptors per each segment) */
  55254. + uint8_t bCount;
  55255. + /* Size (in byte) of each transfer segment */
  55256. + uint16_t wSize;
  55257. +} __attribute__ ((packed));
  55258. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  55259. +
  55260. +/** Descriptor DMA Concatenation Buffer setup structure */
  55261. +struct _ddma_concat_buffer_setup_hdr {
  55262. +#define BS_CONCAT_VAL_HDR_LEN 4
  55263. + /* The endpoint for which the buffer is to be set up */
  55264. + uint8_t bEndpointAddress;
  55265. + /* The count of descriptors to be used */
  55266. + uint8_t bDescCount;
  55267. + /* The total size of the transfer */
  55268. + uint16_t wSize;
  55269. +} __attribute__ ((packed));
  55270. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  55271. +
  55272. +/** Descriptor DMA Concatenation Buffer setup structure */
  55273. +struct _ddma_concat_buffer_setup {
  55274. + /* The SG header */
  55275. + ddma_concat_buffer_setup_hdr_t hdr;
  55276. +
  55277. + /* The XFER sizes pointer (allocated dynamically) */
  55278. + uint16_t *wTxBytes;
  55279. +} __attribute__ ((packed));
  55280. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  55281. +
  55282. +/** Descriptor DMA Alignment Buffer setup structure */
  55283. +struct _ddma_align_buffer_setup {
  55284. +#define BS_ALIGN_VAL_HDR_LEN 2
  55285. + uint8_t bEndpointAddress;
  55286. + uint8_t bAlign;
  55287. +} __attribute__ ((packed));
  55288. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  55289. +
  55290. +/** Transmit FIFO Size setup structure */
  55291. +struct _tx_fifo_size_setup {
  55292. + uint8_t bEndpointAddress;
  55293. + uint16_t wDepth;
  55294. +} __attribute__ ((packed));
  55295. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  55296. +
  55297. +/** Transmit FIFO Size setup structure */
  55298. +struct _rx_fifo_size_setup {
  55299. + uint16_t wDepth;
  55300. +} __attribute__ ((packed));
  55301. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  55302. +
  55303. +/**
  55304. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  55305. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  55306. + * to the data returned in the data stage of a 3-stage Control Write requests.
  55307. + */
  55308. +struct cfi_usb_ctrlrequest {
  55309. + uint8_t bRequestType;
  55310. + uint8_t bRequest;
  55311. + uint16_t wValue;
  55312. + uint16_t wIndex;
  55313. + uint16_t wLength;
  55314. + uint8_t *data;
  55315. +} UPACKED;
  55316. +
  55317. +/*---------------------------------------------------------------------------*/
  55318. +
  55319. +/**
  55320. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  55321. + * This structure is used to store the buffer setup data for any
  55322. + * enabled endpoint in the PCD.
  55323. + */
  55324. +struct cfi_ep {
  55325. + /* Entry for the list container */
  55326. + dwc_list_link_t lh;
  55327. + /* Pointer to the active PCD endpoint structure */
  55328. + struct dwc_otg_pcd_ep *ep;
  55329. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  55330. + struct dwc_otg_dma_desc *dma_desc_last;
  55331. + /* The SG feature value */
  55332. + ddma_sg_buffer_setup_t *bm_sg;
  55333. + /* The Circular feature value */
  55334. + ddma_sg_buffer_setup_t *bm_circ;
  55335. + /* The Concatenation feature value */
  55336. + ddma_concat_buffer_setup_t *bm_concat;
  55337. + /* The Alignment feature value */
  55338. + ddma_align_buffer_setup_t *bm_align;
  55339. + /* XFER length */
  55340. + uint32_t xfer_len;
  55341. + /*
  55342. + * Count of DMA descriptors currently used.
  55343. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  55344. + * defined in the dwc_otg_cil.h
  55345. + */
  55346. + uint32_t desc_count;
  55347. +};
  55348. +typedef struct cfi_ep cfi_ep_t;
  55349. +
  55350. +typedef struct cfi_dma_buff {
  55351. +#define CFI_IN_BUF_LEN 1024
  55352. +#define CFI_OUT_BUF_LEN 1024
  55353. + dma_addr_t addr;
  55354. + uint8_t *buf;
  55355. +} cfi_dma_buff_t;
  55356. +
  55357. +struct cfiobject;
  55358. +
  55359. +/**
  55360. + * This is the interface for the CFI operations.
  55361. + *
  55362. + * @param ep_enable Called when any endpoint is enabled and activated.
  55363. + * @param release Called when the CFI object is released and it needs to correctly
  55364. + * deallocate the dynamic memory
  55365. + * @param ctrl_write_complete Called when the data stage of the request is complete
  55366. + */
  55367. +typedef struct cfi_ops {
  55368. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  55369. + struct dwc_otg_pcd_ep * ep);
  55370. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  55371. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  55372. + unsigned size, gfp_t flags);
  55373. + void (*release) (struct cfiobject * cfi);
  55374. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  55375. + struct dwc_otg_pcd * pcd);
  55376. + void (*build_descriptors) (struct cfiobject * cfi,
  55377. + struct dwc_otg_pcd * pcd,
  55378. + struct dwc_otg_pcd_ep * ep,
  55379. + dwc_otg_pcd_request_t * req);
  55380. +} cfi_ops_t;
  55381. +
  55382. +struct cfiobject {
  55383. + cfi_ops_t ops;
  55384. + struct dwc_otg_pcd *pcd;
  55385. + struct usb_gadget *gadget;
  55386. +
  55387. + /* Buffers used to send/receive CFI-related request data */
  55388. + cfi_dma_buff_t buf_in;
  55389. + cfi_dma_buff_t buf_out;
  55390. +
  55391. + /* CFI specific Control request wrapper */
  55392. + struct cfi_usb_ctrlrequest ctrl_req;
  55393. +
  55394. + /* The list of active EP's in the PCD of type cfi_ep_t */
  55395. + dwc_list_link_t active_eps;
  55396. +
  55397. + /* This flag shall control the propagation of a specific request
  55398. + * to the gadget's processing routines.
  55399. + * 0 - no gadget handling
  55400. + * 1 - the gadget needs to know about this request (w/o completing a status
  55401. + * phase - just return a 0 to the _setup callback)
  55402. + */
  55403. + uint8_t need_gadget_att;
  55404. +
  55405. + /* Flag indicating whether the status IN phase needs to be
  55406. + * completed by the PCD
  55407. + */
  55408. + uint8_t need_status_in_complete;
  55409. +};
  55410. +typedef struct cfiobject cfiobject_t;
  55411. +
  55412. +#define DUMP_MSG
  55413. +
  55414. +#if defined(DUMP_MSG)
  55415. +static inline void dump_msg(const u8 * buf, unsigned int length)
  55416. +{
  55417. + unsigned int start, num, i;
  55418. + char line[52], *p;
  55419. +
  55420. + if (length >= 512)
  55421. + return;
  55422. +
  55423. + start = 0;
  55424. + while (length > 0) {
  55425. + num = min(length, 16u);
  55426. + p = line;
  55427. + for (i = 0; i < num; ++i) {
  55428. + if (i == 8)
  55429. + *p++ = ' ';
  55430. + DWC_SPRINTF(p, " %02x", buf[i]);
  55431. + p += 3;
  55432. + }
  55433. + *p = 0;
  55434. + DWC_DEBUG("%6x: %s\n", start, line);
  55435. + buf += num;
  55436. + start += num;
  55437. + length -= num;
  55438. + }
  55439. +}
  55440. +#else
  55441. +static inline void dump_msg(const u8 * buf, unsigned int length)
  55442. +{
  55443. +}
  55444. +#endif
  55445. +
  55446. +/**
  55447. + * This function returns a pointer to cfi_ep_t object with the addr address.
  55448. + */
  55449. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  55450. + uint8_t addr)
  55451. +{
  55452. + struct cfi_ep *pcfiep;
  55453. + dwc_list_link_t *tmp;
  55454. +
  55455. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  55456. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  55457. +
  55458. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  55459. + return pcfiep;
  55460. + }
  55461. + }
  55462. +
  55463. + return NULL;
  55464. +}
  55465. +
  55466. +/**
  55467. + * This function returns a pointer to cfi_ep_t object that matches
  55468. + * the dwc_otg_pcd_ep object.
  55469. + */
  55470. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  55471. + struct dwc_otg_pcd_ep *ep)
  55472. +{
  55473. + struct cfi_ep *pcfiep = NULL;
  55474. + dwc_list_link_t *tmp;
  55475. +
  55476. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  55477. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  55478. + if (pcfiep->ep == ep) {
  55479. + return pcfiep;
  55480. + }
  55481. + }
  55482. + return NULL;
  55483. +}
  55484. +
  55485. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  55486. +
  55487. +#endif /* (__DWC_OTG_CFI_H__) */
  55488. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cil.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  55489. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cil.c 1970-01-01 01:00:00.000000000 +0100
  55490. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c 2014-04-24 15:35:04.173565776 +0200
  55491. @@ -0,0 +1,7151 @@
  55492. +/* ==========================================================================
  55493. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  55494. + * $Revision: #191 $
  55495. + * $Date: 2012/08/10 $
  55496. + * $Change: 2047372 $
  55497. + *
  55498. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  55499. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  55500. + * otherwise expressly agreed to in writing between Synopsys and you.
  55501. + *
  55502. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  55503. + * any End User Software License Agreement or Agreement for Licensed Product
  55504. + * with Synopsys or any supplement thereto. You are permitted to use and
  55505. + * redistribute this Software in source and binary forms, with or without
  55506. + * modification, provided that redistributions of source code must retain this
  55507. + * notice. You may not view, use, disclose, copy or distribute this file or
  55508. + * any information contained herein except pursuant to this license grant from
  55509. + * Synopsys. If you do not agree with this notice, including the disclaimer
  55510. + * below, then you are not authorized to use the Software.
  55511. + *
  55512. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  55513. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55514. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55515. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  55516. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  55517. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  55518. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  55519. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55520. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55521. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  55522. + * DAMAGE.
  55523. + * ========================================================================== */
  55524. +
  55525. +/** @file
  55526. + *
  55527. + * The Core Interface Layer provides basic services for accessing and
  55528. + * managing the DWC_otg hardware. These services are used by both the
  55529. + * Host Controller Driver and the Peripheral Controller Driver.
  55530. + *
  55531. + * The CIL manages the memory map for the core so that the HCD and PCD
  55532. + * don't have to do this separately. It also handles basic tasks like
  55533. + * reading/writing the registers and data FIFOs in the controller.
  55534. + * Some of the data access functions provide encapsulation of several
  55535. + * operations required to perform a task, such as writing multiple
  55536. + * registers to start a transfer. Finally, the CIL performs basic
  55537. + * services that are not specific to either the host or device modes
  55538. + * of operation. These services include management of the OTG Host
  55539. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  55540. + * Diagnostic API is also provided to allow testing of the controller
  55541. + * hardware.
  55542. + *
  55543. + * The Core Interface Layer has the following requirements:
  55544. + * - Provides basic controller operations.
  55545. + * - Minimal use of OS services.
  55546. + * - The OS services used will be abstracted by using inline functions
  55547. + * or macros.
  55548. + *
  55549. + */
  55550. +
  55551. +#include "dwc_os.h"
  55552. +#include "dwc_otg_regs.h"
  55553. +#include "dwc_otg_cil.h"
  55554. +
  55555. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  55556. +
  55557. +/**
  55558. + * This function is called to initialize the DWC_otg CSR data
  55559. + * structures. The register addresses in the device and host
  55560. + * structures are initialized from the base address supplied by the
  55561. + * caller. The calling function must make the OS calls to get the
  55562. + * base address of the DWC_otg controller registers. The core_params
  55563. + * argument holds the parameters that specify how the core should be
  55564. + * configured.
  55565. + *
  55566. + * @param reg_base_addr Base address of DWC_otg core registers
  55567. + *
  55568. + */
  55569. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  55570. +{
  55571. + dwc_otg_core_if_t *core_if = 0;
  55572. + dwc_otg_dev_if_t *dev_if = 0;
  55573. + dwc_otg_host_if_t *host_if = 0;
  55574. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  55575. + int i = 0;
  55576. +
  55577. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  55578. +
  55579. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  55580. +
  55581. + if (core_if == NULL) {
  55582. + DWC_DEBUGPL(DBG_CIL,
  55583. + "Allocation of dwc_otg_core_if_t failed\n");
  55584. + return 0;
  55585. + }
  55586. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  55587. +
  55588. + /*
  55589. + * Allocate the Device Mode structures.
  55590. + */
  55591. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  55592. +
  55593. + if (dev_if == NULL) {
  55594. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  55595. + DWC_FREE(core_if);
  55596. + return 0;
  55597. + }
  55598. +
  55599. + dev_if->dev_global_regs =
  55600. + (dwc_otg_device_global_regs_t *) (reg_base +
  55601. + DWC_DEV_GLOBAL_REG_OFFSET);
  55602. +
  55603. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55604. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  55605. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  55606. + (i * DWC_EP_REG_OFFSET));
  55607. +
  55608. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  55609. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  55610. + (i * DWC_EP_REG_OFFSET));
  55611. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  55612. + i, &dev_if->in_ep_regs[i]->diepctl);
  55613. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  55614. + i, &dev_if->out_ep_regs[i]->doepctl);
  55615. + }
  55616. +
  55617. + dev_if->speed = 0; // unknown
  55618. +
  55619. + core_if->dev_if = dev_if;
  55620. +
  55621. + /*
  55622. + * Allocate the Host Mode structures.
  55623. + */
  55624. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  55625. +
  55626. + if (host_if == NULL) {
  55627. + DWC_DEBUGPL(DBG_CIL,
  55628. + "Allocation of dwc_otg_host_if_t failed\n");
  55629. + DWC_FREE(dev_if);
  55630. + DWC_FREE(core_if);
  55631. + return 0;
  55632. + }
  55633. +
  55634. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  55635. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  55636. +
  55637. + host_if->hprt0 =
  55638. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  55639. +
  55640. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55641. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  55642. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  55643. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  55644. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  55645. + i, &host_if->hc_regs[i]->hcchar);
  55646. + }
  55647. +
  55648. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  55649. + core_if->host_if = host_if;
  55650. +
  55651. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55652. + core_if->data_fifo[i] =
  55653. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  55654. + (i * DWC_OTG_DATA_FIFO_SIZE));
  55655. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  55656. + i, (unsigned long)core_if->data_fifo[i]);
  55657. + }
  55658. +
  55659. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  55660. +
  55661. + /* Initiate lx_state to L3 disconnected state */
  55662. + core_if->lx_state = DWC_OTG_L3;
  55663. + /*
  55664. + * Store the contents of the hardware configuration registers here for
  55665. + * easy access later.
  55666. + */
  55667. + core_if->hwcfg1.d32 =
  55668. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  55669. + core_if->hwcfg2.d32 =
  55670. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  55671. + core_if->hwcfg3.d32 =
  55672. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  55673. + core_if->hwcfg4.d32 =
  55674. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  55675. +
  55676. + /* Force host mode to get HPTXFSIZ exact power on value */
  55677. + {
  55678. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  55679. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55680. + gusbcfg.b.force_host_mode = 1;
  55681. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55682. + dwc_mdelay(100);
  55683. + core_if->hptxfsiz.d32 =
  55684. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  55685. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55686. + gusbcfg.b.force_host_mode = 0;
  55687. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55688. + dwc_mdelay(100);
  55689. + }
  55690. +
  55691. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  55692. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  55693. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  55694. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  55695. +
  55696. + core_if->hcfg.d32 =
  55697. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  55698. + core_if->dcfg.d32 =
  55699. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  55700. +
  55701. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  55702. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  55703. +
  55704. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  55705. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  55706. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  55707. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  55708. + core_if->hwcfg2.b.num_host_chan);
  55709. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  55710. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  55711. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  55712. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  55713. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  55714. + core_if->hwcfg2.b.dev_token_q_depth);
  55715. +
  55716. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  55717. + core_if->hwcfg3.b.dfifo_depth);
  55718. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  55719. + core_if->hwcfg3.b.xfer_size_cntr_width);
  55720. +
  55721. + /*
  55722. + * Set the SRP sucess bit for FS-I2c
  55723. + */
  55724. + core_if->srp_success = 0;
  55725. + core_if->srp_timer_started = 0;
  55726. +
  55727. + /*
  55728. + * Create new workqueue and init works
  55729. + */
  55730. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  55731. + if (core_if->wq_otg == 0) {
  55732. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  55733. + DWC_FREE(host_if);
  55734. + DWC_FREE(dev_if);
  55735. + DWC_FREE(core_if);
  55736. + return 0;
  55737. + }
  55738. +
  55739. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  55740. +
  55741. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  55742. + (core_if->snpsid >> 12 & 0xF),
  55743. + (core_if->snpsid >> 8 & 0xF),
  55744. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  55745. +
  55746. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  55747. + w_wakeup_detected, core_if);
  55748. + if (core_if->wkp_timer == 0) {
  55749. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  55750. + DWC_FREE(host_if);
  55751. + DWC_FREE(dev_if);
  55752. + DWC_WORKQ_FREE(core_if->wq_otg);
  55753. + DWC_FREE(core_if);
  55754. + return 0;
  55755. + }
  55756. +
  55757. + if (dwc_otg_setup_params(core_if)) {
  55758. + DWC_WARN("Error while setting core params\n");
  55759. + }
  55760. +
  55761. + core_if->hibernation_suspend = 0;
  55762. +
  55763. + /** ADP initialization */
  55764. + dwc_otg_adp_init(core_if);
  55765. +
  55766. + return core_if;
  55767. +}
  55768. +
  55769. +/**
  55770. + * This function frees the structures allocated by dwc_otg_cil_init().
  55771. + *
  55772. + * @param core_if The core interface pointer returned from
  55773. + * dwc_otg_cil_init().
  55774. + *
  55775. + */
  55776. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  55777. +{
  55778. + dctl_data_t dctl = {.d32 = 0 };
  55779. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  55780. +
  55781. + /* Disable all interrupts */
  55782. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  55783. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  55784. +
  55785. + dctl.b.sftdiscon = 1;
  55786. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55787. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  55788. + dctl.d32);
  55789. + }
  55790. +
  55791. + if (core_if->wq_otg) {
  55792. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  55793. + DWC_WORKQ_FREE(core_if->wq_otg);
  55794. + }
  55795. + if (core_if->dev_if) {
  55796. + DWC_FREE(core_if->dev_if);
  55797. + }
  55798. + if (core_if->host_if) {
  55799. + DWC_FREE(core_if->host_if);
  55800. + }
  55801. +
  55802. + /** Remove ADP Stuff */
  55803. + dwc_otg_adp_remove(core_if);
  55804. + if (core_if->core_params) {
  55805. + DWC_FREE(core_if->core_params);
  55806. + }
  55807. + if (core_if->wkp_timer) {
  55808. + DWC_TIMER_FREE(core_if->wkp_timer);
  55809. + }
  55810. + if (core_if->srp_timer) {
  55811. + DWC_TIMER_FREE(core_if->srp_timer);
  55812. + }
  55813. + DWC_FREE(core_if);
  55814. +}
  55815. +
  55816. +/**
  55817. + * This function enables the controller's Global Interrupt in the AHB Config
  55818. + * register.
  55819. + *
  55820. + * @param core_if Programming view of DWC_otg controller.
  55821. + */
  55822. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  55823. +{
  55824. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  55825. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  55826. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  55827. +}
  55828. +
  55829. +/**
  55830. + * This function disables the controller's Global Interrupt in the AHB Config
  55831. + * register.
  55832. + *
  55833. + * @param core_if Programming view of DWC_otg controller.
  55834. + */
  55835. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  55836. +{
  55837. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  55838. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  55839. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  55840. +}
  55841. +
  55842. +/**
  55843. + * This function initializes the commmon interrupts, used in both
  55844. + * device and host modes.
  55845. + *
  55846. + * @param core_if Programming view of the DWC_otg controller
  55847. + *
  55848. + */
  55849. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  55850. +{
  55851. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  55852. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55853. +
  55854. + /* Clear any pending OTG Interrupts */
  55855. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  55856. +
  55857. + /* Clear any pending interrupts */
  55858. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  55859. +
  55860. + /*
  55861. + * Enable the interrupts in the GINTMSK.
  55862. + */
  55863. + intr_mask.b.modemismatch = 1;
  55864. + intr_mask.b.otgintr = 1;
  55865. +
  55866. + if (!core_if->dma_enable) {
  55867. + intr_mask.b.rxstsqlvl = 1;
  55868. + }
  55869. +
  55870. + intr_mask.b.conidstschng = 1;
  55871. + intr_mask.b.wkupintr = 1;
  55872. + intr_mask.b.disconnect = 0;
  55873. + intr_mask.b.usbsuspend = 1;
  55874. + intr_mask.b.sessreqintr = 1;
  55875. +#ifdef CONFIG_USB_DWC_OTG_LPM
  55876. + if (core_if->core_params->lpm_enable) {
  55877. + intr_mask.b.lpmtranrcvd = 1;
  55878. + }
  55879. +#endif
  55880. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  55881. +}
  55882. +
  55883. +/*
  55884. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  55885. + * Hibernation. This function is for exiting from Device mode hibernation by
  55886. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  55887. + * @param core_if Programming view of DWC_otg controller.
  55888. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  55889. + * @param reset - indicates whether resume is initiated by Reset.
  55890. + */
  55891. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  55892. + int rem_wakeup, int reset)
  55893. +{
  55894. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  55895. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  55896. + dctl_data_t dctl = {.d32 = 0 };
  55897. +
  55898. + int timeout = 2000;
  55899. +
  55900. + if (!core_if->hibernation_suspend) {
  55901. + DWC_PRINTF("Already exited from Hibernation\n");
  55902. + return 1;
  55903. + }
  55904. +
  55905. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  55906. + /* Switch-on voltage to the core */
  55907. + gpwrdn.b.pwrdnswtch = 1;
  55908. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55909. + dwc_udelay(10);
  55910. +
  55911. + /* Reset core */
  55912. + gpwrdn.d32 = 0;
  55913. + gpwrdn.b.pwrdnrstn = 1;
  55914. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55915. + dwc_udelay(10);
  55916. +
  55917. + /* Assert Restore signal */
  55918. + gpwrdn.d32 = 0;
  55919. + gpwrdn.b.restore = 1;
  55920. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55921. + dwc_udelay(10);
  55922. +
  55923. + /* Disable power clamps */
  55924. + gpwrdn.d32 = 0;
  55925. + gpwrdn.b.pwrdnclmp = 1;
  55926. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55927. +
  55928. + if (rem_wakeup) {
  55929. + dwc_udelay(70);
  55930. + }
  55931. +
  55932. + /* Deassert Reset core */
  55933. + gpwrdn.d32 = 0;
  55934. + gpwrdn.b.pwrdnrstn = 1;
  55935. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55936. + dwc_udelay(10);
  55937. +
  55938. + /* Disable PMU interrupt */
  55939. + gpwrdn.d32 = 0;
  55940. + gpwrdn.b.pmuintsel = 1;
  55941. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55942. +
  55943. + /* Mask interrupts from gpwrdn */
  55944. + gpwrdn.d32 = 0;
  55945. + gpwrdn.b.connect_det_msk = 1;
  55946. + gpwrdn.b.srp_det_msk = 1;
  55947. + gpwrdn.b.disconn_det_msk = 1;
  55948. + gpwrdn.b.rst_det_msk = 1;
  55949. + gpwrdn.b.lnstchng_msk = 1;
  55950. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55951. +
  55952. + /* Indicates that we are going out from hibernation */
  55953. + core_if->hibernation_suspend = 0;
  55954. +
  55955. + /*
  55956. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  55957. + * indicates restore from remote_wakeup
  55958. + */
  55959. + restore_essential_regs(core_if, rem_wakeup, 0);
  55960. +
  55961. + /*
  55962. + * Wait a little for seeing new value of variable hibernation_suspend if
  55963. + * Restore done interrupt received before polling
  55964. + */
  55965. + dwc_udelay(10);
  55966. +
  55967. + if (core_if->hibernation_suspend == 0) {
  55968. + /*
  55969. + * Wait For Restore_done Interrupt. This mechanism of polling the
  55970. + * interrupt is introduced to avoid any possible race conditions
  55971. + */
  55972. + do {
  55973. + gintsts_data_t gintsts;
  55974. + gintsts.d32 =
  55975. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55976. + if (gintsts.b.restoredone) {
  55977. + gintsts.d32 = 0;
  55978. + gintsts.b.restoredone = 1;
  55979. + DWC_WRITE_REG32(&core_if->core_global_regs->
  55980. + gintsts, gintsts.d32);
  55981. + DWC_PRINTF("Restore Done Interrupt seen\n");
  55982. + break;
  55983. + }
  55984. + dwc_udelay(10);
  55985. + } while (--timeout);
  55986. + if (!timeout) {
  55987. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  55988. + }
  55989. + }
  55990. + /* Clear all pending interupts */
  55991. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55992. +
  55993. + /* De-assert Restore */
  55994. + gpwrdn.d32 = 0;
  55995. + gpwrdn.b.restore = 1;
  55996. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55997. + dwc_udelay(10);
  55998. +
  55999. + if (!rem_wakeup) {
  56000. + pcgcctl.d32 = 0;
  56001. + pcgcctl.b.rstpdwnmodule = 1;
  56002. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  56003. + }
  56004. +
  56005. + /* Restore GUSBCFG and DCFG */
  56006. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  56007. + core_if->gr_backup->gusbcfg_local);
  56008. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  56009. + core_if->dr_backup->dcfg);
  56010. +
  56011. + /* De-assert Wakeup Logic */
  56012. + gpwrdn.d32 = 0;
  56013. + gpwrdn.b.pmuactv = 1;
  56014. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56015. + dwc_udelay(10);
  56016. +
  56017. + if (!rem_wakeup) {
  56018. + /* Set Device programming done bit */
  56019. + dctl.b.pwronprgdone = 1;
  56020. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  56021. + } else {
  56022. + /* Start Remote Wakeup Signaling */
  56023. + dctl.d32 = core_if->dr_backup->dctl;
  56024. + dctl.b.rmtwkupsig = 1;
  56025. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  56026. + }
  56027. +
  56028. + dwc_mdelay(2);
  56029. + /* Clear all pending interupts */
  56030. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56031. +
  56032. + /* Restore global registers */
  56033. + dwc_otg_restore_global_regs(core_if);
  56034. + /* Restore device global registers */
  56035. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  56036. +
  56037. + if (rem_wakeup) {
  56038. + dwc_mdelay(7);
  56039. + dctl.d32 = 0;
  56040. + dctl.b.rmtwkupsig = 1;
  56041. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  56042. + }
  56043. +
  56044. + core_if->hibernation_suspend = 0;
  56045. + /* The core will be in ON STATE */
  56046. + core_if->lx_state = DWC_OTG_L0;
  56047. + DWC_PRINTF("Hibernation recovery completes here\n");
  56048. +
  56049. + return 1;
  56050. +}
  56051. +
  56052. +/*
  56053. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  56054. + * Hibernation. This function is for exiting from Host mode hibernation by
  56055. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  56056. + * @param core_if Programming view of DWC_otg controller.
  56057. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  56058. + * @param reset - indicates whether resume is initiated by Reset.
  56059. + */
  56060. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  56061. + int rem_wakeup, int reset)
  56062. +{
  56063. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  56064. + hprt0_data_t hprt0 = {.d32 = 0 };
  56065. +
  56066. + int timeout = 2000;
  56067. +
  56068. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  56069. + /* Switch-on voltage to the core */
  56070. + gpwrdn.b.pwrdnswtch = 1;
  56071. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56072. + dwc_udelay(10);
  56073. +
  56074. + /* Reset core */
  56075. + gpwrdn.d32 = 0;
  56076. + gpwrdn.b.pwrdnrstn = 1;
  56077. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56078. + dwc_udelay(10);
  56079. +
  56080. + /* Assert Restore signal */
  56081. + gpwrdn.d32 = 0;
  56082. + gpwrdn.b.restore = 1;
  56083. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  56084. + dwc_udelay(10);
  56085. +
  56086. + /* Disable power clamps */
  56087. + gpwrdn.d32 = 0;
  56088. + gpwrdn.b.pwrdnclmp = 1;
  56089. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56090. +
  56091. + if (!rem_wakeup) {
  56092. + dwc_udelay(50);
  56093. + }
  56094. +
  56095. + /* Deassert Reset core */
  56096. + gpwrdn.d32 = 0;
  56097. + gpwrdn.b.pwrdnrstn = 1;
  56098. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  56099. + dwc_udelay(10);
  56100. +
  56101. + /* Disable PMU interrupt */
  56102. + gpwrdn.d32 = 0;
  56103. + gpwrdn.b.pmuintsel = 1;
  56104. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56105. +
  56106. + gpwrdn.d32 = 0;
  56107. + gpwrdn.b.connect_det_msk = 1;
  56108. + gpwrdn.b.srp_det_msk = 1;
  56109. + gpwrdn.b.disconn_det_msk = 1;
  56110. + gpwrdn.b.rst_det_msk = 1;
  56111. + gpwrdn.b.lnstchng_msk = 1;
  56112. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56113. +
  56114. + /* Indicates that we are going out from hibernation */
  56115. + core_if->hibernation_suspend = 0;
  56116. +
  56117. + /* Set Restore Essential Regs bit in PCGCCTL register */
  56118. + restore_essential_regs(core_if, rem_wakeup, 1);
  56119. +
  56120. + /* Wait a little for seeing new value of variable hibernation_suspend if
  56121. + * Restore done interrupt received before polling */
  56122. + dwc_udelay(10);
  56123. +
  56124. + if (core_if->hibernation_suspend == 0) {
  56125. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  56126. + * interrupt is introduced to avoid any possible race conditions
  56127. + */
  56128. + do {
  56129. + gintsts_data_t gintsts;
  56130. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  56131. + if (gintsts.b.restoredone) {
  56132. + gintsts.d32 = 0;
  56133. + gintsts.b.restoredone = 1;
  56134. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  56135. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  56136. + break;
  56137. + }
  56138. + dwc_udelay(10);
  56139. + } while (--timeout);
  56140. + if (!timeout) {
  56141. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  56142. + }
  56143. + }
  56144. +
  56145. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  56146. + core_if->hibernation_suspend = 0;
  56147. +
  56148. + /* This step is not described in functional spec but if not wait for this
  56149. + * delay, mismatch interrupts occurred because just after restore core is
  56150. + * in Device mode(gintsts.curmode == 0) */
  56151. + dwc_mdelay(100);
  56152. +
  56153. + /* Clear all pending interrupts */
  56154. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56155. +
  56156. + /* De-assert Restore */
  56157. + gpwrdn.d32 = 0;
  56158. + gpwrdn.b.restore = 1;
  56159. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56160. + dwc_udelay(10);
  56161. +
  56162. + /* Restore GUSBCFG and HCFG */
  56163. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  56164. + core_if->gr_backup->gusbcfg_local);
  56165. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  56166. + core_if->hr_backup->hcfg_local);
  56167. +
  56168. + /* De-assert Wakeup Logic */
  56169. + gpwrdn.d32 = 0;
  56170. + gpwrdn.b.pmuactv = 1;
  56171. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  56172. + dwc_udelay(10);
  56173. +
  56174. + /* Start the Resume operation by programming HPRT0 */
  56175. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  56176. + hprt0.b.prtpwr = 1;
  56177. + hprt0.b.prtena = 0;
  56178. + hprt0.b.prtsusp = 0;
  56179. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56180. +
  56181. + DWC_PRINTF("Resume Starts Now\n");
  56182. + if (!reset) { // Indicates it is Resume Operation
  56183. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  56184. + hprt0.b.prtres = 1;
  56185. + hprt0.b.prtpwr = 1;
  56186. + hprt0.b.prtena = 0;
  56187. + hprt0.b.prtsusp = 0;
  56188. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56189. +
  56190. + if (!rem_wakeup)
  56191. + hprt0.b.prtres = 0;
  56192. + /* Wait for Resume time and then program HPRT again */
  56193. + dwc_mdelay(100);
  56194. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56195. +
  56196. + } else { // Indicates it is Reset Operation
  56197. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  56198. + hprt0.b.prtrst = 1;
  56199. + hprt0.b.prtpwr = 1;
  56200. + hprt0.b.prtena = 0;
  56201. + hprt0.b.prtsusp = 0;
  56202. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56203. + /* Wait for Reset time and then program HPRT again */
  56204. + dwc_mdelay(60);
  56205. + hprt0.b.prtrst = 0;
  56206. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56207. + }
  56208. + /* Clear all interrupt status */
  56209. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  56210. + hprt0.b.prtconndet = 1;
  56211. + hprt0.b.prtenchng = 1;
  56212. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  56213. +
  56214. + /* Clear all pending interupts */
  56215. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56216. +
  56217. + /* Restore global registers */
  56218. + dwc_otg_restore_global_regs(core_if);
  56219. + /* Restore host global registers */
  56220. + dwc_otg_restore_host_regs(core_if, reset);
  56221. +
  56222. + /* The core will be in ON STATE */
  56223. + core_if->lx_state = DWC_OTG_L0;
  56224. + DWC_PRINTF("Hibernation recovery is complete here\n");
  56225. + return 0;
  56226. +}
  56227. +
  56228. +/** Saves some register values into system memory. */
  56229. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  56230. +{
  56231. + struct dwc_otg_global_regs_backup *gr;
  56232. + int i;
  56233. +
  56234. + gr = core_if->gr_backup;
  56235. + if (!gr) {
  56236. + gr = DWC_ALLOC(sizeof(*gr));
  56237. + if (!gr) {
  56238. + return -DWC_E_NO_MEMORY;
  56239. + }
  56240. + core_if->gr_backup = gr;
  56241. + }
  56242. +
  56243. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  56244. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  56245. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  56246. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  56247. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  56248. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  56249. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  56250. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56251. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  56252. +#endif
  56253. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  56254. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  56255. + gr->gdfifocfg_local =
  56256. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  56257. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  56258. + gr->dtxfsiz_local[i] =
  56259. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  56260. + }
  56261. +
  56262. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  56263. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  56264. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  56265. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  56266. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  56267. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  56268. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  56269. + gr->gnptxfsiz_local);
  56270. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  56271. + gr->hptxfsiz_local);
  56272. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56273. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  56274. +#endif
  56275. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  56276. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  56277. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  56278. +
  56279. + return 0;
  56280. +}
  56281. +
  56282. +/** Saves GINTMSK register before setting the msk bits. */
  56283. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  56284. +{
  56285. + struct dwc_otg_global_regs_backup *gr;
  56286. +
  56287. + gr = core_if->gr_backup;
  56288. + if (!gr) {
  56289. + gr = DWC_ALLOC(sizeof(*gr));
  56290. + if (!gr) {
  56291. + return -DWC_E_NO_MEMORY;
  56292. + }
  56293. + core_if->gr_backup = gr;
  56294. + }
  56295. +
  56296. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  56297. +
  56298. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  56299. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  56300. +
  56301. + return 0;
  56302. +}
  56303. +
  56304. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  56305. +{
  56306. + struct dwc_otg_dev_regs_backup *dr;
  56307. + int i;
  56308. +
  56309. + dr = core_if->dr_backup;
  56310. + if (!dr) {
  56311. + dr = DWC_ALLOC(sizeof(*dr));
  56312. + if (!dr) {
  56313. + return -DWC_E_NO_MEMORY;
  56314. + }
  56315. + core_if->dr_backup = dr;
  56316. + }
  56317. +
  56318. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  56319. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  56320. + dr->daintmsk =
  56321. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  56322. + dr->diepmsk =
  56323. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  56324. + dr->doepmsk =
  56325. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  56326. +
  56327. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56328. + dr->diepctl[i] =
  56329. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  56330. + dr->dieptsiz[i] =
  56331. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  56332. + dr->diepdma[i] =
  56333. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  56334. + }
  56335. +
  56336. + DWC_DEBUGPL(DBG_ANY,
  56337. + "=============Backing Host registers==============\n");
  56338. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  56339. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  56340. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  56341. + dr->daintmsk);
  56342. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  56343. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  56344. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56345. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  56346. + dr->diepctl[i]);
  56347. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  56348. + i, dr->dieptsiz[i]);
  56349. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  56350. + dr->diepdma[i]);
  56351. + }
  56352. +
  56353. + return 0;
  56354. +}
  56355. +
  56356. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  56357. +{
  56358. + struct dwc_otg_host_regs_backup *hr;
  56359. + int i;
  56360. +
  56361. + hr = core_if->hr_backup;
  56362. + if (!hr) {
  56363. + hr = DWC_ALLOC(sizeof(*hr));
  56364. + if (!hr) {
  56365. + return -DWC_E_NO_MEMORY;
  56366. + }
  56367. + core_if->hr_backup = hr;
  56368. + }
  56369. +
  56370. + hr->hcfg_local =
  56371. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  56372. + hr->haintmsk_local =
  56373. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  56374. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  56375. + hr->hcintmsk_local[i] =
  56376. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  56377. + }
  56378. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  56379. + hr->hfir_local =
  56380. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  56381. +
  56382. + DWC_DEBUGPL(DBG_ANY,
  56383. + "=============Backing Host registers===============\n");
  56384. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  56385. + hr->hcfg_local);
  56386. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  56387. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  56388. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  56389. + hr->hcintmsk_local[i]);
  56390. + }
  56391. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  56392. + hr->hprt0_local);
  56393. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  56394. + hr->hfir_local);
  56395. +
  56396. + return 0;
  56397. +}
  56398. +
  56399. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  56400. +{
  56401. + struct dwc_otg_global_regs_backup *gr;
  56402. + int i;
  56403. +
  56404. + gr = core_if->gr_backup;
  56405. + if (!gr) {
  56406. + return -DWC_E_INVALID;
  56407. + }
  56408. +
  56409. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  56410. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  56411. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  56412. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  56413. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  56414. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  56415. + gr->gnptxfsiz_local);
  56416. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  56417. + gr->hptxfsiz_local);
  56418. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  56419. + gr->gdfifocfg_local);
  56420. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  56421. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  56422. + gr->dtxfsiz_local[i]);
  56423. + }
  56424. +
  56425. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56426. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  56427. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  56428. + (gr->gahbcfg_local));
  56429. + return 0;
  56430. +}
  56431. +
  56432. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  56433. +{
  56434. + struct dwc_otg_dev_regs_backup *dr;
  56435. + int i;
  56436. +
  56437. + dr = core_if->dr_backup;
  56438. +
  56439. + if (!dr) {
  56440. + return -DWC_E_INVALID;
  56441. + }
  56442. +
  56443. + if (!rem_wakeup) {
  56444. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  56445. + dr->dctl);
  56446. + }
  56447. +
  56448. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  56449. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  56450. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  56451. +
  56452. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56453. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  56454. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  56455. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  56456. + }
  56457. +
  56458. + return 0;
  56459. +}
  56460. +
  56461. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  56462. +{
  56463. + struct dwc_otg_host_regs_backup *hr;
  56464. + int i;
  56465. + hr = core_if->hr_backup;
  56466. +
  56467. + if (!hr) {
  56468. + return -DWC_E_INVALID;
  56469. + }
  56470. +
  56471. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  56472. + //if (!reset)
  56473. + //{
  56474. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  56475. + //}
  56476. +
  56477. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  56478. + hr->haintmsk_local);
  56479. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  56480. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  56481. + hr->hcintmsk_local[i]);
  56482. + }
  56483. +
  56484. + return 0;
  56485. +}
  56486. +
  56487. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  56488. +{
  56489. + struct dwc_otg_global_regs_backup *gr;
  56490. +
  56491. + gr = core_if->gr_backup;
  56492. +
  56493. + /* Restore values for LPM and I2C */
  56494. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56495. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  56496. +#endif
  56497. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  56498. +
  56499. + return 0;
  56500. +}
  56501. +
  56502. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  56503. +{
  56504. + struct dwc_otg_global_regs_backup *gr;
  56505. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  56506. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  56507. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  56508. + gintmsk_data_t gintmsk = {.d32 = 0 };
  56509. +
  56510. + /* Restore LPM and I2C registers */
  56511. + restore_lpm_i2c_regs(core_if);
  56512. +
  56513. + /* Set PCGCCTL to 0 */
  56514. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  56515. +
  56516. + gr = core_if->gr_backup;
  56517. + /* Load restore values for [31:14] bits */
  56518. + DWC_WRITE_REG32(core_if->pcgcctl,
  56519. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  56520. +
  56521. + /* Umnask global Interrupt in GAHBCFG and restore it */
  56522. + gahbcfg.d32 = gr->gahbcfg_local;
  56523. + gahbcfg.b.glblintrmsk = 1;
  56524. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  56525. +
  56526. + /* Clear all pending interupts */
  56527. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  56528. +
  56529. + /* Unmask restore done interrupt */
  56530. + gintmsk.b.restoredone = 1;
  56531. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  56532. +
  56533. + /* Restore GUSBCFG and HCFG/DCFG */
  56534. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  56535. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  56536. +
  56537. + if (is_host) {
  56538. + hcfg_data_t hcfg = {.d32 = 0 };
  56539. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  56540. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  56541. + hcfg.d32);
  56542. +
  56543. + /* Load restore values for [31:14] bits */
  56544. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56545. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  56546. +
  56547. + if (rmode)
  56548. + pcgcctl.b.restoremode = 1;
  56549. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56550. + dwc_udelay(10);
  56551. +
  56552. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  56553. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  56554. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56555. + pcgcctl.b.ess_reg_restored = 1;
  56556. + if (rmode)
  56557. + pcgcctl.b.restoremode = 1;
  56558. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56559. + } else {
  56560. + dcfg_data_t dcfg = {.d32 = 0 };
  56561. + dcfg.d32 = core_if->dr_backup->dcfg;
  56562. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  56563. +
  56564. + /* Load restore values for [31:14] bits */
  56565. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56566. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  56567. + if (!rmode) {
  56568. + pcgcctl.d32 |= 0x208;
  56569. + }
  56570. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56571. + dwc_udelay(10);
  56572. +
  56573. + /* Load restore values for [31:14] bits */
  56574. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  56575. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  56576. + pcgcctl.b.ess_reg_restored = 1;
  56577. + if (!rmode)
  56578. + pcgcctl.d32 |= 0x208;
  56579. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  56580. + }
  56581. +
  56582. + return 0;
  56583. +}
  56584. +
  56585. +/**
  56586. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  56587. + * type.
  56588. + */
  56589. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  56590. +{
  56591. + uint32_t val;
  56592. + hcfg_data_t hcfg;
  56593. +
  56594. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56595. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56596. + (core_if->core_params->ulpi_fs_ls)) ||
  56597. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56598. + /* Full speed PHY */
  56599. + val = DWC_HCFG_48_MHZ;
  56600. + } else {
  56601. + /* High speed PHY running at full speed or high speed */
  56602. + val = DWC_HCFG_30_60_MHZ;
  56603. + }
  56604. +
  56605. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  56606. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  56607. + hcfg.b.fslspclksel = val;
  56608. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  56609. +}
  56610. +
  56611. +/**
  56612. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  56613. + * and the enumeration speed of the device.
  56614. + */
  56615. +static void init_devspd(dwc_otg_core_if_t * core_if)
  56616. +{
  56617. + uint32_t val;
  56618. + dcfg_data_t dcfg;
  56619. +
  56620. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56621. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56622. + (core_if->core_params->ulpi_fs_ls)) ||
  56623. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56624. + /* Full speed PHY */
  56625. + val = 0x3;
  56626. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  56627. + /* High speed PHY running at full speed */
  56628. + val = 0x1;
  56629. + } else {
  56630. + /* High speed PHY running at high speed */
  56631. + val = 0x0;
  56632. + }
  56633. +
  56634. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  56635. +
  56636. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  56637. + dcfg.b.devspd = val;
  56638. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  56639. +}
  56640. +
  56641. +/**
  56642. + * This function calculates the number of IN EPS
  56643. + * using GHWCFG1 and GHWCFG2 registers values
  56644. + *
  56645. + * @param core_if Programming view of the DWC_otg controller
  56646. + */
  56647. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  56648. +{
  56649. + uint32_t num_in_eps = 0;
  56650. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  56651. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  56652. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  56653. + int i;
  56654. +
  56655. + for (i = 0; i < num_eps; ++i) {
  56656. + if (!(hwcfg1 & 0x1))
  56657. + num_in_eps++;
  56658. +
  56659. + hwcfg1 >>= 2;
  56660. + }
  56661. +
  56662. + if (core_if->hwcfg4.b.ded_fifo_en) {
  56663. + num_in_eps =
  56664. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  56665. + }
  56666. +
  56667. + return num_in_eps;
  56668. +}
  56669. +
  56670. +/**
  56671. + * This function calculates the number of OUT EPS
  56672. + * using GHWCFG1 and GHWCFG2 registers values
  56673. + *
  56674. + * @param core_if Programming view of the DWC_otg controller
  56675. + */
  56676. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  56677. +{
  56678. + uint32_t num_out_eps = 0;
  56679. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  56680. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  56681. + int i;
  56682. +
  56683. + for (i = 0; i < num_eps; ++i) {
  56684. + if (!(hwcfg1 & 0x1))
  56685. + num_out_eps++;
  56686. +
  56687. + hwcfg1 >>= 2;
  56688. + }
  56689. + return num_out_eps;
  56690. +}
  56691. +
  56692. +/**
  56693. + * This function initializes the DWC_otg controller registers and
  56694. + * prepares the core for device mode or host mode operation.
  56695. + *
  56696. + * @param core_if Programming view of the DWC_otg controller
  56697. + *
  56698. + */
  56699. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  56700. +{
  56701. + int i = 0;
  56702. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56703. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  56704. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  56705. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  56706. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  56707. +
  56708. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  56709. + core_if, global_regs);
  56710. +
  56711. + /* Common Initialization */
  56712. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56713. +
  56714. + /* Program the ULPI External VBUS bit if needed */
  56715. + usbcfg.b.ulpi_ext_vbus_drv =
  56716. + (core_if->core_params->phy_ulpi_ext_vbus ==
  56717. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  56718. +
  56719. + /* Set external TS Dline pulsing */
  56720. + usbcfg.b.term_sel_dl_pulse =
  56721. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  56722. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56723. +
  56724. + /* Reset the Controller */
  56725. + dwc_otg_core_reset(core_if);
  56726. +
  56727. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  56728. + core_if->power_down = core_if->core_params->power_down;
  56729. + core_if->otg_sts = 0;
  56730. +
  56731. + /* Initialize parameters from Hardware configuration registers. */
  56732. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  56733. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  56734. +
  56735. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  56736. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  56737. +
  56738. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  56739. + dev_if->perio_tx_fifo_size[i] =
  56740. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  56741. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  56742. + i, dev_if->perio_tx_fifo_size[i]);
  56743. + }
  56744. +
  56745. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  56746. + dev_if->tx_fifo_size[i] =
  56747. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  56748. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  56749. + i, dev_if->tx_fifo_size[i]);
  56750. + }
  56751. +
  56752. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  56753. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  56754. + core_if->nperio_tx_fifo_size =
  56755. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  56756. +
  56757. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  56758. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  56759. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  56760. + core_if->nperio_tx_fifo_size);
  56761. +
  56762. + /* This programming sequence needs to happen in FS mode before any other
  56763. + * programming occurs */
  56764. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  56765. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56766. + /* If FS mode with FS PHY */
  56767. +
  56768. + /* core_init() is now called on every switch so only call the
  56769. + * following for the first time through. */
  56770. + if (!core_if->phy_init_done) {
  56771. + core_if->phy_init_done = 1;
  56772. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  56773. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56774. + usbcfg.b.physel = 1;
  56775. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56776. +
  56777. + /* Reset after a PHY select */
  56778. + dwc_otg_core_reset(core_if);
  56779. + }
  56780. +
  56781. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  56782. + * do this on HNP Dev/Host mode switches (done in dev_init and
  56783. + * host_init). */
  56784. + if (dwc_otg_is_host_mode(core_if)) {
  56785. + init_fslspclksel(core_if);
  56786. + } else {
  56787. + init_devspd(core_if);
  56788. + }
  56789. +
  56790. + if (core_if->core_params->i2c_enable) {
  56791. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  56792. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  56793. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56794. + usbcfg.b.otgutmifssel = 1;
  56795. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56796. +
  56797. + /* Program GI2CCTL.I2CEn */
  56798. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  56799. + i2cctl.b.i2cdevaddr = 1;
  56800. + i2cctl.b.i2cen = 0;
  56801. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  56802. + i2cctl.b.i2cen = 1;
  56803. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  56804. + }
  56805. +
  56806. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  56807. + else {
  56808. + /* High speed PHY. */
  56809. + if (!core_if->phy_init_done) {
  56810. + core_if->phy_init_done = 1;
  56811. + /* HS PHY parameters. These parameters are preserved
  56812. + * during soft reset so only program the first time. Do
  56813. + * a soft reset immediately after setting phyif. */
  56814. +
  56815. + if (core_if->core_params->phy_type == 2) {
  56816. + /* ULPI interface */
  56817. + usbcfg.b.ulpi_utmi_sel = 1;
  56818. + usbcfg.b.phyif = 0;
  56819. + usbcfg.b.ddrsel =
  56820. + core_if->core_params->phy_ulpi_ddr;
  56821. + } else if (core_if->core_params->phy_type == 1) {
  56822. + /* UTMI+ interface */
  56823. + usbcfg.b.ulpi_utmi_sel = 0;
  56824. + if (core_if->core_params->phy_utmi_width == 16) {
  56825. + usbcfg.b.phyif = 1;
  56826. +
  56827. + } else {
  56828. + usbcfg.b.phyif = 0;
  56829. + }
  56830. + } else {
  56831. + DWC_ERROR("FS PHY TYPE\n");
  56832. + }
  56833. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56834. + /* Reset after setting the PHY parameters */
  56835. + dwc_otg_core_reset(core_if);
  56836. + }
  56837. + }
  56838. +
  56839. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56840. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56841. + (core_if->core_params->ulpi_fs_ls)) {
  56842. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  56843. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56844. + usbcfg.b.ulpi_fsls = 1;
  56845. + usbcfg.b.ulpi_clk_sus_m = 1;
  56846. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56847. + } else {
  56848. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56849. + usbcfg.b.ulpi_fsls = 0;
  56850. + usbcfg.b.ulpi_clk_sus_m = 0;
  56851. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56852. + }
  56853. +
  56854. + /* Program the GAHBCFG Register. */
  56855. + switch (core_if->hwcfg2.b.architecture) {
  56856. +
  56857. + case DWC_SLAVE_ONLY_ARCH:
  56858. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  56859. + ahbcfg.b.nptxfemplvl_txfemplvl =
  56860. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  56861. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  56862. + core_if->dma_enable = 0;
  56863. + core_if->dma_desc_enable = 0;
  56864. + break;
  56865. +
  56866. + case DWC_EXT_DMA_ARCH:
  56867. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  56868. + {
  56869. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  56870. + ahbcfg.b.hburstlen = 0;
  56871. + while (brst_sz > 1) {
  56872. + ahbcfg.b.hburstlen++;
  56873. + brst_sz >>= 1;
  56874. + }
  56875. + }
  56876. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  56877. + core_if->dma_desc_enable =
  56878. + (core_if->core_params->dma_desc_enable != 0);
  56879. + break;
  56880. +
  56881. + case DWC_INT_DMA_ARCH:
  56882. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  56883. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  56884. + Host mode ISOC in issue fix - vahrama */
  56885. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  56886. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  56887. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  56888. + core_if->dma_desc_enable =
  56889. + (core_if->core_params->dma_desc_enable != 0);
  56890. + break;
  56891. +
  56892. + }
  56893. + if (core_if->dma_enable) {
  56894. + if (core_if->dma_desc_enable) {
  56895. + DWC_PRINTF("Using Descriptor DMA mode\n");
  56896. + } else {
  56897. + DWC_PRINTF("Using Buffer DMA mode\n");
  56898. +
  56899. + }
  56900. + } else {
  56901. + DWC_PRINTF("Using Slave mode\n");
  56902. + core_if->dma_desc_enable = 0;
  56903. + }
  56904. +
  56905. + if (core_if->core_params->ahb_single) {
  56906. + ahbcfg.b.ahbsingle = 1;
  56907. + }
  56908. +
  56909. + ahbcfg.b.dmaenable = core_if->dma_enable;
  56910. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  56911. +
  56912. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  56913. +
  56914. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  56915. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  56916. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  56917. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  56918. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  56919. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  56920. +
  56921. + /*
  56922. + * Program the GUSBCFG register.
  56923. + */
  56924. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56925. +
  56926. + switch (core_if->hwcfg2.b.op_mode) {
  56927. + case DWC_MODE_HNP_SRP_CAPABLE:
  56928. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  56929. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  56930. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56931. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56932. + break;
  56933. +
  56934. + case DWC_MODE_SRP_ONLY_CAPABLE:
  56935. + usbcfg.b.hnpcap = 0;
  56936. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56937. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56938. + break;
  56939. +
  56940. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  56941. + usbcfg.b.hnpcap = 0;
  56942. + usbcfg.b.srpcap = 0;
  56943. + break;
  56944. +
  56945. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  56946. + usbcfg.b.hnpcap = 0;
  56947. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56948. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56949. + break;
  56950. +
  56951. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  56952. + usbcfg.b.hnpcap = 0;
  56953. + usbcfg.b.srpcap = 0;
  56954. + break;
  56955. +
  56956. + case DWC_MODE_SRP_CAPABLE_HOST:
  56957. + usbcfg.b.hnpcap = 0;
  56958. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56959. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56960. + break;
  56961. +
  56962. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  56963. + usbcfg.b.hnpcap = 0;
  56964. + usbcfg.b.srpcap = 0;
  56965. + break;
  56966. + }
  56967. +
  56968. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56969. +
  56970. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56971. + if (core_if->core_params->lpm_enable) {
  56972. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  56973. +
  56974. + /* To enable LPM support set lpm_cap_en bit */
  56975. + lpmcfg.b.lpm_cap_en = 1;
  56976. +
  56977. + /* Make AppL1Res ACK */
  56978. + lpmcfg.b.appl_resp = 1;
  56979. +
  56980. + /* Retry 3 times */
  56981. + lpmcfg.b.retry_count = 3;
  56982. +
  56983. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  56984. + 0, lpmcfg.d32);
  56985. +
  56986. + }
  56987. +#endif
  56988. + if (core_if->core_params->ic_usb_cap) {
  56989. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  56990. + gusbcfg.b.ic_usb_cap = 1;
  56991. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  56992. + 0, gusbcfg.d32);
  56993. + }
  56994. + {
  56995. + gotgctl_data_t gotgctl = {.d32 = 0 };
  56996. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  56997. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  56998. + gotgctl.d32);
  56999. + /* Set OTG version supported */
  57000. + core_if->otg_ver = core_if->core_params->otg_ver;
  57001. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  57002. + core_if->core_params->otg_ver, core_if->otg_ver);
  57003. + }
  57004. +
  57005. +
  57006. + /* Enable common interrupts */
  57007. + dwc_otg_enable_common_interrupts(core_if);
  57008. +
  57009. + /* Do device or host intialization based on mode during PCD
  57010. + * and HCD initialization */
  57011. + if (dwc_otg_is_host_mode(core_if)) {
  57012. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  57013. + core_if->op_state = A_HOST;
  57014. + } else {
  57015. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  57016. + core_if->op_state = B_PERIPHERAL;
  57017. +#ifdef DWC_DEVICE_ONLY
  57018. + dwc_otg_core_dev_init(core_if);
  57019. +#endif
  57020. + }
  57021. +}
  57022. +
  57023. +/**
  57024. + * This function enables the Device mode interrupts.
  57025. + *
  57026. + * @param core_if Programming view of DWC_otg controller
  57027. + */
  57028. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  57029. +{
  57030. + gintmsk_data_t intr_mask = {.d32 = 0 };
  57031. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57032. +
  57033. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  57034. +
  57035. + /* Disable all interrupts. */
  57036. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  57037. +
  57038. + /* Clear any pending interrupts */
  57039. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  57040. +
  57041. + /* Enable the common interrupts */
  57042. + dwc_otg_enable_common_interrupts(core_if);
  57043. +
  57044. + /* Enable interrupts */
  57045. + intr_mask.b.usbreset = 1;
  57046. + intr_mask.b.enumdone = 1;
  57047. + /* Disable Disconnect interrupt in Device mode */
  57048. + intr_mask.b.disconnect = 0;
  57049. +
  57050. + if (!core_if->multiproc_int_enable) {
  57051. + intr_mask.b.inepintr = 1;
  57052. + intr_mask.b.outepintr = 1;
  57053. + }
  57054. +
  57055. + intr_mask.b.erlysuspend = 1;
  57056. +
  57057. + if (core_if->en_multiple_tx_fifo == 0) {
  57058. + intr_mask.b.epmismatch = 1;
  57059. + }
  57060. +
  57061. + //intr_mask.b.incomplisoout = 1;
  57062. + intr_mask.b.incomplisoin = 1;
  57063. +
  57064. +/* Enable the ignore frame number for ISOC xfers - MAS */
  57065. +/* Disable to support high bandwith ISOC transfers - manukz */
  57066. +#if 0
  57067. +#ifdef DWC_UTE_PER_IO
  57068. + if (core_if->dma_enable) {
  57069. + if (core_if->dma_desc_enable) {
  57070. + dctl_data_t dctl1 = {.d32 = 0 };
  57071. + dctl1.b.ifrmnum = 1;
  57072. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  57073. + dctl, 0, dctl1.d32);
  57074. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  57075. + DWC_READ_REG32(&core_if->dev_if->
  57076. + dev_global_regs->dctl));
  57077. + }
  57078. + }
  57079. +#endif
  57080. +#endif
  57081. +#ifdef DWC_EN_ISOC
  57082. + if (core_if->dma_enable) {
  57083. + if (core_if->dma_desc_enable == 0) {
  57084. + if (core_if->pti_enh_enable) {
  57085. + dctl_data_t dctl = {.d32 = 0 };
  57086. + dctl.b.ifrmnum = 1;
  57087. + DWC_MODIFY_REG32(&core_if->
  57088. + dev_if->dev_global_regs->dctl,
  57089. + 0, dctl.d32);
  57090. + } else {
  57091. + intr_mask.b.incomplisoin = 1;
  57092. + intr_mask.b.incomplisoout = 1;
  57093. + }
  57094. + }
  57095. + } else {
  57096. + intr_mask.b.incomplisoin = 1;
  57097. + intr_mask.b.incomplisoout = 1;
  57098. + }
  57099. +#endif /* DWC_EN_ISOC */
  57100. +
  57101. + /** @todo NGS: Should this be a module parameter? */
  57102. +#ifdef USE_PERIODIC_EP
  57103. + intr_mask.b.isooutdrop = 1;
  57104. + intr_mask.b.eopframe = 1;
  57105. + intr_mask.b.incomplisoin = 1;
  57106. + intr_mask.b.incomplisoout = 1;
  57107. +#endif
  57108. +
  57109. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  57110. +
  57111. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  57112. + DWC_READ_REG32(&global_regs->gintmsk));
  57113. +}
  57114. +
  57115. +/**
  57116. + * This function initializes the DWC_otg controller registers for
  57117. + * device mode.
  57118. + *
  57119. + * @param core_if Programming view of DWC_otg controller
  57120. + *
  57121. + */
  57122. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  57123. +{
  57124. + int i;
  57125. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57126. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  57127. + dwc_otg_core_params_t *params = core_if->core_params;
  57128. + dcfg_data_t dcfg = {.d32 = 0 };
  57129. + depctl_data_t diepctl = {.d32 = 0 };
  57130. + grstctl_t resetctl = {.d32 = 0 };
  57131. + uint32_t rx_fifo_size;
  57132. + fifosize_data_t nptxfifosize;
  57133. + fifosize_data_t txfifosize;
  57134. + dthrctl_data_t dthrctl;
  57135. + fifosize_data_t ptxfifosize;
  57136. + uint16_t rxfsiz, nptxfsiz;
  57137. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  57138. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  57139. +
  57140. + /* Restart the Phy Clock */
  57141. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  57142. +
  57143. + /* Device configuration register */
  57144. + init_devspd(core_if);
  57145. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  57146. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  57147. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  57148. + /* Enable Device OUT NAK in case of DDMA mode*/
  57149. + if (core_if->core_params->dev_out_nak) {
  57150. + dcfg.b.endevoutnak = 1;
  57151. + }
  57152. +
  57153. + if (core_if->core_params->cont_on_bna) {
  57154. + dctl_data_t dctl = {.d32 = 0 };
  57155. + dctl.b.encontonbna = 1;
  57156. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57157. + }
  57158. +
  57159. +
  57160. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  57161. +
  57162. + /* Configure data FIFO sizes */
  57163. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  57164. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  57165. + core_if->total_fifo_size);
  57166. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  57167. + params->dev_rx_fifo_size);
  57168. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  57169. + params->dev_nperio_tx_fifo_size);
  57170. +
  57171. + /* Rx FIFO */
  57172. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  57173. + DWC_READ_REG32(&global_regs->grxfsiz));
  57174. +
  57175. +#ifdef DWC_UTE_CFI
  57176. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  57177. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  57178. +#endif
  57179. + rx_fifo_size = params->dev_rx_fifo_size;
  57180. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  57181. +
  57182. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  57183. + DWC_READ_REG32(&global_regs->grxfsiz));
  57184. +
  57185. + /** Set Periodic Tx FIFO Mask all bits 0 */
  57186. + core_if->p_tx_msk = 0;
  57187. +
  57188. + /** Set Tx FIFO Mask all bits 0 */
  57189. + core_if->tx_msk = 0;
  57190. +
  57191. + if (core_if->en_multiple_tx_fifo == 0) {
  57192. + /* Non-periodic Tx FIFO */
  57193. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  57194. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57195. +
  57196. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  57197. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  57198. +
  57199. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  57200. + nptxfifosize.d32);
  57201. +
  57202. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  57203. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57204. +
  57205. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  57206. + /*
  57207. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  57208. + * Indexes of the FIFO size module parameters in the
  57209. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  57210. + * the dptxfsiz array run from 0 to 14.
  57211. + */
  57212. + /** @todo Finish debug of this */
  57213. + ptxfifosize.b.startaddr =
  57214. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  57215. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  57216. + ptxfifosize.b.depth =
  57217. + params->dev_perio_tx_fifo_size[i];
  57218. + DWC_DEBUGPL(DBG_CIL,
  57219. + "initial dtxfsiz[%d]=%08x\n", i,
  57220. + DWC_READ_REG32(&global_regs->dtxfsiz
  57221. + [i]));
  57222. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  57223. + ptxfifosize.d32);
  57224. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  57225. + i,
  57226. + DWC_READ_REG32(&global_regs->dtxfsiz
  57227. + [i]));
  57228. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  57229. + }
  57230. + } else {
  57231. + /*
  57232. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  57233. + * Indexes of the FIFO size module parameters in the
  57234. + * dev_tx_fifo_size array and the FIFO size registers in
  57235. + * the dtxfsiz array run from 0 to 14.
  57236. + */
  57237. +
  57238. + /* Non-periodic Tx FIFO */
  57239. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  57240. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57241. +
  57242. +#ifdef DWC_UTE_CFI
  57243. + core_if->pwron_gnptxfsiz =
  57244. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  57245. + core_if->init_gnptxfsiz =
  57246. + params->dev_nperio_tx_fifo_size;
  57247. +#endif
  57248. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  57249. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  57250. +
  57251. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  57252. + nptxfifosize.d32);
  57253. +
  57254. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  57255. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57256. +
  57257. + txfifosize.b.startaddr =
  57258. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  57259. +
  57260. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  57261. +
  57262. + txfifosize.b.depth =
  57263. + params->dev_tx_fifo_size[i];
  57264. +
  57265. + DWC_DEBUGPL(DBG_CIL,
  57266. + "initial dtxfsiz[%d]=%08x\n",
  57267. + i,
  57268. + DWC_READ_REG32(&global_regs->dtxfsiz
  57269. + [i]));
  57270. +
  57271. +#ifdef DWC_UTE_CFI
  57272. + core_if->pwron_txfsiz[i] =
  57273. + (DWC_READ_REG32
  57274. + (&global_regs->dtxfsiz[i]) >> 16);
  57275. + core_if->init_txfsiz[i] =
  57276. + params->dev_tx_fifo_size[i];
  57277. +#endif
  57278. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  57279. + txfifosize.d32);
  57280. +
  57281. + DWC_DEBUGPL(DBG_CIL,
  57282. + "new dtxfsiz[%d]=%08x\n",
  57283. + i,
  57284. + DWC_READ_REG32(&global_regs->dtxfsiz
  57285. + [i]));
  57286. +
  57287. + txfifosize.b.startaddr += txfifosize.b.depth;
  57288. + }
  57289. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  57290. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  57291. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  57292. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  57293. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  57294. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  57295. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  57296. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  57297. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  57298. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  57299. + }
  57300. + }
  57301. +
  57302. + /* Flush the FIFOs */
  57303. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  57304. + dwc_otg_flush_rx_fifo(core_if);
  57305. +
  57306. + /* Flush the Learning Queue. */
  57307. + resetctl.b.intknqflsh = 1;
  57308. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  57309. +
  57310. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  57311. + core_if->start_predict = 0;
  57312. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  57313. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  57314. + }
  57315. + core_if->nextep_seq[0] = 0;
  57316. + core_if->first_in_nextep_seq = 0;
  57317. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  57318. + diepctl.b.nextep = 0;
  57319. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  57320. +
  57321. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  57322. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  57323. + dcfg.b.epmscnt = 2;
  57324. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  57325. +
  57326. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  57327. + __func__, core_if->first_in_nextep_seq);
  57328. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  57329. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  57330. + }
  57331. + DWC_DEBUGPL(DBG_CILV,"\n");
  57332. + }
  57333. +
  57334. + /* Clear all pending Device Interrupts */
  57335. + /** @todo - if the condition needed to be checked
  57336. + * or in any case all pending interrutps should be cleared?
  57337. + */
  57338. + if (core_if->multiproc_int_enable) {
  57339. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  57340. + DWC_WRITE_REG32(&dev_if->
  57341. + dev_global_regs->diepeachintmsk[i], 0);
  57342. + }
  57343. + }
  57344. +
  57345. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  57346. + DWC_WRITE_REG32(&dev_if->
  57347. + dev_global_regs->doepeachintmsk[i], 0);
  57348. + }
  57349. +
  57350. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  57351. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  57352. + } else {
  57353. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  57354. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  57355. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  57356. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  57357. + }
  57358. +
  57359. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  57360. + depctl_data_t depctl;
  57361. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  57362. + if (depctl.b.epena) {
  57363. + depctl.d32 = 0;
  57364. + depctl.b.epdis = 1;
  57365. + depctl.b.snak = 1;
  57366. + } else {
  57367. + depctl.d32 = 0;
  57368. + }
  57369. +
  57370. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  57371. +
  57372. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  57373. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  57374. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  57375. + }
  57376. +
  57377. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  57378. + depctl_data_t depctl;
  57379. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  57380. + if (depctl.b.epena) {
  57381. + dctl_data_t dctl = {.d32 = 0 };
  57382. + gintmsk_data_t gintsts = {.d32 = 0 };
  57383. + doepint_data_t doepint = {.d32 = 0 };
  57384. + dctl.b.sgoutnak = 1;
  57385. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57386. + do {
  57387. + dwc_udelay(10);
  57388. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  57389. + } while (!gintsts.b.goutnakeff);
  57390. + gintsts.d32 = 0;
  57391. + gintsts.b.goutnakeff = 1;
  57392. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  57393. +
  57394. + depctl.d32 = 0;
  57395. + depctl.b.epdis = 1;
  57396. + depctl.b.snak = 1;
  57397. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  57398. + do {
  57399. + dwc_udelay(10);
  57400. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  57401. + out_ep_regs[i]->doepint);
  57402. + } while (!doepint.b.epdisabled);
  57403. +
  57404. + doepint.b.epdisabled = 1;
  57405. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  57406. +
  57407. + dctl.d32 = 0;
  57408. + dctl.b.cgoutnak = 1;
  57409. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57410. + } else {
  57411. + depctl.d32 = 0;
  57412. + }
  57413. +
  57414. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  57415. +
  57416. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  57417. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  57418. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  57419. + }
  57420. +
  57421. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  57422. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  57423. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  57424. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  57425. +
  57426. + dev_if->rx_thr_length = params->rx_thr_length;
  57427. + dev_if->tx_thr_length = params->tx_thr_length;
  57428. +
  57429. + dev_if->setup_desc_index = 0;
  57430. +
  57431. + dthrctl.d32 = 0;
  57432. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  57433. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  57434. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  57435. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  57436. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  57437. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  57438. +
  57439. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  57440. + dthrctl.d32);
  57441. +
  57442. + DWC_DEBUGPL(DBG_CIL,
  57443. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  57444. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  57445. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  57446. + dthrctl.b.rx_thr_len);
  57447. +
  57448. + }
  57449. +
  57450. + dwc_otg_enable_device_interrupts(core_if);
  57451. +
  57452. + {
  57453. + diepmsk_data_t msk = {.d32 = 0 };
  57454. + msk.b.txfifoundrn = 1;
  57455. + if (core_if->multiproc_int_enable) {
  57456. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  57457. + diepeachintmsk[0], msk.d32, msk.d32);
  57458. + } else {
  57459. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  57460. + msk.d32, msk.d32);
  57461. + }
  57462. + }
  57463. +
  57464. + if (core_if->multiproc_int_enable) {
  57465. + /* Set NAK on Babble */
  57466. + dctl_data_t dctl = {.d32 = 0 };
  57467. + dctl.b.nakonbble = 1;
  57468. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  57469. + }
  57470. +
  57471. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  57472. + dctl_data_t dctl = {.d32 = 0 };
  57473. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  57474. + dctl.b.sftdiscon = 0;
  57475. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  57476. + }
  57477. +}
  57478. +
  57479. +/**
  57480. + * This function enables the Host mode interrupts.
  57481. + *
  57482. + * @param core_if Programming view of DWC_otg controller
  57483. + */
  57484. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  57485. +{
  57486. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57487. + gintmsk_data_t intr_mask = {.d32 = 0 };
  57488. +
  57489. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  57490. +
  57491. + /* Disable all interrupts. */
  57492. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  57493. +
  57494. + /* Clear any pending interrupts. */
  57495. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  57496. +
  57497. + /* Enable the common interrupts */
  57498. + dwc_otg_enable_common_interrupts(core_if);
  57499. +
  57500. + /*
  57501. + * Enable host mode interrupts without disturbing common
  57502. + * interrupts.
  57503. + */
  57504. +
  57505. + intr_mask.b.disconnect = 1;
  57506. + intr_mask.b.portintr = 1;
  57507. + intr_mask.b.hcintr = 1;
  57508. +
  57509. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  57510. +}
  57511. +
  57512. +/**
  57513. + * This function disables the Host Mode interrupts.
  57514. + *
  57515. + * @param core_if Programming view of DWC_otg controller
  57516. + */
  57517. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  57518. +{
  57519. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57520. + gintmsk_data_t intr_mask = {.d32 = 0 };
  57521. +
  57522. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  57523. +
  57524. + /*
  57525. + * Disable host mode interrupts without disturbing common
  57526. + * interrupts.
  57527. + */
  57528. + intr_mask.b.sofintr = 1;
  57529. + intr_mask.b.portintr = 1;
  57530. + intr_mask.b.hcintr = 1;
  57531. + intr_mask.b.ptxfempty = 1;
  57532. + intr_mask.b.nptxfempty = 1;
  57533. +
  57534. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  57535. +}
  57536. +
  57537. +/**
  57538. + * This function initializes the DWC_otg controller registers for
  57539. + * host mode.
  57540. + *
  57541. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  57542. + * request queues. Host channels are reset to ensure that they are ready for
  57543. + * performing transfers.
  57544. + *
  57545. + * @param core_if Programming view of DWC_otg controller
  57546. + *
  57547. + */
  57548. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  57549. +{
  57550. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  57551. + dwc_otg_host_if_t *host_if = core_if->host_if;
  57552. + dwc_otg_core_params_t *params = core_if->core_params;
  57553. + hprt0_data_t hprt0 = {.d32 = 0 };
  57554. + fifosize_data_t nptxfifosize;
  57555. + fifosize_data_t ptxfifosize;
  57556. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  57557. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  57558. + int i;
  57559. + hcchar_data_t hcchar;
  57560. + hcfg_data_t hcfg;
  57561. + hfir_data_t hfir;
  57562. + dwc_otg_hc_regs_t *hc_regs;
  57563. + int num_channels;
  57564. + gotgctl_data_t gotgctl = {.d32 = 0 };
  57565. +
  57566. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  57567. +
  57568. + /* Restart the Phy Clock */
  57569. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  57570. +
  57571. + /* Initialize Host Configuration Register */
  57572. + init_fslspclksel(core_if);
  57573. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  57574. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  57575. + hcfg.b.fslssupp = 1;
  57576. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  57577. +
  57578. + }
  57579. +
  57580. + /* This bit allows dynamic reloading of the HFIR register
  57581. + * during runtime. This bit needs to be programmed during
  57582. + * initial configuration and its value must not be changed
  57583. + * during runtime.*/
  57584. + if (core_if->core_params->reload_ctl == 1) {
  57585. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  57586. + hfir.b.hfirrldctrl = 1;
  57587. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  57588. + }
  57589. +
  57590. + if (core_if->core_params->dma_desc_enable) {
  57591. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  57592. + if (!
  57593. + (core_if->hwcfg4.b.desc_dma
  57594. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  57595. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  57596. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  57597. + || (op_mode ==
  57598. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  57599. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  57600. + || (op_mode ==
  57601. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  57602. +
  57603. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  57604. + "Either core version is below 2.90a or "
  57605. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  57606. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  57607. + "module parameter to 0.\n");
  57608. + return;
  57609. + }
  57610. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  57611. + hcfg.b.descdma = 1;
  57612. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  57613. + }
  57614. +
  57615. + /* Configure data FIFO sizes */
  57616. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  57617. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  57618. + core_if->total_fifo_size);
  57619. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  57620. + params->host_rx_fifo_size);
  57621. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  57622. + params->host_nperio_tx_fifo_size);
  57623. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  57624. + params->host_perio_tx_fifo_size);
  57625. +
  57626. + /* Rx FIFO */
  57627. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  57628. + DWC_READ_REG32(&global_regs->grxfsiz));
  57629. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  57630. + params->host_rx_fifo_size);
  57631. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  57632. + DWC_READ_REG32(&global_regs->grxfsiz));
  57633. +
  57634. + /* Non-periodic Tx FIFO */
  57635. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  57636. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57637. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  57638. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  57639. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  57640. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  57641. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  57642. +
  57643. + /* Periodic Tx FIFO */
  57644. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  57645. + DWC_READ_REG32(&global_regs->hptxfsiz));
  57646. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  57647. + ptxfifosize.b.startaddr =
  57648. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  57649. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  57650. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  57651. + DWC_READ_REG32(&global_regs->hptxfsiz));
  57652. +
  57653. + if (core_if->en_multiple_tx_fifo
  57654. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  57655. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  57656. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  57657. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  57658. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  57659. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  57660. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  57661. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  57662. + }
  57663. + }
  57664. +
  57665. + /* TODO - check this */
  57666. + /* Clear Host Set HNP Enable in the OTG Control Register */
  57667. + gotgctl.b.hstsethnpen = 1;
  57668. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  57669. + /* Make sure the FIFOs are flushed. */
  57670. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  57671. + dwc_otg_flush_rx_fifo(core_if);
  57672. +
  57673. + /* Clear Host Set HNP Enable in the OTG Control Register */
  57674. + gotgctl.b.hstsethnpen = 1;
  57675. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  57676. +
  57677. + if (!core_if->core_params->dma_desc_enable) {
  57678. + /* Flush out any leftover queued requests. */
  57679. + num_channels = core_if->core_params->host_channels;
  57680. +
  57681. + for (i = 0; i < num_channels; i++) {
  57682. + hc_regs = core_if->host_if->hc_regs[i];
  57683. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57684. + hcchar.b.chen = 0;
  57685. + hcchar.b.chdis = 1;
  57686. + hcchar.b.epdir = 0;
  57687. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57688. + }
  57689. +
  57690. + /* Halt all channels to put them into a known state. */
  57691. + for (i = 0; i < num_channels; i++) {
  57692. + int count = 0;
  57693. + hc_regs = core_if->host_if->hc_regs[i];
  57694. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57695. + hcchar.b.chen = 1;
  57696. + hcchar.b.chdis = 1;
  57697. + hcchar.b.epdir = 0;
  57698. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57699. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  57700. + do {
  57701. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57702. + if (++count > 1000) {
  57703. + DWC_ERROR
  57704. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  57705. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  57706. + break;
  57707. + }
  57708. + dwc_udelay(1);
  57709. + } while (hcchar.b.chen);
  57710. + }
  57711. + }
  57712. +
  57713. + /* Turn on the vbus power. */
  57714. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  57715. + if (core_if->op_state == A_HOST) {
  57716. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  57717. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  57718. + if (hprt0.b.prtpwr == 0) {
  57719. + hprt0.b.prtpwr = 1;
  57720. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  57721. + }
  57722. + }
  57723. +
  57724. + dwc_otg_enable_host_interrupts(core_if);
  57725. +}
  57726. +
  57727. +/**
  57728. + * Prepares a host channel for transferring packets to/from a specific
  57729. + * endpoint. The HCCHARn register is set up with the characteristics specified
  57730. + * in _hc. Host channel interrupts that may need to be serviced while this
  57731. + * transfer is in progress are enabled.
  57732. + *
  57733. + * @param core_if Programming view of DWC_otg controller
  57734. + * @param hc Information needed to initialize the host channel
  57735. + */
  57736. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57737. +{
  57738. + uint32_t intr_enable;
  57739. + hcintmsk_data_t hc_intr_mask;
  57740. + gintmsk_data_t gintmsk = {.d32 = 0 };
  57741. + hcchar_data_t hcchar;
  57742. + hcsplt_data_t hcsplt;
  57743. +
  57744. + uint8_t hc_num = hc->hc_num;
  57745. + dwc_otg_host_if_t *host_if = core_if->host_if;
  57746. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  57747. +
  57748. + /* Clear old interrupt conditions for this host channel. */
  57749. + hc_intr_mask.d32 = 0xFFFFFFFF;
  57750. + hc_intr_mask.b.reserved14_31 = 0;
  57751. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  57752. +
  57753. + /* Enable channel interrupts required for this transfer. */
  57754. + hc_intr_mask.d32 = 0;
  57755. + hc_intr_mask.b.chhltd = 1;
  57756. + if (core_if->dma_enable) {
  57757. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  57758. + if (!core_if->dma_desc_enable)
  57759. + hc_intr_mask.b.ahberr = 1;
  57760. + else {
  57761. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  57762. + hc_intr_mask.b.xfercompl = 1;
  57763. + }
  57764. +
  57765. + if (hc->error_state && !hc->do_split &&
  57766. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  57767. + hc_intr_mask.b.ack = 1;
  57768. + if (hc->ep_is_in) {
  57769. + hc_intr_mask.b.datatglerr = 1;
  57770. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  57771. + hc_intr_mask.b.nak = 1;
  57772. + }
  57773. + }
  57774. + }
  57775. + } else {
  57776. + switch (hc->ep_type) {
  57777. + case DWC_OTG_EP_TYPE_CONTROL:
  57778. + case DWC_OTG_EP_TYPE_BULK:
  57779. + hc_intr_mask.b.xfercompl = 1;
  57780. + hc_intr_mask.b.stall = 1;
  57781. + hc_intr_mask.b.xacterr = 1;
  57782. + hc_intr_mask.b.datatglerr = 1;
  57783. + if (hc->ep_is_in) {
  57784. + hc_intr_mask.b.bblerr = 1;
  57785. + } else {
  57786. + hc_intr_mask.b.nak = 1;
  57787. + hc_intr_mask.b.nyet = 1;
  57788. + if (hc->do_ping) {
  57789. + hc_intr_mask.b.ack = 1;
  57790. + }
  57791. + }
  57792. +
  57793. + if (hc->do_split) {
  57794. + hc_intr_mask.b.nak = 1;
  57795. + if (hc->complete_split) {
  57796. + hc_intr_mask.b.nyet = 1;
  57797. + } else {
  57798. + hc_intr_mask.b.ack = 1;
  57799. + }
  57800. + }
  57801. +
  57802. + if (hc->error_state) {
  57803. + hc_intr_mask.b.ack = 1;
  57804. + }
  57805. + break;
  57806. + case DWC_OTG_EP_TYPE_INTR:
  57807. + hc_intr_mask.b.xfercompl = 1;
  57808. + hc_intr_mask.b.nak = 1;
  57809. + hc_intr_mask.b.stall = 1;
  57810. + hc_intr_mask.b.xacterr = 1;
  57811. + hc_intr_mask.b.datatglerr = 1;
  57812. + hc_intr_mask.b.frmovrun = 1;
  57813. +
  57814. + if (hc->ep_is_in) {
  57815. + hc_intr_mask.b.bblerr = 1;
  57816. + }
  57817. + if (hc->error_state) {
  57818. + hc_intr_mask.b.ack = 1;
  57819. + }
  57820. + if (hc->do_split) {
  57821. + if (hc->complete_split) {
  57822. + hc_intr_mask.b.nyet = 1;
  57823. + } else {
  57824. + hc_intr_mask.b.ack = 1;
  57825. + }
  57826. + }
  57827. + break;
  57828. + case DWC_OTG_EP_TYPE_ISOC:
  57829. + hc_intr_mask.b.xfercompl = 1;
  57830. + hc_intr_mask.b.frmovrun = 1;
  57831. + hc_intr_mask.b.ack = 1;
  57832. +
  57833. + if (hc->ep_is_in) {
  57834. + hc_intr_mask.b.xacterr = 1;
  57835. + hc_intr_mask.b.bblerr = 1;
  57836. + }
  57837. + break;
  57838. + }
  57839. + }
  57840. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  57841. +
  57842. + /* Enable the top level host channel interrupt. */
  57843. + intr_enable = (1 << hc_num);
  57844. + DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  57845. +
  57846. + /* Make sure host channel interrupts are enabled. */
  57847. + gintmsk.b.hcintr = 1;
  57848. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  57849. +
  57850. + /*
  57851. + * Program the HCCHARn register with the endpoint characteristics for
  57852. + * the current transfer.
  57853. + */
  57854. + hcchar.d32 = 0;
  57855. + hcchar.b.devaddr = hc->dev_addr;
  57856. + hcchar.b.epnum = hc->ep_num;
  57857. + hcchar.b.epdir = hc->ep_is_in;
  57858. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  57859. + hcchar.b.eptype = hc->ep_type;
  57860. + hcchar.b.mps = hc->max_packet;
  57861. +
  57862. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  57863. +
  57864. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  57865. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  57866. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  57867. + "Max Pkt %d, Multi Cnt %d\n",
  57868. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  57869. + hcchar.b.mps, hcchar.b.multicnt);
  57870. +
  57871. + /*
  57872. + * Program the HCSPLIT register for SPLITs
  57873. + */
  57874. + hcsplt.d32 = 0;
  57875. + if (hc->do_split) {
  57876. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  57877. + hc->hc_num,
  57878. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  57879. + hcsplt.b.compsplt = hc->complete_split;
  57880. + hcsplt.b.xactpos = hc->xact_pos;
  57881. + hcsplt.b.hubaddr = hc->hub_addr;
  57882. + hcsplt.b.prtaddr = hc->port_addr;
  57883. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  57884. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  57885. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  57886. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  57887. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  57888. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  57889. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  57890. + }
  57891. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  57892. +
  57893. +}
  57894. +
  57895. +/**
  57896. + * Attempts to halt a host channel. This function should only be called in
  57897. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  57898. + * normal circumstances in DMA mode, the controller halts the channel when the
  57899. + * transfer is complete or a condition occurs that requires application
  57900. + * intervention.
  57901. + *
  57902. + * In slave mode, checks for a free request queue entry, then sets the Channel
  57903. + * Enable and Channel Disable bits of the Host Channel Characteristics
  57904. + * register of the specified channel to intiate the halt. If there is no free
  57905. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  57906. + * register to flush requests for this channel. In the latter case, sets a
  57907. + * flag to indicate that the host channel needs to be halted when a request
  57908. + * queue slot is open.
  57909. + *
  57910. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  57911. + * HCCHARn register. The controller ensures there is space in the request
  57912. + * queue before submitting the halt request.
  57913. + *
  57914. + * Some time may elapse before the core flushes any posted requests for this
  57915. + * host channel and halts. The Channel Halted interrupt handler completes the
  57916. + * deactivation of the host channel.
  57917. + *
  57918. + * @param core_if Controller register interface.
  57919. + * @param hc Host channel to halt.
  57920. + * @param halt_status Reason for halting the channel.
  57921. + */
  57922. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  57923. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  57924. +{
  57925. + gnptxsts_data_t nptxsts;
  57926. + hptxsts_data_t hptxsts;
  57927. + hcchar_data_t hcchar;
  57928. + dwc_otg_hc_regs_t *hc_regs;
  57929. + dwc_otg_core_global_regs_t *global_regs;
  57930. + dwc_otg_host_global_regs_t *host_global_regs;
  57931. +
  57932. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57933. + global_regs = core_if->core_global_regs;
  57934. + host_global_regs = core_if->host_if->host_global_regs;
  57935. +
  57936. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  57937. + "halt_status = %d\n", halt_status);
  57938. +
  57939. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  57940. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  57941. + /*
  57942. + * Disable all channel interrupts except Ch Halted. The QTD
  57943. + * and QH state associated with this transfer has been cleared
  57944. + * (in the case of URB_DEQUEUE), so the channel needs to be
  57945. + * shut down carefully to prevent crashes.
  57946. + */
  57947. + hcintmsk_data_t hcintmsk;
  57948. + hcintmsk.d32 = 0;
  57949. + hcintmsk.b.chhltd = 1;
  57950. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  57951. +
  57952. + /*
  57953. + * Make sure no other interrupts besides halt are currently
  57954. + * pending. Handling another interrupt could cause a crash due
  57955. + * to the QTD and QH state.
  57956. + */
  57957. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  57958. +
  57959. + /*
  57960. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  57961. + * even if the channel was already halted for some other
  57962. + * reason.
  57963. + */
  57964. + hc->halt_status = halt_status;
  57965. +
  57966. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57967. + if (hcchar.b.chen == 0) {
  57968. + /*
  57969. + * The channel is either already halted or it hasn't
  57970. + * started yet. In DMA mode, the transfer may halt if
  57971. + * it finishes normally or a condition occurs that
  57972. + * requires driver intervention. Don't want to halt
  57973. + * the channel again. In either Slave or DMA mode,
  57974. + * it's possible that the transfer has been assigned
  57975. + * to a channel, but not started yet when an URB is
  57976. + * dequeued. Don't want to halt a channel that hasn't
  57977. + * started yet.
  57978. + */
  57979. + return;
  57980. + }
  57981. + }
  57982. + if (hc->halt_pending) {
  57983. + /*
  57984. + * A halt has already been issued for this channel. This might
  57985. + * happen when a transfer is aborted by a higher level in
  57986. + * the stack.
  57987. + */
  57988. +#ifdef DEBUG
  57989. + DWC_PRINTF
  57990. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  57991. + __func__, hc->hc_num);
  57992. +
  57993. +#endif
  57994. + return;
  57995. + }
  57996. +
  57997. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57998. +
  57999. + /* No need to set the bit in DDMA for disabling the channel */
  58000. + //TODO check it everywhere channel is disabled
  58001. + if (!core_if->core_params->dma_desc_enable)
  58002. + hcchar.b.chen = 1;
  58003. + hcchar.b.chdis = 1;
  58004. +
  58005. + if (!core_if->dma_enable) {
  58006. + /* Check for space in the request queue to issue the halt. */
  58007. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  58008. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  58009. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  58010. + if (nptxsts.b.nptxqspcavail == 0) {
  58011. + hcchar.b.chen = 0;
  58012. + }
  58013. + } else {
  58014. + hptxsts.d32 =
  58015. + DWC_READ_REG32(&host_global_regs->hptxsts);
  58016. + if ((hptxsts.b.ptxqspcavail == 0)
  58017. + || (core_if->queuing_high_bandwidth)) {
  58018. + hcchar.b.chen = 0;
  58019. + }
  58020. + }
  58021. + }
  58022. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58023. +
  58024. + hc->halt_status = halt_status;
  58025. +
  58026. + if (hcchar.b.chen) {
  58027. + hc->halt_pending = 1;
  58028. + hc->halt_on_queue = 0;
  58029. + } else {
  58030. + hc->halt_on_queue = 1;
  58031. + }
  58032. +
  58033. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58034. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  58035. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  58036. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  58037. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  58038. +
  58039. + return;
  58040. +}
  58041. +
  58042. +/**
  58043. + * Clears the transfer state for a host channel. This function is normally
  58044. + * called after a transfer is done and the host channel is being released.
  58045. + *
  58046. + * @param core_if Programming view of DWC_otg controller.
  58047. + * @param hc Identifies the host channel to clean up.
  58048. + */
  58049. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58050. +{
  58051. + dwc_otg_hc_regs_t *hc_regs;
  58052. +
  58053. + hc->xfer_started = 0;
  58054. +
  58055. + /*
  58056. + * Clear channel interrupt enables and any unhandled channel interrupt
  58057. + * conditions.
  58058. + */
  58059. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58060. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  58061. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  58062. +#ifdef DEBUG
  58063. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  58064. +#endif
  58065. +}
  58066. +
  58067. +/**
  58068. + * Sets the channel property that indicates in which frame a periodic transfer
  58069. + * should occur. This is always set to the _next_ frame. This function has no
  58070. + * effect on non-periodic transfers.
  58071. + *
  58072. + * @param core_if Programming view of DWC_otg controller.
  58073. + * @param hc Identifies the host channel to set up and its properties.
  58074. + * @param hcchar Current value of the HCCHAR register for the specified host
  58075. + * channel.
  58076. + */
  58077. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  58078. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  58079. +{
  58080. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58081. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58082. + hfnum_data_t hfnum;
  58083. + hfnum.d32 =
  58084. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  58085. +
  58086. + /* 1 if _next_ frame is odd, 0 if it's even */
  58087. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  58088. +#ifdef DEBUG
  58089. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  58090. + && !hc->complete_split) {
  58091. + switch (hfnum.b.frnum & 0x7) {
  58092. + case 7:
  58093. + core_if->hfnum_7_samples++;
  58094. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  58095. + break;
  58096. + case 0:
  58097. + core_if->hfnum_0_samples++;
  58098. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  58099. + break;
  58100. + default:
  58101. + core_if->hfnum_other_samples++;
  58102. + core_if->hfnum_other_frrem_accum +=
  58103. + hfnum.b.frrem;
  58104. + break;
  58105. + }
  58106. + }
  58107. +#endif
  58108. + }
  58109. +}
  58110. +
  58111. +#ifdef DEBUG
  58112. +void hc_xfer_timeout(void *ptr)
  58113. +{
  58114. + hc_xfer_info_t *xfer_info = NULL;
  58115. + int hc_num = 0;
  58116. +
  58117. + if (ptr)
  58118. + xfer_info = (hc_xfer_info_t *) ptr;
  58119. +
  58120. + if (!xfer_info->hc) {
  58121. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  58122. + return;
  58123. + }
  58124. +
  58125. + hc_num = xfer_info->hc->hc_num;
  58126. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  58127. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  58128. + xfer_info->core_if->start_hcchar_val[hc_num]);
  58129. +}
  58130. +#endif
  58131. +
  58132. +void ep_xfer_timeout(void *ptr)
  58133. +{
  58134. + ep_xfer_info_t *xfer_info = NULL;
  58135. + int ep_num = 0;
  58136. + dctl_data_t dctl = {.d32 = 0 };
  58137. + gintsts_data_t gintsts = {.d32 = 0 };
  58138. + gintmsk_data_t gintmsk = {.d32 = 0 };
  58139. +
  58140. + if (ptr)
  58141. + xfer_info = (ep_xfer_info_t *) ptr;
  58142. +
  58143. + if (!xfer_info->ep) {
  58144. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  58145. + return;
  58146. + }
  58147. +
  58148. + ep_num = xfer_info->ep->num;
  58149. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  58150. + /* Put the sate to 2 as it was time outed */
  58151. + xfer_info->state = 2;
  58152. +
  58153. + dctl.d32 =
  58154. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  58155. + gintsts.d32 =
  58156. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  58157. + gintmsk.d32 =
  58158. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  58159. +
  58160. + if (!gintmsk.b.goutnakeff) {
  58161. + /* Unmask it */
  58162. + gintmsk.b.goutnakeff = 1;
  58163. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  58164. + gintmsk.d32);
  58165. +
  58166. + }
  58167. +
  58168. + if (!gintsts.b.goutnakeff) {
  58169. + dctl.b.sgoutnak = 1;
  58170. + }
  58171. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  58172. + dctl.d32);
  58173. +
  58174. +}
  58175. +
  58176. +void set_pid_isoc(dwc_hc_t * hc)
  58177. +{
  58178. + /* Set up the initial PID for the transfer. */
  58179. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  58180. + if (hc->ep_is_in) {
  58181. + if (hc->multi_count == 1) {
  58182. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  58183. + } else if (hc->multi_count == 2) {
  58184. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  58185. + } else {
  58186. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  58187. + }
  58188. + } else {
  58189. + if (hc->multi_count == 1) {
  58190. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  58191. + } else {
  58192. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  58193. + }
  58194. + }
  58195. + } else {
  58196. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  58197. + }
  58198. +}
  58199. +
  58200. +/**
  58201. + * This function does the setup for a data transfer for a host channel and
  58202. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  58203. + * Slave mode, the caller must ensure that there is sufficient space in the
  58204. + * request queue and Tx Data FIFO.
  58205. + *
  58206. + * For an OUT transfer in Slave mode, it loads a data packet into the
  58207. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  58208. + * the Host ISR.
  58209. + *
  58210. + * For an IN transfer in Slave mode, a data packet is requested. The data
  58211. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  58212. + * additional data packets are requested in the Host ISR.
  58213. + *
  58214. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  58215. + * register along with a packet count of 1 and the channel is enabled. This
  58216. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  58217. + * simply set to 0 since no data transfer occurs in this case.
  58218. + *
  58219. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  58220. + * all the information required to perform the subsequent data transfer. In
  58221. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  58222. + * controller performs the entire PING protocol, then starts the data
  58223. + * transfer.
  58224. + *
  58225. + * @param core_if Programming view of DWC_otg controller.
  58226. + * @param hc Information needed to initialize the host channel. The xfer_len
  58227. + * value may be reduced to accommodate the max widths of the XferSize and
  58228. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  58229. + * to reflect the final xfer_len value.
  58230. + */
  58231. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58232. +{
  58233. + hcchar_data_t hcchar;
  58234. + hctsiz_data_t hctsiz;
  58235. + uint16_t num_packets;
  58236. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  58237. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  58238. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58239. +
  58240. + hctsiz.d32 = 0;
  58241. +
  58242. + if (hc->do_ping) {
  58243. + if (!core_if->dma_enable) {
  58244. + dwc_otg_hc_do_ping(core_if, hc);
  58245. + hc->xfer_started = 1;
  58246. + return;
  58247. + } else {
  58248. + hctsiz.b.dopng = 1;
  58249. + }
  58250. + }
  58251. +
  58252. + if (hc->do_split) {
  58253. + num_packets = 1;
  58254. +
  58255. + if (hc->complete_split && !hc->ep_is_in) {
  58256. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  58257. + * core doesn't expect any data written to the FIFO */
  58258. + hc->xfer_len = 0;
  58259. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  58260. + hc->xfer_len = hc->max_packet;
  58261. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  58262. + hc->xfer_len = 188;
  58263. + }
  58264. +
  58265. + hctsiz.b.xfersize = hc->xfer_len;
  58266. + } else {
  58267. + /*
  58268. + * Ensure that the transfer length and packet count will fit
  58269. + * in the widths allocated for them in the HCTSIZn register.
  58270. + */
  58271. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58272. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58273. + /*
  58274. + * Make sure the transfer size is no larger than one
  58275. + * (micro)frame's worth of data. (A check was done
  58276. + * when the periodic transfer was accepted to ensure
  58277. + * that a (micro)frame's worth of data can be
  58278. + * programmed into a channel.)
  58279. + */
  58280. + uint32_t max_periodic_len =
  58281. + hc->multi_count * hc->max_packet;
  58282. + if (hc->xfer_len > max_periodic_len) {
  58283. + hc->xfer_len = max_periodic_len;
  58284. + } else {
  58285. + }
  58286. + } else if (hc->xfer_len > max_hc_xfer_size) {
  58287. + /* Make sure that xfer_len is a multiple of max packet size. */
  58288. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  58289. + }
  58290. +
  58291. + if (hc->xfer_len > 0) {
  58292. + num_packets =
  58293. + (hc->xfer_len + hc->max_packet -
  58294. + 1) / hc->max_packet;
  58295. + if (num_packets > max_hc_pkt_count) {
  58296. + num_packets = max_hc_pkt_count;
  58297. + hc->xfer_len = num_packets * hc->max_packet;
  58298. + }
  58299. + } else {
  58300. + /* Need 1 packet for transfer length of 0. */
  58301. + num_packets = 1;
  58302. + }
  58303. +
  58304. + if (hc->ep_is_in) {
  58305. + /* Always program an integral # of max packets for IN transfers. */
  58306. + hc->xfer_len = num_packets * hc->max_packet;
  58307. + }
  58308. +
  58309. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58310. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58311. + /*
  58312. + * Make sure that the multi_count field matches the
  58313. + * actual transfer length.
  58314. + */
  58315. + hc->multi_count = num_packets;
  58316. + }
  58317. +
  58318. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  58319. + set_pid_isoc(hc);
  58320. +
  58321. + hctsiz.b.xfersize = hc->xfer_len;
  58322. + }
  58323. +
  58324. + hc->start_pkt_count = num_packets;
  58325. + hctsiz.b.pktcnt = num_packets;
  58326. + hctsiz.b.pid = hc->data_pid_start;
  58327. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  58328. +
  58329. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58330. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  58331. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  58332. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  58333. +
  58334. + if (core_if->dma_enable) {
  58335. + dwc_dma_t dma_addr;
  58336. + if (hc->align_buff) {
  58337. + dma_addr = hc->align_buff;
  58338. + } else {
  58339. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  58340. + }
  58341. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  58342. + }
  58343. +
  58344. + /* Start the split */
  58345. + if (hc->do_split) {
  58346. + hcsplt_data_t hcsplt;
  58347. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  58348. + hcsplt.b.spltena = 1;
  58349. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  58350. + }
  58351. +
  58352. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58353. + hcchar.b.multicnt = hc->multi_count;
  58354. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  58355. +#ifdef DEBUG
  58356. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  58357. + if (hcchar.b.chdis) {
  58358. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  58359. + __func__, hc->hc_num, hcchar.d32);
  58360. + }
  58361. +#endif
  58362. +
  58363. + /* Set host channel enable after all other setup is complete. */
  58364. + hcchar.b.chen = 1;
  58365. + hcchar.b.chdis = 0;
  58366. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58367. +
  58368. + hc->xfer_started = 1;
  58369. + hc->requests++;
  58370. +
  58371. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  58372. + /* Load OUT packet into the appropriate Tx FIFO. */
  58373. + dwc_otg_hc_write_packet(core_if, hc);
  58374. + }
  58375. +#ifdef DEBUG
  58376. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  58377. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  58378. + hc->hc_num, core_if);//GRAYG
  58379. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  58380. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  58381. +
  58382. + /* Start a timer for this transfer. */
  58383. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  58384. + }
  58385. +#endif
  58386. +}
  58387. +
  58388. +/**
  58389. + * This function does the setup for a data transfer for a host channel
  58390. + * and starts the transfer in Descriptor DMA mode.
  58391. + *
  58392. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  58393. + * Sets PID and NTD values. For periodic transfers
  58394. + * initializes SCHED_INFO field with micro-frame bitmap.
  58395. + *
  58396. + * Initializes HCDMA register with descriptor list address and CTD value
  58397. + * then starts the transfer via enabling the channel.
  58398. + *
  58399. + * @param core_if Programming view of DWC_otg controller.
  58400. + * @param hc Information needed to initialize the host channel.
  58401. + */
  58402. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58403. +{
  58404. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58405. + hcchar_data_t hcchar;
  58406. + hctsiz_data_t hctsiz;
  58407. + hcdma_data_t hcdma;
  58408. +
  58409. + hctsiz.d32 = 0;
  58410. +
  58411. + if (hc->do_ping)
  58412. + hctsiz.b_ddma.dopng = 1;
  58413. +
  58414. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  58415. + set_pid_isoc(hc);
  58416. +
  58417. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  58418. + hctsiz.b_ddma.pid = hc->data_pid_start;
  58419. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  58420. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  58421. +
  58422. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58423. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  58424. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  58425. +
  58426. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  58427. +
  58428. + hcdma.d32 = 0;
  58429. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  58430. +
  58431. + /* Always start from first descriptor. */
  58432. + hcdma.b.ctd = 0;
  58433. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  58434. +
  58435. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58436. + hcchar.b.multicnt = hc->multi_count;
  58437. +
  58438. +#ifdef DEBUG
  58439. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  58440. + if (hcchar.b.chdis) {
  58441. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  58442. + __func__, hc->hc_num, hcchar.d32);
  58443. + }
  58444. +#endif
  58445. +
  58446. + /* Set host channel enable after all other setup is complete. */
  58447. + hcchar.b.chen = 1;
  58448. + hcchar.b.chdis = 0;
  58449. +
  58450. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58451. +
  58452. + hc->xfer_started = 1;
  58453. + hc->requests++;
  58454. +
  58455. +#ifdef DEBUG
  58456. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  58457. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  58458. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  58459. + hc->hc_num, core_if);//GRAYG
  58460. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  58461. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  58462. + /* Start a timer for this transfer. */
  58463. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  58464. + }
  58465. +#endif
  58466. +
  58467. +}
  58468. +
  58469. +/**
  58470. + * This function continues a data transfer that was started by previous call
  58471. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  58472. + * sufficient space in the request queue and Tx Data FIFO. This function
  58473. + * should only be called in Slave mode. In DMA mode, the controller acts
  58474. + * autonomously to complete transfers programmed to a host channel.
  58475. + *
  58476. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  58477. + * if there is any data remaining to be queued. For an IN transfer, another
  58478. + * data packet is always requested. For the SETUP phase of a control transfer,
  58479. + * this function does nothing.
  58480. + *
  58481. + * @return 1 if a new request is queued, 0 if no more requests are required
  58482. + * for this transfer.
  58483. + */
  58484. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58485. +{
  58486. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58487. +
  58488. + if (hc->do_split) {
  58489. + /* SPLITs always queue just once per channel */
  58490. + return 0;
  58491. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  58492. + /* SETUPs are queued only once since they can't be NAKed. */
  58493. + return 0;
  58494. + } else if (hc->ep_is_in) {
  58495. + /*
  58496. + * Always queue another request for other IN transfers. If
  58497. + * back-to-back INs are issued and NAKs are received for both,
  58498. + * the driver may still be processing the first NAK when the
  58499. + * second NAK is received. When the interrupt handler clears
  58500. + * the NAK interrupt for the first NAK, the second NAK will
  58501. + * not be seen. So we can't depend on the NAK interrupt
  58502. + * handler to requeue a NAKed request. Instead, IN requests
  58503. + * are issued each time this function is called. When the
  58504. + * transfer completes, the extra requests for the channel will
  58505. + * be flushed.
  58506. + */
  58507. + hcchar_data_t hcchar;
  58508. + dwc_otg_hc_regs_t *hc_regs =
  58509. + core_if->host_if->hc_regs[hc->hc_num];
  58510. +
  58511. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58512. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  58513. + hcchar.b.chen = 1;
  58514. + hcchar.b.chdis = 0;
  58515. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  58516. + hcchar.d32);
  58517. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58518. + hc->requests++;
  58519. + return 1;
  58520. + } else {
  58521. + /* OUT transfers. */
  58522. + if (hc->xfer_count < hc->xfer_len) {
  58523. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  58524. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  58525. + hcchar_data_t hcchar;
  58526. + dwc_otg_hc_regs_t *hc_regs;
  58527. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58528. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58529. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  58530. + }
  58531. +
  58532. + /* Load OUT packet into the appropriate Tx FIFO. */
  58533. + dwc_otg_hc_write_packet(core_if, hc);
  58534. + hc->requests++;
  58535. + return 1;
  58536. + } else {
  58537. + return 0;
  58538. + }
  58539. + }
  58540. +}
  58541. +
  58542. +/**
  58543. + * Starts a PING transfer. This function should only be called in Slave mode.
  58544. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  58545. + */
  58546. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58547. +{
  58548. + hcchar_data_t hcchar;
  58549. + hctsiz_data_t hctsiz;
  58550. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  58551. +
  58552. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  58553. +
  58554. + hctsiz.d32 = 0;
  58555. + hctsiz.b.dopng = 1;
  58556. + hctsiz.b.pktcnt = 1;
  58557. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  58558. +
  58559. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  58560. + hcchar.b.chen = 1;
  58561. + hcchar.b.chdis = 0;
  58562. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  58563. +}
  58564. +
  58565. +/*
  58566. + * This function writes a packet into the Tx FIFO associated with the Host
  58567. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  58568. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  58569. + * periodic Tx FIFO is written. This function should only be called in Slave
  58570. + * mode.
  58571. + *
  58572. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  58573. + * then number of bytes written to the Tx FIFO.
  58574. + */
  58575. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  58576. +{
  58577. + uint32_t i;
  58578. + uint32_t remaining_count;
  58579. + uint32_t byte_count;
  58580. + uint32_t dword_count;
  58581. +
  58582. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  58583. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  58584. +
  58585. + remaining_count = hc->xfer_len - hc->xfer_count;
  58586. + if (remaining_count > hc->max_packet) {
  58587. + byte_count = hc->max_packet;
  58588. + } else {
  58589. + byte_count = remaining_count;
  58590. + }
  58591. +
  58592. + dword_count = (byte_count + 3) / 4;
  58593. +
  58594. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  58595. + /* xfer_buff is DWORD aligned. */
  58596. + for (i = 0; i < dword_count; i++, data_buff++) {
  58597. + DWC_WRITE_REG32(data_fifo, *data_buff);
  58598. + }
  58599. + } else {
  58600. + /* xfer_buff is not DWORD aligned. */
  58601. + for (i = 0; i < dword_count; i++, data_buff++) {
  58602. + uint32_t data;
  58603. + data =
  58604. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  58605. + 16 | data_buff[3] << 24);
  58606. + DWC_WRITE_REG32(data_fifo, data);
  58607. + }
  58608. + }
  58609. +
  58610. + hc->xfer_count += byte_count;
  58611. + hc->xfer_buff += byte_count;
  58612. +}
  58613. +
  58614. +/**
  58615. + * Gets the current USB frame number. This is the frame number from the last
  58616. + * SOF packet.
  58617. + */
  58618. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  58619. +{
  58620. + dsts_data_t dsts;
  58621. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  58622. +
  58623. + /* read current frame/microframe number from DSTS register */
  58624. + return dsts.b.soffn;
  58625. +}
  58626. +
  58627. +/**
  58628. + * Calculates and gets the frame Interval value of HFIR register according PHY
  58629. + * type and speed.The application can modify a value of HFIR register only after
  58630. + * the Port Enable bit of the Host Port Control and Status register
  58631. + * (HPRT.PrtEnaPort) has been set.
  58632. +*/
  58633. +
  58634. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  58635. +{
  58636. + gusbcfg_data_t usbcfg;
  58637. + hwcfg2_data_t hwcfg2;
  58638. + hprt0_data_t hprt0;
  58639. + int clock = 60; // default value
  58640. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  58641. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  58642. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  58643. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  58644. + clock = 60;
  58645. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  58646. + clock = 48;
  58647. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  58648. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  58649. + clock = 30;
  58650. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  58651. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  58652. + clock = 60;
  58653. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  58654. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  58655. + clock = 48;
  58656. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  58657. + clock = 48;
  58658. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  58659. + clock = 48;
  58660. + if (hprt0.b.prtspd == 0)
  58661. + /* High speed case */
  58662. + return 125 * clock;
  58663. + else
  58664. + /* FS/LS case */
  58665. + return 1000 * clock;
  58666. +}
  58667. +
  58668. +/**
  58669. + * This function reads a setup packet from the Rx FIFO into the destination
  58670. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  58671. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  58672. + *
  58673. + * @param core_if Programming view of DWC_otg controller.
  58674. + * @param dest Destination buffer for packet data.
  58675. + */
  58676. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  58677. +{
  58678. + device_grxsts_data_t status;
  58679. + /* Get the 8 bytes of a setup transaction data */
  58680. +
  58681. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  58682. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  58683. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  58684. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  58685. + status.d32 =
  58686. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  58687. + DWC_DEBUGPL(DBG_ANY,
  58688. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  58689. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  58690. + status.b.fn, status.b.fn);
  58691. + }
  58692. +}
  58693. +
  58694. +/**
  58695. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  58696. + * IN for transmitting packets. It is normally called when the
  58697. + * "Enumeration Done" interrupt occurs.
  58698. + *
  58699. + * @param core_if Programming view of DWC_otg controller.
  58700. + * @param ep The EP0 data.
  58701. + */
  58702. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58703. +{
  58704. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58705. + dsts_data_t dsts;
  58706. + depctl_data_t diepctl;
  58707. + depctl_data_t doepctl;
  58708. + dctl_data_t dctl = {.d32 = 0 };
  58709. +
  58710. + ep->stp_rollover = 0;
  58711. + /* Read the Device Status and Endpoint 0 Control registers */
  58712. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  58713. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  58714. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  58715. +
  58716. + /* Set the MPS of the IN EP based on the enumeration speed */
  58717. + switch (dsts.b.enumspd) {
  58718. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  58719. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  58720. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  58721. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  58722. + break;
  58723. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  58724. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  58725. + break;
  58726. + }
  58727. +
  58728. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  58729. +
  58730. + /* Enable OUT EP for receive */
  58731. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  58732. + doepctl.b.epena = 1;
  58733. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  58734. + }
  58735. +#ifdef VERBOSE
  58736. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  58737. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  58738. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  58739. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  58740. +#endif
  58741. + dctl.b.cgnpinnak = 1;
  58742. +
  58743. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  58744. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  58745. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  58746. +
  58747. +}
  58748. +
  58749. +/**
  58750. + * This function activates an EP. The Device EP control register for
  58751. + * the EP is configured as defined in the ep structure. Note: This
  58752. + * function is not used for EP0.
  58753. + *
  58754. + * @param core_if Programming view of DWC_otg controller.
  58755. + * @param ep The EP to activate.
  58756. + */
  58757. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58758. +{
  58759. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58760. + depctl_data_t depctl;
  58761. + volatile uint32_t *addr;
  58762. + daint_data_t daintmsk = {.d32 = 0 };
  58763. + dcfg_data_t dcfg;
  58764. + uint8_t i;
  58765. +
  58766. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  58767. + (ep->is_in ? "IN" : "OUT"));
  58768. +
  58769. +#ifdef DWC_UTE_PER_IO
  58770. + ep->xiso_frame_num = 0xFFFFFFFF;
  58771. + ep->xiso_active_xfers = 0;
  58772. + ep->xiso_queued_xfers = 0;
  58773. +#endif
  58774. + /* Read DEPCTLn register */
  58775. + if (ep->is_in == 1) {
  58776. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  58777. + daintmsk.ep.in = 1 << ep->num;
  58778. + } else {
  58779. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  58780. + daintmsk.ep.out = 1 << ep->num;
  58781. + }
  58782. +
  58783. + /* If the EP is already active don't change the EP Control
  58784. + * register. */
  58785. + depctl.d32 = DWC_READ_REG32(addr);
  58786. + if (!depctl.b.usbactep) {
  58787. + depctl.b.mps = ep->maxpacket;
  58788. + depctl.b.eptype = ep->type;
  58789. + depctl.b.txfnum = ep->tx_fifo_num;
  58790. +
  58791. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58792. + depctl.b.setd0pid = 1; // ???
  58793. + } else {
  58794. + depctl.b.setd0pid = 1;
  58795. + }
  58796. + depctl.b.usbactep = 1;
  58797. +
  58798. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  58799. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  58800. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58801. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  58802. + break;
  58803. + }
  58804. + core_if->nextep_seq[i] = ep->num;
  58805. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  58806. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  58807. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  58808. + dcfg.b.epmscnt++;
  58809. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  58810. +
  58811. + DWC_DEBUGPL(DBG_PCDV,
  58812. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  58813. + __func__, core_if->first_in_nextep_seq);
  58814. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  58815. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  58816. + core_if->nextep_seq[i]);
  58817. + }
  58818. +
  58819. + }
  58820. +
  58821. +
  58822. + DWC_WRITE_REG32(addr, depctl.d32);
  58823. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  58824. + }
  58825. +
  58826. + /* Enable the Interrupt for this EP */
  58827. + if (core_if->multiproc_int_enable) {
  58828. + if (ep->is_in == 1) {
  58829. + diepmsk_data_t diepmsk = {.d32 = 0 };
  58830. + diepmsk.b.xfercompl = 1;
  58831. + diepmsk.b.timeout = 1;
  58832. + diepmsk.b.epdisabled = 1;
  58833. + diepmsk.b.ahberr = 1;
  58834. + diepmsk.b.intknepmis = 1;
  58835. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  58836. + diepmsk.b.intknepmis = 0;
  58837. + diepmsk.b.txfifoundrn = 1; //?????
  58838. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58839. + diepmsk.b.nak = 1;
  58840. + }
  58841. +
  58842. +
  58843. +
  58844. +/*
  58845. + if (core_if->dma_desc_enable) {
  58846. + diepmsk.b.bna = 1;
  58847. + }
  58848. +*/
  58849. +/*
  58850. + if (core_if->dma_enable) {
  58851. + doepmsk.b.nak = 1;
  58852. + }
  58853. +*/
  58854. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  58855. + diepeachintmsk[ep->num], diepmsk.d32);
  58856. +
  58857. + } else {
  58858. + doepmsk_data_t doepmsk = {.d32 = 0 };
  58859. + doepmsk.b.xfercompl = 1;
  58860. + doepmsk.b.ahberr = 1;
  58861. + doepmsk.b.epdisabled = 1;
  58862. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  58863. + doepmsk.b.outtknepdis = 1;
  58864. +
  58865. +/*
  58866. +
  58867. + if (core_if->dma_desc_enable) {
  58868. + doepmsk.b.bna = 1;
  58869. + }
  58870. +*/
  58871. +/*
  58872. + doepmsk.b.babble = 1;
  58873. + doepmsk.b.nyet = 1;
  58874. + doepmsk.b.nak = 1;
  58875. +*/
  58876. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  58877. + doepeachintmsk[ep->num], doepmsk.d32);
  58878. + }
  58879. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  58880. + 0, daintmsk.d32);
  58881. + } else {
  58882. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58883. + if (ep->is_in) {
  58884. + diepmsk_data_t diepmsk = {.d32 = 0 };
  58885. + diepmsk.b.nak = 1;
  58886. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  58887. + } else {
  58888. + doepmsk_data_t doepmsk = {.d32 = 0 };
  58889. + doepmsk.b.outtknepdis = 1;
  58890. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  58891. + }
  58892. + }
  58893. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  58894. + 0, daintmsk.d32);
  58895. + }
  58896. +
  58897. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  58898. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  58899. +
  58900. + ep->stall_clear_flag = 0;
  58901. +
  58902. + return;
  58903. +}
  58904. +
  58905. +/**
  58906. + * This function deactivates an EP. This is done by clearing the USB Active
  58907. + * EP bit in the Device EP control register. Note: This function is not used
  58908. + * for EP0. EP0 cannot be deactivated.
  58909. + *
  58910. + * @param core_if Programming view of DWC_otg controller.
  58911. + * @param ep The EP to deactivate.
  58912. + */
  58913. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58914. +{
  58915. + depctl_data_t depctl = {.d32 = 0 };
  58916. + volatile uint32_t *addr;
  58917. + daint_data_t daintmsk = {.d32 = 0 };
  58918. + dcfg_data_t dcfg;
  58919. + uint8_t i = 0;
  58920. +
  58921. +#ifdef DWC_UTE_PER_IO
  58922. + ep->xiso_frame_num = 0xFFFFFFFF;
  58923. + ep->xiso_active_xfers = 0;
  58924. + ep->xiso_queued_xfers = 0;
  58925. +#endif
  58926. +
  58927. + /* Read DEPCTLn register */
  58928. + if (ep->is_in == 1) {
  58929. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  58930. + daintmsk.ep.in = 1 << ep->num;
  58931. + } else {
  58932. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  58933. + daintmsk.ep.out = 1 << ep->num;
  58934. + }
  58935. +
  58936. + depctl.d32 = DWC_READ_REG32(addr);
  58937. +
  58938. + depctl.b.usbactep = 0;
  58939. +
  58940. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  58941. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  58942. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58943. + if (core_if->nextep_seq[i] == ep->num)
  58944. + break;
  58945. + }
  58946. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  58947. + if (core_if->first_in_nextep_seq == ep->num)
  58948. + core_if->first_in_nextep_seq = i;
  58949. + core_if->nextep_seq[ep->num] = 0xff;
  58950. + depctl.b.nextep = 0;
  58951. + dcfg.d32 =
  58952. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  58953. + dcfg.b.epmscnt--;
  58954. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  58955. + dcfg.d32);
  58956. +
  58957. + DWC_DEBUGPL(DBG_PCDV,
  58958. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  58959. + __func__, core_if->first_in_nextep_seq);
  58960. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  58961. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  58962. + }
  58963. + }
  58964. +
  58965. + if (ep->is_in == 1)
  58966. + depctl.b.txfnum = 0;
  58967. +
  58968. + if (core_if->dma_desc_enable)
  58969. + depctl.b.epdis = 1;
  58970. +
  58971. + DWC_WRITE_REG32(addr, depctl.d32);
  58972. + depctl.d32 = DWC_READ_REG32(addr);
  58973. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  58974. + && depctl.b.epena) {
  58975. + depctl_data_t depctl = {.d32 = 0};
  58976. + if (ep->is_in) {
  58977. + diepint_data_t diepint = {.d32 = 0};
  58978. +
  58979. + depctl.b.snak = 1;
  58980. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58981. + diepctl, depctl.d32);
  58982. + do {
  58983. + dwc_udelay(10);
  58984. + diepint.d32 =
  58985. + DWC_READ_REG32(&core_if->
  58986. + dev_if->in_ep_regs[ep->num]->
  58987. + diepint);
  58988. + } while (!diepint.b.inepnakeff);
  58989. + diepint.b.inepnakeff = 1;
  58990. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58991. + diepint, diepint.d32);
  58992. + depctl.d32 = 0;
  58993. + depctl.b.epdis = 1;
  58994. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58995. + diepctl, depctl.d32);
  58996. + do {
  58997. + dwc_udelay(10);
  58998. + diepint.d32 =
  58999. + DWC_READ_REG32(&core_if->
  59000. + dev_if->in_ep_regs[ep->num]->
  59001. + diepint);
  59002. + } while (!diepint.b.epdisabled);
  59003. + diepint.b.epdisabled = 1;
  59004. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  59005. + diepint, diepint.d32);
  59006. + } else {
  59007. + dctl_data_t dctl = {.d32 = 0};
  59008. + gintmsk_data_t gintsts = {.d32 = 0};
  59009. + doepint_data_t doepint = {.d32 = 0};
  59010. + dctl.b.sgoutnak = 1;
  59011. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  59012. + dctl, 0, dctl.d32);
  59013. + do {
  59014. + dwc_udelay(10);
  59015. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  59016. + } while (!gintsts.b.goutnakeff);
  59017. + gintsts.d32 = 0;
  59018. + gintsts.b.goutnakeff = 1;
  59019. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  59020. +
  59021. + depctl.d32 = 0;
  59022. + depctl.b.epdis = 1;
  59023. + depctl.b.snak = 1;
  59024. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  59025. + do
  59026. + {
  59027. + dwc_udelay(10);
  59028. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  59029. + out_ep_regs[ep->num]->doepint);
  59030. + } while (!doepint.b.epdisabled);
  59031. +
  59032. + doepint.b.epdisabled = 1;
  59033. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  59034. +
  59035. + dctl.d32 = 0;
  59036. + dctl.b.cgoutnak = 1;
  59037. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  59038. + }
  59039. + }
  59040. +
  59041. + /* Disable the Interrupt for this EP */
  59042. + if (core_if->multiproc_int_enable) {
  59043. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  59044. + daintmsk.d32, 0);
  59045. +
  59046. + if (ep->is_in == 1) {
  59047. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  59048. + diepeachintmsk[ep->num], 0);
  59049. + } else {
  59050. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  59051. + doepeachintmsk[ep->num], 0);
  59052. + }
  59053. + } else {
  59054. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  59055. + daintmsk.d32, 0);
  59056. + }
  59057. +
  59058. +}
  59059. +
  59060. +/**
  59061. + * This function initializes dma descriptor chain.
  59062. + *
  59063. + * @param core_if Programming view of DWC_otg controller.
  59064. + * @param ep The EP to start the transfer on.
  59065. + */
  59066. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59067. +{
  59068. + dwc_otg_dev_dma_desc_t *dma_desc;
  59069. + uint32_t offset;
  59070. + uint32_t xfer_est;
  59071. + int i;
  59072. + unsigned maxxfer_local, total_len;
  59073. +
  59074. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  59075. + (ep->maxpacket%4)) {
  59076. + maxxfer_local = ep->maxpacket;
  59077. + total_len = ep->xfer_len;
  59078. + } else {
  59079. + maxxfer_local = ep->maxxfer;
  59080. + total_len = ep->total_len;
  59081. + }
  59082. +
  59083. + ep->desc_cnt = (total_len / maxxfer_local) +
  59084. + ((total_len % maxxfer_local) ? 1 : 0);
  59085. +
  59086. + if (!ep->desc_cnt)
  59087. + ep->desc_cnt = 1;
  59088. +
  59089. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  59090. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  59091. +
  59092. + dma_desc = ep->desc_addr;
  59093. + if (maxxfer_local == ep->maxpacket) {
  59094. + if ((total_len % maxxfer_local) &&
  59095. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  59096. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  59097. + (total_len % maxxfer_local);
  59098. + } else
  59099. + xfer_est = ep->desc_cnt * maxxfer_local;
  59100. + } else
  59101. + xfer_est = total_len;
  59102. + offset = 0;
  59103. + for (i = 0; i < ep->desc_cnt; ++i) {
  59104. + /** DMA Descriptor Setup */
  59105. + if (xfer_est > maxxfer_local) {
  59106. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59107. + dma_desc->status.b.l = 0;
  59108. + dma_desc->status.b.ioc = 0;
  59109. + dma_desc->status.b.sp = 0;
  59110. + dma_desc->status.b.bytes = maxxfer_local;
  59111. + dma_desc->buf = ep->dma_addr + offset;
  59112. + dma_desc->status.b.sts = 0;
  59113. + dma_desc->status.b.bs = BS_HOST_READY;
  59114. +
  59115. + xfer_est -= maxxfer_local;
  59116. + offset += maxxfer_local;
  59117. + } else {
  59118. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59119. + dma_desc->status.b.l = 1;
  59120. + dma_desc->status.b.ioc = 1;
  59121. + if (ep->is_in) {
  59122. + dma_desc->status.b.sp =
  59123. + (xfer_est %
  59124. + ep->maxpacket) ? 1 : ((ep->
  59125. + sent_zlp) ? 1 : 0);
  59126. + dma_desc->status.b.bytes = xfer_est;
  59127. + } else {
  59128. + if (maxxfer_local == ep->maxpacket)
  59129. + dma_desc->status.b.bytes = xfer_est;
  59130. + else
  59131. + dma_desc->status.b.bytes =
  59132. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  59133. + }
  59134. +
  59135. + dma_desc->buf = ep->dma_addr + offset;
  59136. + dma_desc->status.b.sts = 0;
  59137. + dma_desc->status.b.bs = BS_HOST_READY;
  59138. + }
  59139. + dma_desc++;
  59140. + }
  59141. +}
  59142. +/**
  59143. + * This function is called when to write ISOC data into appropriate dedicated
  59144. + * periodic FIFO.
  59145. + */
  59146. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  59147. +{
  59148. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  59149. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  59150. + dtxfsts_data_t txstatus = {.d32 = 0 };
  59151. + uint32_t len = 0;
  59152. + int epnum = dwc_ep->num;
  59153. + int dwords;
  59154. +
  59155. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  59156. +
  59157. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  59158. +
  59159. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  59160. +
  59161. + if (len > dwc_ep->maxpacket) {
  59162. + len = dwc_ep->maxpacket;
  59163. + }
  59164. +
  59165. + dwords = (len + 3) / 4;
  59166. +
  59167. + /* While there is space in the queue and space in the FIFO and
  59168. + * More data to tranfer, Write packets to the Tx FIFO */
  59169. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  59170. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  59171. +
  59172. + while (txstatus.b.txfspcavail > dwords &&
  59173. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  59174. + /* Write the FIFO */
  59175. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  59176. +
  59177. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  59178. + if (len > dwc_ep->maxpacket) {
  59179. + len = dwc_ep->maxpacket;
  59180. + }
  59181. +
  59182. + dwords = (len + 3) / 4;
  59183. + txstatus.d32 =
  59184. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  59185. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  59186. + txstatus.d32);
  59187. + }
  59188. +
  59189. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  59190. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  59191. +
  59192. + return 1;
  59193. +}
  59194. +/**
  59195. + * This function does the setup for a data transfer for an EP and
  59196. + * starts the transfer. For an IN transfer, the packets will be
  59197. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  59198. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  59199. + *
  59200. + * @param core_if Programming view of DWC_otg controller.
  59201. + * @param ep The EP to start the transfer on.
  59202. + */
  59203. +
  59204. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59205. +{
  59206. + depctl_data_t depctl;
  59207. + deptsiz_data_t deptsiz;
  59208. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59209. +
  59210. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  59211. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  59212. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  59213. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  59214. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  59215. + ep->total_len);
  59216. + /* IN endpoint */
  59217. + if (ep->is_in == 1) {
  59218. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59219. + core_if->dev_if->in_ep_regs[ep->num];
  59220. +
  59221. + gnptxsts_data_t gtxstatus;
  59222. +
  59223. + gtxstatus.d32 =
  59224. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59225. +
  59226. + if (core_if->en_multiple_tx_fifo == 0
  59227. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  59228. +#ifdef DEBUG
  59229. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  59230. +#endif
  59231. + return;
  59232. + }
  59233. +
  59234. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  59235. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  59236. +
  59237. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  59238. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  59239. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  59240. + else
  59241. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  59242. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  59243. +
  59244. +
  59245. + /* Zero Length Packet? */
  59246. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  59247. + deptsiz.b.xfersize = 0;
  59248. + deptsiz.b.pktcnt = 1;
  59249. + } else {
  59250. + /* Program the transfer size and packet count
  59251. + * as follows: xfersize = N * maxpacket +
  59252. + * short_packet pktcnt = N + (short_packet
  59253. + * exist ? 1 : 0)
  59254. + */
  59255. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  59256. + deptsiz.b.pktcnt =
  59257. + (ep->xfer_len - ep->xfer_count - 1 +
  59258. + ep->maxpacket) / ep->maxpacket;
  59259. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  59260. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  59261. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  59262. + }
  59263. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  59264. + deptsiz.b.mc = deptsiz.b.pktcnt;
  59265. + }
  59266. +
  59267. + /* Write the DMA register */
  59268. + if (core_if->dma_enable) {
  59269. + if (core_if->dma_desc_enable == 0) {
  59270. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  59271. + deptsiz.b.mc = 1;
  59272. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59273. + deptsiz.d32);
  59274. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59275. + (uint32_t) ep->dma_addr);
  59276. + } else {
  59277. +#ifdef DWC_UTE_CFI
  59278. + /* The descriptor chain should be already initialized by now */
  59279. + if (ep->buff_mode != BM_STANDARD) {
  59280. + DWC_WRITE_REG32(&in_regs->diepdma,
  59281. + ep->descs_dma_addr);
  59282. + } else {
  59283. +#endif
  59284. + init_dma_desc_chain(core_if, ep);
  59285. + /** DIEPDMAn Register write */
  59286. + DWC_WRITE_REG32(&in_regs->diepdma,
  59287. + ep->dma_desc_addr);
  59288. +#ifdef DWC_UTE_CFI
  59289. + }
  59290. +#endif
  59291. + }
  59292. + } else {
  59293. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59294. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  59295. + /**
  59296. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  59297. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  59298. + * the data will be written into the fifo by the ISR.
  59299. + */
  59300. + if (core_if->en_multiple_tx_fifo == 0) {
  59301. + intr_mask.b.nptxfempty = 1;
  59302. + DWC_MODIFY_REG32
  59303. + (&core_if->core_global_regs->gintmsk,
  59304. + intr_mask.d32, intr_mask.d32);
  59305. + } else {
  59306. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59307. + if (ep->xfer_len > 0) {
  59308. + uint32_t fifoemptymsk = 0;
  59309. + fifoemptymsk = 1 << ep->num;
  59310. + DWC_MODIFY_REG32
  59311. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59312. + 0, fifoemptymsk);
  59313. +
  59314. + }
  59315. + }
  59316. + } else {
  59317. + write_isoc_tx_fifo(core_if, ep);
  59318. + }
  59319. + }
  59320. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59321. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59322. +
  59323. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  59324. + dsts_data_t dsts = {.d32 = 0};
  59325. + if (ep->bInterval == 1) {
  59326. + dsts.d32 =
  59327. + DWC_READ_REG32(&core_if->dev_if->
  59328. + dev_global_regs->dsts);
  59329. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  59330. + if (ep->frame_num > 0x3FFF) {
  59331. + ep->frm_overrun = 1;
  59332. + ep->frame_num &= 0x3FFF;
  59333. + } else
  59334. + ep->frm_overrun = 0;
  59335. + if (ep->frame_num & 0x1) {
  59336. + depctl.b.setd1pid = 1;
  59337. + } else {
  59338. + depctl.b.setd0pid = 1;
  59339. + }
  59340. + }
  59341. + }
  59342. + /* EP enable, IN data in FIFO */
  59343. + depctl.b.cnak = 1;
  59344. + depctl.b.epena = 1;
  59345. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59346. +
  59347. + } else {
  59348. + /* OUT endpoint */
  59349. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59350. + core_if->dev_if->out_ep_regs[ep->num];
  59351. +
  59352. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  59353. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  59354. +
  59355. + if (!core_if->dma_desc_enable) {
  59356. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  59357. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  59358. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  59359. + else
  59360. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  59361. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  59362. + }
  59363. +
  59364. + /* Program the transfer size and packet count as follows:
  59365. + *
  59366. + * pktcnt = N
  59367. + * xfersize = N * maxpacket
  59368. + */
  59369. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  59370. + /* Zero Length Packet */
  59371. + deptsiz.b.xfersize = ep->maxpacket;
  59372. + deptsiz.b.pktcnt = 1;
  59373. + } else {
  59374. + deptsiz.b.pktcnt =
  59375. + (ep->xfer_len - ep->xfer_count +
  59376. + (ep->maxpacket - 1)) / ep->maxpacket;
  59377. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  59378. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  59379. + }
  59380. + if (!core_if->dma_desc_enable) {
  59381. + ep->xfer_len =
  59382. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  59383. + }
  59384. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  59385. + }
  59386. +
  59387. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  59388. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59389. +
  59390. + if (core_if->dma_enable) {
  59391. + if (!core_if->dma_desc_enable) {
  59392. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59393. + deptsiz.d32);
  59394. +
  59395. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59396. + (uint32_t) ep->dma_addr);
  59397. + } else {
  59398. +#ifdef DWC_UTE_CFI
  59399. + /* The descriptor chain should be already initialized by now */
  59400. + if (ep->buff_mode != BM_STANDARD) {
  59401. + DWC_WRITE_REG32(&out_regs->doepdma,
  59402. + ep->descs_dma_addr);
  59403. + } else {
  59404. +#endif
  59405. + /** This is used for interrupt out transfers*/
  59406. + if (!ep->xfer_len)
  59407. + ep->xfer_len = ep->total_len;
  59408. + init_dma_desc_chain(core_if, ep);
  59409. +
  59410. + if (core_if->core_params->dev_out_nak) {
  59411. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  59412. + deptsiz.b.pktcnt = (ep->total_len +
  59413. + (ep->maxpacket - 1)) / ep->maxpacket;
  59414. + deptsiz.b.xfersize = ep->total_len;
  59415. + /* Remember initial value of doeptsiz */
  59416. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  59417. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59418. + deptsiz.d32);
  59419. + }
  59420. + }
  59421. + /** DOEPDMAn Register write */
  59422. + DWC_WRITE_REG32(&out_regs->doepdma,
  59423. + ep->dma_desc_addr);
  59424. +#ifdef DWC_UTE_CFI
  59425. + }
  59426. +#endif
  59427. + }
  59428. + } else {
  59429. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59430. + }
  59431. +
  59432. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  59433. + dsts_data_t dsts = {.d32 = 0};
  59434. + if (ep->bInterval == 1) {
  59435. + dsts.d32 =
  59436. + DWC_READ_REG32(&core_if->dev_if->
  59437. + dev_global_regs->dsts);
  59438. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  59439. + if (ep->frame_num > 0x3FFF) {
  59440. + ep->frm_overrun = 1;
  59441. + ep->frame_num &= 0x3FFF;
  59442. + } else
  59443. + ep->frm_overrun = 0;
  59444. +
  59445. + if (ep->frame_num & 0x1) {
  59446. + depctl.b.setd1pid = 1;
  59447. + } else {
  59448. + depctl.b.setd0pid = 1;
  59449. + }
  59450. + }
  59451. + }
  59452. +
  59453. + /* EP enable */
  59454. + depctl.b.cnak = 1;
  59455. + depctl.b.epena = 1;
  59456. +
  59457. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59458. +
  59459. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  59460. + DWC_READ_REG32(&out_regs->doepctl),
  59461. + DWC_READ_REG32(&out_regs->doeptsiz));
  59462. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  59463. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  59464. + daintmsk),
  59465. + DWC_READ_REG32(&core_if->core_global_regs->
  59466. + gintmsk));
  59467. +
  59468. + /* Timer is scheduling only for out bulk transfers for
  59469. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  59470. + * about received data payload in case of timeout
  59471. + */
  59472. + if (core_if->core_params->dev_out_nak) {
  59473. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  59474. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  59475. + core_if->ep_xfer_info[ep->num].ep = ep;
  59476. + core_if->ep_xfer_info[ep->num].state = 1;
  59477. +
  59478. + /* Start a timer for this transfer. */
  59479. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  59480. + }
  59481. + }
  59482. + }
  59483. +}
  59484. +
  59485. +/**
  59486. + * This function setup a zero length transfer in Buffer DMA and
  59487. + * Slave modes for usb requests with zero field set
  59488. + *
  59489. + * @param core_if Programming view of DWC_otg controller.
  59490. + * @param ep The EP to start the transfer on.
  59491. + *
  59492. + */
  59493. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59494. +{
  59495. +
  59496. + depctl_data_t depctl;
  59497. + deptsiz_data_t deptsiz;
  59498. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59499. +
  59500. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  59501. + DWC_PRINTF("zero length transfer is called\n");
  59502. +
  59503. + /* IN endpoint */
  59504. + if (ep->is_in == 1) {
  59505. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59506. + core_if->dev_if->in_ep_regs[ep->num];
  59507. +
  59508. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  59509. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  59510. +
  59511. + deptsiz.b.xfersize = 0;
  59512. + deptsiz.b.pktcnt = 1;
  59513. +
  59514. + /* Write the DMA register */
  59515. + if (core_if->dma_enable) {
  59516. + if (core_if->dma_desc_enable == 0) {
  59517. + deptsiz.b.mc = 1;
  59518. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59519. + deptsiz.d32);
  59520. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59521. + (uint32_t) ep->dma_addr);
  59522. + }
  59523. + } else {
  59524. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59525. + /**
  59526. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  59527. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  59528. + * the data will be written into the fifo by the ISR.
  59529. + */
  59530. + if (core_if->en_multiple_tx_fifo == 0) {
  59531. + intr_mask.b.nptxfempty = 1;
  59532. + DWC_MODIFY_REG32(&core_if->
  59533. + core_global_regs->gintmsk,
  59534. + intr_mask.d32, intr_mask.d32);
  59535. + } else {
  59536. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59537. + if (ep->xfer_len > 0) {
  59538. + uint32_t fifoemptymsk = 0;
  59539. + fifoemptymsk = 1 << ep->num;
  59540. + DWC_MODIFY_REG32(&core_if->
  59541. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59542. + 0, fifoemptymsk);
  59543. + }
  59544. + }
  59545. + }
  59546. +
  59547. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59548. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59549. + /* EP enable, IN data in FIFO */
  59550. + depctl.b.cnak = 1;
  59551. + depctl.b.epena = 1;
  59552. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59553. +
  59554. + } else {
  59555. + /* OUT endpoint */
  59556. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59557. + core_if->dev_if->out_ep_regs[ep->num];
  59558. +
  59559. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  59560. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  59561. +
  59562. + /* Zero Length Packet */
  59563. + deptsiz.b.xfersize = ep->maxpacket;
  59564. + deptsiz.b.pktcnt = 1;
  59565. +
  59566. + if (core_if->dma_enable) {
  59567. + if (!core_if->dma_desc_enable) {
  59568. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59569. + deptsiz.d32);
  59570. +
  59571. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59572. + (uint32_t) ep->dma_addr);
  59573. + }
  59574. + } else {
  59575. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59576. + }
  59577. +
  59578. + /* EP enable */
  59579. + depctl.b.cnak = 1;
  59580. + depctl.b.epena = 1;
  59581. +
  59582. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59583. +
  59584. + }
  59585. +}
  59586. +
  59587. +/**
  59588. + * This function does the setup for a data transfer for EP0 and starts
  59589. + * the transfer. For an IN transfer, the packets will be loaded into
  59590. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  59591. + * unloaded from the Rx FIFO in the ISR.
  59592. + *
  59593. + * @param core_if Programming view of DWC_otg controller.
  59594. + * @param ep The EP0 data.
  59595. + */
  59596. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59597. +{
  59598. + depctl_data_t depctl;
  59599. + deptsiz0_data_t deptsiz;
  59600. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59601. + dwc_otg_dev_dma_desc_t *dma_desc;
  59602. +
  59603. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  59604. + "xfer_buff=%p start_xfer_buff=%p \n",
  59605. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  59606. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  59607. +
  59608. + ep->total_len = ep->xfer_len;
  59609. +
  59610. + /* IN endpoint */
  59611. + if (ep->is_in == 1) {
  59612. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59613. + core_if->dev_if->in_ep_regs[0];
  59614. +
  59615. + gnptxsts_data_t gtxstatus;
  59616. +
  59617. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  59618. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59619. + if (depctl.b.epena)
  59620. + return;
  59621. + }
  59622. +
  59623. + gtxstatus.d32 =
  59624. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59625. +
  59626. + /* If dedicated FIFO every time flush fifo before enable ep*/
  59627. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  59628. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  59629. +
  59630. + if (core_if->en_multiple_tx_fifo == 0
  59631. + && gtxstatus.b.nptxqspcavail == 0
  59632. + && !core_if->dma_enable) {
  59633. +#ifdef DEBUG
  59634. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59635. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  59636. + DWC_READ_REG32(&in_regs->diepctl));
  59637. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  59638. + deptsiz.d32,
  59639. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59640. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  59641. + gtxstatus.d32);
  59642. +#endif
  59643. + return;
  59644. + }
  59645. +
  59646. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59647. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59648. +
  59649. + /* Zero Length Packet? */
  59650. + if (ep->xfer_len == 0) {
  59651. + deptsiz.b.xfersize = 0;
  59652. + deptsiz.b.pktcnt = 1;
  59653. + } else {
  59654. + /* Program the transfer size and packet count
  59655. + * as follows: xfersize = N * maxpacket +
  59656. + * short_packet pktcnt = N + (short_packet
  59657. + * exist ? 1 : 0)
  59658. + */
  59659. + if (ep->xfer_len > ep->maxpacket) {
  59660. + ep->xfer_len = ep->maxpacket;
  59661. + deptsiz.b.xfersize = ep->maxpacket;
  59662. + } else {
  59663. + deptsiz.b.xfersize = ep->xfer_len;
  59664. + }
  59665. + deptsiz.b.pktcnt = 1;
  59666. +
  59667. + }
  59668. + DWC_DEBUGPL(DBG_PCDV,
  59669. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59670. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59671. + deptsiz.d32);
  59672. +
  59673. + /* Write the DMA register */
  59674. + if (core_if->dma_enable) {
  59675. + if (core_if->dma_desc_enable == 0) {
  59676. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59677. + deptsiz.d32);
  59678. +
  59679. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59680. + (uint32_t) ep->dma_addr);
  59681. + } else {
  59682. + dma_desc = core_if->dev_if->in_desc_addr;
  59683. +
  59684. + /** DMA Descriptor Setup */
  59685. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59686. + dma_desc->status.b.l = 1;
  59687. + dma_desc->status.b.ioc = 1;
  59688. + dma_desc->status.b.sp =
  59689. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  59690. + dma_desc->status.b.bytes = ep->xfer_len;
  59691. + dma_desc->buf = ep->dma_addr;
  59692. + dma_desc->status.b.sts = 0;
  59693. + dma_desc->status.b.bs = BS_HOST_READY;
  59694. +
  59695. + /** DIEPDMA0 Register write */
  59696. + DWC_WRITE_REG32(&in_regs->diepdma,
  59697. + core_if->
  59698. + dev_if->dma_in_desc_addr);
  59699. + }
  59700. + } else {
  59701. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59702. + }
  59703. +
  59704. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59705. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59706. + /* EP enable, IN data in FIFO */
  59707. + depctl.b.cnak = 1;
  59708. + depctl.b.epena = 1;
  59709. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59710. +
  59711. + /**
  59712. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  59713. + * data will be written into the fifo by the ISR.
  59714. + */
  59715. + if (!core_if->dma_enable) {
  59716. + if (core_if->en_multiple_tx_fifo == 0) {
  59717. + intr_mask.b.nptxfempty = 1;
  59718. + DWC_MODIFY_REG32(&core_if->
  59719. + core_global_regs->gintmsk,
  59720. + intr_mask.d32, intr_mask.d32);
  59721. + } else {
  59722. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59723. + if (ep->xfer_len > 0) {
  59724. + uint32_t fifoemptymsk = 0;
  59725. + fifoemptymsk |= 1 << ep->num;
  59726. + DWC_MODIFY_REG32(&core_if->
  59727. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59728. + 0, fifoemptymsk);
  59729. + }
  59730. + }
  59731. + }
  59732. + } else {
  59733. + /* OUT endpoint */
  59734. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59735. + core_if->dev_if->out_ep_regs[0];
  59736. +
  59737. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  59738. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  59739. +
  59740. + /* Program the transfer size and packet count as follows:
  59741. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  59742. + * pktcnt = N */
  59743. + /* Zero Length Packet */
  59744. + deptsiz.b.xfersize = ep->maxpacket;
  59745. + deptsiz.b.pktcnt = 1;
  59746. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  59747. + deptsiz.b.supcnt = 3;
  59748. +
  59749. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  59750. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59751. +
  59752. + if (core_if->dma_enable) {
  59753. + if (!core_if->dma_desc_enable) {
  59754. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59755. + deptsiz.d32);
  59756. +
  59757. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59758. + (uint32_t) ep->dma_addr);
  59759. + } else {
  59760. + dma_desc = core_if->dev_if->out_desc_addr;
  59761. +
  59762. + /** DMA Descriptor Setup */
  59763. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59764. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  59765. + dma_desc->status.b.mtrf = 0;
  59766. + dma_desc->status.b.sr = 0;
  59767. + }
  59768. + dma_desc->status.b.l = 1;
  59769. + dma_desc->status.b.ioc = 1;
  59770. + dma_desc->status.b.bytes = ep->maxpacket;
  59771. + dma_desc->buf = ep->dma_addr;
  59772. + dma_desc->status.b.sts = 0;
  59773. + dma_desc->status.b.bs = BS_HOST_READY;
  59774. +
  59775. + /** DOEPDMA0 Register write */
  59776. + DWC_WRITE_REG32(&out_regs->doepdma,
  59777. + core_if->dev_if->
  59778. + dma_out_desc_addr);
  59779. + }
  59780. + } else {
  59781. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59782. + }
  59783. +
  59784. + /* EP enable */
  59785. + depctl.b.cnak = 1;
  59786. + depctl.b.epena = 1;
  59787. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  59788. + }
  59789. +}
  59790. +
  59791. +/**
  59792. + * This function continues control IN transfers started by
  59793. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  59794. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  59795. + * bit for the packet count.
  59796. + *
  59797. + * @param core_if Programming view of DWC_otg controller.
  59798. + * @param ep The EP0 data.
  59799. + */
  59800. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59801. +{
  59802. + depctl_data_t depctl;
  59803. + deptsiz0_data_t deptsiz;
  59804. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59805. + dwc_otg_dev_dma_desc_t *dma_desc;
  59806. +
  59807. + if (ep->is_in == 1) {
  59808. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59809. + core_if->dev_if->in_ep_regs[0];
  59810. + gnptxsts_data_t tx_status = {.d32 = 0 };
  59811. +
  59812. + tx_status.d32 =
  59813. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59814. + /** @todo Should there be check for room in the Tx
  59815. + * Status Queue. If not remove the code above this comment. */
  59816. +
  59817. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59818. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59819. +
  59820. + /* Program the transfer size and packet count
  59821. + * as follows: xfersize = N * maxpacket +
  59822. + * short_packet pktcnt = N + (short_packet
  59823. + * exist ? 1 : 0)
  59824. + */
  59825. +
  59826. + if (core_if->dma_desc_enable == 0) {
  59827. + deptsiz.b.xfersize =
  59828. + (ep->total_len - ep->xfer_count) >
  59829. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  59830. + ep->xfer_count);
  59831. + deptsiz.b.pktcnt = 1;
  59832. + if (core_if->dma_enable == 0) {
  59833. + ep->xfer_len += deptsiz.b.xfersize;
  59834. + } else {
  59835. + ep->xfer_len = deptsiz.b.xfersize;
  59836. + }
  59837. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59838. + } else {
  59839. + ep->xfer_len =
  59840. + (ep->total_len - ep->xfer_count) >
  59841. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  59842. + ep->xfer_count);
  59843. +
  59844. + dma_desc = core_if->dev_if->in_desc_addr;
  59845. +
  59846. + /** DMA Descriptor Setup */
  59847. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59848. + dma_desc->status.b.l = 1;
  59849. + dma_desc->status.b.ioc = 1;
  59850. + dma_desc->status.b.sp =
  59851. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  59852. + dma_desc->status.b.bytes = ep->xfer_len;
  59853. + dma_desc->buf = ep->dma_addr;
  59854. + dma_desc->status.b.sts = 0;
  59855. + dma_desc->status.b.bs = BS_HOST_READY;
  59856. +
  59857. + /** DIEPDMA0 Register write */
  59858. + DWC_WRITE_REG32(&in_regs->diepdma,
  59859. + core_if->dev_if->dma_in_desc_addr);
  59860. + }
  59861. +
  59862. + DWC_DEBUGPL(DBG_PCDV,
  59863. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59864. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59865. + deptsiz.d32);
  59866. +
  59867. + /* Write the DMA register */
  59868. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  59869. + if (core_if->dma_desc_enable == 0)
  59870. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59871. + (uint32_t) ep->dma_addr);
  59872. + }
  59873. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59874. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59875. + /* EP enable, IN data in FIFO */
  59876. + depctl.b.cnak = 1;
  59877. + depctl.b.epena = 1;
  59878. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59879. +
  59880. + /**
  59881. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  59882. + * data will be written into the fifo by the ISR.
  59883. + */
  59884. + if (!core_if->dma_enable) {
  59885. + if (core_if->en_multiple_tx_fifo == 0) {
  59886. + /* First clear it from GINTSTS */
  59887. + intr_mask.b.nptxfempty = 1;
  59888. + DWC_MODIFY_REG32(&core_if->
  59889. + core_global_regs->gintmsk,
  59890. + intr_mask.d32, intr_mask.d32);
  59891. +
  59892. + } else {
  59893. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59894. + if (ep->xfer_len > 0) {
  59895. + uint32_t fifoemptymsk = 0;
  59896. + fifoemptymsk |= 1 << ep->num;
  59897. + DWC_MODIFY_REG32(&core_if->
  59898. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59899. + 0, fifoemptymsk);
  59900. + }
  59901. + }
  59902. + }
  59903. + } else {
  59904. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59905. + core_if->dev_if->out_ep_regs[0];
  59906. +
  59907. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  59908. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  59909. +
  59910. + /* Program the transfer size and packet count
  59911. + * as follows: xfersize = N * maxpacket +
  59912. + * short_packet pktcnt = N + (short_packet
  59913. + * exist ? 1 : 0)
  59914. + */
  59915. + deptsiz.b.xfersize = ep->maxpacket;
  59916. + deptsiz.b.pktcnt = 1;
  59917. +
  59918. + if (core_if->dma_desc_enable == 0) {
  59919. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59920. + } else {
  59921. + dma_desc = core_if->dev_if->out_desc_addr;
  59922. +
  59923. + /** DMA Descriptor Setup */
  59924. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59925. + dma_desc->status.b.l = 1;
  59926. + dma_desc->status.b.ioc = 1;
  59927. + dma_desc->status.b.bytes = ep->maxpacket;
  59928. + dma_desc->buf = ep->dma_addr;
  59929. + dma_desc->status.b.sts = 0;
  59930. + dma_desc->status.b.bs = BS_HOST_READY;
  59931. +
  59932. + /** DOEPDMA0 Register write */
  59933. + DWC_WRITE_REG32(&out_regs->doepdma,
  59934. + core_if->dev_if->dma_out_desc_addr);
  59935. + }
  59936. +
  59937. + DWC_DEBUGPL(DBG_PCDV,
  59938. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59939. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59940. + deptsiz.d32);
  59941. +
  59942. + /* Write the DMA register */
  59943. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  59944. + if (core_if->dma_desc_enable == 0)
  59945. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59946. + (uint32_t) ep->dma_addr);
  59947. +
  59948. + }
  59949. +
  59950. + /* EP enable, IN data in FIFO */
  59951. + depctl.b.cnak = 1;
  59952. + depctl.b.epena = 1;
  59953. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59954. +
  59955. + }
  59956. +}
  59957. +
  59958. +#ifdef DEBUG
  59959. +void dump_msg(const u8 * buf, unsigned int length)
  59960. +{
  59961. + unsigned int start, num, i;
  59962. + char line[52], *p;
  59963. +
  59964. + if (length >= 512)
  59965. + return;
  59966. + start = 0;
  59967. + while (length > 0) {
  59968. + num = length < 16u ? length : 16u;
  59969. + p = line;
  59970. + for (i = 0; i < num; ++i) {
  59971. + if (i == 8)
  59972. + *p++ = ' ';
  59973. + DWC_SPRINTF(p, " %02x", buf[i]);
  59974. + p += 3;
  59975. + }
  59976. + *p = 0;
  59977. + DWC_PRINTF("%6x: %s\n", start, line);
  59978. + buf += num;
  59979. + start += num;
  59980. + length -= num;
  59981. + }
  59982. +}
  59983. +#else
  59984. +static inline void dump_msg(const u8 * buf, unsigned int length)
  59985. +{
  59986. +}
  59987. +#endif
  59988. +
  59989. +/**
  59990. + * This function writes a packet into the Tx FIFO associated with the
  59991. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  59992. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  59993. + * with all packets for the next micro-frame.
  59994. + *
  59995. + * @param core_if Programming view of DWC_otg controller.
  59996. + * @param ep The EP to write packet for.
  59997. + * @param dma Indicates if DMA is being used.
  59998. + */
  59999. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  60000. + int dma)
  60001. +{
  60002. + /**
  60003. + * The buffer is padded to DWORD on a per packet basis in
  60004. + * slave/dma mode if the MPS is not DWORD aligned. The last
  60005. + * packet, if short, is also padded to a multiple of DWORD.
  60006. + *
  60007. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  60008. + * multiple of DWORD in length
  60009. + *
  60010. + * ep->xfer_len can be any number of bytes
  60011. + *
  60012. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  60013. + * packet
  60014. + *
  60015. + * FIFO access is DWORD */
  60016. +
  60017. + uint32_t i;
  60018. + uint32_t byte_count;
  60019. + uint32_t dword_count;
  60020. + uint32_t *fifo;
  60021. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  60022. +
  60023. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  60024. + ep);
  60025. + if (ep->xfer_count >= ep->xfer_len) {
  60026. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  60027. + return;
  60028. + }
  60029. +
  60030. + /* Find the byte length of the packet either short packet or MPS */
  60031. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  60032. + byte_count = ep->xfer_len - ep->xfer_count;
  60033. + } else {
  60034. + byte_count = ep->maxpacket;
  60035. + }
  60036. +
  60037. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  60038. + * is not a multiple of DWORD */
  60039. + dword_count = (byte_count + 3) / 4;
  60040. +
  60041. +#ifdef VERBOSE
  60042. + dump_msg(ep->xfer_buff, byte_count);
  60043. +#endif
  60044. +
  60045. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  60046. + * intialized? What should this be? */
  60047. +
  60048. + fifo = core_if->data_fifo[ep->num];
  60049. +
  60050. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  60051. + fifo, data_buff, *data_buff, byte_count);
  60052. +
  60053. + if (!dma) {
  60054. + for (i = 0; i < dword_count; i++, data_buff++) {
  60055. + DWC_WRITE_REG32(fifo, *data_buff);
  60056. + }
  60057. + }
  60058. +
  60059. + ep->xfer_count += byte_count;
  60060. + ep->xfer_buff += byte_count;
  60061. + ep->dma_addr += byte_count;
  60062. +}
  60063. +
  60064. +/**
  60065. + * Set the EP STALL.
  60066. + *
  60067. + * @param core_if Programming view of DWC_otg controller.
  60068. + * @param ep The EP to set the stall on.
  60069. + */
  60070. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60071. +{
  60072. + depctl_data_t depctl;
  60073. + volatile uint32_t *depctl_addr;
  60074. +
  60075. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  60076. + (ep->is_in ? "IN" : "OUT"));
  60077. +
  60078. + if (ep->is_in == 1) {
  60079. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  60080. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  60081. +
  60082. + /* set the disable and stall bits */
  60083. + if (depctl.b.epena) {
  60084. + depctl.b.epdis = 1;
  60085. + }
  60086. + depctl.b.stall = 1;
  60087. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  60088. + } else {
  60089. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  60090. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  60091. +
  60092. + /* set the stall bit */
  60093. + depctl.b.stall = 1;
  60094. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  60095. + }
  60096. +
  60097. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  60098. +
  60099. + return;
  60100. +}
  60101. +
  60102. +/**
  60103. + * Clear the EP STALL.
  60104. + *
  60105. + * @param core_if Programming view of DWC_otg controller.
  60106. + * @param ep The EP to clear stall from.
  60107. + */
  60108. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60109. +{
  60110. + depctl_data_t depctl;
  60111. + volatile uint32_t *depctl_addr;
  60112. +
  60113. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  60114. + (ep->is_in ? "IN" : "OUT"));
  60115. +
  60116. + if (ep->is_in == 1) {
  60117. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  60118. + } else {
  60119. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  60120. + }
  60121. +
  60122. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  60123. +
  60124. + /* clear the stall bits */
  60125. + depctl.b.stall = 0;
  60126. +
  60127. + /*
  60128. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  60129. + * of whether an endpoint has the Halt feature set, a
  60130. + * ClearFeature(ENDPOINT_HALT) request always results in the
  60131. + * data toggle being reinitialized to DATA0.
  60132. + */
  60133. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  60134. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  60135. + depctl.b.setd0pid = 1; /* DATA0 */
  60136. + }
  60137. +
  60138. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  60139. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  60140. + return;
  60141. +}
  60142. +
  60143. +/**
  60144. + * This function reads a packet from the Rx FIFO into the destination
  60145. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  60146. + *
  60147. + * @param core_if Programming view of DWC_otg controller.
  60148. + * @param dest Destination buffer for the packet.
  60149. + * @param bytes Number of bytes to copy to the destination.
  60150. + */
  60151. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  60152. + uint8_t * dest, uint16_t bytes)
  60153. +{
  60154. + int i;
  60155. + int word_count = (bytes + 3) / 4;
  60156. +
  60157. + volatile uint32_t *fifo = core_if->data_fifo[0];
  60158. + uint32_t *data_buff = (uint32_t *) dest;
  60159. +
  60160. + /**
  60161. + * @todo Account for the case where _dest is not dword aligned. This
  60162. + * requires reading data from the FIFO into a uint32_t temp buffer,
  60163. + * then moving it into the data buffer.
  60164. + */
  60165. +
  60166. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  60167. + core_if, dest, bytes);
  60168. +
  60169. + for (i = 0; i < word_count; i++, data_buff++) {
  60170. + *data_buff = DWC_READ_REG32(fifo);
  60171. + }
  60172. +
  60173. + return;
  60174. +}
  60175. +
  60176. +/**
  60177. + * This functions reads the device registers and prints them
  60178. + *
  60179. + * @param core_if Programming view of DWC_otg controller.
  60180. + */
  60181. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  60182. +{
  60183. + int i;
  60184. + volatile uint32_t *addr;
  60185. +
  60186. + DWC_PRINTF("Device Global Registers\n");
  60187. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  60188. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  60189. + (unsigned long)addr, DWC_READ_REG32(addr));
  60190. + addr = &core_if->dev_if->dev_global_regs->dctl;
  60191. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  60192. + (unsigned long)addr, DWC_READ_REG32(addr));
  60193. + addr = &core_if->dev_if->dev_global_regs->dsts;
  60194. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  60195. + (unsigned long)addr, DWC_READ_REG32(addr));
  60196. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  60197. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60198. + DWC_READ_REG32(addr));
  60199. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  60200. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60201. + DWC_READ_REG32(addr));
  60202. + addr = &core_if->dev_if->dev_global_regs->daint;
  60203. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60204. + DWC_READ_REG32(addr));
  60205. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  60206. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60207. + DWC_READ_REG32(addr));
  60208. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  60209. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60210. + DWC_READ_REG32(addr));
  60211. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  60212. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  60213. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  60214. + (unsigned long)addr, DWC_READ_REG32(addr));
  60215. + }
  60216. +
  60217. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  60218. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60219. + DWC_READ_REG32(addr));
  60220. +
  60221. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  60222. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  60223. + (unsigned long)addr, DWC_READ_REG32(addr));
  60224. +
  60225. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  60226. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  60227. + (unsigned long)addr, DWC_READ_REG32(addr));
  60228. +
  60229. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  60230. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  60231. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  60232. + (unsigned long)addr, DWC_READ_REG32(addr));
  60233. + }
  60234. +
  60235. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  60236. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60237. + DWC_READ_REG32(addr));
  60238. +
  60239. + if (core_if->hwcfg2.b.multi_proc_int) {
  60240. +
  60241. + addr = &core_if->dev_if->dev_global_regs->deachint;
  60242. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  60243. + (unsigned long)addr, DWC_READ_REG32(addr));
  60244. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  60245. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  60246. + (unsigned long)addr, DWC_READ_REG32(addr));
  60247. +
  60248. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  60249. + addr =
  60250. + &core_if->dev_if->
  60251. + dev_global_regs->diepeachintmsk[i];
  60252. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  60253. + i, (unsigned long)addr,
  60254. + DWC_READ_REG32(addr));
  60255. + }
  60256. +
  60257. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  60258. + addr =
  60259. + &core_if->dev_if->
  60260. + dev_global_regs->doepeachintmsk[i];
  60261. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  60262. + i, (unsigned long)addr,
  60263. + DWC_READ_REG32(addr));
  60264. + }
  60265. + }
  60266. +
  60267. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  60268. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  60269. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  60270. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  60271. + (unsigned long)addr, DWC_READ_REG32(addr));
  60272. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  60273. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  60274. + (unsigned long)addr, DWC_READ_REG32(addr));
  60275. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  60276. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  60277. + (unsigned long)addr, DWC_READ_REG32(addr));
  60278. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  60279. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  60280. + (unsigned long)addr, DWC_READ_REG32(addr));
  60281. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  60282. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  60283. + (unsigned long)addr, DWC_READ_REG32(addr));
  60284. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  60285. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  60286. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  60287. + }
  60288. +
  60289. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  60290. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  60291. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  60292. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  60293. + (unsigned long)addr, DWC_READ_REG32(addr));
  60294. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  60295. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  60296. + (unsigned long)addr, DWC_READ_REG32(addr));
  60297. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  60298. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  60299. + (unsigned long)addr, DWC_READ_REG32(addr));
  60300. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  60301. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  60302. + (unsigned long)addr, DWC_READ_REG32(addr));
  60303. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  60304. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  60305. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  60306. + (unsigned long)addr, DWC_READ_REG32(addr));
  60307. + }
  60308. +
  60309. + }
  60310. +}
  60311. +
  60312. +/**
  60313. + * This functions reads the SPRAM and prints its content
  60314. + *
  60315. + * @param core_if Programming view of DWC_otg controller.
  60316. + */
  60317. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  60318. +{
  60319. + volatile uint8_t *addr, *start_addr, *end_addr;
  60320. +
  60321. + DWC_PRINTF("SPRAM Data:\n");
  60322. + start_addr = (void *)core_if->core_global_regs;
  60323. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  60324. + start_addr += 0x00028000;
  60325. + end_addr = (void *)core_if->core_global_regs;
  60326. + end_addr += 0x000280e0;
  60327. +
  60328. + for (addr = start_addr; addr < end_addr; addr += 16) {
  60329. + DWC_PRINTF
  60330. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  60331. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  60332. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  60333. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  60334. + );
  60335. + }
  60336. +
  60337. + return;
  60338. +}
  60339. +
  60340. +/**
  60341. + * This function reads the host registers and prints them
  60342. + *
  60343. + * @param core_if Programming view of DWC_otg controller.
  60344. + */
  60345. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  60346. +{
  60347. + int i;
  60348. + volatile uint32_t *addr;
  60349. +
  60350. + DWC_PRINTF("Host Global Registers\n");
  60351. + addr = &core_if->host_if->host_global_regs->hcfg;
  60352. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  60353. + (unsigned long)addr, DWC_READ_REG32(addr));
  60354. + addr = &core_if->host_if->host_global_regs->hfir;
  60355. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  60356. + (unsigned long)addr, DWC_READ_REG32(addr));
  60357. + addr = &core_if->host_if->host_global_regs->hfnum;
  60358. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60359. + DWC_READ_REG32(addr));
  60360. + addr = &core_if->host_if->host_global_regs->hptxsts;
  60361. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60362. + DWC_READ_REG32(addr));
  60363. + addr = &core_if->host_if->host_global_regs->haint;
  60364. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60365. + DWC_READ_REG32(addr));
  60366. + addr = &core_if->host_if->host_global_regs->haintmsk;
  60367. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60368. + DWC_READ_REG32(addr));
  60369. + if (core_if->dma_desc_enable) {
  60370. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  60371. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  60372. + (unsigned long)addr, DWC_READ_REG32(addr));
  60373. + }
  60374. +
  60375. + addr = core_if->host_if->hprt0;
  60376. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60377. + DWC_READ_REG32(addr));
  60378. +
  60379. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  60380. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  60381. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  60382. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  60383. + (unsigned long)addr, DWC_READ_REG32(addr));
  60384. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  60385. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  60386. + (unsigned long)addr, DWC_READ_REG32(addr));
  60387. + addr = &core_if->host_if->hc_regs[i]->hcint;
  60388. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  60389. + (unsigned long)addr, DWC_READ_REG32(addr));
  60390. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  60391. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  60392. + (unsigned long)addr, DWC_READ_REG32(addr));
  60393. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  60394. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  60395. + (unsigned long)addr, DWC_READ_REG32(addr));
  60396. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  60397. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  60398. + (unsigned long)addr, DWC_READ_REG32(addr));
  60399. + if (core_if->dma_desc_enable) {
  60400. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  60401. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  60402. + (unsigned long)addr, DWC_READ_REG32(addr));
  60403. + }
  60404. +
  60405. + }
  60406. + return;
  60407. +}
  60408. +
  60409. +/**
  60410. + * This function reads the core global registers and prints them
  60411. + *
  60412. + * @param core_if Programming view of DWC_otg controller.
  60413. + */
  60414. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  60415. +{
  60416. + int i, ep_num;
  60417. + volatile uint32_t *addr;
  60418. + char *txfsiz;
  60419. +
  60420. + DWC_PRINTF("Core Global Registers\n");
  60421. + addr = &core_if->core_global_regs->gotgctl;
  60422. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60423. + DWC_READ_REG32(addr));
  60424. + addr = &core_if->core_global_regs->gotgint;
  60425. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60426. + DWC_READ_REG32(addr));
  60427. + addr = &core_if->core_global_regs->gahbcfg;
  60428. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60429. + DWC_READ_REG32(addr));
  60430. + addr = &core_if->core_global_regs->gusbcfg;
  60431. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60432. + DWC_READ_REG32(addr));
  60433. + addr = &core_if->core_global_regs->grstctl;
  60434. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60435. + DWC_READ_REG32(addr));
  60436. + addr = &core_if->core_global_regs->gintsts;
  60437. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60438. + DWC_READ_REG32(addr));
  60439. + addr = &core_if->core_global_regs->gintmsk;
  60440. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60441. + DWC_READ_REG32(addr));
  60442. + addr = &core_if->core_global_regs->grxstsr;
  60443. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60444. + DWC_READ_REG32(addr));
  60445. + addr = &core_if->core_global_regs->grxfsiz;
  60446. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60447. + DWC_READ_REG32(addr));
  60448. + addr = &core_if->core_global_regs->gnptxfsiz;
  60449. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60450. + DWC_READ_REG32(addr));
  60451. + addr = &core_if->core_global_regs->gnptxsts;
  60452. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60453. + DWC_READ_REG32(addr));
  60454. + addr = &core_if->core_global_regs->gi2cctl;
  60455. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60456. + DWC_READ_REG32(addr));
  60457. + addr = &core_if->core_global_regs->gpvndctl;
  60458. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60459. + DWC_READ_REG32(addr));
  60460. + addr = &core_if->core_global_regs->ggpio;
  60461. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60462. + DWC_READ_REG32(addr));
  60463. + addr = &core_if->core_global_regs->guid;
  60464. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  60465. + (unsigned long)addr, DWC_READ_REG32(addr));
  60466. + addr = &core_if->core_global_regs->gsnpsid;
  60467. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60468. + DWC_READ_REG32(addr));
  60469. + addr = &core_if->core_global_regs->ghwcfg1;
  60470. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60471. + DWC_READ_REG32(addr));
  60472. + addr = &core_if->core_global_regs->ghwcfg2;
  60473. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60474. + DWC_READ_REG32(addr));
  60475. + addr = &core_if->core_global_regs->ghwcfg3;
  60476. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60477. + DWC_READ_REG32(addr));
  60478. + addr = &core_if->core_global_regs->ghwcfg4;
  60479. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60480. + DWC_READ_REG32(addr));
  60481. + addr = &core_if->core_global_regs->glpmcfg;
  60482. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60483. + DWC_READ_REG32(addr));
  60484. + addr = &core_if->core_global_regs->gpwrdn;
  60485. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60486. + DWC_READ_REG32(addr));
  60487. + addr = &core_if->core_global_regs->gdfifocfg;
  60488. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60489. + DWC_READ_REG32(addr));
  60490. + addr = &core_if->core_global_regs->adpctl;
  60491. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60492. + dwc_otg_adp_read_reg(core_if));
  60493. + addr = &core_if->core_global_regs->hptxfsiz;
  60494. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60495. + DWC_READ_REG32(addr));
  60496. +
  60497. + if (core_if->en_multiple_tx_fifo == 0) {
  60498. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  60499. + txfsiz = "DPTXFSIZ";
  60500. + } else {
  60501. + ep_num = core_if->hwcfg4.b.num_in_eps;
  60502. + txfsiz = "DIENPTXF";
  60503. + }
  60504. + for (i = 0; i < ep_num; i++) {
  60505. + addr = &core_if->core_global_regs->dtxfsiz[i];
  60506. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  60507. + (unsigned long)addr, DWC_READ_REG32(addr));
  60508. + }
  60509. + addr = core_if->pcgcctl;
  60510. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  60511. + DWC_READ_REG32(addr));
  60512. +}
  60513. +
  60514. +/**
  60515. + * Flush a Tx FIFO.
  60516. + *
  60517. + * @param core_if Programming view of DWC_otg controller.
  60518. + * @param num Tx FIFO to flush.
  60519. + */
  60520. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  60521. +{
  60522. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60523. + volatile grstctl_t greset = {.d32 = 0 };
  60524. + int count = 0;
  60525. +
  60526. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  60527. +
  60528. + greset.b.txfflsh = 1;
  60529. + greset.b.txfnum = num;
  60530. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  60531. +
  60532. + do {
  60533. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60534. + if (++count > 10000) {
  60535. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  60536. + __func__, greset.d32,
  60537. + DWC_READ_REG32(&global_regs->gnptxsts));
  60538. + break;
  60539. + }
  60540. + dwc_udelay(1);
  60541. + } while (greset.b.txfflsh == 1);
  60542. +
  60543. + /* Wait for 3 PHY Clocks */
  60544. + dwc_udelay(1);
  60545. +}
  60546. +
  60547. +/**
  60548. + * Flush Rx FIFO.
  60549. + *
  60550. + * @param core_if Programming view of DWC_otg controller.
  60551. + */
  60552. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  60553. +{
  60554. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60555. + volatile grstctl_t greset = {.d32 = 0 };
  60556. + int count = 0;
  60557. +
  60558. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  60559. + /*
  60560. + *
  60561. + */
  60562. + greset.b.rxfflsh = 1;
  60563. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  60564. +
  60565. + do {
  60566. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60567. + if (++count > 10000) {
  60568. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  60569. + greset.d32);
  60570. + break;
  60571. + }
  60572. + dwc_udelay(1);
  60573. + } while (greset.b.rxfflsh == 1);
  60574. +
  60575. + /* Wait for 3 PHY Clocks */
  60576. + dwc_udelay(1);
  60577. +}
  60578. +
  60579. +/**
  60580. + * Do core a soft reset of the core. Be careful with this because it
  60581. + * resets all the internal state machines of the core.
  60582. + */
  60583. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  60584. +{
  60585. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60586. + volatile grstctl_t greset = {.d32 = 0 };
  60587. + int count = 0;
  60588. +
  60589. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  60590. + /* Wait for AHB master IDLE state. */
  60591. + do {
  60592. + dwc_udelay(10);
  60593. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60594. + if (++count > 100000) {
  60595. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  60596. + greset.d32);
  60597. + return;
  60598. + }
  60599. + }
  60600. + while (greset.b.ahbidle == 0);
  60601. +
  60602. + /* Core Soft Reset */
  60603. + count = 0;
  60604. + greset.b.csftrst = 1;
  60605. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  60606. + do {
  60607. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  60608. + if (++count > 10000) {
  60609. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  60610. + __func__, greset.d32);
  60611. + break;
  60612. + }
  60613. + dwc_udelay(1);
  60614. + }
  60615. + while (greset.b.csftrst == 1);
  60616. +
  60617. + /* Wait for 3 PHY Clocks */
  60618. + dwc_mdelay(100);
  60619. +}
  60620. +
  60621. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  60622. +{
  60623. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  60624. +}
  60625. +
  60626. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  60627. +{
  60628. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  60629. +}
  60630. +
  60631. +/**
  60632. + * Register HCD callbacks. The callbacks are used to start and stop
  60633. + * the HCD for interrupt processing.
  60634. + *
  60635. + * @param core_if Programming view of DWC_otg controller.
  60636. + * @param cb the HCD callback structure.
  60637. + * @param p pointer to be passed to callback function (usb_hcd*).
  60638. + */
  60639. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  60640. + dwc_otg_cil_callbacks_t * cb, void *p)
  60641. +{
  60642. + core_if->hcd_cb = cb;
  60643. + cb->p = p;
  60644. +}
  60645. +
  60646. +/**
  60647. + * Register PCD callbacks. The callbacks are used to start and stop
  60648. + * the PCD for interrupt processing.
  60649. + *
  60650. + * @param core_if Programming view of DWC_otg controller.
  60651. + * @param cb the PCD callback structure.
  60652. + * @param p pointer to be passed to callback function (pcd*).
  60653. + */
  60654. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  60655. + dwc_otg_cil_callbacks_t * cb, void *p)
  60656. +{
  60657. + core_if->pcd_cb = cb;
  60658. + cb->p = p;
  60659. +}
  60660. +
  60661. +#ifdef DWC_EN_ISOC
  60662. +
  60663. +/**
  60664. + * This function writes isoc data per 1 (micro)frame into tx fifo
  60665. + *
  60666. + * @param core_if Programming view of DWC_otg controller.
  60667. + * @param ep The EP to start the transfer on.
  60668. + *
  60669. + */
  60670. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60671. +{
  60672. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  60673. + dtxfsts_data_t txstatus = {.d32 = 0 };
  60674. + uint32_t len = 0;
  60675. + uint32_t dwords;
  60676. +
  60677. + ep->xfer_len = ep->data_per_frame;
  60678. + ep->xfer_count = 0;
  60679. +
  60680. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  60681. +
  60682. + len = ep->xfer_len - ep->xfer_count;
  60683. +
  60684. + if (len > ep->maxpacket) {
  60685. + len = ep->maxpacket;
  60686. + }
  60687. +
  60688. + dwords = (len + 3) / 4;
  60689. +
  60690. + /* While there is space in the queue and space in the FIFO and
  60691. + * More data to tranfer, Write packets to the Tx FIFO */
  60692. + txstatus.d32 =
  60693. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  60694. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  60695. +
  60696. + while (txstatus.b.txfspcavail > dwords &&
  60697. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  60698. + /* Write the FIFO */
  60699. + dwc_otg_ep_write_packet(core_if, ep, 0);
  60700. +
  60701. + len = ep->xfer_len - ep->xfer_count;
  60702. + if (len > ep->maxpacket) {
  60703. + len = ep->maxpacket;
  60704. + }
  60705. +
  60706. + dwords = (len + 3) / 4;
  60707. + txstatus.d32 =
  60708. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  60709. + dtxfsts);
  60710. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  60711. + txstatus.d32);
  60712. + }
  60713. +}
  60714. +
  60715. +/**
  60716. + * This function initializes a descriptor chain for Isochronous transfer
  60717. + *
  60718. + * @param core_if Programming view of DWC_otg controller.
  60719. + * @param ep The EP to start the transfer on.
  60720. + *
  60721. + */
  60722. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  60723. + dwc_ep_t * ep)
  60724. +{
  60725. + deptsiz_data_t deptsiz = {.d32 = 0 };
  60726. + depctl_data_t depctl = {.d32 = 0 };
  60727. + dsts_data_t dsts = {.d32 = 0 };
  60728. + volatile uint32_t *addr;
  60729. +
  60730. + if (ep->is_in) {
  60731. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  60732. + } else {
  60733. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  60734. + }
  60735. +
  60736. + ep->xfer_len = ep->data_per_frame;
  60737. + ep->xfer_count = 0;
  60738. + ep->xfer_buff = ep->cur_pkt_addr;
  60739. + ep->dma_addr = ep->cur_pkt_dma_addr;
  60740. +
  60741. + if (ep->is_in) {
  60742. + /* Program the transfer size and packet count
  60743. + * as follows: xfersize = N * maxpacket +
  60744. + * short_packet pktcnt = N + (short_packet
  60745. + * exist ? 1 : 0)
  60746. + */
  60747. + deptsiz.b.xfersize = ep->xfer_len;
  60748. + deptsiz.b.pktcnt =
  60749. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  60750. + deptsiz.b.mc = deptsiz.b.pktcnt;
  60751. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  60752. + deptsiz.d32);
  60753. +
  60754. + /* Write the DMA register */
  60755. + if (core_if->dma_enable) {
  60756. + DWC_WRITE_REG32(&
  60757. + (core_if->dev_if->in_ep_regs[ep->num]->
  60758. + diepdma), (uint32_t) ep->dma_addr);
  60759. + }
  60760. + } else {
  60761. + deptsiz.b.pktcnt =
  60762. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  60763. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  60764. +
  60765. + DWC_WRITE_REG32(&core_if->dev_if->
  60766. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  60767. +
  60768. + if (core_if->dma_enable) {
  60769. + DWC_WRITE_REG32(&
  60770. + (core_if->dev_if->
  60771. + out_ep_regs[ep->num]->doepdma),
  60772. + (uint32_t) ep->dma_addr);
  60773. + }
  60774. + }
  60775. +
  60776. + /** Enable endpoint, clear nak */
  60777. +
  60778. + depctl.d32 = 0;
  60779. + if (ep->bInterval == 1) {
  60780. + dsts.d32 =
  60781. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  60782. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  60783. +
  60784. + if (ep->next_frame & 0x1) {
  60785. + depctl.b.setd1pid = 1;
  60786. + } else {
  60787. + depctl.b.setd0pid = 1;
  60788. + }
  60789. + } else {
  60790. + ep->next_frame += ep->bInterval;
  60791. +
  60792. + if (ep->next_frame & 0x1) {
  60793. + depctl.b.setd1pid = 1;
  60794. + } else {
  60795. + depctl.b.setd0pid = 1;
  60796. + }
  60797. + }
  60798. + depctl.b.epena = 1;
  60799. + depctl.b.cnak = 1;
  60800. +
  60801. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  60802. + depctl.d32 = DWC_READ_REG32(addr);
  60803. +
  60804. + if (ep->is_in && core_if->dma_enable == 0) {
  60805. + write_isoc_frame_data(core_if, ep);
  60806. + }
  60807. +
  60808. +}
  60809. +#endif /* DWC_EN_ISOC */
  60810. +
  60811. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  60812. +{
  60813. + int i;
  60814. + for (i = 0; i < size; i++) {
  60815. + p[i] = -1;
  60816. + }
  60817. +}
  60818. +
  60819. +static int dwc_otg_param_initialized(int32_t val)
  60820. +{
  60821. + return val != -1;
  60822. +}
  60823. +
  60824. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  60825. +{
  60826. + int i;
  60827. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  60828. + if (!core_if->core_params) {
  60829. + return -DWC_E_NO_MEMORY;
  60830. + }
  60831. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  60832. + sizeof(*core_if->core_params) /
  60833. + sizeof(int32_t));
  60834. + DWC_PRINTF("Setting default values for core params\n");
  60835. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  60836. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  60837. + dwc_otg_set_param_dma_desc_enable(core_if,
  60838. + dwc_param_dma_desc_enable_default);
  60839. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  60840. + dwc_otg_set_param_dma_burst_size(core_if,
  60841. + dwc_param_dma_burst_size_default);
  60842. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  60843. + dwc_param_host_support_fs_ls_low_power_default);
  60844. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  60845. + dwc_param_enable_dynamic_fifo_default);
  60846. + dwc_otg_set_param_data_fifo_size(core_if,
  60847. + dwc_param_data_fifo_size_default);
  60848. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  60849. + dwc_param_dev_rx_fifo_size_default);
  60850. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  60851. + dwc_param_dev_nperio_tx_fifo_size_default);
  60852. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  60853. + dwc_param_host_rx_fifo_size_default);
  60854. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  60855. + dwc_param_host_nperio_tx_fifo_size_default);
  60856. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  60857. + dwc_param_host_perio_tx_fifo_size_default);
  60858. + dwc_otg_set_param_max_transfer_size(core_if,
  60859. + dwc_param_max_transfer_size_default);
  60860. + dwc_otg_set_param_max_packet_count(core_if,
  60861. + dwc_param_max_packet_count_default);
  60862. + dwc_otg_set_param_host_channels(core_if,
  60863. + dwc_param_host_channels_default);
  60864. + dwc_otg_set_param_dev_endpoints(core_if,
  60865. + dwc_param_dev_endpoints_default);
  60866. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  60867. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  60868. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  60869. + dwc_param_host_ls_low_power_phy_clk_default);
  60870. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  60871. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  60872. + dwc_param_phy_ulpi_ext_vbus_default);
  60873. + dwc_otg_set_param_phy_utmi_width(core_if,
  60874. + dwc_param_phy_utmi_width_default);
  60875. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  60876. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  60877. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  60878. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  60879. + dwc_param_en_multiple_tx_fifo_default);
  60880. + for (i = 0; i < 15; i++) {
  60881. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  60882. + dwc_param_dev_perio_tx_fifo_size_default,
  60883. + i);
  60884. + }
  60885. +
  60886. + for (i = 0; i < 15; i++) {
  60887. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  60888. + dwc_param_dev_tx_fifo_size_default,
  60889. + i);
  60890. + }
  60891. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  60892. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  60893. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  60894. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  60895. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  60896. + dwc_otg_set_param_tx_thr_length(core_if,
  60897. + dwc_param_tx_thr_length_default);
  60898. + dwc_otg_set_param_rx_thr_length(core_if,
  60899. + dwc_param_rx_thr_length_default);
  60900. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  60901. + dwc_param_ahb_thr_ratio_default);
  60902. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  60903. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  60904. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  60905. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  60906. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  60907. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  60908. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  60909. + DWC_PRINTF("Finished setting default values for core params\n");
  60910. +
  60911. + return 0;
  60912. +}
  60913. +
  60914. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  60915. +{
  60916. + return core_if->dma_enable;
  60917. +}
  60918. +
  60919. +/* Checks if the parameter is outside of its valid range of values */
  60920. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  60921. + (((_param_) < (_low_)) || \
  60922. + ((_param_) > (_high_)))
  60923. +
  60924. +/* Parameter access functions */
  60925. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  60926. +{
  60927. + int valid;
  60928. + int retval = 0;
  60929. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  60930. + DWC_WARN("Wrong value for otg_cap parameter\n");
  60931. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  60932. + retval = -DWC_E_INVALID;
  60933. + goto out;
  60934. + }
  60935. +
  60936. + valid = 1;
  60937. + switch (val) {
  60938. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  60939. + if (core_if->hwcfg2.b.op_mode !=
  60940. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60941. + valid = 0;
  60942. + break;
  60943. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  60944. + if ((core_if->hwcfg2.b.op_mode !=
  60945. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60946. + && (core_if->hwcfg2.b.op_mode !=
  60947. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  60948. + && (core_if->hwcfg2.b.op_mode !=
  60949. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  60950. + && (core_if->hwcfg2.b.op_mode !=
  60951. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  60952. + valid = 0;
  60953. + }
  60954. + break;
  60955. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  60956. + /* always valid */
  60957. + break;
  60958. + }
  60959. + if (!valid) {
  60960. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  60961. + DWC_ERROR
  60962. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  60963. + val);
  60964. + }
  60965. + val =
  60966. + (((core_if->hwcfg2.b.op_mode ==
  60967. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60968. + || (core_if->hwcfg2.b.op_mode ==
  60969. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  60970. + || (core_if->hwcfg2.b.op_mode ==
  60971. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  60972. + || (core_if->hwcfg2.b.op_mode ==
  60973. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  60974. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  60975. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  60976. + retval = -DWC_E_INVALID;
  60977. + }
  60978. +
  60979. + core_if->core_params->otg_cap = val;
  60980. +out:
  60981. + return retval;
  60982. +}
  60983. +
  60984. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  60985. +{
  60986. + return core_if->core_params->otg_cap;
  60987. +}
  60988. +
  60989. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  60990. +{
  60991. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60992. + DWC_WARN("Wrong value for opt parameter\n");
  60993. + return -DWC_E_INVALID;
  60994. + }
  60995. + core_if->core_params->opt = val;
  60996. + return 0;
  60997. +}
  60998. +
  60999. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  61000. +{
  61001. + return core_if->core_params->opt;
  61002. +}
  61003. +
  61004. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61005. +{
  61006. + int retval = 0;
  61007. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61008. + DWC_WARN("Wrong value for dma enable\n");
  61009. + return -DWC_E_INVALID;
  61010. + }
  61011. +
  61012. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  61013. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  61014. + DWC_ERROR
  61015. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  61016. + val);
  61017. + }
  61018. + val = 0;
  61019. + retval = -DWC_E_INVALID;
  61020. + }
  61021. +
  61022. + core_if->core_params->dma_enable = val;
  61023. + if (val == 0) {
  61024. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  61025. + }
  61026. + return retval;
  61027. +}
  61028. +
  61029. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  61030. +{
  61031. + return core_if->core_params->dma_enable;
  61032. +}
  61033. +
  61034. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61035. +{
  61036. + int retval = 0;
  61037. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61038. + DWC_WARN("Wrong value for dma_enable\n");
  61039. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  61040. + return -DWC_E_INVALID;
  61041. + }
  61042. +
  61043. + if ((val == 1)
  61044. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  61045. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  61046. + if (dwc_otg_param_initialized
  61047. + (core_if->core_params->dma_desc_enable)) {
  61048. + DWC_ERROR
  61049. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  61050. + val);
  61051. + }
  61052. + val = 0;
  61053. + retval = -DWC_E_INVALID;
  61054. + }
  61055. + core_if->core_params->dma_desc_enable = val;
  61056. + return retval;
  61057. +}
  61058. +
  61059. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  61060. +{
  61061. + return core_if->core_params->dma_desc_enable;
  61062. +}
  61063. +
  61064. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  61065. + int32_t val)
  61066. +{
  61067. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61068. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  61069. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  61070. + return -DWC_E_INVALID;
  61071. + }
  61072. + core_if->core_params->host_support_fs_ls_low_power = val;
  61073. + return 0;
  61074. +}
  61075. +
  61076. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  61077. + core_if)
  61078. +{
  61079. + return core_if->core_params->host_support_fs_ls_low_power;
  61080. +}
  61081. +
  61082. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  61083. + int32_t val)
  61084. +{
  61085. + int retval = 0;
  61086. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61087. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  61088. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  61089. + return -DWC_E_INVALID;
  61090. + }
  61091. +
  61092. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  61093. + if (dwc_otg_param_initialized
  61094. + (core_if->core_params->enable_dynamic_fifo)) {
  61095. + DWC_ERROR
  61096. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  61097. + val);
  61098. + }
  61099. + val = 0;
  61100. + retval = -DWC_E_INVALID;
  61101. + }
  61102. + core_if->core_params->enable_dynamic_fifo = val;
  61103. + return retval;
  61104. +}
  61105. +
  61106. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  61107. +{
  61108. + return core_if->core_params->enable_dynamic_fifo;
  61109. +}
  61110. +
  61111. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  61112. +{
  61113. + int retval = 0;
  61114. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  61115. + DWC_WARN("Wrong value for data_fifo_size\n");
  61116. + DWC_WARN("data_fifo_size must be 32-32768\n");
  61117. + return -DWC_E_INVALID;
  61118. + }
  61119. +
  61120. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  61121. + if (dwc_otg_param_initialized
  61122. + (core_if->core_params->data_fifo_size)) {
  61123. + DWC_ERROR
  61124. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  61125. + val);
  61126. + }
  61127. + val = core_if->hwcfg3.b.dfifo_depth;
  61128. + retval = -DWC_E_INVALID;
  61129. + }
  61130. +
  61131. + core_if->core_params->data_fifo_size = val;
  61132. + return retval;
  61133. +}
  61134. +
  61135. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  61136. +{
  61137. + return core_if->core_params->data_fifo_size;
  61138. +}
  61139. +
  61140. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  61141. +{
  61142. + int retval = 0;
  61143. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61144. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  61145. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  61146. + return -DWC_E_INVALID;
  61147. + }
  61148. +
  61149. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  61150. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  61151. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  61152. + }
  61153. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  61154. + retval = -DWC_E_INVALID;
  61155. + }
  61156. +
  61157. + core_if->core_params->dev_rx_fifo_size = val;
  61158. + return retval;
  61159. +}
  61160. +
  61161. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  61162. +{
  61163. + return core_if->core_params->dev_rx_fifo_size;
  61164. +}
  61165. +
  61166. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61167. + int32_t val)
  61168. +{
  61169. + int retval = 0;
  61170. +
  61171. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61172. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  61173. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  61174. + return -DWC_E_INVALID;
  61175. + }
  61176. +
  61177. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  61178. + if (dwc_otg_param_initialized
  61179. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  61180. + DWC_ERROR
  61181. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  61182. + val);
  61183. + }
  61184. + val =
  61185. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  61186. + 16);
  61187. + retval = -DWC_E_INVALID;
  61188. + }
  61189. +
  61190. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  61191. + return retval;
  61192. +}
  61193. +
  61194. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  61195. +{
  61196. + return core_if->core_params->dev_nperio_tx_fifo_size;
  61197. +}
  61198. +
  61199. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  61200. + int32_t val)
  61201. +{
  61202. + int retval = 0;
  61203. +
  61204. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61205. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  61206. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  61207. + return -DWC_E_INVALID;
  61208. + }
  61209. +
  61210. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  61211. + if (dwc_otg_param_initialized
  61212. + (core_if->core_params->host_rx_fifo_size)) {
  61213. + DWC_ERROR
  61214. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  61215. + val);
  61216. + }
  61217. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  61218. + retval = -DWC_E_INVALID;
  61219. + }
  61220. +
  61221. + core_if->core_params->host_rx_fifo_size = val;
  61222. + return retval;
  61223. +
  61224. +}
  61225. +
  61226. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  61227. +{
  61228. + return core_if->core_params->host_rx_fifo_size;
  61229. +}
  61230. +
  61231. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61232. + int32_t val)
  61233. +{
  61234. + int retval = 0;
  61235. +
  61236. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61237. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  61238. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  61239. + return -DWC_E_INVALID;
  61240. + }
  61241. +
  61242. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  61243. + if (dwc_otg_param_initialized
  61244. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  61245. + DWC_ERROR
  61246. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  61247. + val);
  61248. + }
  61249. + val =
  61250. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  61251. + 16);
  61252. + retval = -DWC_E_INVALID;
  61253. + }
  61254. +
  61255. + core_if->core_params->host_nperio_tx_fifo_size = val;
  61256. + return retval;
  61257. +}
  61258. +
  61259. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  61260. +{
  61261. + return core_if->core_params->host_nperio_tx_fifo_size;
  61262. +}
  61263. +
  61264. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61265. + int32_t val)
  61266. +{
  61267. + int retval = 0;
  61268. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  61269. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  61270. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  61271. + return -DWC_E_INVALID;
  61272. + }
  61273. +
  61274. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  61275. + if (dwc_otg_param_initialized
  61276. + (core_if->core_params->host_perio_tx_fifo_size)) {
  61277. + DWC_ERROR
  61278. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  61279. + val);
  61280. + }
  61281. + val = (core_if->hptxfsiz.d32) >> 16;
  61282. + retval = -DWC_E_INVALID;
  61283. + }
  61284. +
  61285. + core_if->core_params->host_perio_tx_fifo_size = val;
  61286. + return retval;
  61287. +}
  61288. +
  61289. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  61290. +{
  61291. + return core_if->core_params->host_perio_tx_fifo_size;
  61292. +}
  61293. +
  61294. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  61295. + int32_t val)
  61296. +{
  61297. + int retval = 0;
  61298. +
  61299. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  61300. + DWC_WARN("Wrong value for max_transfer_size\n");
  61301. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  61302. + return -DWC_E_INVALID;
  61303. + }
  61304. +
  61305. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  61306. + if (dwc_otg_param_initialized
  61307. + (core_if->core_params->max_transfer_size)) {
  61308. + DWC_ERROR
  61309. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  61310. + val);
  61311. + }
  61312. + val =
  61313. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  61314. + 1);
  61315. + retval = -DWC_E_INVALID;
  61316. + }
  61317. +
  61318. + core_if->core_params->max_transfer_size = val;
  61319. + return retval;
  61320. +}
  61321. +
  61322. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  61323. +{
  61324. + return core_if->core_params->max_transfer_size;
  61325. +}
  61326. +
  61327. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  61328. +{
  61329. + int retval = 0;
  61330. +
  61331. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  61332. + DWC_WARN("Wrong value for max_packet_count\n");
  61333. + DWC_WARN("max_packet_count must be 15-511\n");
  61334. + return -DWC_E_INVALID;
  61335. + }
  61336. +
  61337. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  61338. + if (dwc_otg_param_initialized
  61339. + (core_if->core_params->max_packet_count)) {
  61340. + DWC_ERROR
  61341. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  61342. + val);
  61343. + }
  61344. + val =
  61345. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  61346. + retval = -DWC_E_INVALID;
  61347. + }
  61348. +
  61349. + core_if->core_params->max_packet_count = val;
  61350. + return retval;
  61351. +}
  61352. +
  61353. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  61354. +{
  61355. + return core_if->core_params->max_packet_count;
  61356. +}
  61357. +
  61358. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  61359. +{
  61360. + int retval = 0;
  61361. +
  61362. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  61363. + DWC_WARN("Wrong value for host_channels\n");
  61364. + DWC_WARN("host_channels must be 1-16\n");
  61365. + return -DWC_E_INVALID;
  61366. + }
  61367. +
  61368. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  61369. + if (dwc_otg_param_initialized
  61370. + (core_if->core_params->host_channels)) {
  61371. + DWC_ERROR
  61372. + ("%d invalid for host_channels. Check HW configurations.\n",
  61373. + val);
  61374. + }
  61375. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  61376. + retval = -DWC_E_INVALID;
  61377. + }
  61378. +
  61379. + core_if->core_params->host_channels = val;
  61380. + return retval;
  61381. +}
  61382. +
  61383. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  61384. +{
  61385. + return core_if->core_params->host_channels;
  61386. +}
  61387. +
  61388. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  61389. +{
  61390. + int retval = 0;
  61391. +
  61392. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  61393. + DWC_WARN("Wrong value for dev_endpoints\n");
  61394. + DWC_WARN("dev_endpoints must be 1-15\n");
  61395. + return -DWC_E_INVALID;
  61396. + }
  61397. +
  61398. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  61399. + if (dwc_otg_param_initialized
  61400. + (core_if->core_params->dev_endpoints)) {
  61401. + DWC_ERROR
  61402. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  61403. + val);
  61404. + }
  61405. + val = core_if->hwcfg2.b.num_dev_ep;
  61406. + retval = -DWC_E_INVALID;
  61407. + }
  61408. +
  61409. + core_if->core_params->dev_endpoints = val;
  61410. + return retval;
  61411. +}
  61412. +
  61413. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  61414. +{
  61415. + return core_if->core_params->dev_endpoints;
  61416. +}
  61417. +
  61418. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  61419. +{
  61420. + int retval = 0;
  61421. + int valid = 0;
  61422. +
  61423. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  61424. + DWC_WARN("Wrong value for phy_type\n");
  61425. + DWC_WARN("phy_type must be 0,1 or 2\n");
  61426. + return -DWC_E_INVALID;
  61427. + }
  61428. +#ifndef NO_FS_PHY_HW_CHECKS
  61429. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  61430. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  61431. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  61432. + valid = 1;
  61433. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  61434. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  61435. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  61436. + valid = 1;
  61437. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  61438. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  61439. + valid = 1;
  61440. + }
  61441. + if (!valid) {
  61442. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  61443. + DWC_ERROR
  61444. + ("%d invalid for phy_type. Check HW configurations.\n",
  61445. + val);
  61446. + }
  61447. + if (core_if->hwcfg2.b.hs_phy_type) {
  61448. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  61449. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  61450. + val = DWC_PHY_TYPE_PARAM_UTMI;
  61451. + } else {
  61452. + val = DWC_PHY_TYPE_PARAM_ULPI;
  61453. + }
  61454. + }
  61455. + retval = -DWC_E_INVALID;
  61456. + }
  61457. +#endif
  61458. + core_if->core_params->phy_type = val;
  61459. + return retval;
  61460. +}
  61461. +
  61462. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  61463. +{
  61464. + return core_if->core_params->phy_type;
  61465. +}
  61466. +
  61467. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  61468. +{
  61469. + int retval = 0;
  61470. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61471. + DWC_WARN("Wrong value for speed parameter\n");
  61472. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  61473. + return -DWC_E_INVALID;
  61474. + }
  61475. + if ((val == 0)
  61476. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  61477. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  61478. + DWC_ERROR
  61479. + ("%d invalid for speed paremter. Check HW configuration.\n",
  61480. + val);
  61481. + }
  61482. + val =
  61483. + (dwc_otg_get_param_phy_type(core_if) ==
  61484. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  61485. + retval = -DWC_E_INVALID;
  61486. + }
  61487. + core_if->core_params->speed = val;
  61488. + return retval;
  61489. +}
  61490. +
  61491. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  61492. +{
  61493. + return core_if->core_params->speed;
  61494. +}
  61495. +
  61496. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  61497. + int32_t val)
  61498. +{
  61499. + int retval = 0;
  61500. +
  61501. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61502. + DWC_WARN
  61503. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  61504. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  61505. + return -DWC_E_INVALID;
  61506. + }
  61507. +
  61508. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  61509. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  61510. + if (dwc_otg_param_initialized
  61511. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  61512. + DWC_ERROR
  61513. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  61514. + val);
  61515. + }
  61516. + val =
  61517. + (dwc_otg_get_param_phy_type(core_if) ==
  61518. + DWC_PHY_TYPE_PARAM_FS) ?
  61519. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  61520. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  61521. + retval = -DWC_E_INVALID;
  61522. + }
  61523. +
  61524. + core_if->core_params->host_ls_low_power_phy_clk = val;
  61525. + return retval;
  61526. +}
  61527. +
  61528. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  61529. +{
  61530. + return core_if->core_params->host_ls_low_power_phy_clk;
  61531. +}
  61532. +
  61533. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  61534. +{
  61535. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61536. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  61537. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  61538. + return -DWC_E_INVALID;
  61539. + }
  61540. +
  61541. + core_if->core_params->phy_ulpi_ddr = val;
  61542. + return 0;
  61543. +}
  61544. +
  61545. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  61546. +{
  61547. + return core_if->core_params->phy_ulpi_ddr;
  61548. +}
  61549. +
  61550. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  61551. + int32_t val)
  61552. +{
  61553. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61554. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  61555. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  61556. + return -DWC_E_INVALID;
  61557. + }
  61558. +
  61559. + core_if->core_params->phy_ulpi_ext_vbus = val;
  61560. + return 0;
  61561. +}
  61562. +
  61563. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  61564. +{
  61565. + return core_if->core_params->phy_ulpi_ext_vbus;
  61566. +}
  61567. +
  61568. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  61569. +{
  61570. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  61571. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  61572. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  61573. + return -DWC_E_INVALID;
  61574. + }
  61575. +
  61576. + core_if->core_params->phy_utmi_width = val;
  61577. + return 0;
  61578. +}
  61579. +
  61580. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  61581. +{
  61582. + return core_if->core_params->phy_utmi_width;
  61583. +}
  61584. +
  61585. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  61586. +{
  61587. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61588. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  61589. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  61590. + return -DWC_E_INVALID;
  61591. + }
  61592. +
  61593. + core_if->core_params->ulpi_fs_ls = val;
  61594. + return 0;
  61595. +}
  61596. +
  61597. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  61598. +{
  61599. + return core_if->core_params->ulpi_fs_ls;
  61600. +}
  61601. +
  61602. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  61603. +{
  61604. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61605. + DWC_WARN("Wrong valaue for ts_dline\n");
  61606. + DWC_WARN("ts_dline must be 0 or 1\n");
  61607. + return -DWC_E_INVALID;
  61608. + }
  61609. +
  61610. + core_if->core_params->ts_dline = val;
  61611. + return 0;
  61612. +}
  61613. +
  61614. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  61615. +{
  61616. + return core_if->core_params->ts_dline;
  61617. +}
  61618. +
  61619. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61620. +{
  61621. + int retval = 0;
  61622. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61623. + DWC_WARN("Wrong valaue for i2c_enable\n");
  61624. + DWC_WARN("i2c_enable must be 0 or 1\n");
  61625. + return -DWC_E_INVALID;
  61626. + }
  61627. +#ifndef NO_FS_PHY_HW_CHECK
  61628. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  61629. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  61630. + DWC_ERROR
  61631. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  61632. + val);
  61633. + }
  61634. + val = 0;
  61635. + retval = -DWC_E_INVALID;
  61636. + }
  61637. +#endif
  61638. +
  61639. + core_if->core_params->i2c_enable = val;
  61640. + return retval;
  61641. +}
  61642. +
  61643. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  61644. +{
  61645. + return core_if->core_params->i2c_enable;
  61646. +}
  61647. +
  61648. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61649. + int32_t val, int fifo_num)
  61650. +{
  61651. + int retval = 0;
  61652. +
  61653. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  61654. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  61655. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  61656. + return -DWC_E_INVALID;
  61657. + }
  61658. +
  61659. + if (val >
  61660. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  61661. + if (dwc_otg_param_initialized
  61662. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  61663. + DWC_ERROR
  61664. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  61665. + val, fifo_num);
  61666. + }
  61667. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  61668. + retval = -DWC_E_INVALID;
  61669. + }
  61670. +
  61671. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  61672. + return retval;
  61673. +}
  61674. +
  61675. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61676. + int fifo_num)
  61677. +{
  61678. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  61679. +}
  61680. +
  61681. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  61682. + int32_t val)
  61683. +{
  61684. + int retval = 0;
  61685. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61686. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  61687. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  61688. + return -DWC_E_INVALID;
  61689. + }
  61690. +
  61691. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  61692. + if (dwc_otg_param_initialized
  61693. + (core_if->core_params->en_multiple_tx_fifo)) {
  61694. + DWC_ERROR
  61695. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  61696. + val);
  61697. + }
  61698. + val = 0;
  61699. + retval = -DWC_E_INVALID;
  61700. + }
  61701. +
  61702. + core_if->core_params->en_multiple_tx_fifo = val;
  61703. + return retval;
  61704. +}
  61705. +
  61706. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  61707. +{
  61708. + return core_if->core_params->en_multiple_tx_fifo;
  61709. +}
  61710. +
  61711. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  61712. + int fifo_num)
  61713. +{
  61714. + int retval = 0;
  61715. +
  61716. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  61717. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  61718. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  61719. + return -DWC_E_INVALID;
  61720. + }
  61721. +
  61722. + if (val >
  61723. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  61724. + if (dwc_otg_param_initialized
  61725. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  61726. + DWC_ERROR
  61727. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  61728. + val, fifo_num);
  61729. + }
  61730. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  61731. + retval = -DWC_E_INVALID;
  61732. + }
  61733. +
  61734. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  61735. + return retval;
  61736. +}
  61737. +
  61738. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61739. + int fifo_num)
  61740. +{
  61741. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  61742. +}
  61743. +
  61744. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  61745. +{
  61746. + int retval = 0;
  61747. +
  61748. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  61749. + DWC_WARN("Wrong value for thr_ctl\n");
  61750. + DWC_WARN("thr_ctl must be 0-7\n");
  61751. + return -DWC_E_INVALID;
  61752. + }
  61753. +
  61754. + if ((val != 0) &&
  61755. + (!dwc_otg_get_param_dma_enable(core_if) ||
  61756. + !core_if->hwcfg4.b.ded_fifo_en)) {
  61757. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  61758. + DWC_ERROR
  61759. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  61760. + val);
  61761. + }
  61762. + val = 0;
  61763. + retval = -DWC_E_INVALID;
  61764. + }
  61765. +
  61766. + core_if->core_params->thr_ctl = val;
  61767. + return retval;
  61768. +}
  61769. +
  61770. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  61771. +{
  61772. + return core_if->core_params->thr_ctl;
  61773. +}
  61774. +
  61775. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61776. +{
  61777. + int retval = 0;
  61778. +
  61779. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61780. + DWC_WARN("Wrong value for lpm_enable\n");
  61781. + DWC_WARN("lpm_enable must be 0 or 1\n");
  61782. + return -DWC_E_INVALID;
  61783. + }
  61784. +
  61785. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  61786. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  61787. + DWC_ERROR
  61788. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  61789. + val);
  61790. + }
  61791. + val = 0;
  61792. + retval = -DWC_E_INVALID;
  61793. + }
  61794. +
  61795. + core_if->core_params->lpm_enable = val;
  61796. + return retval;
  61797. +}
  61798. +
  61799. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  61800. +{
  61801. + return core_if->core_params->lpm_enable;
  61802. +}
  61803. +
  61804. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  61805. +{
  61806. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  61807. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  61808. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  61809. + return -DWC_E_INVALID;
  61810. + }
  61811. +
  61812. + core_if->core_params->tx_thr_length = val;
  61813. + return 0;
  61814. +}
  61815. +
  61816. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  61817. +{
  61818. + return core_if->core_params->tx_thr_length;
  61819. +}
  61820. +
  61821. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  61822. +{
  61823. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  61824. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  61825. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  61826. + return -DWC_E_INVALID;
  61827. + }
  61828. +
  61829. + core_if->core_params->rx_thr_length = val;
  61830. + return 0;
  61831. +}
  61832. +
  61833. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  61834. +{
  61835. + return core_if->core_params->rx_thr_length;
  61836. +}
  61837. +
  61838. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  61839. +{
  61840. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  61841. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  61842. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  61843. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  61844. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  61845. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  61846. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  61847. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  61848. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  61849. + return -DWC_E_INVALID;
  61850. + }
  61851. + core_if->core_params->dma_burst_size = val;
  61852. + return 0;
  61853. +}
  61854. +
  61855. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  61856. +{
  61857. + return core_if->core_params->dma_burst_size;
  61858. +}
  61859. +
  61860. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61861. +{
  61862. + int retval = 0;
  61863. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61864. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  61865. + return -DWC_E_INVALID;
  61866. + }
  61867. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  61868. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  61869. + DWC_ERROR
  61870. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  61871. + val);
  61872. + }
  61873. + retval = -DWC_E_INVALID;
  61874. + val = 0;
  61875. + }
  61876. + core_if->core_params->pti_enable = val;
  61877. + return retval;
  61878. +}
  61879. +
  61880. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  61881. +{
  61882. + return core_if->core_params->pti_enable;
  61883. +}
  61884. +
  61885. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61886. +{
  61887. + int retval = 0;
  61888. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61889. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  61890. + return -DWC_E_INVALID;
  61891. + }
  61892. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  61893. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  61894. + DWC_ERROR
  61895. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  61896. + val);
  61897. + }
  61898. + retval = -DWC_E_INVALID;
  61899. + val = 0;
  61900. + }
  61901. + core_if->core_params->mpi_enable = val;
  61902. + return retval;
  61903. +}
  61904. +
  61905. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  61906. +{
  61907. + return core_if->core_params->mpi_enable;
  61908. +}
  61909. +
  61910. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61911. +{
  61912. + int retval = 0;
  61913. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61914. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  61915. + return -DWC_E_INVALID;
  61916. + }
  61917. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  61918. + if (dwc_otg_param_initialized
  61919. + (core_if->core_params->adp_supp_enable)) {
  61920. + DWC_ERROR
  61921. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  61922. + val);
  61923. + }
  61924. + retval = -DWC_E_INVALID;
  61925. + val = 0;
  61926. + }
  61927. + core_if->core_params->adp_supp_enable = val;
  61928. + /*Set OTG version 2.0 in case of enabling ADP*/
  61929. + if (val)
  61930. + dwc_otg_set_param_otg_ver(core_if, 1);
  61931. +
  61932. + return retval;
  61933. +}
  61934. +
  61935. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  61936. +{
  61937. + return core_if->core_params->adp_supp_enable;
  61938. +}
  61939. +
  61940. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  61941. +{
  61942. + int retval = 0;
  61943. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61944. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  61945. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  61946. + return -DWC_E_INVALID;
  61947. + }
  61948. +
  61949. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  61950. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  61951. + DWC_ERROR
  61952. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  61953. + val);
  61954. + }
  61955. + retval = -DWC_E_INVALID;
  61956. + val = 0;
  61957. + }
  61958. + core_if->core_params->ic_usb_cap = val;
  61959. + return retval;
  61960. +}
  61961. +
  61962. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  61963. +{
  61964. + return core_if->core_params->ic_usb_cap;
  61965. +}
  61966. +
  61967. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  61968. +{
  61969. + int retval = 0;
  61970. + int valid = 1;
  61971. +
  61972. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  61973. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  61974. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  61975. + return -DWC_E_INVALID;
  61976. + }
  61977. +
  61978. + if (val
  61979. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  61980. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  61981. + valid = 0;
  61982. + } else if (val
  61983. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  61984. + 4)) {
  61985. + valid = 0;
  61986. + }
  61987. + if (valid == 0) {
  61988. + if (dwc_otg_param_initialized
  61989. + (core_if->core_params->ahb_thr_ratio)) {
  61990. + DWC_ERROR
  61991. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  61992. + val);
  61993. + }
  61994. + retval = -DWC_E_INVALID;
  61995. + val = 0;
  61996. + }
  61997. +
  61998. + core_if->core_params->ahb_thr_ratio = val;
  61999. + return retval;
  62000. +}
  62001. +
  62002. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  62003. +{
  62004. + return core_if->core_params->ahb_thr_ratio;
  62005. +}
  62006. +
  62007. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  62008. +{
  62009. + int retval = 0;
  62010. + int valid = 1;
  62011. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  62012. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  62013. +
  62014. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  62015. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  62016. + DWC_WARN("power_down must be 0 - 2\n");
  62017. + return -DWC_E_INVALID;
  62018. + }
  62019. +
  62020. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  62021. + valid = 0;
  62022. + }
  62023. + if ((val == 3)
  62024. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  62025. + || (hwcfg4.b.xhiber == 0))) {
  62026. + valid = 0;
  62027. + }
  62028. + if (valid == 0) {
  62029. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  62030. + DWC_ERROR
  62031. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  62032. + val);
  62033. + }
  62034. + retval = -DWC_E_INVALID;
  62035. + val = 0;
  62036. + }
  62037. + core_if->core_params->power_down = val;
  62038. + return retval;
  62039. +}
  62040. +
  62041. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  62042. +{
  62043. + return core_if->core_params->power_down;
  62044. +}
  62045. +
  62046. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  62047. +{
  62048. + int retval = 0;
  62049. + int valid = 1;
  62050. +
  62051. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62052. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  62053. + DWC_WARN("reload_ctl must be 0 or 1\n");
  62054. + return -DWC_E_INVALID;
  62055. + }
  62056. +
  62057. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  62058. + valid = 0;
  62059. + }
  62060. + if (valid == 0) {
  62061. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  62062. + DWC_ERROR("%d invalid for parameter reload_ctl."
  62063. + "Check HW configuration.\n", val);
  62064. + }
  62065. + retval = -DWC_E_INVALID;
  62066. + val = 0;
  62067. + }
  62068. + core_if->core_params->reload_ctl = val;
  62069. + return retval;
  62070. +}
  62071. +
  62072. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  62073. +{
  62074. + return core_if->core_params->reload_ctl;
  62075. +}
  62076. +
  62077. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  62078. +{
  62079. + int retval = 0;
  62080. + int valid = 1;
  62081. +
  62082. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62083. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  62084. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  62085. + return -DWC_E_INVALID;
  62086. + }
  62087. +
  62088. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  62089. + !(core_if->core_params->dma_desc_enable))) {
  62090. + valid = 0;
  62091. + }
  62092. + if (valid == 0) {
  62093. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  62094. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  62095. + "Check HW configuration.\n", val);
  62096. + }
  62097. + retval = -DWC_E_INVALID;
  62098. + val = 0;
  62099. + }
  62100. + core_if->core_params->dev_out_nak = val;
  62101. + return retval;
  62102. +}
  62103. +
  62104. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  62105. +{
  62106. + return core_if->core_params->dev_out_nak;
  62107. +}
  62108. +
  62109. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  62110. +{
  62111. + int retval = 0;
  62112. + int valid = 1;
  62113. +
  62114. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62115. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  62116. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  62117. + return -DWC_E_INVALID;
  62118. + }
  62119. +
  62120. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  62121. + !(core_if->core_params->dma_desc_enable))) {
  62122. + valid = 0;
  62123. + }
  62124. + if (valid == 0) {
  62125. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  62126. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  62127. + "Check HW configuration.\n", val);
  62128. + }
  62129. + retval = -DWC_E_INVALID;
  62130. + val = 0;
  62131. + }
  62132. + core_if->core_params->cont_on_bna = val;
  62133. + return retval;
  62134. +}
  62135. +
  62136. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  62137. +{
  62138. + return core_if->core_params->cont_on_bna;
  62139. +}
  62140. +
  62141. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  62142. +{
  62143. + int retval = 0;
  62144. + int valid = 1;
  62145. +
  62146. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62147. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  62148. + DWC_WARN("ahb_single must be 0 or 1\n");
  62149. + return -DWC_E_INVALID;
  62150. + }
  62151. +
  62152. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  62153. + valid = 0;
  62154. + }
  62155. + if (valid == 0) {
  62156. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  62157. + DWC_ERROR("%d invalid for parameter ahb_single."
  62158. + "Check HW configuration.\n", val);
  62159. + }
  62160. + retval = -DWC_E_INVALID;
  62161. + val = 0;
  62162. + }
  62163. + core_if->core_params->ahb_single = val;
  62164. + return retval;
  62165. +}
  62166. +
  62167. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  62168. +{
  62169. + return core_if->core_params->ahb_single;
  62170. +}
  62171. +
  62172. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  62173. +{
  62174. + int retval = 0;
  62175. +
  62176. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  62177. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  62178. + DWC_WARN
  62179. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  62180. + return -DWC_E_INVALID;
  62181. + }
  62182. +
  62183. + core_if->core_params->otg_ver = val;
  62184. + return retval;
  62185. +}
  62186. +
  62187. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  62188. +{
  62189. + return core_if->core_params->otg_ver;
  62190. +}
  62191. +
  62192. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  62193. +{
  62194. + gotgctl_data_t otgctl;
  62195. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62196. + return otgctl.b.hstnegscs;
  62197. +}
  62198. +
  62199. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  62200. +{
  62201. + gotgctl_data_t otgctl;
  62202. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62203. + return otgctl.b.sesreqscs;
  62204. +}
  62205. +
  62206. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  62207. +{
  62208. + if(core_if->otg_ver == 0) {
  62209. + gotgctl_data_t otgctl;
  62210. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62211. + otgctl.b.hnpreq = val;
  62212. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  62213. + } else {
  62214. + core_if->otg_sts = val;
  62215. + }
  62216. +}
  62217. +
  62218. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  62219. +{
  62220. + return core_if->snpsid;
  62221. +}
  62222. +
  62223. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  62224. +{
  62225. + gintsts_data_t gintsts;
  62226. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  62227. + return gintsts.b.curmode;
  62228. +}
  62229. +
  62230. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  62231. +{
  62232. + gusbcfg_data_t usbcfg;
  62233. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62234. + return usbcfg.b.hnpcap;
  62235. +}
  62236. +
  62237. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  62238. +{
  62239. + gusbcfg_data_t usbcfg;
  62240. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62241. + usbcfg.b.hnpcap = val;
  62242. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  62243. +}
  62244. +
  62245. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  62246. +{
  62247. + gusbcfg_data_t usbcfg;
  62248. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62249. + return usbcfg.b.srpcap;
  62250. +}
  62251. +
  62252. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  62253. +{
  62254. + gusbcfg_data_t usbcfg;
  62255. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62256. + usbcfg.b.srpcap = val;
  62257. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  62258. +}
  62259. +
  62260. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  62261. +{
  62262. + dcfg_data_t dcfg;
  62263. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  62264. +
  62265. + dcfg.d32 = -1; //GRAYG
  62266. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  62267. + if (NULL == core_if)
  62268. + DWC_ERROR("reg request with NULL core_if\n");
  62269. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  62270. + core_if, core_if->dev_if);
  62271. + if (NULL == core_if->dev_if)
  62272. + DWC_ERROR("reg request with NULL dev_if\n");
  62273. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  62274. + "dev_global_regs(%p)\n", __func__,
  62275. + core_if, core_if->dev_if,
  62276. + core_if->dev_if->dev_global_regs);
  62277. + if (NULL == core_if->dev_if->dev_global_regs)
  62278. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  62279. + else {
  62280. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  62281. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  62282. + core_if, core_if->dev_if,
  62283. + core_if->dev_if->dev_global_regs,
  62284. + &core_if->dev_if->dev_global_regs->dcfg);
  62285. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62286. + }
  62287. + return dcfg.b.devspd;
  62288. +}
  62289. +
  62290. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  62291. +{
  62292. + dcfg_data_t dcfg;
  62293. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62294. + dcfg.b.devspd = val;
  62295. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  62296. +}
  62297. +
  62298. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  62299. +{
  62300. + hprt0_data_t hprt0;
  62301. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  62302. + return hprt0.b.prtconnsts;
  62303. +}
  62304. +
  62305. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  62306. +{
  62307. + dsts_data_t dsts;
  62308. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  62309. + return dsts.b.enumspd;
  62310. +}
  62311. +
  62312. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  62313. +{
  62314. + hprt0_data_t hprt0;
  62315. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  62316. + return hprt0.b.prtpwr;
  62317. +
  62318. +}
  62319. +
  62320. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  62321. +{
  62322. + return core_if->hibernation_suspend;
  62323. +}
  62324. +
  62325. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  62326. +{
  62327. + hprt0_data_t hprt0;
  62328. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62329. + hprt0.b.prtpwr = val;
  62330. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62331. +}
  62332. +
  62333. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  62334. +{
  62335. + hprt0_data_t hprt0;
  62336. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  62337. + return hprt0.b.prtsusp;
  62338. +
  62339. +}
  62340. +
  62341. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  62342. +{
  62343. + hprt0_data_t hprt0;
  62344. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62345. + hprt0.b.prtsusp = val;
  62346. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62347. +}
  62348. +
  62349. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  62350. +{
  62351. + hfir_data_t hfir;
  62352. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  62353. + return hfir.b.frint;
  62354. +
  62355. +}
  62356. +
  62357. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  62358. +{
  62359. + hfir_data_t hfir;
  62360. + uint32_t fram_int;
  62361. + fram_int = calc_frame_interval(core_if);
  62362. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  62363. + if (!core_if->core_params->reload_ctl) {
  62364. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  62365. + "not set to 1.\nShould load driver with reload_ctl=1"
  62366. + " module parameter\n");
  62367. + return;
  62368. + }
  62369. + switch (fram_int) {
  62370. + case 3750:
  62371. + if ((val < 3350) || (val > 4150)) {
  62372. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  62373. + "clock freq should be from 3350 to 4150\n");
  62374. + return;
  62375. + }
  62376. + break;
  62377. + case 30000:
  62378. + if ((val < 26820) || (val > 33180)) {
  62379. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  62380. + "clock freq should be from 26820 to 33180\n");
  62381. + return;
  62382. + }
  62383. + break;
  62384. + case 6000:
  62385. + if ((val < 5360) || (val > 6640)) {
  62386. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  62387. + "clock freq should be from 5360 to 6640\n");
  62388. + return;
  62389. + }
  62390. + break;
  62391. + case 48000:
  62392. + if ((val < 42912) || (val > 53088)) {
  62393. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  62394. + "clock freq should be from 42912 to 53088\n");
  62395. + return;
  62396. + }
  62397. + break;
  62398. + case 7500:
  62399. + if ((val < 6700) || (val > 8300)) {
  62400. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  62401. + "clock freq should be from 6700 to 8300\n");
  62402. + return;
  62403. + }
  62404. + break;
  62405. + case 60000:
  62406. + if ((val < 53640) || (val > 65536)) {
  62407. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  62408. + "clock freq should be from 53640 to 65536\n");
  62409. + return;
  62410. + }
  62411. + break;
  62412. + default:
  62413. + DWC_WARN("Unknown frame interval\n");
  62414. + return;
  62415. + break;
  62416. +
  62417. + }
  62418. + hfir.b.frint = val;
  62419. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  62420. +}
  62421. +
  62422. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  62423. +{
  62424. + hcfg_data_t hcfg;
  62425. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62426. + return hcfg.b.modechtimen;
  62427. +
  62428. +}
  62429. +
  62430. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  62431. +{
  62432. + hcfg_data_t hcfg;
  62433. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62434. + hcfg.b.modechtimen = val;
  62435. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  62436. +}
  62437. +
  62438. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  62439. +{
  62440. + hprt0_data_t hprt0;
  62441. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62442. + hprt0.b.prtres = val;
  62443. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62444. +}
  62445. +
  62446. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  62447. +{
  62448. + dctl_data_t dctl;
  62449. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  62450. + return dctl.b.rmtwkupsig;
  62451. +}
  62452. +
  62453. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  62454. +{
  62455. + glpmcfg_data_t lpmcfg;
  62456. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62457. +
  62458. + DWC_ASSERT(!
  62459. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  62460. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  62461. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  62462. +
  62463. + return lpmcfg.b.prt_sleep_sts;
  62464. +}
  62465. +
  62466. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  62467. +{
  62468. + glpmcfg_data_t lpmcfg;
  62469. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62470. + return lpmcfg.b.rem_wkup_en;
  62471. +}
  62472. +
  62473. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  62474. +{
  62475. + glpmcfg_data_t lpmcfg;
  62476. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62477. + return lpmcfg.b.appl_resp;
  62478. +}
  62479. +
  62480. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  62481. +{
  62482. + glpmcfg_data_t lpmcfg;
  62483. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62484. + lpmcfg.b.appl_resp = val;
  62485. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  62486. +}
  62487. +
  62488. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  62489. +{
  62490. + glpmcfg_data_t lpmcfg;
  62491. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62492. + return lpmcfg.b.hsic_connect;
  62493. +}
  62494. +
  62495. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  62496. +{
  62497. + glpmcfg_data_t lpmcfg;
  62498. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62499. + lpmcfg.b.hsic_connect = val;
  62500. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  62501. +}
  62502. +
  62503. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  62504. +{
  62505. + glpmcfg_data_t lpmcfg;
  62506. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62507. + return lpmcfg.b.inv_sel_hsic;
  62508. +
  62509. +}
  62510. +
  62511. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  62512. +{
  62513. + glpmcfg_data_t lpmcfg;
  62514. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62515. + lpmcfg.b.inv_sel_hsic = val;
  62516. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  62517. +}
  62518. +
  62519. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  62520. +{
  62521. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62522. +}
  62523. +
  62524. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  62525. +{
  62526. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  62527. +}
  62528. +
  62529. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  62530. +{
  62531. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62532. +}
  62533. +
  62534. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  62535. +{
  62536. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  62537. +}
  62538. +
  62539. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  62540. +{
  62541. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  62542. +}
  62543. +
  62544. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  62545. +{
  62546. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  62547. +}
  62548. +
  62549. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  62550. +{
  62551. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  62552. +}
  62553. +
  62554. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  62555. +{
  62556. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  62557. +}
  62558. +
  62559. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  62560. +{
  62561. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  62562. +}
  62563. +
  62564. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  62565. +{
  62566. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  62567. +}
  62568. +
  62569. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  62570. +{
  62571. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  62572. +}
  62573. +
  62574. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  62575. +{
  62576. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  62577. +}
  62578. +
  62579. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  62580. +{
  62581. + return DWC_READ_REG32(core_if->host_if->hprt0);
  62582. +
  62583. +}
  62584. +
  62585. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  62586. +{
  62587. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  62588. +}
  62589. +
  62590. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  62591. +{
  62592. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  62593. +}
  62594. +
  62595. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  62596. +{
  62597. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  62598. +}
  62599. +
  62600. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  62601. +{
  62602. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  62603. +}
  62604. +
  62605. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  62606. +{
  62607. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  62608. +}
  62609. +
  62610. +/**
  62611. + * Start the SRP timer to detect when the SRP does not complete within
  62612. + * 6 seconds.
  62613. + *
  62614. + * @param core_if the pointer to core_if strucure.
  62615. + */
  62616. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  62617. +{
  62618. + core_if->srp_timer_started = 1;
  62619. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  62620. +}
  62621. +
  62622. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  62623. +{
  62624. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  62625. + gotgctl_data_t mem;
  62626. + gotgctl_data_t val;
  62627. +
  62628. + val.d32 = DWC_READ_REG32(addr);
  62629. + if (val.b.sesreq) {
  62630. + DWC_ERROR("Session Request Already active!\n");
  62631. + return;
  62632. + }
  62633. +
  62634. + DWC_INFO("Session Request Initated\n"); //NOTICE
  62635. + mem.d32 = DWC_READ_REG32(addr);
  62636. + mem.b.sesreq = 1;
  62637. + DWC_WRITE_REG32(addr, mem.d32);
  62638. +
  62639. + /* Start the SRP timer */
  62640. + dwc_otg_pcd_start_srp_timer(core_if);
  62641. + return;
  62642. +}
  62643. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cil.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  62644. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cil.h 1970-01-01 01:00:00.000000000 +0100
  62645. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h 2014-04-24 15:35:04.173565776 +0200
  62646. @@ -0,0 +1,1464 @@
  62647. +/* ==========================================================================
  62648. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  62649. + * $Revision: #123 $
  62650. + * $Date: 2012/08/10 $
  62651. + * $Change: 2047372 $
  62652. + *
  62653. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  62654. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  62655. + * otherwise expressly agreed to in writing between Synopsys and you.
  62656. + *
  62657. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  62658. + * any End User Software License Agreement or Agreement for Licensed Product
  62659. + * with Synopsys or any supplement thereto. You are permitted to use and
  62660. + * redistribute this Software in source and binary forms, with or without
  62661. + * modification, provided that redistributions of source code must retain this
  62662. + * notice. You may not view, use, disclose, copy or distribute this file or
  62663. + * any information contained herein except pursuant to this license grant from
  62664. + * Synopsys. If you do not agree with this notice, including the disclaimer
  62665. + * below, then you are not authorized to use the Software.
  62666. + *
  62667. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  62668. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62669. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62670. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  62671. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  62672. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  62673. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  62674. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  62675. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  62676. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  62677. + * DAMAGE.
  62678. + * ========================================================================== */
  62679. +
  62680. +#if !defined(__DWC_CIL_H__)
  62681. +#define __DWC_CIL_H__
  62682. +
  62683. +#include "dwc_list.h"
  62684. +#include "dwc_otg_dbg.h"
  62685. +#include "dwc_otg_regs.h"
  62686. +
  62687. +#include "dwc_otg_core_if.h"
  62688. +#include "dwc_otg_adp.h"
  62689. +
  62690. +/**
  62691. + * @file
  62692. + * This file contains the interface to the Core Interface Layer.
  62693. + */
  62694. +
  62695. +#ifdef DWC_UTE_CFI
  62696. +
  62697. +#define MAX_DMA_DESCS_PER_EP 256
  62698. +
  62699. +/**
  62700. + * Enumeration for the data buffer mode
  62701. + */
  62702. +typedef enum _data_buffer_mode {
  62703. + BM_STANDARD = 0, /* data buffer is in normal mode */
  62704. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  62705. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  62706. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  62707. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  62708. +} data_buffer_mode_e;
  62709. +#endif //DWC_UTE_CFI
  62710. +
  62711. +/** Macros defined for DWC OTG HW Release version */
  62712. +
  62713. +#define OTG_CORE_REV_2_60a 0x4F54260A
  62714. +#define OTG_CORE_REV_2_71a 0x4F54271A
  62715. +#define OTG_CORE_REV_2_72a 0x4F54272A
  62716. +#define OTG_CORE_REV_2_80a 0x4F54280A
  62717. +#define OTG_CORE_REV_2_81a 0x4F54281A
  62718. +#define OTG_CORE_REV_2_90a 0x4F54290A
  62719. +#define OTG_CORE_REV_2_91a 0x4F54291A
  62720. +#define OTG_CORE_REV_2_92a 0x4F54292A
  62721. +#define OTG_CORE_REV_2_93a 0x4F54293A
  62722. +#define OTG_CORE_REV_2_94a 0x4F54294A
  62723. +#define OTG_CORE_REV_3_00a 0x4F54300A
  62724. +
  62725. +/**
  62726. + * Information for each ISOC packet.
  62727. + */
  62728. +typedef struct iso_pkt_info {
  62729. + uint32_t offset;
  62730. + uint32_t length;
  62731. + int32_t status;
  62732. +} iso_pkt_info_t;
  62733. +
  62734. +/**
  62735. + * The <code>dwc_ep</code> structure represents the state of a single
  62736. + * endpoint when acting in device mode. It contains the data items
  62737. + * needed for an endpoint to be activated and transfer packets.
  62738. + */
  62739. +typedef struct dwc_ep {
  62740. + /** EP number used for register address lookup */
  62741. + uint8_t num;
  62742. + /** EP direction 0 = OUT */
  62743. + unsigned is_in:1;
  62744. + /** EP active. */
  62745. + unsigned active:1;
  62746. +
  62747. + /**
  62748. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  62749. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  62750. + unsigned tx_fifo_num:4;
  62751. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  62752. + unsigned type:2;
  62753. +#define DWC_OTG_EP_TYPE_CONTROL 0
  62754. +#define DWC_OTG_EP_TYPE_ISOC 1
  62755. +#define DWC_OTG_EP_TYPE_BULK 2
  62756. +#define DWC_OTG_EP_TYPE_INTR 3
  62757. +
  62758. + /** DATA start PID for INTR and BULK EP */
  62759. + unsigned data_pid_start:1;
  62760. + /** Frame (even/odd) for ISOC EP */
  62761. + unsigned even_odd_frame:1;
  62762. + /** Max Packet bytes */
  62763. + unsigned maxpacket:11;
  62764. +
  62765. + /** Max Transfer size */
  62766. + uint32_t maxxfer;
  62767. +
  62768. + /** @name Transfer state */
  62769. + /** @{ */
  62770. +
  62771. + /**
  62772. + * Pointer to the beginning of the transfer buffer -- do not modify
  62773. + * during transfer.
  62774. + */
  62775. +
  62776. + dwc_dma_t dma_addr;
  62777. +
  62778. + dwc_dma_t dma_desc_addr;
  62779. + dwc_otg_dev_dma_desc_t *desc_addr;
  62780. +
  62781. + uint8_t *start_xfer_buff;
  62782. + /** pointer to the transfer buffer */
  62783. + uint8_t *xfer_buff;
  62784. + /** Number of bytes to transfer */
  62785. + unsigned xfer_len:19;
  62786. + /** Number of bytes transferred. */
  62787. + unsigned xfer_count:19;
  62788. + /** Sent ZLP */
  62789. + unsigned sent_zlp:1;
  62790. + /** Total len for control transfer */
  62791. + unsigned total_len:19;
  62792. +
  62793. + /** stall clear flag */
  62794. + unsigned stall_clear_flag:1;
  62795. +
  62796. + /** SETUP pkt cnt rollover flag for EP0 out*/
  62797. + unsigned stp_rollover;
  62798. +
  62799. +#ifdef DWC_UTE_CFI
  62800. + /* The buffer mode */
  62801. + data_buffer_mode_e buff_mode;
  62802. +
  62803. + /* The chain of DMA descriptors.
  62804. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  62805. + */
  62806. + dwc_otg_dma_desc_t *descs;
  62807. +
  62808. + /* The DMA address of the descriptors chain start */
  62809. + dma_addr_t descs_dma_addr;
  62810. + /** This variable stores the length of the last enqueued request */
  62811. + uint32_t cfi_req_len;
  62812. +#endif //DWC_UTE_CFI
  62813. +
  62814. +/** Max DMA Descriptor count for any EP */
  62815. +#define MAX_DMA_DESC_CNT 256
  62816. + /** Allocated DMA Desc count */
  62817. + uint32_t desc_cnt;
  62818. +
  62819. + /** bInterval */
  62820. + uint32_t bInterval;
  62821. + /** Next frame num to setup next ISOC transfer */
  62822. + uint32_t frame_num;
  62823. + /** Indicates SOF number overrun in DSTS */
  62824. + uint8_t frm_overrun;
  62825. +
  62826. +#ifdef DWC_UTE_PER_IO
  62827. + /** Next frame num for which will be setup DMA Desc */
  62828. + uint32_t xiso_frame_num;
  62829. + /** bInterval */
  62830. + uint32_t xiso_bInterval;
  62831. + /** Count of currently active transfers - shall be either 0 or 1 */
  62832. + int xiso_active_xfers;
  62833. + int xiso_queued_xfers;
  62834. +#endif
  62835. +#ifdef DWC_EN_ISOC
  62836. + /**
  62837. + * Variables specific for ISOC EPs
  62838. + *
  62839. + */
  62840. + /** DMA addresses of ISOC buffers */
  62841. + dwc_dma_t dma_addr0;
  62842. + dwc_dma_t dma_addr1;
  62843. +
  62844. + dwc_dma_t iso_dma_desc_addr;
  62845. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  62846. +
  62847. + /** pointer to the transfer buffers */
  62848. + uint8_t *xfer_buff0;
  62849. + uint8_t *xfer_buff1;
  62850. +
  62851. + /** number of ISOC Buffer is processing */
  62852. + uint32_t proc_buf_num;
  62853. + /** Interval of ISOC Buffer processing */
  62854. + uint32_t buf_proc_intrvl;
  62855. + /** Data size for regular frame */
  62856. + uint32_t data_per_frame;
  62857. +
  62858. + /* todo - pattern data support is to be implemented in the future */
  62859. + /** Data size for pattern frame */
  62860. + uint32_t data_pattern_frame;
  62861. + /** Frame number of pattern data */
  62862. + uint32_t sync_frame;
  62863. +
  62864. + /** bInterval */
  62865. + uint32_t bInterval;
  62866. + /** ISO Packet number per frame */
  62867. + uint32_t pkt_per_frm;
  62868. + /** Next frame num for which will be setup DMA Desc */
  62869. + uint32_t next_frame;
  62870. + /** Number of packets per buffer processing */
  62871. + uint32_t pkt_cnt;
  62872. + /** Info for all isoc packets */
  62873. + iso_pkt_info_t *pkt_info;
  62874. + /** current pkt number */
  62875. + uint32_t cur_pkt;
  62876. + /** current pkt number */
  62877. + uint8_t *cur_pkt_addr;
  62878. + /** current pkt number */
  62879. + uint32_t cur_pkt_dma_addr;
  62880. +#endif /* DWC_EN_ISOC */
  62881. +
  62882. +/** @} */
  62883. +} dwc_ep_t;
  62884. +
  62885. +/*
  62886. + * Reasons for halting a host channel.
  62887. + */
  62888. +typedef enum dwc_otg_halt_status {
  62889. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  62890. + DWC_OTG_HC_XFER_COMPLETE,
  62891. + DWC_OTG_HC_XFER_URB_COMPLETE,
  62892. + DWC_OTG_HC_XFER_ACK,
  62893. + DWC_OTG_HC_XFER_NAK,
  62894. + DWC_OTG_HC_XFER_NYET,
  62895. + DWC_OTG_HC_XFER_STALL,
  62896. + DWC_OTG_HC_XFER_XACT_ERR,
  62897. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  62898. + DWC_OTG_HC_XFER_BABBLE_ERR,
  62899. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  62900. + DWC_OTG_HC_XFER_AHB_ERR,
  62901. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  62902. + DWC_OTG_HC_XFER_URB_DEQUEUE
  62903. +} dwc_otg_halt_status_e;
  62904. +
  62905. +/**
  62906. + * Host channel descriptor. This structure represents the state of a single
  62907. + * host channel when acting in host mode. It contains the data items needed to
  62908. + * transfer packets to an endpoint via a host channel.
  62909. + */
  62910. +typedef struct dwc_hc {
  62911. + /** Host channel number used for register address lookup */
  62912. + uint8_t hc_num;
  62913. +
  62914. + /** Device to access */
  62915. + unsigned dev_addr:7;
  62916. +
  62917. + /** EP to access */
  62918. + unsigned ep_num:4;
  62919. +
  62920. + /** EP direction. 0: OUT, 1: IN */
  62921. + unsigned ep_is_in:1;
  62922. +
  62923. + /**
  62924. + * EP speed.
  62925. + * One of the following values:
  62926. + * - DWC_OTG_EP_SPEED_LOW
  62927. + * - DWC_OTG_EP_SPEED_FULL
  62928. + * - DWC_OTG_EP_SPEED_HIGH
  62929. + */
  62930. + unsigned speed:2;
  62931. +#define DWC_OTG_EP_SPEED_LOW 0
  62932. +#define DWC_OTG_EP_SPEED_FULL 1
  62933. +#define DWC_OTG_EP_SPEED_HIGH 2
  62934. +
  62935. + /**
  62936. + * Endpoint type.
  62937. + * One of the following values:
  62938. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  62939. + * - DWC_OTG_EP_TYPE_ISOC: 1
  62940. + * - DWC_OTG_EP_TYPE_BULK: 2
  62941. + * - DWC_OTG_EP_TYPE_INTR: 3
  62942. + */
  62943. + unsigned ep_type:2;
  62944. +
  62945. + /** Max packet size in bytes */
  62946. + unsigned max_packet:11;
  62947. +
  62948. + /**
  62949. + * PID for initial transaction.
  62950. + * 0: DATA0,<br>
  62951. + * 1: DATA2,<br>
  62952. + * 2: DATA1,<br>
  62953. + * 3: MDATA (non-Control EP),
  62954. + * SETUP (Control EP)
  62955. + */
  62956. + unsigned data_pid_start:2;
  62957. +#define DWC_OTG_HC_PID_DATA0 0
  62958. +#define DWC_OTG_HC_PID_DATA2 1
  62959. +#define DWC_OTG_HC_PID_DATA1 2
  62960. +#define DWC_OTG_HC_PID_MDATA 3
  62961. +#define DWC_OTG_HC_PID_SETUP 3
  62962. +
  62963. + /** Number of periodic transactions per (micro)frame */
  62964. + unsigned multi_count:2;
  62965. +
  62966. + /** @name Transfer State */
  62967. + /** @{ */
  62968. +
  62969. + /** Pointer to the current transfer buffer position. */
  62970. + uint8_t *xfer_buff;
  62971. + /**
  62972. + * In Buffer DMA mode this buffer will be used
  62973. + * if xfer_buff is not DWORD aligned.
  62974. + */
  62975. + dwc_dma_t align_buff;
  62976. + /** Total number of bytes to transfer. */
  62977. + uint32_t xfer_len;
  62978. + /** Number of bytes transferred so far. */
  62979. + uint32_t xfer_count;
  62980. + /** Packet count at start of transfer.*/
  62981. + uint16_t start_pkt_count;
  62982. +
  62983. + /**
  62984. + * Flag to indicate whether the transfer has been started. Set to 1 if
  62985. + * it has been started, 0 otherwise.
  62986. + */
  62987. + uint8_t xfer_started;
  62988. +
  62989. + /**
  62990. + * Set to 1 to indicate that a PING request should be issued on this
  62991. + * channel. If 0, process normally.
  62992. + */
  62993. + uint8_t do_ping;
  62994. +
  62995. + /**
  62996. + * Set to 1 to indicate that the error count for this transaction is
  62997. + * non-zero. Set to 0 if the error count is 0.
  62998. + */
  62999. + uint8_t error_state;
  63000. +
  63001. + /**
  63002. + * Set to 1 to indicate that this channel should be halted the next
  63003. + * time a request is queued for the channel. This is necessary in
  63004. + * slave mode if no request queue space is available when an attempt
  63005. + * is made to halt the channel.
  63006. + */
  63007. + uint8_t halt_on_queue;
  63008. +
  63009. + /**
  63010. + * Set to 1 if the host channel has been halted, but the core is not
  63011. + * finished flushing queued requests. Otherwise 0.
  63012. + */
  63013. + uint8_t halt_pending;
  63014. +
  63015. + /**
  63016. + * Reason for halting the host channel.
  63017. + */
  63018. + dwc_otg_halt_status_e halt_status;
  63019. +
  63020. + /*
  63021. + * Split settings for the host channel
  63022. + */
  63023. + uint8_t do_split; /**< Enable split for the channel */
  63024. + uint8_t complete_split; /**< Enable complete split */
  63025. + uint8_t hub_addr; /**< Address of high speed hub */
  63026. +
  63027. + uint8_t port_addr; /**< Port of the low/full speed device */
  63028. + /** Split transaction position
  63029. + * One of the following values:
  63030. + * - DWC_HCSPLIT_XACTPOS_MID
  63031. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  63032. + * - DWC_HCSPLIT_XACTPOS_END
  63033. + * - DWC_HCSPLIT_XACTPOS_ALL */
  63034. + uint8_t xact_pos;
  63035. +
  63036. + /** Set when the host channel does a short read. */
  63037. + uint8_t short_read;
  63038. +
  63039. + /**
  63040. + * Number of requests issued for this channel since it was assigned to
  63041. + * the current transfer (not counting PINGs).
  63042. + */
  63043. + uint8_t requests;
  63044. +
  63045. + /**
  63046. + * Queue Head for the transfer being processed by this channel.
  63047. + */
  63048. + struct dwc_otg_qh *qh;
  63049. +
  63050. + /** @} */
  63051. +
  63052. + /** Entry in list of host channels. */
  63053. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  63054. +
  63055. + /** @name Descriptor DMA support */
  63056. + /** @{ */
  63057. +
  63058. + /** Number of Transfer Descriptors */
  63059. + uint16_t ntd;
  63060. +
  63061. + /** Descriptor List DMA address */
  63062. + dwc_dma_t desc_list_addr;
  63063. +
  63064. + /** Scheduling micro-frame bitmap. */
  63065. + uint8_t schinfo;
  63066. +
  63067. + /** @} */
  63068. +} dwc_hc_t;
  63069. +
  63070. +/**
  63071. + * The following parameters may be specified when starting the module. These
  63072. + * parameters define how the DWC_otg controller should be configured.
  63073. + */
  63074. +typedef struct dwc_otg_core_params {
  63075. + int32_t opt;
  63076. +
  63077. + /**
  63078. + * Specifies the OTG capabilities. The driver will automatically
  63079. + * detect the value for this parameter if none is specified.
  63080. + * 0 - HNP and SRP capable (default)
  63081. + * 1 - SRP Only capable
  63082. + * 2 - No HNP/SRP capable
  63083. + */
  63084. + int32_t otg_cap;
  63085. +
  63086. + /**
  63087. + * Specifies whether to use slave or DMA mode for accessing the data
  63088. + * FIFOs. The driver will automatically detect the value for this
  63089. + * parameter if none is specified.
  63090. + * 0 - Slave
  63091. + * 1 - DMA (default, if available)
  63092. + */
  63093. + int32_t dma_enable;
  63094. +
  63095. + /**
  63096. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  63097. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  63098. + * will automatically detect the value for this if none is specified.
  63099. + * 0 - address DMA
  63100. + * 1 - DMA Descriptor(default, if available)
  63101. + */
  63102. + int32_t dma_desc_enable;
  63103. + /** The DMA Burst size (applicable only for External DMA
  63104. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  63105. + */
  63106. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  63107. +
  63108. + /**
  63109. + * Specifies the maximum speed of operation in host and device mode.
  63110. + * The actual speed depends on the speed of the attached device and
  63111. + * the value of phy_type. The actual speed depends on the speed of the
  63112. + * attached device.
  63113. + * 0 - High Speed (default)
  63114. + * 1 - Full Speed
  63115. + */
  63116. + int32_t speed;
  63117. + /** Specifies whether low power mode is supported when attached
  63118. + * to a Full Speed or Low Speed device in host mode.
  63119. + * 0 - Don't support low power mode (default)
  63120. + * 1 - Support low power mode
  63121. + */
  63122. + int32_t host_support_fs_ls_low_power;
  63123. +
  63124. + /** Specifies the PHY clock rate in low power mode when connected to a
  63125. + * Low Speed device in host mode. This parameter is applicable only if
  63126. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  63127. + * then defaults to 6 MHZ otherwise 48 MHZ.
  63128. + *
  63129. + * 0 - 48 MHz
  63130. + * 1 - 6 MHz
  63131. + */
  63132. + int32_t host_ls_low_power_phy_clk;
  63133. +
  63134. + /**
  63135. + * 0 - Use cC FIFO size parameters
  63136. + * 1 - Allow dynamic FIFO sizing (default)
  63137. + */
  63138. + int32_t enable_dynamic_fifo;
  63139. +
  63140. + /** Total number of 4-byte words in the data FIFO memory. This
  63141. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  63142. + * Tx FIFOs.
  63143. + * 32 to 32768 (default 8192)
  63144. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  63145. + */
  63146. + int32_t data_fifo_size;
  63147. +
  63148. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  63149. + * FIFO sizing is enabled.
  63150. + * 16 to 32768 (default 1064)
  63151. + */
  63152. + int32_t dev_rx_fifo_size;
  63153. +
  63154. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  63155. + * when dynamic FIFO sizing is enabled.
  63156. + * 16 to 32768 (default 1024)
  63157. + */
  63158. + int32_t dev_nperio_tx_fifo_size;
  63159. +
  63160. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  63161. + * mode when dynamic FIFO sizing is enabled.
  63162. + * 4 to 768 (default 256)
  63163. + */
  63164. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  63165. +
  63166. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  63167. + * FIFO sizing is enabled.
  63168. + * 16 to 32768 (default 1024)
  63169. + */
  63170. + int32_t host_rx_fifo_size;
  63171. +
  63172. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  63173. + * when Dynamic FIFO sizing is enabled in the core.
  63174. + * 16 to 32768 (default 1024)
  63175. + */
  63176. + int32_t host_nperio_tx_fifo_size;
  63177. +
  63178. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  63179. + * FIFO sizing is enabled.
  63180. + * 16 to 32768 (default 1024)
  63181. + */
  63182. + int32_t host_perio_tx_fifo_size;
  63183. +
  63184. + /** The maximum transfer size supported in bytes.
  63185. + * 2047 to 65,535 (default 65,535)
  63186. + */
  63187. + int32_t max_transfer_size;
  63188. +
  63189. + /** The maximum number of packets in a transfer.
  63190. + * 15 to 511 (default 511)
  63191. + */
  63192. + int32_t max_packet_count;
  63193. +
  63194. + /** The number of host channel registers to use.
  63195. + * 1 to 16 (default 12)
  63196. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  63197. + */
  63198. + int32_t host_channels;
  63199. +
  63200. + /** The number of endpoints in addition to EP0 available for device
  63201. + * mode operations.
  63202. + * 1 to 15 (default 6 IN and OUT)
  63203. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  63204. + * endpoints in addition to EP0.
  63205. + */
  63206. + int32_t dev_endpoints;
  63207. +
  63208. + /**
  63209. + * Specifies the type of PHY interface to use. By default, the driver
  63210. + * will automatically detect the phy_type.
  63211. + *
  63212. + * 0 - Full Speed PHY
  63213. + * 1 - UTMI+ (default)
  63214. + * 2 - ULPI
  63215. + */
  63216. + int32_t phy_type;
  63217. +
  63218. + /**
  63219. + * Specifies the UTMI+ Data Width. This parameter is
  63220. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  63221. + * PHY_TYPE, this parameter indicates the data width between
  63222. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  63223. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  63224. + * to "8 and 16 bits", meaning that the core has been
  63225. + * configured to work at either data path width.
  63226. + *
  63227. + * 8 or 16 bits (default 16)
  63228. + */
  63229. + int32_t phy_utmi_width;
  63230. +
  63231. + /**
  63232. + * Specifies whether the ULPI operates at double or single
  63233. + * data rate. This parameter is only applicable if PHY_TYPE is
  63234. + * ULPI.
  63235. + *
  63236. + * 0 - single data rate ULPI interface with 8 bit wide data
  63237. + * bus (default)
  63238. + * 1 - double data rate ULPI interface with 4 bit wide data
  63239. + * bus
  63240. + */
  63241. + int32_t phy_ulpi_ddr;
  63242. +
  63243. + /**
  63244. + * Specifies whether to use the internal or external supply to
  63245. + * drive the vbus with a ULPI phy.
  63246. + */
  63247. + int32_t phy_ulpi_ext_vbus;
  63248. +
  63249. + /**
  63250. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  63251. + * parameter is only applicable if PHY_TYPE is FS.
  63252. + * 0 - No (default)
  63253. + * 1 - Yes
  63254. + */
  63255. + int32_t i2c_enable;
  63256. +
  63257. + int32_t ulpi_fs_ls;
  63258. +
  63259. + int32_t ts_dline;
  63260. +
  63261. + /**
  63262. + * Specifies whether dedicated transmit FIFOs are
  63263. + * enabled for non periodic IN endpoints in device mode
  63264. + * 0 - No
  63265. + * 1 - Yes
  63266. + */
  63267. + int32_t en_multiple_tx_fifo;
  63268. +
  63269. + /** Number of 4-byte words in each of the Tx FIFOs in device
  63270. + * mode when dynamic FIFO sizing is enabled.
  63271. + * 4 to 768 (default 256)
  63272. + */
  63273. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  63274. +
  63275. + /** Thresholding enable flag-
  63276. + * bit 0 - enable non-ISO Tx thresholding
  63277. + * bit 1 - enable ISO Tx thresholding
  63278. + * bit 2 - enable Rx thresholding
  63279. + */
  63280. + uint32_t thr_ctl;
  63281. +
  63282. + /** Thresholding length for Tx
  63283. + * FIFOs in 32 bit DWORDs
  63284. + */
  63285. + uint32_t tx_thr_length;
  63286. +
  63287. + /** Thresholding length for Rx
  63288. + * FIFOs in 32 bit DWORDs
  63289. + */
  63290. + uint32_t rx_thr_length;
  63291. +
  63292. + /**
  63293. + * Specifies whether LPM (Link Power Management) support is enabled
  63294. + */
  63295. + int32_t lpm_enable;
  63296. +
  63297. + /** Per Transfer Interrupt
  63298. + * mode enable flag
  63299. + * 1 - Enabled
  63300. + * 0 - Disabled
  63301. + */
  63302. + int32_t pti_enable;
  63303. +
  63304. + /** Multi Processor Interrupt
  63305. + * mode enable flag
  63306. + * 1 - Enabled
  63307. + * 0 - Disabled
  63308. + */
  63309. + int32_t mpi_enable;
  63310. +
  63311. + /** IS_USB Capability
  63312. + * 1 - Enabled
  63313. + * 0 - Disabled
  63314. + */
  63315. + int32_t ic_usb_cap;
  63316. +
  63317. + /** AHB Threshold Ratio
  63318. + * 2'b00 AHB Threshold = MAC Threshold
  63319. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  63320. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  63321. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  63322. + */
  63323. + int32_t ahb_thr_ratio;
  63324. +
  63325. + /** ADP Support
  63326. + * 1 - Enabled
  63327. + * 0 - Disabled
  63328. + */
  63329. + int32_t adp_supp_enable;
  63330. +
  63331. + /** HFIR Reload Control
  63332. + * 0 - The HFIR cannot be reloaded dynamically.
  63333. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  63334. + */
  63335. + int32_t reload_ctl;
  63336. +
  63337. + /** DCFG: Enable device Out NAK
  63338. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  63339. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  63340. + */
  63341. + int32_t dev_out_nak;
  63342. +
  63343. + /** DCFG: Enable Continue on BNA
  63344. + * After receiving BNA interrupt the core disables the endpoint,when the
  63345. + * endpoint is re-enabled by the application the core starts processing
  63346. + * 0 - from the DOEPDMA descriptor
  63347. + * 1 - from the descriptor which received the BNA.
  63348. + */
  63349. + int32_t cont_on_bna;
  63350. +
  63351. + /** GAHBCFG: AHB Single Support
  63352. + * This bit when programmed supports SINGLE transfers for remainder
  63353. + * data in a transfer for DMA mode of operation.
  63354. + * 0 - in this case the remainder data will be sent using INCR burst size.
  63355. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  63356. + */
  63357. + int32_t ahb_single;
  63358. +
  63359. + /** Core Power down mode
  63360. + * 0 - No Power Down is enabled
  63361. + * 1 - Reserved
  63362. + * 2 - Complete Power Down (Hibernation)
  63363. + */
  63364. + int32_t power_down;
  63365. +
  63366. + /** OTG revision supported
  63367. + * 0 - OTG 1.3 revision
  63368. + * 1 - OTG 2.0 revision
  63369. + */
  63370. + int32_t otg_ver;
  63371. +
  63372. +} dwc_otg_core_params_t;
  63373. +
  63374. +#ifdef DEBUG
  63375. +struct dwc_otg_core_if;
  63376. +typedef struct hc_xfer_info {
  63377. + struct dwc_otg_core_if *core_if;
  63378. + dwc_hc_t *hc;
  63379. +} hc_xfer_info_t;
  63380. +#endif
  63381. +
  63382. +typedef struct ep_xfer_info {
  63383. + struct dwc_otg_core_if *core_if;
  63384. + dwc_ep_t *ep;
  63385. + uint8_t state;
  63386. +} ep_xfer_info_t;
  63387. +/*
  63388. + * Device States
  63389. + */
  63390. +typedef enum dwc_otg_lx_state {
  63391. + /** On state */
  63392. + DWC_OTG_L0,
  63393. + /** LPM sleep state*/
  63394. + DWC_OTG_L1,
  63395. + /** USB suspend state*/
  63396. + DWC_OTG_L2,
  63397. + /** Off state*/
  63398. + DWC_OTG_L3
  63399. +} dwc_otg_lx_state_e;
  63400. +
  63401. +struct dwc_otg_global_regs_backup {
  63402. + uint32_t gotgctl_local;
  63403. + uint32_t gintmsk_local;
  63404. + uint32_t gahbcfg_local;
  63405. + uint32_t gusbcfg_local;
  63406. + uint32_t grxfsiz_local;
  63407. + uint32_t gnptxfsiz_local;
  63408. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63409. + uint32_t glpmcfg_local;
  63410. +#endif
  63411. + uint32_t gi2cctl_local;
  63412. + uint32_t hptxfsiz_local;
  63413. + uint32_t pcgcctl_local;
  63414. + uint32_t gdfifocfg_local;
  63415. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  63416. + uint32_t gpwrdn_local;
  63417. + uint32_t xhib_pcgcctl;
  63418. + uint32_t xhib_gpwrdn;
  63419. +};
  63420. +
  63421. +struct dwc_otg_host_regs_backup {
  63422. + uint32_t hcfg_local;
  63423. + uint32_t haintmsk_local;
  63424. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  63425. + uint32_t hprt0_local;
  63426. + uint32_t hfir_local;
  63427. +};
  63428. +
  63429. +struct dwc_otg_dev_regs_backup {
  63430. + uint32_t dcfg;
  63431. + uint32_t dctl;
  63432. + uint32_t daintmsk;
  63433. + uint32_t diepmsk;
  63434. + uint32_t doepmsk;
  63435. + uint32_t diepctl[MAX_EPS_CHANNELS];
  63436. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  63437. + uint32_t diepdma[MAX_EPS_CHANNELS];
  63438. +};
  63439. +/**
  63440. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  63441. + * the DWC_otg controller acting in either host or device mode. It
  63442. + * represents the programming view of the controller as a whole.
  63443. + */
  63444. +struct dwc_otg_core_if {
  63445. + /** Parameters that define how the core should be configured.*/
  63446. + dwc_otg_core_params_t *core_params;
  63447. +
  63448. + /** Core Global registers starting at offset 000h. */
  63449. + dwc_otg_core_global_regs_t *core_global_regs;
  63450. +
  63451. + /** Device-specific information */
  63452. + dwc_otg_dev_if_t *dev_if;
  63453. + /** Host-specific information */
  63454. + dwc_otg_host_if_t *host_if;
  63455. +
  63456. + /** Value from SNPSID register */
  63457. + uint32_t snpsid;
  63458. +
  63459. + /*
  63460. + * Set to 1 if the core PHY interface bits in USBCFG have been
  63461. + * initialized.
  63462. + */
  63463. + uint8_t phy_init_done;
  63464. +
  63465. + /*
  63466. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  63467. + */
  63468. + uint8_t srp_success;
  63469. + uint8_t srp_timer_started;
  63470. + /** Timer for SRP. If it expires before SRP is successful
  63471. + * clear the SRP. */
  63472. + dwc_timer_t *srp_timer;
  63473. +
  63474. +#ifdef DWC_DEV_SRPCAP
  63475. + /* This timer is needed to power on the hibernated host core if SRP is not
  63476. + * initiated on connected SRP capable device for limited period of time
  63477. + */
  63478. + uint8_t pwron_timer_started;
  63479. + dwc_timer_t *pwron_timer;
  63480. +#endif
  63481. + /* Common configuration information */
  63482. + /** Power and Clock Gating Control Register */
  63483. + volatile uint32_t *pcgcctl;
  63484. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  63485. +
  63486. + /** Push/pop addresses for endpoints or host channels.*/
  63487. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  63488. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  63489. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  63490. +
  63491. + /** Total RAM for FIFOs (Bytes) */
  63492. + uint16_t total_fifo_size;
  63493. + /** Size of Rx FIFO (Bytes) */
  63494. + uint16_t rx_fifo_size;
  63495. + /** Size of Non-periodic Tx FIFO (Bytes) */
  63496. + uint16_t nperio_tx_fifo_size;
  63497. +
  63498. + /** 1 if DMA is enabled, 0 otherwise. */
  63499. + uint8_t dma_enable;
  63500. +
  63501. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  63502. + uint8_t dma_desc_enable;
  63503. +
  63504. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  63505. + uint8_t pti_enh_enable;
  63506. +
  63507. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  63508. + uint8_t multiproc_int_enable;
  63509. +
  63510. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  63511. + uint8_t en_multiple_tx_fifo;
  63512. +
  63513. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  63514. + * process of being queued */
  63515. + uint8_t queuing_high_bandwidth;
  63516. +
  63517. + /** Hardware Configuration -- stored here for convenience.*/
  63518. + hwcfg1_data_t hwcfg1;
  63519. + hwcfg2_data_t hwcfg2;
  63520. + hwcfg3_data_t hwcfg3;
  63521. + hwcfg4_data_t hwcfg4;
  63522. + fifosize_data_t hptxfsiz;
  63523. +
  63524. + /** Host and Device Configuration -- stored here for convenience.*/
  63525. + hcfg_data_t hcfg;
  63526. + dcfg_data_t dcfg;
  63527. +
  63528. + /** The operational State, during transations
  63529. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  63530. + * match the core but allows the software to determine
  63531. + * transitions.
  63532. + */
  63533. + uint8_t op_state;
  63534. +
  63535. + /**
  63536. + * Set to 1 if the HCD needs to be restarted on a session request
  63537. + * interrupt. This is required if no connector ID status change has
  63538. + * occurred since the HCD was last disconnected.
  63539. + */
  63540. + uint8_t restart_hcd_on_session_req;
  63541. +
  63542. + /** HCD callbacks */
  63543. + /** A-Device is a_host */
  63544. +#define A_HOST (1)
  63545. + /** A-Device is a_suspend */
  63546. +#define A_SUSPEND (2)
  63547. + /** A-Device is a_peripherial */
  63548. +#define A_PERIPHERAL (3)
  63549. + /** B-Device is operating as a Peripheral. */
  63550. +#define B_PERIPHERAL (4)
  63551. + /** B-Device is operating as a Host. */
  63552. +#define B_HOST (5)
  63553. +
  63554. + /** HCD callbacks */
  63555. + struct dwc_otg_cil_callbacks *hcd_cb;
  63556. + /** PCD callbacks */
  63557. + struct dwc_otg_cil_callbacks *pcd_cb;
  63558. +
  63559. + /** Device mode Periodic Tx FIFO Mask */
  63560. + uint32_t p_tx_msk;
  63561. + /** Device mode Periodic Tx FIFO Mask */
  63562. + uint32_t tx_msk;
  63563. +
  63564. + /** Workqueue object used for handling several interrupts */
  63565. + dwc_workq_t *wq_otg;
  63566. +
  63567. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  63568. + dwc_timer_t *wkp_timer;
  63569. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  63570. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  63571. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  63572. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  63573. +#ifdef DEBUG
  63574. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  63575. +
  63576. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  63577. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  63578. +
  63579. + uint32_t hfnum_7_samples;
  63580. + uint64_t hfnum_7_frrem_accum;
  63581. + uint32_t hfnum_0_samples;
  63582. + uint64_t hfnum_0_frrem_accum;
  63583. + uint32_t hfnum_other_samples;
  63584. + uint64_t hfnum_other_frrem_accum;
  63585. +#endif
  63586. +
  63587. +#ifdef DWC_UTE_CFI
  63588. + uint16_t pwron_rxfsiz;
  63589. + uint16_t pwron_gnptxfsiz;
  63590. + uint16_t pwron_txfsiz[15];
  63591. +
  63592. + uint16_t init_rxfsiz;
  63593. + uint16_t init_gnptxfsiz;
  63594. + uint16_t init_txfsiz[15];
  63595. +#endif
  63596. +
  63597. + /** Lx state of device */
  63598. + dwc_otg_lx_state_e lx_state;
  63599. +
  63600. + /** Saved Core Global registers */
  63601. + struct dwc_otg_global_regs_backup *gr_backup;
  63602. + /** Saved Host registers */
  63603. + struct dwc_otg_host_regs_backup *hr_backup;
  63604. + /** Saved Device registers */
  63605. + struct dwc_otg_dev_regs_backup *dr_backup;
  63606. +
  63607. + /** Power Down Enable */
  63608. + uint32_t power_down;
  63609. +
  63610. + /** ADP support Enable */
  63611. + uint32_t adp_enable;
  63612. +
  63613. + /** ADP structure object */
  63614. + dwc_otg_adp_t adp;
  63615. +
  63616. + /** hibernation/suspend flag */
  63617. + int hibernation_suspend;
  63618. +
  63619. + /** Device mode extended hibernation flag */
  63620. + int xhib;
  63621. +
  63622. + /** OTG revision supported */
  63623. + uint32_t otg_ver;
  63624. +
  63625. + /** OTG status flag used for HNP polling */
  63626. + uint8_t otg_sts;
  63627. +
  63628. + /** Pointer to either hcd->lock or pcd->lock */
  63629. + dwc_spinlock_t *lock;
  63630. +
  63631. + /** Start predict NextEP based on Learning Queue if equal 1,
  63632. + * also used as counter of disabled NP IN EP's */
  63633. + uint8_t start_predict;
  63634. +
  63635. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  63636. + * active, 0xff otherwise */
  63637. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  63638. +
  63639. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  63640. + uint8_t first_in_nextep_seq;
  63641. +
  63642. + /** Frame number while entering to ISR - needed for ISOCs **/
  63643. + uint32_t frame_num;
  63644. +
  63645. +};
  63646. +
  63647. +#ifdef DEBUG
  63648. +/*
  63649. + * This function is called when transfer is timed out.
  63650. + */
  63651. +extern void hc_xfer_timeout(void *ptr);
  63652. +#endif
  63653. +
  63654. +/*
  63655. + * This function is called when transfer is timed out on endpoint.
  63656. + */
  63657. +extern void ep_xfer_timeout(void *ptr);
  63658. +
  63659. +/*
  63660. + * The following functions are functions for works
  63661. + * using during handling some interrupts
  63662. + */
  63663. +extern void w_conn_id_status_change(void *p);
  63664. +
  63665. +extern void w_wakeup_detected(void *p);
  63666. +
  63667. +/** Saves global register values into system memory. */
  63668. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  63669. +/** Saves device register values into system memory. */
  63670. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  63671. +/** Saves host register values into system memory. */
  63672. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  63673. +/** Restore global register values. */
  63674. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  63675. +/** Restore host register values. */
  63676. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  63677. +/** Restore device register values. */
  63678. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  63679. + int rem_wakeup);
  63680. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  63681. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  63682. + int is_host);
  63683. +
  63684. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  63685. + int restore_mode, int reset);
  63686. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  63687. + int rem_wakeup, int reset);
  63688. +
  63689. +/*
  63690. + * The following functions support initialization of the CIL driver component
  63691. + * and the DWC_otg controller.
  63692. + */
  63693. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  63694. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  63695. +
  63696. +/** @name Device CIL Functions
  63697. + * The following functions support managing the DWC_otg controller in device
  63698. + * mode.
  63699. + */
  63700. +/**@{*/
  63701. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  63702. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  63703. + uint32_t * _dest);
  63704. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  63705. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63706. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63707. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63708. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  63709. + dwc_ep_t * _ep);
  63710. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  63711. + dwc_ep_t * _ep);
  63712. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  63713. + dwc_ep_t * _ep);
  63714. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  63715. + dwc_ep_t * _ep);
  63716. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  63717. + dwc_ep_t * _ep, int _dma);
  63718. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63719. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  63720. + dwc_ep_t * _ep);
  63721. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  63722. +
  63723. +#ifdef DWC_EN_ISOC
  63724. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  63725. + dwc_ep_t * ep);
  63726. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  63727. + dwc_ep_t * ep);
  63728. +#endif /* DWC_EN_ISOC */
  63729. +/**@}*/
  63730. +
  63731. +/** @name Host CIL Functions
  63732. + * The following functions support managing the DWC_otg controller in host
  63733. + * mode.
  63734. + */
  63735. +/**@{*/
  63736. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63737. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  63738. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  63739. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63740. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  63741. + dwc_hc_t * _hc);
  63742. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  63743. + dwc_hc_t * _hc);
  63744. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63745. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  63746. + dwc_hc_t * _hc);
  63747. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  63748. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  63749. +
  63750. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  63751. + dwc_hc_t * hc);
  63752. +
  63753. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  63754. +
  63755. +/* Macro used to clear one channel interrupt */
  63756. +#define clear_hc_int(_hc_regs_, _intr_) \
  63757. +do { \
  63758. + hcint_data_t hcint_clear = {.d32 = 0}; \
  63759. + hcint_clear.b._intr_ = 1; \
  63760. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  63761. +} while (0)
  63762. +
  63763. +/*
  63764. + * Macro used to disable one channel interrupt. Channel interrupts are
  63765. + * disabled when the channel is halted or released by the interrupt handler.
  63766. + * There is no need to handle further interrupts of that type until the
  63767. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  63768. + * because the channel structures are cleaned up when the channel is released.
  63769. + */
  63770. +#define disable_hc_int(_hc_regs_, _intr_) \
  63771. +do { \
  63772. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  63773. + hcintmsk.b._intr_ = 1; \
  63774. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  63775. +} while (0)
  63776. +
  63777. +/**
  63778. + * This function Reads HPRT0 in preparation to modify. It keeps the
  63779. + * WC bits 0 so that if they are read as 1, they won't clear when you
  63780. + * write it back
  63781. + */
  63782. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  63783. +{
  63784. + hprt0_data_t hprt0;
  63785. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  63786. + hprt0.b.prtena = 0;
  63787. + hprt0.b.prtconndet = 0;
  63788. + hprt0.b.prtenchng = 0;
  63789. + hprt0.b.prtovrcurrchng = 0;
  63790. + return hprt0.d32;
  63791. +}
  63792. +
  63793. +/**@}*/
  63794. +
  63795. +/** @name Common CIL Functions
  63796. + * The following functions support managing the DWC_otg controller in either
  63797. + * device or host mode.
  63798. + */
  63799. +/**@{*/
  63800. +
  63801. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  63802. + uint8_t * dest, uint16_t bytes);
  63803. +
  63804. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  63805. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  63806. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  63807. +
  63808. +/**
  63809. + * This function returns the Core Interrupt register.
  63810. + */
  63811. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  63812. +{
  63813. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  63814. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  63815. +}
  63816. +
  63817. +/**
  63818. + * This function returns the OTG Interrupt register.
  63819. + */
  63820. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  63821. +{
  63822. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  63823. +}
  63824. +
  63825. +/**
  63826. + * This function reads the Device All Endpoints Interrupt register and
  63827. + * returns the IN endpoint interrupt bits.
  63828. + */
  63829. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  63830. + core_if)
  63831. +{
  63832. +
  63833. + uint32_t v;
  63834. +
  63835. + if (core_if->multiproc_int_enable) {
  63836. + v = DWC_READ_REG32(&core_if->dev_if->
  63837. + dev_global_regs->deachint) &
  63838. + DWC_READ_REG32(&core_if->
  63839. + dev_if->dev_global_regs->deachintmsk);
  63840. + } else {
  63841. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  63842. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63843. + }
  63844. + return (v & 0xffff);
  63845. +}
  63846. +
  63847. +/**
  63848. + * This function reads the Device All Endpoints Interrupt register and
  63849. + * returns the OUT endpoint interrupt bits.
  63850. + */
  63851. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  63852. + core_if)
  63853. +{
  63854. + uint32_t v;
  63855. +
  63856. + if (core_if->multiproc_int_enable) {
  63857. + v = DWC_READ_REG32(&core_if->dev_if->
  63858. + dev_global_regs->deachint) &
  63859. + DWC_READ_REG32(&core_if->
  63860. + dev_if->dev_global_regs->deachintmsk);
  63861. + } else {
  63862. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  63863. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63864. + }
  63865. +
  63866. + return ((v & 0xffff0000) >> 16);
  63867. +}
  63868. +
  63869. +/**
  63870. + * This function returns the Device IN EP Interrupt register
  63871. + */
  63872. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  63873. + dwc_ep_t * ep)
  63874. +{
  63875. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  63876. + uint32_t v, msk, emp;
  63877. +
  63878. + if (core_if->multiproc_int_enable) {
  63879. + msk =
  63880. + DWC_READ_REG32(&dev_if->
  63881. + dev_global_regs->diepeachintmsk[ep->num]);
  63882. + emp =
  63883. + DWC_READ_REG32(&dev_if->
  63884. + dev_global_regs->dtknqr4_fifoemptymsk);
  63885. + msk |= ((emp >> ep->num) & 0x1) << 7;
  63886. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  63887. + } else {
  63888. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  63889. + emp =
  63890. + DWC_READ_REG32(&dev_if->
  63891. + dev_global_regs->dtknqr4_fifoemptymsk);
  63892. + msk |= ((emp >> ep->num) & 0x1) << 7;
  63893. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  63894. + }
  63895. +
  63896. + return v;
  63897. +}
  63898. +
  63899. +/**
  63900. + * This function returns the Device OUT EP Interrupt register
  63901. + */
  63902. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  63903. + _core_if, dwc_ep_t * _ep)
  63904. +{
  63905. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  63906. + uint32_t v;
  63907. + doepmsk_data_t msk = {.d32 = 0 };
  63908. +
  63909. + if (_core_if->multiproc_int_enable) {
  63910. + msk.d32 =
  63911. + DWC_READ_REG32(&dev_if->
  63912. + dev_global_regs->doepeachintmsk[_ep->num]);
  63913. + if (_core_if->pti_enh_enable) {
  63914. + msk.b.pktdrpsts = 1;
  63915. + }
  63916. + v = DWC_READ_REG32(&dev_if->
  63917. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  63918. + } else {
  63919. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  63920. + if (_core_if->pti_enh_enable) {
  63921. + msk.b.pktdrpsts = 1;
  63922. + }
  63923. + v = DWC_READ_REG32(&dev_if->
  63924. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  63925. + }
  63926. + return v;
  63927. +}
  63928. +
  63929. +/**
  63930. + * This function returns the Host All Channel Interrupt register
  63931. + */
  63932. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  63933. + _core_if)
  63934. +{
  63935. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  63936. +}
  63937. +
  63938. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  63939. + _core_if, dwc_hc_t * _hc)
  63940. +{
  63941. + return (DWC_READ_REG32
  63942. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  63943. +}
  63944. +
  63945. +/**
  63946. + * This function returns the mode of the operation, host or device.
  63947. + *
  63948. + * @return 0 - Device Mode, 1 - Host Mode
  63949. + */
  63950. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  63951. +{
  63952. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  63953. +}
  63954. +
  63955. +/**@}*/
  63956. +
  63957. +/**
  63958. + * DWC_otg CIL callback structure. This structure allows the HCD and
  63959. + * PCD to register functions used for starting and stopping the PCD
  63960. + * and HCD for role change on for a DRD.
  63961. + */
  63962. +typedef struct dwc_otg_cil_callbacks {
  63963. + /** Start function for role change */
  63964. + int (*start) (void *_p);
  63965. + /** Stop Function for role change */
  63966. + int (*stop) (void *_p);
  63967. + /** Disconnect Function for role change */
  63968. + int (*disconnect) (void *_p);
  63969. + /** Resume/Remote wakeup Function */
  63970. + int (*resume_wakeup) (void *_p);
  63971. + /** Suspend function */
  63972. + int (*suspend) (void *_p);
  63973. + /** Session Start (SRP) */
  63974. + int (*session_start) (void *_p);
  63975. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63976. + /** Sleep (switch to L0 state) */
  63977. + int (*sleep) (void *_p);
  63978. +#endif
  63979. + /** Pointer passed to start() and stop() */
  63980. + void *p;
  63981. +} dwc_otg_cil_callbacks_t;
  63982. +
  63983. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  63984. + dwc_otg_cil_callbacks_t * _cb,
  63985. + void *_p);
  63986. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  63987. + dwc_otg_cil_callbacks_t * _cb,
  63988. + void *_p);
  63989. +
  63990. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  63991. +
  63992. +//////////////////////////////////////////////////////////////////////
  63993. +/** Start the HCD. Helper function for using the HCD callbacks.
  63994. + *
  63995. + * @param core_if Programming view of DWC_otg controller.
  63996. + */
  63997. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  63998. +{
  63999. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  64000. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  64001. + }
  64002. +}
  64003. +
  64004. +/** Stop the HCD. Helper function for using the HCD callbacks.
  64005. + *
  64006. + * @param core_if Programming view of DWC_otg controller.
  64007. + */
  64008. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  64009. +{
  64010. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  64011. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  64012. + }
  64013. +}
  64014. +
  64015. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  64016. + *
  64017. + * @param core_if Programming view of DWC_otg controller.
  64018. + */
  64019. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  64020. +{
  64021. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  64022. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  64023. + }
  64024. +}
  64025. +
  64026. +/** Inform the HCD the a New Session has begun. Helper function for
  64027. + * using the HCD callbacks.
  64028. + *
  64029. + * @param core_if Programming view of DWC_otg controller.
  64030. + */
  64031. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  64032. +{
  64033. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  64034. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  64035. + }
  64036. +}
  64037. +
  64038. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64039. +/**
  64040. + * Inform the HCD about LPM sleep.
  64041. + * Helper function for using the HCD callbacks.
  64042. + *
  64043. + * @param core_if Programming view of DWC_otg controller.
  64044. + */
  64045. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  64046. +{
  64047. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  64048. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  64049. + }
  64050. +}
  64051. +#endif
  64052. +
  64053. +/** Resume the HCD. Helper function for using the HCD callbacks.
  64054. + *
  64055. + * @param core_if Programming view of DWC_otg controller.
  64056. + */
  64057. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  64058. +{
  64059. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  64060. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  64061. + }
  64062. +}
  64063. +
  64064. +/** Start the PCD. Helper function for using the PCD callbacks.
  64065. + *
  64066. + * @param core_if Programming view of DWC_otg controller.
  64067. + */
  64068. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  64069. +{
  64070. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  64071. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  64072. + }
  64073. +}
  64074. +
  64075. +/** Stop the PCD. Helper function for using the PCD callbacks.
  64076. + *
  64077. + * @param core_if Programming view of DWC_otg controller.
  64078. + */
  64079. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  64080. +{
  64081. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  64082. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  64083. + }
  64084. +}
  64085. +
  64086. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  64087. + *
  64088. + * @param core_if Programming view of DWC_otg controller.
  64089. + */
  64090. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  64091. +{
  64092. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  64093. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  64094. + }
  64095. +}
  64096. +
  64097. +/** Resume the PCD. Helper function for using the PCD callbacks.
  64098. + *
  64099. + * @param core_if Programming view of DWC_otg controller.
  64100. + */
  64101. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  64102. +{
  64103. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  64104. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  64105. + }
  64106. +}
  64107. +
  64108. +//////////////////////////////////////////////////////////////////////
  64109. +
  64110. +#endif
  64111. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  64112. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 1970-01-01 01:00:00.000000000 +0100
  64113. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 2014-04-24 15:35:04.173565776 +0200
  64114. @@ -0,0 +1,1588 @@
  64115. +/* ==========================================================================
  64116. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  64117. + * $Revision: #32 $
  64118. + * $Date: 2012/08/10 $
  64119. + * $Change: 2047372 $
  64120. + *
  64121. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  64122. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  64123. + * otherwise expressly agreed to in writing between Synopsys and you.
  64124. + *
  64125. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  64126. + * any End User Software License Agreement or Agreement for Licensed Product
  64127. + * with Synopsys or any supplement thereto. You are permitted to use and
  64128. + * redistribute this Software in source and binary forms, with or without
  64129. + * modification, provided that redistributions of source code must retain this
  64130. + * notice. You may not view, use, disclose, copy or distribute this file or
  64131. + * any information contained herein except pursuant to this license grant from
  64132. + * Synopsys. If you do not agree with this notice, including the disclaimer
  64133. + * below, then you are not authorized to use the Software.
  64134. + *
  64135. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  64136. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  64137. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  64138. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  64139. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  64140. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  64141. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  64142. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  64143. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64144. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  64145. + * DAMAGE.
  64146. + * ========================================================================== */
  64147. +
  64148. +/** @file
  64149. + *
  64150. + * The Core Interface Layer provides basic services for accessing and
  64151. + * managing the DWC_otg hardware. These services are used by both the
  64152. + * Host Controller Driver and the Peripheral Controller Driver.
  64153. + *
  64154. + * This file contains the Common Interrupt handlers.
  64155. + */
  64156. +#include "dwc_os.h"
  64157. +#include "dwc_otg_regs.h"
  64158. +#include "dwc_otg_cil.h"
  64159. +#include "dwc_otg_driver.h"
  64160. +#include "dwc_otg_pcd.h"
  64161. +#include "dwc_otg_hcd.h"
  64162. +#include "dwc_otg_mphi_fix.h"
  64163. +
  64164. +#ifdef DEBUG
  64165. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  64166. +{
  64167. + return (core_if->op_state == A_HOST ? "a_host" :
  64168. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  64169. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  64170. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  64171. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  64172. +}
  64173. +#endif
  64174. +
  64175. +/** This function will log a debug message
  64176. + *
  64177. + * @param core_if Programming view of DWC_otg controller.
  64178. + */
  64179. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  64180. +{
  64181. + gintsts_data_t gintsts;
  64182. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  64183. + dwc_otg_mode(core_if) ? "Host" : "Device");
  64184. +
  64185. + /* Clear interrupt */
  64186. + gintsts.d32 = 0;
  64187. + gintsts.b.modemismatch = 1;
  64188. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64189. + return 1;
  64190. +}
  64191. +
  64192. +/**
  64193. + * This function handles the OTG Interrupts. It reads the OTG
  64194. + * Interrupt Register (GOTGINT) to determine what interrupt has
  64195. + * occurred.
  64196. + *
  64197. + * @param core_if Programming view of DWC_otg controller.
  64198. + */
  64199. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  64200. +{
  64201. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  64202. + gotgint_data_t gotgint;
  64203. + gotgctl_data_t gotgctl;
  64204. + gintmsk_data_t gintmsk;
  64205. + gpwrdn_data_t gpwrdn;
  64206. +
  64207. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  64208. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64209. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  64210. + op_state_str(core_if));
  64211. +
  64212. + if (gotgint.b.sesenddet) {
  64213. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64214. + "Session End Detected++ (%s)\n",
  64215. + op_state_str(core_if));
  64216. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64217. +
  64218. + if (core_if->op_state == B_HOST) {
  64219. + cil_pcd_start(core_if);
  64220. + core_if->op_state = B_PERIPHERAL;
  64221. + } else {
  64222. + /* If not B_HOST and Device HNP still set. HNP
  64223. + * Did not succeed!*/
  64224. + if (gotgctl.b.devhnpen) {
  64225. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  64226. + __DWC_ERROR("Device Not Connected/Responding!\n");
  64227. + }
  64228. +
  64229. + /* If Session End Detected the B-Cable has
  64230. + * been disconnected. */
  64231. + /* Reset PCD and Gadget driver to a
  64232. + * clean state. */
  64233. + core_if->lx_state = DWC_OTG_L0;
  64234. + DWC_SPINUNLOCK(core_if->lock);
  64235. + cil_pcd_stop(core_if);
  64236. + DWC_SPINLOCK(core_if->lock);
  64237. +
  64238. + if (core_if->adp_enable) {
  64239. + if (core_if->power_down == 2) {
  64240. + gpwrdn.d32 = 0;
  64241. + gpwrdn.b.pwrdnswtch = 1;
  64242. + DWC_MODIFY_REG32(&core_if->
  64243. + core_global_regs->
  64244. + gpwrdn, gpwrdn.d32, 0);
  64245. + }
  64246. +
  64247. + gpwrdn.d32 = 0;
  64248. + gpwrdn.b.pmuintsel = 1;
  64249. + gpwrdn.b.pmuactv = 1;
  64250. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64251. + gpwrdn, 0, gpwrdn.d32);
  64252. +
  64253. + dwc_otg_adp_sense_start(core_if);
  64254. + }
  64255. + }
  64256. +
  64257. + gotgctl.d32 = 0;
  64258. + gotgctl.b.devhnpen = 1;
  64259. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  64260. + }
  64261. + if (gotgint.b.sesreqsucstschng) {
  64262. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64263. + "Session Reqeust Success Status Change++\n");
  64264. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64265. + if (gotgctl.b.sesreqscs) {
  64266. +
  64267. + if ((core_if->core_params->phy_type ==
  64268. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  64269. + core_if->srp_success = 1;
  64270. + } else {
  64271. + DWC_SPINUNLOCK(core_if->lock);
  64272. + cil_pcd_resume(core_if);
  64273. + DWC_SPINLOCK(core_if->lock);
  64274. + /* Clear Session Request */
  64275. + gotgctl.d32 = 0;
  64276. + gotgctl.b.sesreq = 1;
  64277. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  64278. + gotgctl.d32, 0);
  64279. + }
  64280. + }
  64281. + }
  64282. + if (gotgint.b.hstnegsucstschng) {
  64283. + /* Print statements during the HNP interrupt handling
  64284. + * can cause it to fail.*/
  64285. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  64286. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  64287. + * this does not help*/
  64288. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  64289. + dwc_udelay(100);
  64290. + if (gotgctl.b.hstnegscs) {
  64291. + if (dwc_otg_is_host_mode(core_if)) {
  64292. + core_if->op_state = B_HOST;
  64293. + /*
  64294. + * Need to disable SOF interrupt immediately.
  64295. + * When switching from device to host, the PCD
  64296. + * interrupt handler won't handle the
  64297. + * interrupt if host mode is already set. The
  64298. + * HCD interrupt handler won't get called if
  64299. + * the HCD state is HALT. This means that the
  64300. + * interrupt does not get handled and Linux
  64301. + * complains loudly.
  64302. + */
  64303. + gintmsk.d32 = 0;
  64304. + gintmsk.b.sofintr = 1;
  64305. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  64306. + gintmsk.d32, 0);
  64307. + /* Call callback function with spin lock released */
  64308. + DWC_SPINUNLOCK(core_if->lock);
  64309. + cil_pcd_stop(core_if);
  64310. + /*
  64311. + * Initialize the Core for Host mode.
  64312. + */
  64313. + cil_hcd_start(core_if);
  64314. + DWC_SPINLOCK(core_if->lock);
  64315. + core_if->op_state = B_HOST;
  64316. + }
  64317. + } else {
  64318. + gotgctl.d32 = 0;
  64319. + gotgctl.b.hnpreq = 1;
  64320. + gotgctl.b.devhnpen = 1;
  64321. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  64322. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  64323. + __DWC_ERROR("Device Not Connected/Responding\n");
  64324. + }
  64325. + }
  64326. + if (gotgint.b.hstnegdet) {
  64327. + /* The disconnect interrupt is set at the same time as
  64328. + * Host Negotiation Detected. During the mode
  64329. + * switch all interrupts are cleared so the disconnect
  64330. + * interrupt handler will not get executed.
  64331. + */
  64332. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64333. + "Host Negotiation Detected++ (%s)\n",
  64334. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  64335. + "Device"));
  64336. + if (dwc_otg_is_device_mode(core_if)) {
  64337. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  64338. + core_if->op_state);
  64339. + DWC_SPINUNLOCK(core_if->lock);
  64340. + cil_hcd_disconnect(core_if);
  64341. + cil_pcd_start(core_if);
  64342. + DWC_SPINLOCK(core_if->lock);
  64343. + core_if->op_state = A_PERIPHERAL;
  64344. + } else {
  64345. + /*
  64346. + * Need to disable SOF interrupt immediately. When
  64347. + * switching from device to host, the PCD interrupt
  64348. + * handler won't handle the interrupt if host mode is
  64349. + * already set. The HCD interrupt handler won't get
  64350. + * called if the HCD state is HALT. This means that
  64351. + * the interrupt does not get handled and Linux
  64352. + * complains loudly.
  64353. + */
  64354. + gintmsk.d32 = 0;
  64355. + gintmsk.b.sofintr = 1;
  64356. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  64357. + DWC_SPINUNLOCK(core_if->lock);
  64358. + cil_pcd_stop(core_if);
  64359. + cil_hcd_start(core_if);
  64360. + DWC_SPINLOCK(core_if->lock);
  64361. + core_if->op_state = A_HOST;
  64362. + }
  64363. + }
  64364. + if (gotgint.b.adevtoutchng) {
  64365. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  64366. + "A-Device Timeout Change++\n");
  64367. + }
  64368. + if (gotgint.b.debdone) {
  64369. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  64370. + }
  64371. +
  64372. + /* Clear GOTGINT */
  64373. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  64374. +
  64375. + return 1;
  64376. +}
  64377. +
  64378. +void w_conn_id_status_change(void *p)
  64379. +{
  64380. + dwc_otg_core_if_t *core_if = p;
  64381. + uint32_t count = 0;
  64382. + gotgctl_data_t gotgctl = {.d32 = 0 };
  64383. +
  64384. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  64385. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  64386. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  64387. +
  64388. + /* B-Device connector (Device Mode) */
  64389. + if (gotgctl.b.conidsts) {
  64390. + /* Wait for switch to device mode. */
  64391. + while (!dwc_otg_is_device_mode(core_if)) {
  64392. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  64393. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  64394. + "Peripheral"));
  64395. + dwc_mdelay(100);
  64396. + if (++count > 10000)
  64397. + break;
  64398. + }
  64399. + DWC_ASSERT(++count < 10000,
  64400. + "Connection id status change timed out");
  64401. + core_if->op_state = B_PERIPHERAL;
  64402. + dwc_otg_core_init(core_if);
  64403. + dwc_otg_enable_global_interrupts(core_if);
  64404. + cil_pcd_start(core_if);
  64405. + } else {
  64406. + /* A-Device connector (Host Mode) */
  64407. + while (!dwc_otg_is_host_mode(core_if)) {
  64408. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  64409. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  64410. + "Peripheral"));
  64411. + dwc_mdelay(100);
  64412. + if (++count > 10000)
  64413. + break;
  64414. + }
  64415. + DWC_ASSERT(++count < 10000,
  64416. + "Connection id status change timed out");
  64417. + core_if->op_state = A_HOST;
  64418. + /*
  64419. + * Initialize the Core for Host mode.
  64420. + */
  64421. + dwc_otg_core_init(core_if);
  64422. + dwc_otg_enable_global_interrupts(core_if);
  64423. + cil_hcd_start(core_if);
  64424. + }
  64425. +}
  64426. +
  64427. +/**
  64428. + * This function handles the Connector ID Status Change Interrupt. It
  64429. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  64430. + * is a Device to Host Mode transition or a Host Mode to Device
  64431. + * Transition.
  64432. + *
  64433. + * This only occurs when the cable is connected/removed from the PHY
  64434. + * connector.
  64435. + *
  64436. + * @param core_if Programming view of DWC_otg controller.
  64437. + */
  64438. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  64439. +{
  64440. +
  64441. + /*
  64442. + * Need to disable SOF interrupt immediately. If switching from device
  64443. + * to host, the PCD interrupt handler won't handle the interrupt if
  64444. + * host mode is already set. The HCD interrupt handler won't get
  64445. + * called if the HCD state is HALT. This means that the interrupt does
  64446. + * not get handled and Linux complains loudly.
  64447. + */
  64448. + gintmsk_data_t gintmsk = {.d32 = 0 };
  64449. + gintsts_data_t gintsts = {.d32 = 0 };
  64450. +
  64451. + gintmsk.b.sofintr = 1;
  64452. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  64453. +
  64454. + DWC_DEBUGPL(DBG_CIL,
  64455. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  64456. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  64457. +
  64458. + DWC_SPINUNLOCK(core_if->lock);
  64459. +
  64460. + /*
  64461. + * Need to schedule a work, as there are possible DELAY function calls
  64462. + * Release lock before scheduling workq as it holds spinlock during scheduling
  64463. + */
  64464. +
  64465. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  64466. + core_if, "connection id status change");
  64467. + DWC_SPINLOCK(core_if->lock);
  64468. +
  64469. + /* Set flag and clear interrupt */
  64470. + gintsts.b.conidstschng = 1;
  64471. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64472. +
  64473. + return 1;
  64474. +}
  64475. +
  64476. +/**
  64477. + * This interrupt indicates that a device is initiating the Session
  64478. + * Request Protocol to request the host to turn on bus power so a new
  64479. + * session can begin. The handler responds by turning on bus power. If
  64480. + * the DWC_otg controller is in low power mode, the handler brings the
  64481. + * controller out of low power mode before turning on bus power.
  64482. + *
  64483. + * @param core_if Programming view of DWC_otg controller.
  64484. + */
  64485. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  64486. +{
  64487. + gintsts_data_t gintsts;
  64488. +
  64489. +#ifndef DWC_HOST_ONLY
  64490. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  64491. +
  64492. + if (dwc_otg_is_device_mode(core_if)) {
  64493. + DWC_PRINTF("SRP: Device mode\n");
  64494. + } else {
  64495. + hprt0_data_t hprt0;
  64496. + DWC_PRINTF("SRP: Host mode\n");
  64497. +
  64498. + /* Turn on the port power bit. */
  64499. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  64500. + hprt0.b.prtpwr = 1;
  64501. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64502. +
  64503. + /* Start the Connection timer. So a message can be displayed
  64504. + * if connect does not occur within 10 seconds. */
  64505. + cil_hcd_session_start(core_if);
  64506. + }
  64507. +#endif
  64508. +
  64509. + /* Clear interrupt */
  64510. + gintsts.d32 = 0;
  64511. + gintsts.b.sessreqintr = 1;
  64512. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64513. +
  64514. + return 1;
  64515. +}
  64516. +
  64517. +void w_wakeup_detected(void *p)
  64518. +{
  64519. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  64520. + /*
  64521. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  64522. + * so that OPT tests pass with all PHYs).
  64523. + */
  64524. + hprt0_data_t hprt0 = {.d32 = 0 };
  64525. +#if 0
  64526. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64527. + /* Restart the Phy Clock */
  64528. + pcgcctl.b.stoppclk = 1;
  64529. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64530. + dwc_udelay(10);
  64531. +#endif //0
  64532. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  64533. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  64534. +// dwc_mdelay(70);
  64535. + hprt0.b.prtres = 0; /* Resume */
  64536. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64537. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  64538. + DWC_READ_REG32(core_if->host_if->hprt0));
  64539. +
  64540. + cil_hcd_resume(core_if);
  64541. +
  64542. + /** Change to L0 state*/
  64543. + core_if->lx_state = DWC_OTG_L0;
  64544. +}
  64545. +
  64546. +/**
  64547. + * This interrupt indicates that the DWC_otg controller has detected a
  64548. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  64549. + * low power mode, the handler must brings the controller out of low
  64550. + * power mode. The controller automatically begins resume
  64551. + * signaling. The handler schedules a time to stop resume signaling.
  64552. + */
  64553. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  64554. +{
  64555. + gintsts_data_t gintsts;
  64556. +
  64557. + DWC_DEBUGPL(DBG_ANY,
  64558. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  64559. +
  64560. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  64561. +
  64562. + if (dwc_otg_is_device_mode(core_if)) {
  64563. + dctl_data_t dctl = {.d32 = 0 };
  64564. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  64565. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  64566. + dsts));
  64567. + if (core_if->lx_state == DWC_OTG_L2) {
  64568. +#ifdef PARTIAL_POWER_DOWN
  64569. + if (core_if->hwcfg4.b.power_optimiz) {
  64570. + pcgcctl_data_t power = {.d32 = 0 };
  64571. +
  64572. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  64573. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  64574. + power.d32);
  64575. +
  64576. + power.b.stoppclk = 0;
  64577. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64578. +
  64579. + power.b.pwrclmp = 0;
  64580. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64581. +
  64582. + power.b.rstpdwnmodule = 0;
  64583. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64584. + }
  64585. +#endif
  64586. + /* Clear the Remote Wakeup Signaling */
  64587. + dctl.b.rmtwkupsig = 1;
  64588. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  64589. + dctl, dctl.d32, 0);
  64590. +
  64591. + DWC_SPINUNLOCK(core_if->lock);
  64592. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  64593. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  64594. + }
  64595. + DWC_SPINLOCK(core_if->lock);
  64596. + } else {
  64597. + glpmcfg_data_t lpmcfg;
  64598. + lpmcfg.d32 =
  64599. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  64600. + lpmcfg.b.hird_thres &= (~(1 << 4));
  64601. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  64602. + lpmcfg.d32);
  64603. + }
  64604. + /** Change to L0 state*/
  64605. + core_if->lx_state = DWC_OTG_L0;
  64606. + } else {
  64607. + if (core_if->lx_state != DWC_OTG_L1) {
  64608. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64609. +
  64610. + /* Restart the Phy Clock */
  64611. + pcgcctl.b.stoppclk = 1;
  64612. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64613. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  64614. + } else {
  64615. + /** Change to L0 state*/
  64616. + core_if->lx_state = DWC_OTG_L0;
  64617. + }
  64618. + }
  64619. +
  64620. + /* Clear interrupt */
  64621. + gintsts.d32 = 0;
  64622. + gintsts.b.wkupintr = 1;
  64623. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64624. +
  64625. + return 1;
  64626. +}
  64627. +
  64628. +/**
  64629. + * This interrupt indicates that the Wakeup Logic has detected a
  64630. + * Device disconnect.
  64631. + */
  64632. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  64633. +{
  64634. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  64635. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  64636. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64637. +
  64638. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64639. +
  64640. + if (!core_if->hibernation_suspend) {
  64641. + DWC_PRINTF("Already exited from Hibernation\n");
  64642. + return 1;
  64643. + }
  64644. +
  64645. + /* Switch on the voltage to the core */
  64646. + gpwrdn.b.pwrdnswtch = 1;
  64647. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64648. + dwc_udelay(10);
  64649. +
  64650. + /* Reset the core */
  64651. + gpwrdn.d32 = 0;
  64652. + gpwrdn.b.pwrdnrstn = 1;
  64653. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64654. + dwc_udelay(10);
  64655. +
  64656. + /* Disable power clamps*/
  64657. + gpwrdn.d32 = 0;
  64658. + gpwrdn.b.pwrdnclmp = 1;
  64659. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64660. +
  64661. + /* Remove reset the core signal */
  64662. + gpwrdn.d32 = 0;
  64663. + gpwrdn.b.pwrdnrstn = 1;
  64664. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64665. + dwc_udelay(10);
  64666. +
  64667. + /* Disable PMU interrupt */
  64668. + gpwrdn.d32 = 0;
  64669. + gpwrdn.b.pmuintsel = 1;
  64670. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64671. +
  64672. + core_if->hibernation_suspend = 0;
  64673. +
  64674. + /* Disable PMU */
  64675. + gpwrdn.d32 = 0;
  64676. + gpwrdn.b.pmuactv = 1;
  64677. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64678. + dwc_udelay(10);
  64679. +
  64680. + if (gpwrdn_temp.b.idsts) {
  64681. + core_if->op_state = B_PERIPHERAL;
  64682. + dwc_otg_core_init(core_if);
  64683. + dwc_otg_enable_global_interrupts(core_if);
  64684. + cil_pcd_start(core_if);
  64685. + } else {
  64686. + core_if->op_state = A_HOST;
  64687. + dwc_otg_core_init(core_if);
  64688. + dwc_otg_enable_global_interrupts(core_if);
  64689. + cil_hcd_start(core_if);
  64690. + }
  64691. +
  64692. + return 1;
  64693. +}
  64694. +
  64695. +/**
  64696. + * This interrupt indicates that the Wakeup Logic has detected a
  64697. + * remote wakeup sequence.
  64698. + */
  64699. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  64700. +{
  64701. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64702. + DWC_DEBUGPL(DBG_ANY,
  64703. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  64704. +
  64705. + if (!core_if->hibernation_suspend) {
  64706. + DWC_PRINTF("Already exited from Hibernation\n");
  64707. + return 1;
  64708. + }
  64709. +
  64710. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64711. + if (gpwrdn.b.idsts) { // Device Mode
  64712. + if ((core_if->power_down == 2)
  64713. + && (core_if->hibernation_suspend == 1)) {
  64714. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  64715. + }
  64716. + } else {
  64717. + if ((core_if->power_down == 2)
  64718. + && (core_if->hibernation_suspend == 1)) {
  64719. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  64720. + }
  64721. + }
  64722. + return 1;
  64723. +}
  64724. +
  64725. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  64726. +{
  64727. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64728. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  64729. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64730. +
  64731. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64732. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64733. + if (core_if->power_down == 2) {
  64734. + if (!core_if->hibernation_suspend) {
  64735. + DWC_PRINTF("Already exited from Hibernation\n");
  64736. + return 1;
  64737. + }
  64738. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  64739. + /* Switch on the voltage to the core */
  64740. + gpwrdn.b.pwrdnswtch = 1;
  64741. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64742. + dwc_udelay(10);
  64743. +
  64744. + /* Reset the core */
  64745. + gpwrdn.d32 = 0;
  64746. + gpwrdn.b.pwrdnrstn = 1;
  64747. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64748. + dwc_udelay(10);
  64749. +
  64750. + /* Disable power clamps */
  64751. + gpwrdn.d32 = 0;
  64752. + gpwrdn.b.pwrdnclmp = 1;
  64753. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64754. +
  64755. + /* Remove reset the core signal */
  64756. + gpwrdn.d32 = 0;
  64757. + gpwrdn.b.pwrdnrstn = 1;
  64758. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64759. + dwc_udelay(10);
  64760. +
  64761. + /* Disable PMU interrupt */
  64762. + gpwrdn.d32 = 0;
  64763. + gpwrdn.b.pmuintsel = 1;
  64764. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64765. +
  64766. + /*Indicates that we are exiting from hibernation */
  64767. + core_if->hibernation_suspend = 0;
  64768. +
  64769. + /* Disable PMU */
  64770. + gpwrdn.d32 = 0;
  64771. + gpwrdn.b.pmuactv = 1;
  64772. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64773. + dwc_udelay(10);
  64774. +
  64775. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  64776. + if (gpwrdn.b.dis_vbus == 1) {
  64777. + gpwrdn.d32 = 0;
  64778. + gpwrdn.b.dis_vbus = 1;
  64779. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64780. + }
  64781. +
  64782. + if (gpwrdn_temp.b.idsts) {
  64783. + core_if->op_state = B_PERIPHERAL;
  64784. + dwc_otg_core_init(core_if);
  64785. + dwc_otg_enable_global_interrupts(core_if);
  64786. + cil_pcd_start(core_if);
  64787. + } else {
  64788. + core_if->op_state = A_HOST;
  64789. + dwc_otg_core_init(core_if);
  64790. + dwc_otg_enable_global_interrupts(core_if);
  64791. + cil_hcd_start(core_if);
  64792. + }
  64793. + }
  64794. +
  64795. + if (core_if->adp_enable) {
  64796. + uint8_t is_host = 0;
  64797. + DWC_SPINUNLOCK(core_if->lock);
  64798. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  64799. +#ifndef DWC_HOST_ONLY
  64800. + if (gpwrdn_temp.b.idsts)
  64801. + core_if->lock = otg_dev->pcd->lock;
  64802. +#endif
  64803. +#ifndef DWC_DEVICE_ONLY
  64804. + if (!gpwrdn_temp.b.idsts) {
  64805. + core_if->lock = otg_dev->hcd->lock;
  64806. + is_host = 1;
  64807. + }
  64808. +#endif
  64809. + DWC_PRINTF("RESTART ADP\n");
  64810. + if (core_if->adp.probe_enabled)
  64811. + dwc_otg_adp_probe_stop(core_if);
  64812. + if (core_if->adp.sense_enabled)
  64813. + dwc_otg_adp_sense_stop(core_if);
  64814. + if (core_if->adp.sense_timer_started)
  64815. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  64816. + if (core_if->adp.vbuson_timer_started)
  64817. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  64818. + core_if->adp.probe_timer_values[0] = -1;
  64819. + core_if->adp.probe_timer_values[1] = -1;
  64820. + core_if->adp.sense_timer_started = 0;
  64821. + core_if->adp.vbuson_timer_started = 0;
  64822. + core_if->adp.probe_counter = 0;
  64823. + core_if->adp.gpwrdn = 0;
  64824. +
  64825. + /* Disable PMU and restart ADP */
  64826. + gpwrdn_temp.d32 = 0;
  64827. + gpwrdn_temp.b.pmuactv = 1;
  64828. + gpwrdn_temp.b.pmuintsel = 1;
  64829. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64830. + DWC_PRINTF("Check point 1\n");
  64831. + dwc_mdelay(110);
  64832. + dwc_otg_adp_start(core_if, is_host);
  64833. + DWC_SPINLOCK(core_if->lock);
  64834. + }
  64835. +
  64836. +
  64837. + return 1;
  64838. +}
  64839. +
  64840. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  64841. +{
  64842. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64843. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  64844. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64845. +
  64846. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64847. + if (core_if->power_down == 2) {
  64848. + if (!core_if->hibernation_suspend) {
  64849. + DWC_PRINTF("Already exited from Hibernation\n");
  64850. + return 1;
  64851. + }
  64852. +
  64853. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  64854. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  64855. + gpwrdn.b.bsessvld == 0) {
  64856. + /* Save gpwrdn register for further usage if stschng interrupt */
  64857. + core_if->gr_backup->gpwrdn_local =
  64858. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64859. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  64860. + return 1;
  64861. + }
  64862. +
  64863. + /* Switch on the voltage to the core */
  64864. + gpwrdn.d32 = 0;
  64865. + gpwrdn.b.pwrdnswtch = 1;
  64866. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64867. + dwc_udelay(10);
  64868. +
  64869. + /* Reset the core */
  64870. + gpwrdn.d32 = 0;
  64871. + gpwrdn.b.pwrdnrstn = 1;
  64872. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64873. + dwc_udelay(10);
  64874. +
  64875. + /* Disable power clamps */
  64876. + gpwrdn.d32 = 0;
  64877. + gpwrdn.b.pwrdnclmp = 1;
  64878. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64879. +
  64880. + /* Remove reset the core signal */
  64881. + gpwrdn.d32 = 0;
  64882. + gpwrdn.b.pwrdnrstn = 1;
  64883. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64884. + dwc_udelay(10);
  64885. +
  64886. + /* Disable PMU interrupt */
  64887. + gpwrdn.d32 = 0;
  64888. + gpwrdn.b.pmuintsel = 1;
  64889. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64890. + dwc_udelay(10);
  64891. +
  64892. + /*Indicates that we are exiting from hibernation */
  64893. + core_if->hibernation_suspend = 0;
  64894. +
  64895. + /* Disable PMU */
  64896. + gpwrdn.d32 = 0;
  64897. + gpwrdn.b.pmuactv = 1;
  64898. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64899. + dwc_udelay(10);
  64900. +
  64901. + core_if->op_state = B_PERIPHERAL;
  64902. + dwc_otg_core_init(core_if);
  64903. + dwc_otg_enable_global_interrupts(core_if);
  64904. + cil_pcd_start(core_if);
  64905. +
  64906. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  64907. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  64908. + /*
  64909. + * Initiate SRP after initial ADP probe.
  64910. + */
  64911. + dwc_otg_initiate_srp(core_if);
  64912. + }
  64913. + }
  64914. +
  64915. + return 1;
  64916. +}
  64917. +/**
  64918. + * This interrupt indicates that the Wakeup Logic has detected a
  64919. + * status change either on IDDIG or BSessVld.
  64920. + */
  64921. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  64922. +{
  64923. + int retval;
  64924. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64925. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  64926. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64927. +
  64928. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64929. +
  64930. + if (core_if->power_down == 2) {
  64931. + if (core_if->hibernation_suspend <= 0) {
  64932. + DWC_PRINTF("Already exited from Hibernation\n");
  64933. + return 1;
  64934. + } else
  64935. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  64936. +
  64937. + } else {
  64938. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  64939. + }
  64940. +
  64941. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64942. +
  64943. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  64944. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  64945. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  64946. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  64947. + }
  64948. +
  64949. + return retval;
  64950. +}
  64951. +
  64952. +/**
  64953. + * This interrupt indicates that the Wakeup Logic has detected a
  64954. + * SRP.
  64955. + */
  64956. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  64957. +{
  64958. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64959. +
  64960. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64961. +
  64962. + if (!core_if->hibernation_suspend) {
  64963. + DWC_PRINTF("Already exited from Hibernation\n");
  64964. + return 1;
  64965. + }
  64966. +#ifdef DWC_DEV_SRPCAP
  64967. + if (core_if->pwron_timer_started) {
  64968. + core_if->pwron_timer_started = 0;
  64969. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  64970. + }
  64971. +#endif
  64972. +
  64973. + /* Switch on the voltage to the core */
  64974. + gpwrdn.b.pwrdnswtch = 1;
  64975. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64976. + dwc_udelay(10);
  64977. +
  64978. + /* Reset the core */
  64979. + gpwrdn.d32 = 0;
  64980. + gpwrdn.b.pwrdnrstn = 1;
  64981. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64982. + dwc_udelay(10);
  64983. +
  64984. + /* Disable power clamps */
  64985. + gpwrdn.d32 = 0;
  64986. + gpwrdn.b.pwrdnclmp = 1;
  64987. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64988. +
  64989. + /* Remove reset the core signal */
  64990. + gpwrdn.d32 = 0;
  64991. + gpwrdn.b.pwrdnrstn = 1;
  64992. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64993. + dwc_udelay(10);
  64994. +
  64995. + /* Disable PMU interrupt */
  64996. + gpwrdn.d32 = 0;
  64997. + gpwrdn.b.pmuintsel = 1;
  64998. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64999. +
  65000. + /* Indicates that we are exiting from hibernation */
  65001. + core_if->hibernation_suspend = 0;
  65002. +
  65003. + /* Disable PMU */
  65004. + gpwrdn.d32 = 0;
  65005. + gpwrdn.b.pmuactv = 1;
  65006. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  65007. + dwc_udelay(10);
  65008. +
  65009. + /* Programm Disable VBUS to 0 */
  65010. + gpwrdn.d32 = 0;
  65011. + gpwrdn.b.dis_vbus = 1;
  65012. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  65013. +
  65014. + /*Initialize the core as Host */
  65015. + core_if->op_state = A_HOST;
  65016. + dwc_otg_core_init(core_if);
  65017. + dwc_otg_enable_global_interrupts(core_if);
  65018. + cil_hcd_start(core_if);
  65019. +
  65020. + return 1;
  65021. +}
  65022. +
  65023. +/** This interrupt indicates that restore command after Hibernation
  65024. + * was completed by the core. */
  65025. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  65026. +{
  65027. + pcgcctl_data_t pcgcctl;
  65028. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  65029. +
  65030. + //TODO De-assert restore signal. 8.a
  65031. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  65032. + if (pcgcctl.b.restoremode == 1) {
  65033. + gintmsk_data_t gintmsk = {.d32 = 0 };
  65034. + /*
  65035. + * If restore mode is Remote Wakeup,
  65036. + * unmask Remote Wakeup interrupt.
  65037. + */
  65038. + gintmsk.b.wkupintr = 1;
  65039. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  65040. + 0, gintmsk.d32);
  65041. + }
  65042. +
  65043. + return 1;
  65044. +}
  65045. +
  65046. +/**
  65047. + * This interrupt indicates that a device has been disconnected from
  65048. + * the root port.
  65049. + */
  65050. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  65051. +{
  65052. + gintsts_data_t gintsts;
  65053. +
  65054. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  65055. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  65056. + op_state_str(core_if));
  65057. +
  65058. +/** @todo Consolidate this if statement. */
  65059. +#ifndef DWC_HOST_ONLY
  65060. + if (core_if->op_state == B_HOST) {
  65061. + /* If in device mode Disconnect and stop the HCD, then
  65062. + * start the PCD. */
  65063. + DWC_SPINUNLOCK(core_if->lock);
  65064. + cil_hcd_disconnect(core_if);
  65065. + cil_pcd_start(core_if);
  65066. + DWC_SPINLOCK(core_if->lock);
  65067. + core_if->op_state = B_PERIPHERAL;
  65068. + } else if (dwc_otg_is_device_mode(core_if)) {
  65069. + gotgctl_data_t gotgctl = {.d32 = 0 };
  65070. + gotgctl.d32 =
  65071. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  65072. + if (gotgctl.b.hstsethnpen == 1) {
  65073. + /* Do nothing, if HNP in process the OTG
  65074. + * interrupt "Host Negotiation Detected"
  65075. + * interrupt will do the mode switch.
  65076. + */
  65077. + } else if (gotgctl.b.devhnpen == 0) {
  65078. + /* If in device mode Disconnect and stop the HCD, then
  65079. + * start the PCD. */
  65080. + DWC_SPINUNLOCK(core_if->lock);
  65081. + cil_hcd_disconnect(core_if);
  65082. + cil_pcd_start(core_if);
  65083. + DWC_SPINLOCK(core_if->lock);
  65084. + core_if->op_state = B_PERIPHERAL;
  65085. + } else {
  65086. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  65087. + }
  65088. + } else {
  65089. + if (core_if->op_state == A_HOST) {
  65090. + /* A-Cable still connected but device disconnected. */
  65091. + cil_hcd_disconnect(core_if);
  65092. + if (core_if->adp_enable) {
  65093. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  65094. + cil_hcd_stop(core_if);
  65095. + /* Enable Power Down Logic */
  65096. + gpwrdn.b.pmuintsel = 1;
  65097. + gpwrdn.b.pmuactv = 1;
  65098. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65099. + gpwrdn, 0, gpwrdn.d32);
  65100. + dwc_otg_adp_probe_start(core_if);
  65101. +
  65102. + /* Power off the core */
  65103. + if (core_if->power_down == 2) {
  65104. + gpwrdn.d32 = 0;
  65105. + gpwrdn.b.pwrdnswtch = 1;
  65106. + DWC_MODIFY_REG32
  65107. + (&core_if->core_global_regs->gpwrdn,
  65108. + gpwrdn.d32, 0);
  65109. + }
  65110. + }
  65111. + }
  65112. + }
  65113. +#endif
  65114. + /* Change to L3(OFF) state */
  65115. + core_if->lx_state = DWC_OTG_L3;
  65116. +
  65117. + gintsts.d32 = 0;
  65118. + gintsts.b.disconnect = 1;
  65119. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65120. + return 1;
  65121. +}
  65122. +
  65123. +/**
  65124. + * This interrupt indicates that SUSPEND state has been detected on
  65125. + * the USB.
  65126. + *
  65127. + * For HNP the USB Suspend interrupt signals the change from
  65128. + * "a_peripheral" to "a_host".
  65129. + *
  65130. + * When power management is enabled the core will be put in low power
  65131. + * mode.
  65132. + */
  65133. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  65134. +{
  65135. + dsts_data_t dsts;
  65136. + gintsts_data_t gintsts;
  65137. + dcfg_data_t dcfg;
  65138. +
  65139. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  65140. +
  65141. + if (dwc_otg_is_device_mode(core_if)) {
  65142. + /* Check the Device status register to determine if the Suspend
  65143. + * state is active. */
  65144. + dsts.d32 =
  65145. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  65146. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  65147. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  65148. + "HWCFG4.power Optimize=%d\n",
  65149. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  65150. +
  65151. +#ifdef PARTIAL_POWER_DOWN
  65152. +/** @todo Add a module parameter for power management. */
  65153. +
  65154. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  65155. + pcgcctl_data_t power = {.d32 = 0 };
  65156. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  65157. +
  65158. + power.b.pwrclmp = 1;
  65159. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  65160. +
  65161. + power.b.rstpdwnmodule = 1;
  65162. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  65163. +
  65164. + power.b.stoppclk = 1;
  65165. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  65166. +
  65167. + } else {
  65168. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  65169. + }
  65170. +#endif
  65171. + /* PCD callback for suspend. Release the lock inside of callback function */
  65172. + cil_pcd_suspend(core_if);
  65173. + if (core_if->power_down == 2)
  65174. + {
  65175. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  65176. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  65177. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  65178. +
  65179. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  65180. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65181. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65182. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  65183. +
  65184. + /* Change to L2(suspend) state */
  65185. + core_if->lx_state = DWC_OTG_L2;
  65186. +
  65187. + /* Clear interrupt in gintsts */
  65188. + gintsts.d32 = 0;
  65189. + gintsts.b.usbsuspend = 1;
  65190. + DWC_WRITE_REG32(&core_if->core_global_regs->
  65191. + gintsts, gintsts.d32);
  65192. + DWC_PRINTF("Start of hibernation completed\n");
  65193. + dwc_otg_save_global_regs(core_if);
  65194. + dwc_otg_save_dev_regs(core_if);
  65195. +
  65196. + gusbcfg.d32 =
  65197. + DWC_READ_REG32(&core_if->core_global_regs->
  65198. + gusbcfg);
  65199. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  65200. + /* ULPI interface */
  65201. + /* Suspend the Phy Clock */
  65202. + pcgcctl.d32 = 0;
  65203. + pcgcctl.b.stoppclk = 1;
  65204. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  65205. + pcgcctl.d32);
  65206. + dwc_udelay(10);
  65207. + gpwrdn.b.pmuactv = 1;
  65208. + DWC_MODIFY_REG32(&core_if->
  65209. + core_global_regs->
  65210. + gpwrdn, 0, gpwrdn.d32);
  65211. + } else {
  65212. + /* UTMI+ Interface */
  65213. + gpwrdn.b.pmuactv = 1;
  65214. + DWC_MODIFY_REG32(&core_if->
  65215. + core_global_regs->
  65216. + gpwrdn, 0, gpwrdn.d32);
  65217. + dwc_udelay(10);
  65218. + pcgcctl.b.stoppclk = 1;
  65219. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  65220. + pcgcctl.d32);
  65221. + dwc_udelay(10);
  65222. + }
  65223. +
  65224. + /* Set flag to indicate that we are in hibernation */
  65225. + core_if->hibernation_suspend = 1;
  65226. + /* Enable interrupts from wake up logic */
  65227. + gpwrdn.d32 = 0;
  65228. + gpwrdn.b.pmuintsel = 1;
  65229. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65230. + gpwrdn, 0, gpwrdn.d32);
  65231. + dwc_udelay(10);
  65232. +
  65233. + /* Unmask device mode interrupts in GPWRDN */
  65234. + gpwrdn.d32 = 0;
  65235. + gpwrdn.b.rst_det_msk = 1;
  65236. + gpwrdn.b.lnstchng_msk = 1;
  65237. + gpwrdn.b.sts_chngint_msk = 1;
  65238. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65239. + gpwrdn, 0, gpwrdn.d32);
  65240. + dwc_udelay(10);
  65241. +
  65242. + /* Enable Power Down Clamp */
  65243. + gpwrdn.d32 = 0;
  65244. + gpwrdn.b.pwrdnclmp = 1;
  65245. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65246. + gpwrdn, 0, gpwrdn.d32);
  65247. + dwc_udelay(10);
  65248. +
  65249. + /* Switch off VDD */
  65250. + gpwrdn.d32 = 0;
  65251. + gpwrdn.b.pwrdnswtch = 1;
  65252. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  65253. + gpwrdn, 0, gpwrdn.d32);
  65254. +
  65255. + /* Save gpwrdn register for further usage if stschng interrupt */
  65256. + core_if->gr_backup->gpwrdn_local =
  65257. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  65258. + DWC_PRINTF("Hibernation completed\n");
  65259. +
  65260. + return 1;
  65261. + }
  65262. + } else if (core_if->power_down == 3) {
  65263. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65264. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  65265. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  65266. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  65267. +
  65268. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  65269. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  65270. + core_if->xhib = 1;
  65271. +
  65272. + /* Clear interrupt in gintsts */
  65273. + gintsts.d32 = 0;
  65274. + gintsts.b.usbsuspend = 1;
  65275. + DWC_WRITE_REG32(&core_if->core_global_regs->
  65276. + gintsts, gintsts.d32);
  65277. +
  65278. + dwc_otg_save_global_regs(core_if);
  65279. + dwc_otg_save_dev_regs(core_if);
  65280. +
  65281. + /* Wait for 10 PHY clocks */
  65282. + dwc_udelay(10);
  65283. +
  65284. + /* Program GPIO register while entering to xHib */
  65285. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  65286. +
  65287. + pcgcctl.b.enbl_extnd_hiber = 1;
  65288. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65289. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65290. +
  65291. + pcgcctl.d32 = 0;
  65292. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  65293. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65294. +
  65295. + pcgcctl.d32 = 0;
  65296. + pcgcctl.b.extnd_hiber_switch = 1;
  65297. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  65298. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  65299. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  65300. +
  65301. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  65302. +
  65303. + return 1;
  65304. + }
  65305. + }
  65306. + } else {
  65307. + if (core_if->op_state == A_PERIPHERAL) {
  65308. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  65309. + /* Clear the a_peripheral flag, back to a_host. */
  65310. + DWC_SPINUNLOCK(core_if->lock);
  65311. + cil_pcd_stop(core_if);
  65312. + cil_hcd_start(core_if);
  65313. + DWC_SPINLOCK(core_if->lock);
  65314. + core_if->op_state = A_HOST;
  65315. + }
  65316. + }
  65317. +
  65318. + /* Change to L2(suspend) state */
  65319. + core_if->lx_state = DWC_OTG_L2;
  65320. +
  65321. + /* Clear interrupt */
  65322. + gintsts.d32 = 0;
  65323. + gintsts.b.usbsuspend = 1;
  65324. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65325. +
  65326. + return 1;
  65327. +}
  65328. +
  65329. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  65330. +{
  65331. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65332. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65333. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  65334. +
  65335. + dwc_udelay(10);
  65336. +
  65337. + /* Program GPIO register while entering to xHib */
  65338. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  65339. +
  65340. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  65341. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  65342. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65343. + dwc_udelay(10);
  65344. +
  65345. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  65346. + gpwrdn.b.restore = 1;
  65347. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  65348. + dwc_udelay(10);
  65349. +
  65350. + restore_lpm_i2c_regs(core_if);
  65351. +
  65352. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  65353. + pcgcctl.b.max_xcvrselect = 1;
  65354. + pcgcctl.b.ess_reg_restored = 0;
  65355. + pcgcctl.b.extnd_hiber_switch = 0;
  65356. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  65357. + pcgcctl.b.enbl_extnd_hiber = 1;
  65358. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65359. +
  65360. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  65361. + gahbcfg.b.glblintrmsk = 1;
  65362. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  65363. +
  65364. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  65365. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  65366. +
  65367. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  65368. + core_if->gr_backup->gusbcfg_local);
  65369. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  65370. + core_if->dr_backup->dcfg);
  65371. +
  65372. + pcgcctl.d32 = 0;
  65373. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  65374. + pcgcctl.b.max_xcvrselect = 1;
  65375. + pcgcctl.d32 |= 0x608;
  65376. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65377. + dwc_udelay(10);
  65378. +
  65379. + pcgcctl.d32 = 0;
  65380. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  65381. + pcgcctl.b.max_xcvrselect = 1;
  65382. + pcgcctl.b.ess_reg_restored = 1;
  65383. + pcgcctl.b.enbl_extnd_hiber = 1;
  65384. + pcgcctl.b.rstpdwnmodule = 1;
  65385. + pcgcctl.b.restoremode = 1;
  65386. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  65387. +
  65388. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  65389. +
  65390. + return 1;
  65391. +}
  65392. +
  65393. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65394. +/**
  65395. + * This function hadles LPM transaction received interrupt.
  65396. + */
  65397. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  65398. +{
  65399. + glpmcfg_data_t lpmcfg;
  65400. + gintsts_data_t gintsts;
  65401. +
  65402. + if (!core_if->core_params->lpm_enable) {
  65403. + DWC_PRINTF("Unexpected LPM interrupt\n");
  65404. + }
  65405. +
  65406. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  65407. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  65408. +
  65409. + if (dwc_otg_is_host_mode(core_if)) {
  65410. + cil_hcd_sleep(core_if);
  65411. + } else {
  65412. + lpmcfg.b.hird_thres |= (1 << 4);
  65413. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  65414. + lpmcfg.d32);
  65415. + }
  65416. +
  65417. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  65418. + dwc_udelay(10);
  65419. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  65420. + if (lpmcfg.b.prt_sleep_sts) {
  65421. + /* Save the current state */
  65422. + core_if->lx_state = DWC_OTG_L1;
  65423. + }
  65424. +
  65425. + /* Clear interrupt */
  65426. + gintsts.d32 = 0;
  65427. + gintsts.b.lpmtranrcvd = 1;
  65428. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65429. + return 1;
  65430. +}
  65431. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  65432. +
  65433. +/**
  65434. + * This function returns the Core Interrupt register.
  65435. + */
  65436. +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk)
  65437. +{
  65438. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  65439. + gintsts_data_t gintsts;
  65440. + gintmsk_data_t gintmsk;
  65441. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  65442. + gintmsk_common.b.wkupintr = 1;
  65443. + gintmsk_common.b.sessreqintr = 1;
  65444. + gintmsk_common.b.conidstschng = 1;
  65445. + gintmsk_common.b.otgintr = 1;
  65446. + gintmsk_common.b.modemismatch = 1;
  65447. + gintmsk_common.b.disconnect = 1;
  65448. + gintmsk_common.b.usbsuspend = 1;
  65449. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65450. + gintmsk_common.b.lpmtranrcvd = 1;
  65451. +#endif
  65452. + gintmsk_common.b.restoredone = 1;
  65453. + if(dwc_otg_is_device_mode(core_if))
  65454. + {
  65455. + /** @todo: The port interrupt occurs while in device
  65456. + * mode. Added code to CIL to clear the interrupt for now!
  65457. + */
  65458. + gintmsk_common.b.portintr = 1;
  65459. + }
  65460. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  65461. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  65462. + {
  65463. + unsigned long flags;
  65464. +
  65465. + // Re-enable the saved interrupts
  65466. + local_irq_save(flags);
  65467. + local_fiq_disable();
  65468. + gintmsk.d32 |= gintmsk_common.d32;
  65469. + gintsts_saved.d32 &= ~gintmsk_common.d32;
  65470. + reenable_gintmsk->d32 = gintmsk.d32;
  65471. + local_irq_restore(flags);
  65472. + }
  65473. +
  65474. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  65475. +
  65476. +#ifdef DEBUG
  65477. + /* if any common interrupts set */
  65478. + if (gintsts.d32 & gintmsk_common.d32) {
  65479. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  65480. + gintsts.d32, gintmsk.d32);
  65481. + }
  65482. +#endif
  65483. + if (!fiq_fix_enable){
  65484. + if (gahbcfg.b.glblintrmsk)
  65485. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  65486. + else
  65487. + return 0;
  65488. + }
  65489. + else {
  65490. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  65491. + }
  65492. +
  65493. +}
  65494. +
  65495. +/* MACRO for clearing interupt bits in GPWRDN register */
  65496. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  65497. +do { \
  65498. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  65499. + gpwrdn.b.__intr = 1; \
  65500. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  65501. + 0, gpwrdn.d32); \
  65502. +} while (0)
  65503. +
  65504. +/**
  65505. + * Common interrupt handler.
  65506. + *
  65507. + * The common interrupts are those that occur in both Host and Device mode.
  65508. + * This handler handles the following interrupts:
  65509. + * - Mode Mismatch Interrupt
  65510. + * - Disconnect Interrupt
  65511. + * - OTG Interrupt
  65512. + * - Connector ID Status Change Interrupt
  65513. + * - Session Request Interrupt.
  65514. + * - Resume / Remote Wakeup Detected Interrupt.
  65515. + * - LPM Transaction Received Interrupt
  65516. + * - ADP Transaction Received Interrupt
  65517. + *
  65518. + */
  65519. +int32_t dwc_otg_handle_common_intr(void *dev)
  65520. +{
  65521. + int retval = 0;
  65522. + gintsts_data_t gintsts;
  65523. + gintmsk_data_t reenable_gintmsk;
  65524. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65525. + dwc_otg_device_t *otg_dev = dev;
  65526. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  65527. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  65528. + if (dwc_otg_is_device_mode(core_if))
  65529. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  65530. +
  65531. + if (core_if->lock)
  65532. + DWC_SPINLOCK(core_if->lock);
  65533. +
  65534. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  65535. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  65536. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  65537. + core_if->xhib = 2;
  65538. + if (core_if->lock)
  65539. + DWC_SPINUNLOCK(core_if->lock);
  65540. +
  65541. + return retval;
  65542. + }
  65543. +
  65544. + if (core_if->hibernation_suspend <= 0) {
  65545. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &reenable_gintmsk);
  65546. +
  65547. + if (gintsts.b.modemismatch) {
  65548. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  65549. + }
  65550. + if (gintsts.b.otgintr) {
  65551. + retval |= dwc_otg_handle_otg_intr(core_if);
  65552. + }
  65553. + if (gintsts.b.conidstschng) {
  65554. + retval |=
  65555. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  65556. + }
  65557. + if (gintsts.b.disconnect) {
  65558. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  65559. + }
  65560. + if (gintsts.b.sessreqintr) {
  65561. + retval |= dwc_otg_handle_session_req_intr(core_if);
  65562. + }
  65563. + if (gintsts.b.wkupintr) {
  65564. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  65565. + }
  65566. + if (gintsts.b.usbsuspend) {
  65567. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  65568. + }
  65569. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65570. + if (gintsts.b.lpmtranrcvd) {
  65571. + retval |= dwc_otg_handle_lpm_intr(core_if);
  65572. + }
  65573. +#endif
  65574. + if (gintsts.b.restoredone) {
  65575. + gintsts.d32 = 0;
  65576. + if (core_if->power_down == 2)
  65577. + core_if->hibernation_suspend = -1;
  65578. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  65579. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  65580. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  65581. + dctl_data_t dctl = {.d32 = 0 };
  65582. +
  65583. + DWC_WRITE_REG32(&core_if->core_global_regs->
  65584. + gintsts, 0xFFFFFFFF);
  65585. +
  65586. + DWC_DEBUGPL(DBG_ANY,
  65587. + "RESTORE DONE generated\n");
  65588. +
  65589. + gpwrdn.b.restore = 1;
  65590. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  65591. + dwc_udelay(10);
  65592. +
  65593. + pcgcctl.b.rstpdwnmodule = 1;
  65594. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  65595. +
  65596. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  65597. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  65598. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  65599. + dwc_udelay(50);
  65600. +
  65601. + dctl.b.pwronprgdone = 1;
  65602. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65603. + dwc_udelay(10);
  65604. +
  65605. + dwc_otg_restore_global_regs(core_if);
  65606. + dwc_otg_restore_dev_regs(core_if, 0);
  65607. +
  65608. + dctl.d32 = 0;
  65609. + dctl.b.pwronprgdone = 1;
  65610. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  65611. + dwc_udelay(10);
  65612. +
  65613. + pcgcctl.d32 = 0;
  65614. + pcgcctl.b.enbl_extnd_hiber = 1;
  65615. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  65616. +
  65617. + /* The core will be in ON STATE */
  65618. + core_if->lx_state = DWC_OTG_L0;
  65619. + core_if->xhib = 0;
  65620. +
  65621. + DWC_SPINUNLOCK(core_if->lock);
  65622. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  65623. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  65624. + }
  65625. + DWC_SPINLOCK(core_if->lock);
  65626. +
  65627. + }
  65628. +
  65629. + gintsts.b.restoredone = 1;
  65630. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  65631. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  65632. + retval |= 1;
  65633. + }
  65634. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  65635. + /* The port interrupt occurs while in device mode with HPRT0
  65636. + * Port Enable/Disable.
  65637. + */
  65638. + gintsts.d32 = 0;
  65639. + gintsts.b.portintr = 1;
  65640. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  65641. + retval |= 1;
  65642. + reenable_gintmsk.b.portintr = 1;
  65643. +
  65644. + }
  65645. +
  65646. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, reenable_gintmsk.d32);
  65647. +
  65648. + } else {
  65649. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  65650. +
  65651. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  65652. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  65653. + if (gpwrdn.b.linestate == 0) {
  65654. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  65655. + } else {
  65656. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  65657. + }
  65658. +
  65659. + retval |= 1;
  65660. + }
  65661. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  65662. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  65663. + /* remote wakeup from hibernation */
  65664. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  65665. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  65666. + } else {
  65667. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  65668. + }
  65669. + retval |= 1;
  65670. + }
  65671. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  65672. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  65673. + if (gpwrdn.b.linestate == 0) {
  65674. + DWC_PRINTF("Reset detected\n");
  65675. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  65676. + }
  65677. + }
  65678. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  65679. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  65680. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  65681. + retval |= 1;
  65682. + }
  65683. + }
  65684. + /* Handle ADP interrupt here */
  65685. + if (gpwrdn.b.adp_int) {
  65686. + DWC_PRINTF("ADP interrupt\n");
  65687. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  65688. + dwc_otg_adp_handle_intr(core_if);
  65689. + retval |= 1;
  65690. + }
  65691. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  65692. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  65693. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  65694. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  65695. +
  65696. + retval |= 1;
  65697. + }
  65698. + if (core_if->lock)
  65699. + DWC_SPINUNLOCK(core_if->lock);
  65700. +
  65701. + return retval;
  65702. +}
  65703. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_core_if.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  65704. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 1970-01-01 01:00:00.000000000 +0100
  65705. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 2014-04-24 15:35:04.173565776 +0200
  65706. @@ -0,0 +1,705 @@
  65707. +/* ==========================================================================
  65708. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  65709. + * $Revision: #13 $
  65710. + * $Date: 2012/08/10 $
  65711. + * $Change: 2047372 $
  65712. + *
  65713. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  65714. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  65715. + * otherwise expressly agreed to in writing between Synopsys and you.
  65716. + *
  65717. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  65718. + * any End User Software License Agreement or Agreement for Licensed Product
  65719. + * with Synopsys or any supplement thereto. You are permitted to use and
  65720. + * redistribute this Software in source and binary forms, with or without
  65721. + * modification, provided that redistributions of source code must retain this
  65722. + * notice. You may not view, use, disclose, copy or distribute this file or
  65723. + * any information contained herein except pursuant to this license grant from
  65724. + * Synopsys. If you do not agree with this notice, including the disclaimer
  65725. + * below, then you are not authorized to use the Software.
  65726. + *
  65727. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  65728. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65729. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  65730. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  65731. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  65732. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65733. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  65734. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  65735. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65736. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  65737. + * DAMAGE.
  65738. + * ========================================================================== */
  65739. +#if !defined(__DWC_CORE_IF_H__)
  65740. +#define __DWC_CORE_IF_H__
  65741. +
  65742. +#include "dwc_os.h"
  65743. +
  65744. +/** @file
  65745. + * This file defines DWC_OTG Core API
  65746. + */
  65747. +
  65748. +struct dwc_otg_core_if;
  65749. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  65750. +
  65751. +/** Maximum number of Periodic FIFOs */
  65752. +#define MAX_PERIO_FIFOS 15
  65753. +/** Maximum number of Periodic FIFOs */
  65754. +#define MAX_TX_FIFOS 15
  65755. +
  65756. +/** Maximum number of Endpoints/HostChannels */
  65757. +#define MAX_EPS_CHANNELS 16
  65758. +
  65759. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  65760. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  65761. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  65762. +
  65763. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  65764. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  65765. +
  65766. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  65767. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  65768. +
  65769. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  65770. +
  65771. +/** This function should be called on every hardware interrupt. */
  65772. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  65773. +
  65774. +/** @name OTG Core Parameters */
  65775. +/** @{ */
  65776. +
  65777. +/**
  65778. + * Specifies the OTG capabilities. The driver will automatically
  65779. + * detect the value for this parameter if none is specified.
  65780. + * 0 - HNP and SRP capable (default)
  65781. + * 1 - SRP Only capable
  65782. + * 2 - No HNP/SRP capable
  65783. + */
  65784. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  65785. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  65786. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  65787. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  65788. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  65789. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  65790. +
  65791. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  65792. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  65793. +#define dwc_param_opt_default 1
  65794. +
  65795. +/**
  65796. + * Specifies whether to use slave or DMA mode for accessing the data
  65797. + * FIFOs. The driver will automatically detect the value for this
  65798. + * parameter if none is specified.
  65799. + * 0 - Slave
  65800. + * 1 - DMA (default, if available)
  65801. + */
  65802. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  65803. + int32_t val);
  65804. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  65805. +#define dwc_param_dma_enable_default 1
  65806. +
  65807. +/**
  65808. + * When DMA mode is enabled specifies whether to use
  65809. + * address DMA or DMA Descritor mode for accessing the data
  65810. + * FIFOs in device mode. The driver will automatically detect
  65811. + * the value for this parameter if none is specified.
  65812. + * 0 - address DMA
  65813. + * 1 - DMA Descriptor(default, if available)
  65814. + */
  65815. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  65816. + int32_t val);
  65817. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  65818. +//#define dwc_param_dma_desc_enable_default 1
  65819. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  65820. +
  65821. +/** The DMA Burst size (applicable only for External DMA
  65822. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  65823. + */
  65824. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  65825. + int32_t val);
  65826. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  65827. +#define dwc_param_dma_burst_size_default 32
  65828. +
  65829. +/**
  65830. + * Specifies the maximum speed of operation in host and device mode.
  65831. + * The actual speed depends on the speed of the attached device and
  65832. + * the value of phy_type. The actual speed depends on the speed of the
  65833. + * attached device.
  65834. + * 0 - High Speed (default)
  65835. + * 1 - Full Speed
  65836. + */
  65837. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  65838. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  65839. +#define dwc_param_speed_default 0
  65840. +#define DWC_SPEED_PARAM_HIGH 0
  65841. +#define DWC_SPEED_PARAM_FULL 1
  65842. +
  65843. +/** Specifies whether low power mode is supported when attached
  65844. + * to a Full Speed or Low Speed device in host mode.
  65845. + * 0 - Don't support low power mode (default)
  65846. + * 1 - Support low power mode
  65847. + */
  65848. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  65849. + core_if, int32_t val);
  65850. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  65851. + * core_if);
  65852. +#define dwc_param_host_support_fs_ls_low_power_default 0
  65853. +
  65854. +/** Specifies the PHY clock rate in low power mode when connected to a
  65855. + * Low Speed device in host mode. This parameter is applicable only if
  65856. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  65857. + * then defaults to 6 MHZ otherwise 48 MHZ.
  65858. + *
  65859. + * 0 - 48 MHz
  65860. + * 1 - 6 MHz
  65861. + */
  65862. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  65863. + core_if, int32_t val);
  65864. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  65865. + core_if);
  65866. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  65867. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  65868. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  65869. +
  65870. +/**
  65871. + * 0 - Use cC FIFO size parameters
  65872. + * 1 - Allow dynamic FIFO sizing (default)
  65873. + */
  65874. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  65875. + int32_t val);
  65876. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  65877. + core_if);
  65878. +#define dwc_param_enable_dynamic_fifo_default 1
  65879. +
  65880. +/** Total number of 4-byte words in the data FIFO memory. This
  65881. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  65882. + * Tx FIFOs.
  65883. + * 32 to 32768 (default 8192)
  65884. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  65885. + */
  65886. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  65887. + int32_t val);
  65888. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  65889. +//#define dwc_param_data_fifo_size_default 8192
  65890. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  65891. +
  65892. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  65893. + * FIFO sizing is enabled.
  65894. + * 16 to 32768 (default 1064)
  65895. + */
  65896. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  65897. + int32_t val);
  65898. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  65899. +#define dwc_param_dev_rx_fifo_size_default 1064
  65900. +
  65901. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  65902. + * when dynamic FIFO sizing is enabled.
  65903. + * 16 to 32768 (default 1024)
  65904. + */
  65905. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65906. + core_if, int32_t val);
  65907. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65908. + core_if);
  65909. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  65910. +
  65911. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  65912. + * mode when dynamic FIFO sizing is enabled.
  65913. + * 4 to 768 (default 256)
  65914. + */
  65915. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  65916. + int32_t val, int fifo_num);
  65917. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  65918. + core_if, int fifo_num);
  65919. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  65920. +
  65921. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  65922. + * FIFO sizing is enabled.
  65923. + * 16 to 32768 (default 1024)
  65924. + */
  65925. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  65926. + int32_t val);
  65927. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  65928. +//#define dwc_param_host_rx_fifo_size_default 1024
  65929. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  65930. +
  65931. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  65932. + * when Dynamic FIFO sizing is enabled in the core.
  65933. + * 16 to 32768 (default 1024)
  65934. + */
  65935. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65936. + core_if, int32_t val);
  65937. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65938. + core_if);
  65939. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  65940. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  65941. +
  65942. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  65943. + * FIFO sizing is enabled.
  65944. + * 16 to 32768 (default 1024)
  65945. + */
  65946. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  65947. + core_if, int32_t val);
  65948. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  65949. + core_if);
  65950. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  65951. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  65952. +
  65953. +/** The maximum transfer size supported in bytes.
  65954. + * 2047 to 65,535 (default 65,535)
  65955. + */
  65956. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  65957. + int32_t val);
  65958. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  65959. +#define dwc_param_max_transfer_size_default 65535
  65960. +
  65961. +/** The maximum number of packets in a transfer.
  65962. + * 15 to 511 (default 511)
  65963. + */
  65964. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  65965. + int32_t val);
  65966. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  65967. +#define dwc_param_max_packet_count_default 511
  65968. +
  65969. +/** The number of host channel registers to use.
  65970. + * 1 to 16 (default 12)
  65971. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  65972. + */
  65973. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  65974. + int32_t val);
  65975. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  65976. +//#define dwc_param_host_channels_default 12
  65977. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  65978. +
  65979. +/** The number of endpoints in addition to EP0 available for device
  65980. + * mode operations.
  65981. + * 1 to 15 (default 6 IN and OUT)
  65982. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  65983. + * endpoints in addition to EP0.
  65984. + */
  65985. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  65986. + int32_t val);
  65987. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  65988. +#define dwc_param_dev_endpoints_default 6
  65989. +
  65990. +/**
  65991. + * Specifies the type of PHY interface to use. By default, the driver
  65992. + * will automatically detect the phy_type.
  65993. + *
  65994. + * 0 - Full Speed PHY
  65995. + * 1 - UTMI+ (default)
  65996. + * 2 - ULPI
  65997. + */
  65998. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  65999. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  66000. +#define DWC_PHY_TYPE_PARAM_FS 0
  66001. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  66002. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  66003. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  66004. +
  66005. +/**
  66006. + * Specifies the UTMI+ Data Width. This parameter is
  66007. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  66008. + * PHY_TYPE, this parameter indicates the data width between
  66009. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  66010. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  66011. + * to "8 and 16 bits", meaning that the core has been
  66012. + * configured to work at either data path width.
  66013. + *
  66014. + * 8 or 16 bits (default 16)
  66015. + */
  66016. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  66017. + int32_t val);
  66018. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  66019. +//#define dwc_param_phy_utmi_width_default 16
  66020. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  66021. +
  66022. +/**
  66023. + * Specifies whether the ULPI operates at double or single
  66024. + * data rate. This parameter is only applicable if PHY_TYPE is
  66025. + * ULPI.
  66026. + *
  66027. + * 0 - single data rate ULPI interface with 8 bit wide data
  66028. + * bus (default)
  66029. + * 1 - double data rate ULPI interface with 4 bit wide data
  66030. + * bus
  66031. + */
  66032. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  66033. + int32_t val);
  66034. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  66035. +#define dwc_param_phy_ulpi_ddr_default 0
  66036. +
  66037. +/**
  66038. + * Specifies whether to use the internal or external supply to
  66039. + * drive the vbus with a ULPI phy.
  66040. + */
  66041. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  66042. + int32_t val);
  66043. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  66044. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  66045. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  66046. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  66047. +
  66048. +/**
  66049. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  66050. + * parameter is only applicable if PHY_TYPE is FS.
  66051. + * 0 - No (default)
  66052. + * 1 - Yes
  66053. + */
  66054. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  66055. + int32_t val);
  66056. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  66057. +#define dwc_param_i2c_enable_default 0
  66058. +
  66059. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  66060. + int32_t val);
  66061. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  66062. +#define dwc_param_ulpi_fs_ls_default 0
  66063. +
  66064. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  66065. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  66066. +#define dwc_param_ts_dline_default 0
  66067. +
  66068. +/**
  66069. + * Specifies whether dedicated transmit FIFOs are
  66070. + * enabled for non periodic IN endpoints in device mode
  66071. + * 0 - No
  66072. + * 1 - Yes
  66073. + */
  66074. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  66075. + int32_t val);
  66076. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  66077. + core_if);
  66078. +#define dwc_param_en_multiple_tx_fifo_default 1
  66079. +
  66080. +/** Number of 4-byte words in each of the Tx FIFOs in device
  66081. + * mode when dynamic FIFO sizing is enabled.
  66082. + * 4 to 768 (default 256)
  66083. + */
  66084. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  66085. + int fifo_num, int32_t val);
  66086. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  66087. + int fifo_num);
  66088. +#define dwc_param_dev_tx_fifo_size_default 768
  66089. +
  66090. +/** Thresholding enable flag-
  66091. + * bit 0 - enable non-ISO Tx thresholding
  66092. + * bit 1 - enable ISO Tx thresholding
  66093. + * bit 2 - enable Rx thresholding
  66094. + */
  66095. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  66096. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  66097. +#define dwc_param_thr_ctl_default 0
  66098. +
  66099. +/** Thresholding length for Tx
  66100. + * FIFOs in 32 bit DWORDs
  66101. + */
  66102. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  66103. + int32_t val);
  66104. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  66105. +#define dwc_param_tx_thr_length_default 64
  66106. +
  66107. +/** Thresholding length for Rx
  66108. + * FIFOs in 32 bit DWORDs
  66109. + */
  66110. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  66111. + int32_t val);
  66112. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  66113. +#define dwc_param_rx_thr_length_default 64
  66114. +
  66115. +/**
  66116. + * Specifies whether LPM (Link Power Management) support is enabled
  66117. + */
  66118. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  66119. + int32_t val);
  66120. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  66121. +#define dwc_param_lpm_enable_default 1
  66122. +
  66123. +/**
  66124. + * Specifies whether PTI enhancement is enabled
  66125. + */
  66126. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  66127. + int32_t val);
  66128. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  66129. +#define dwc_param_pti_enable_default 0
  66130. +
  66131. +/**
  66132. + * Specifies whether MPI enhancement is enabled
  66133. + */
  66134. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  66135. + int32_t val);
  66136. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  66137. +#define dwc_param_mpi_enable_default 0
  66138. +
  66139. +/**
  66140. + * Specifies whether ADP capability is enabled
  66141. + */
  66142. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  66143. + int32_t val);
  66144. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  66145. +#define dwc_param_adp_enable_default 0
  66146. +
  66147. +/**
  66148. + * Specifies whether IC_USB capability is enabled
  66149. + */
  66150. +
  66151. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  66152. + int32_t val);
  66153. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  66154. +#define dwc_param_ic_usb_cap_default 0
  66155. +
  66156. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  66157. + int32_t val);
  66158. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  66159. +#define dwc_param_ahb_thr_ratio_default 0
  66160. +
  66161. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  66162. + int32_t val);
  66163. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  66164. +#define dwc_param_power_down_default 0
  66165. +
  66166. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  66167. + int32_t val);
  66168. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  66169. +#define dwc_param_reload_ctl_default 0
  66170. +
  66171. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  66172. + int32_t val);
  66173. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  66174. +#define dwc_param_dev_out_nak_default 0
  66175. +
  66176. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  66177. + int32_t val);
  66178. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  66179. +#define dwc_param_cont_on_bna_default 0
  66180. +
  66181. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  66182. + int32_t val);
  66183. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  66184. +#define dwc_param_ahb_single_default 0
  66185. +
  66186. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  66187. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  66188. +#define dwc_param_otg_ver_default 0
  66189. +
  66190. +/** @} */
  66191. +
  66192. +/** @name Access to registers and bit-fields */
  66193. +
  66194. +/**
  66195. + * Dump core registers and SPRAM
  66196. + */
  66197. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  66198. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  66199. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  66200. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  66201. +
  66202. +/**
  66203. + * Get host negotiation status.
  66204. + */
  66205. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  66206. +
  66207. +/**
  66208. + * Get srp status
  66209. + */
  66210. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  66211. +
  66212. +/**
  66213. + * Set hnpreq bit in the GOTGCTL register.
  66214. + */
  66215. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  66216. +
  66217. +/**
  66218. + * Get Content of SNPSID register.
  66219. + */
  66220. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  66221. +
  66222. +/**
  66223. + * Get current mode.
  66224. + * Returns 0 if in device mode, and 1 if in host mode.
  66225. + */
  66226. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  66227. +
  66228. +/**
  66229. + * Get value of hnpcapable field in the GUSBCFG register
  66230. + */
  66231. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  66232. +/**
  66233. + * Set value of hnpcapable field in the GUSBCFG register
  66234. + */
  66235. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  66236. +
  66237. +/**
  66238. + * Get value of srpcapable field in the GUSBCFG register
  66239. + */
  66240. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  66241. +/**
  66242. + * Set value of srpcapable field in the GUSBCFG register
  66243. + */
  66244. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  66245. +
  66246. +/**
  66247. + * Get value of devspeed field in the DCFG register
  66248. + */
  66249. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  66250. +/**
  66251. + * Set value of devspeed field in the DCFG register
  66252. + */
  66253. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  66254. +
  66255. +/**
  66256. + * Get the value of busconnected field from the HPRT0 register
  66257. + */
  66258. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  66259. +
  66260. +/**
  66261. + * Gets the device enumeration Speed.
  66262. + */
  66263. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  66264. +
  66265. +/**
  66266. + * Get value of prtpwr field from the HPRT0 register
  66267. + */
  66268. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  66269. +
  66270. +/**
  66271. + * Get value of flag indicating core state - hibernated or not
  66272. + */
  66273. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  66274. +
  66275. +/**
  66276. + * Set value of prtpwr field from the HPRT0 register
  66277. + */
  66278. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  66279. +
  66280. +/**
  66281. + * Get value of prtsusp field from the HPRT0 regsiter
  66282. + */
  66283. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  66284. +/**
  66285. + * Set value of prtpwr field from the HPRT0 register
  66286. + */
  66287. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  66288. +
  66289. +/**
  66290. + * Get value of ModeChTimEn field from the HCFG regsiter
  66291. + */
  66292. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  66293. +/**
  66294. + * Set value of ModeChTimEn field from the HCFG regsiter
  66295. + */
  66296. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  66297. +
  66298. +/**
  66299. + * Get value of Fram Interval field from the HFIR regsiter
  66300. + */
  66301. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  66302. +/**
  66303. + * Set value of Frame Interval field from the HFIR regsiter
  66304. + */
  66305. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  66306. +
  66307. +/**
  66308. + * Set value of prtres field from the HPRT0 register
  66309. + *FIXME Remove?
  66310. + */
  66311. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  66312. +
  66313. +/**
  66314. + * Get value of rmtwkupsig bit in DCTL register
  66315. + */
  66316. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  66317. +
  66318. +/**
  66319. + * Get value of prt_sleep_sts field from the GLPMCFG register
  66320. + */
  66321. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  66322. +
  66323. +/**
  66324. + * Get value of rem_wkup_en field from the GLPMCFG register
  66325. + */
  66326. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  66327. +
  66328. +/**
  66329. + * Get value of appl_resp field from the GLPMCFG register
  66330. + */
  66331. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  66332. +/**
  66333. + * Set value of appl_resp field from the GLPMCFG register
  66334. + */
  66335. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  66336. +
  66337. +/**
  66338. + * Get value of hsic_connect field from the GLPMCFG register
  66339. + */
  66340. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  66341. +/**
  66342. + * Set value of hsic_connect field from the GLPMCFG register
  66343. + */
  66344. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  66345. +
  66346. +/**
  66347. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  66348. + */
  66349. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  66350. +/**
  66351. + * Set value of inv_sel_hsic field from the GLPMFG register.
  66352. + */
  66353. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  66354. +
  66355. +/*
  66356. + * Some functions for accessing registers
  66357. + */
  66358. +
  66359. +/**
  66360. + * GOTGCTL register
  66361. + */
  66362. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  66363. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  66364. +
  66365. +/**
  66366. + * GUSBCFG register
  66367. + */
  66368. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  66369. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  66370. +
  66371. +/**
  66372. + * GRXFSIZ register
  66373. + */
  66374. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  66375. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  66376. +
  66377. +/**
  66378. + * GNPTXFSIZ register
  66379. + */
  66380. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  66381. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  66382. +
  66383. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  66384. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  66385. +
  66386. +/**
  66387. + * GGPIO register
  66388. + */
  66389. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  66390. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  66391. +
  66392. +/**
  66393. + * GUID register
  66394. + */
  66395. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  66396. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  66397. +
  66398. +/**
  66399. + * HPRT0 register
  66400. + */
  66401. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  66402. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  66403. +
  66404. +/**
  66405. + * GHPTXFSIZE
  66406. + */
  66407. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  66408. +
  66409. +/** @} */
  66410. +
  66411. +#endif /* __DWC_CORE_IF_H__ */
  66412. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_dbg.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  66413. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 1970-01-01 01:00:00.000000000 +0100
  66414. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 2014-04-24 15:35:04.173565776 +0200
  66415. @@ -0,0 +1,117 @@
  66416. +/* ==========================================================================
  66417. + *
  66418. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  66419. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  66420. + * otherwise expressly agreed to in writing between Synopsys and you.
  66421. + *
  66422. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  66423. + * any End User Software License Agreement or Agreement for Licensed Product
  66424. + * with Synopsys or any supplement thereto. You are permitted to use and
  66425. + * redistribute this Software in source and binary forms, with or without
  66426. + * modification, provided that redistributions of source code must retain this
  66427. + * notice. You may not view, use, disclose, copy or distribute this file or
  66428. + * any information contained herein except pursuant to this license grant from
  66429. + * Synopsys. If you do not agree with this notice, including the disclaimer
  66430. + * below, then you are not authorized to use the Software.
  66431. + *
  66432. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  66433. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66434. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66435. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  66436. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  66437. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  66438. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66439. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66440. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  66441. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  66442. + * DAMAGE.
  66443. + * ========================================================================== */
  66444. +
  66445. +#ifndef __DWC_OTG_DBG_H__
  66446. +#define __DWC_OTG_DBG_H__
  66447. +
  66448. +/** @file
  66449. + * This file defines debug levels.
  66450. + * Debugging support vanishes in non-debug builds.
  66451. + */
  66452. +
  66453. +/**
  66454. + * The Debug Level bit-mask variable.
  66455. + */
  66456. +extern uint32_t g_dbg_lvl;
  66457. +/**
  66458. + * Set the Debug Level variable.
  66459. + */
  66460. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  66461. +{
  66462. + uint32_t old = g_dbg_lvl;
  66463. + g_dbg_lvl = new;
  66464. + return old;
  66465. +}
  66466. +
  66467. +#define DBG_USER (0x1)
  66468. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  66469. +#define DBG_CIL (0x2)
  66470. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  66471. + * messages */
  66472. +#define DBG_CILV (0x20)
  66473. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  66474. + * messages */
  66475. +#define DBG_PCD (0x4)
  66476. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  66477. + * messages */
  66478. +#define DBG_PCDV (0x40)
  66479. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  66480. +#define DBG_HCD (0x8)
  66481. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  66482. + * messages */
  66483. +#define DBG_HCDV (0x80)
  66484. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  66485. + * mode. */
  66486. +#define DBG_HCD_URB (0x800)
  66487. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  66488. + * messages. */
  66489. +#define DBG_HCDI (0x1000)
  66490. +
  66491. +/** When debug level has any bit set, display debug messages */
  66492. +#define DBG_ANY (0xFF)
  66493. +
  66494. +/** All debug messages off */
  66495. +#define DBG_OFF 0
  66496. +
  66497. +/** Prefix string for DWC_DEBUG print macros. */
  66498. +#define USB_DWC "DWC_otg: "
  66499. +
  66500. +/**
  66501. + * Print a debug message when the Global debug level variable contains
  66502. + * the bit defined in <code>lvl</code>.
  66503. + *
  66504. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  66505. + * @param[in] x - like printf
  66506. + *
  66507. + * Example:<p>
  66508. + * <code>
  66509. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  66510. + * </code>
  66511. + * <br>
  66512. + * results in:<br>
  66513. + * <code>
  66514. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  66515. + * </code>
  66516. + */
  66517. +#ifdef DEBUG
  66518. +
  66519. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  66520. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  66521. +
  66522. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  66523. +
  66524. +#else
  66525. +
  66526. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  66527. +# define DWC_DEBUGP(x...)
  66528. +
  66529. +# define CHK_DEBUG_LEVEL(level) (0)
  66530. +
  66531. +#endif /*DEBUG*/
  66532. +#endif
  66533. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_driver.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  66534. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_driver.c 1970-01-01 01:00:00.000000000 +0100
  66535. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c 2014-04-24 15:35:04.173565776 +0200
  66536. @@ -0,0 +1,1742 @@
  66537. +/* ==========================================================================
  66538. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  66539. + * $Revision: #92 $
  66540. + * $Date: 2012/08/10 $
  66541. + * $Change: 2047372 $
  66542. + *
  66543. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  66544. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  66545. + * otherwise expressly agreed to in writing between Synopsys and you.
  66546. + *
  66547. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  66548. + * any End User Software License Agreement or Agreement for Licensed Product
  66549. + * with Synopsys or any supplement thereto. You are permitted to use and
  66550. + * redistribute this Software in source and binary forms, with or without
  66551. + * modification, provided that redistributions of source code must retain this
  66552. + * notice. You may not view, use, disclose, copy or distribute this file or
  66553. + * any information contained herein except pursuant to this license grant from
  66554. + * Synopsys. If you do not agree with this notice, including the disclaimer
  66555. + * below, then you are not authorized to use the Software.
  66556. + *
  66557. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  66558. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66559. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66560. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  66561. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  66562. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  66563. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66564. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66565. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  66566. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  66567. + * DAMAGE.
  66568. + * ========================================================================== */
  66569. +
  66570. +/** @file
  66571. + * The dwc_otg_driver module provides the initialization and cleanup entry
  66572. + * points for the DWC_otg driver. This module will be dynamically installed
  66573. + * after Linux is booted using the insmod command. When the module is
  66574. + * installed, the dwc_otg_driver_init function is called. When the module is
  66575. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  66576. + *
  66577. + * This module also defines a data structure for the dwc_otg_driver, which is
  66578. + * used in conjunction with the standard ARM lm_device structure. These
  66579. + * structures allow the OTG driver to comply with the standard Linux driver
  66580. + * model in which devices and drivers are registered with a bus driver. This
  66581. + * has the benefit that Linux can expose attributes of the driver and device
  66582. + * in its special sysfs file system. Users can then read or write files in
  66583. + * this file system to perform diagnostics on the driver components or the
  66584. + * device.
  66585. + */
  66586. +
  66587. +#include "dwc_otg_os_dep.h"
  66588. +#include "dwc_os.h"
  66589. +#include "dwc_otg_dbg.h"
  66590. +#include "dwc_otg_driver.h"
  66591. +#include "dwc_otg_attr.h"
  66592. +#include "dwc_otg_core_if.h"
  66593. +#include "dwc_otg_pcd_if.h"
  66594. +#include "dwc_otg_hcd_if.h"
  66595. +
  66596. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  66597. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  66598. +
  66599. +bool microframe_schedule=true;
  66600. +
  66601. +static const char dwc_driver_name[] = "dwc_otg";
  66602. +
  66603. +extern void* dummy_send;
  66604. +
  66605. +extern int pcd_init(
  66606. +#ifdef LM_INTERFACE
  66607. + struct lm_device *_dev
  66608. +#elif defined(PCI_INTERFACE)
  66609. + struct pci_dev *_dev
  66610. +#elif defined(PLATFORM_INTERFACE)
  66611. + struct platform_device *dev
  66612. +#endif
  66613. + );
  66614. +extern int hcd_init(
  66615. +#ifdef LM_INTERFACE
  66616. + struct lm_device *_dev
  66617. +#elif defined(PCI_INTERFACE)
  66618. + struct pci_dev *_dev
  66619. +#elif defined(PLATFORM_INTERFACE)
  66620. + struct platform_device *dev
  66621. +#endif
  66622. + );
  66623. +
  66624. +extern int pcd_remove(
  66625. +#ifdef LM_INTERFACE
  66626. + struct lm_device *_dev
  66627. +#elif defined(PCI_INTERFACE)
  66628. + struct pci_dev *_dev
  66629. +#elif defined(PLATFORM_INTERFACE)
  66630. + struct platform_device *_dev
  66631. +#endif
  66632. + );
  66633. +
  66634. +extern void hcd_remove(
  66635. +#ifdef LM_INTERFACE
  66636. + struct lm_device *_dev
  66637. +#elif defined(PCI_INTERFACE)
  66638. + struct pci_dev *_dev
  66639. +#elif defined(PLATFORM_INTERFACE)
  66640. + struct platform_device *_dev
  66641. +#endif
  66642. + );
  66643. +
  66644. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  66645. +
  66646. +/*-------------------------------------------------------------------------*/
  66647. +/* Encapsulate the module parameter settings */
  66648. +
  66649. +struct dwc_otg_driver_module_params {
  66650. + int32_t opt;
  66651. + int32_t otg_cap;
  66652. + int32_t dma_enable;
  66653. + int32_t dma_desc_enable;
  66654. + int32_t dma_burst_size;
  66655. + int32_t speed;
  66656. + int32_t host_support_fs_ls_low_power;
  66657. + int32_t host_ls_low_power_phy_clk;
  66658. + int32_t enable_dynamic_fifo;
  66659. + int32_t data_fifo_size;
  66660. + int32_t dev_rx_fifo_size;
  66661. + int32_t dev_nperio_tx_fifo_size;
  66662. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  66663. + int32_t host_rx_fifo_size;
  66664. + int32_t host_nperio_tx_fifo_size;
  66665. + int32_t host_perio_tx_fifo_size;
  66666. + int32_t max_transfer_size;
  66667. + int32_t max_packet_count;
  66668. + int32_t host_channels;
  66669. + int32_t dev_endpoints;
  66670. + int32_t phy_type;
  66671. + int32_t phy_utmi_width;
  66672. + int32_t phy_ulpi_ddr;
  66673. + int32_t phy_ulpi_ext_vbus;
  66674. + int32_t i2c_enable;
  66675. + int32_t ulpi_fs_ls;
  66676. + int32_t ts_dline;
  66677. + int32_t en_multiple_tx_fifo;
  66678. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  66679. + uint32_t thr_ctl;
  66680. + uint32_t tx_thr_length;
  66681. + uint32_t rx_thr_length;
  66682. + int32_t pti_enable;
  66683. + int32_t mpi_enable;
  66684. + int32_t lpm_enable;
  66685. + int32_t ic_usb_cap;
  66686. + int32_t ahb_thr_ratio;
  66687. + int32_t power_down;
  66688. + int32_t reload_ctl;
  66689. + int32_t dev_out_nak;
  66690. + int32_t cont_on_bna;
  66691. + int32_t ahb_single;
  66692. + int32_t otg_ver;
  66693. + int32_t adp_enable;
  66694. +};
  66695. +
  66696. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  66697. + .opt = -1,
  66698. + .otg_cap = -1,
  66699. + .dma_enable = -1,
  66700. + .dma_desc_enable = -1,
  66701. + .dma_burst_size = -1,
  66702. + .speed = -1,
  66703. + .host_support_fs_ls_low_power = -1,
  66704. + .host_ls_low_power_phy_clk = -1,
  66705. + .enable_dynamic_fifo = -1,
  66706. + .data_fifo_size = -1,
  66707. + .dev_rx_fifo_size = -1,
  66708. + .dev_nperio_tx_fifo_size = -1,
  66709. + .dev_perio_tx_fifo_size = {
  66710. + /* dev_perio_tx_fifo_size_1 */
  66711. + -1,
  66712. + -1,
  66713. + -1,
  66714. + -1,
  66715. + -1,
  66716. + -1,
  66717. + -1,
  66718. + -1,
  66719. + -1,
  66720. + -1,
  66721. + -1,
  66722. + -1,
  66723. + -1,
  66724. + -1,
  66725. + -1
  66726. + /* 15 */
  66727. + },
  66728. + .host_rx_fifo_size = -1,
  66729. + .host_nperio_tx_fifo_size = -1,
  66730. + .host_perio_tx_fifo_size = -1,
  66731. + .max_transfer_size = -1,
  66732. + .max_packet_count = -1,
  66733. + .host_channels = -1,
  66734. + .dev_endpoints = -1,
  66735. + .phy_type = -1,
  66736. + .phy_utmi_width = -1,
  66737. + .phy_ulpi_ddr = -1,
  66738. + .phy_ulpi_ext_vbus = -1,
  66739. + .i2c_enable = -1,
  66740. + .ulpi_fs_ls = -1,
  66741. + .ts_dline = -1,
  66742. + .en_multiple_tx_fifo = -1,
  66743. + .dev_tx_fifo_size = {
  66744. + /* dev_tx_fifo_size */
  66745. + -1,
  66746. + -1,
  66747. + -1,
  66748. + -1,
  66749. + -1,
  66750. + -1,
  66751. + -1,
  66752. + -1,
  66753. + -1,
  66754. + -1,
  66755. + -1,
  66756. + -1,
  66757. + -1,
  66758. + -1,
  66759. + -1
  66760. + /* 15 */
  66761. + },
  66762. + .thr_ctl = -1,
  66763. + .tx_thr_length = -1,
  66764. + .rx_thr_length = -1,
  66765. + .pti_enable = -1,
  66766. + .mpi_enable = -1,
  66767. + .lpm_enable = 0,
  66768. + .ic_usb_cap = -1,
  66769. + .ahb_thr_ratio = -1,
  66770. + .power_down = -1,
  66771. + .reload_ctl = -1,
  66772. + .dev_out_nak = -1,
  66773. + .cont_on_bna = -1,
  66774. + .ahb_single = -1,
  66775. + .otg_ver = -1,
  66776. + .adp_enable = -1,
  66777. +};
  66778. +
  66779. +//Global variable to switch the fiq fix on or off (declared in bcm2708.c)
  66780. +extern bool fiq_fix_enable;
  66781. +// Global variable to enable the split transaction fix
  66782. +bool fiq_split_enable = true;
  66783. +//Global variable to switch the nak holdoff on or off
  66784. +bool nak_holdoff_enable = true;
  66785. +
  66786. +
  66787. +/**
  66788. + * This function shows the Driver Version.
  66789. + */
  66790. +static ssize_t version_show(struct device_driver *dev, char *buf)
  66791. +{
  66792. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  66793. + DWC_DRIVER_VERSION);
  66794. +}
  66795. +
  66796. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  66797. +
  66798. +/**
  66799. + * Global Debug Level Mask.
  66800. + */
  66801. +uint32_t g_dbg_lvl = 0; /* OFF */
  66802. +
  66803. +/**
  66804. + * This function shows the driver Debug Level.
  66805. + */
  66806. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  66807. +{
  66808. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  66809. +}
  66810. +
  66811. +/**
  66812. + * This function stores the driver Debug Level.
  66813. + */
  66814. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  66815. + size_t count)
  66816. +{
  66817. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  66818. + return count;
  66819. +}
  66820. +
  66821. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  66822. + dbg_level_store);
  66823. +
  66824. +/**
  66825. + * This function is called during module intialization
  66826. + * to pass module parameters to the DWC_OTG CORE.
  66827. + */
  66828. +static int set_parameters(dwc_otg_core_if_t * core_if)
  66829. +{
  66830. + int retval = 0;
  66831. + int i;
  66832. +
  66833. + if (dwc_otg_module_params.otg_cap != -1) {
  66834. + retval +=
  66835. + dwc_otg_set_param_otg_cap(core_if,
  66836. + dwc_otg_module_params.otg_cap);
  66837. + }
  66838. + if (dwc_otg_module_params.dma_enable != -1) {
  66839. + retval +=
  66840. + dwc_otg_set_param_dma_enable(core_if,
  66841. + dwc_otg_module_params.
  66842. + dma_enable);
  66843. + }
  66844. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  66845. + retval +=
  66846. + dwc_otg_set_param_dma_desc_enable(core_if,
  66847. + dwc_otg_module_params.
  66848. + dma_desc_enable);
  66849. + }
  66850. + if (dwc_otg_module_params.opt != -1) {
  66851. + retval +=
  66852. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  66853. + }
  66854. + if (dwc_otg_module_params.dma_burst_size != -1) {
  66855. + retval +=
  66856. + dwc_otg_set_param_dma_burst_size(core_if,
  66857. + dwc_otg_module_params.
  66858. + dma_burst_size);
  66859. + }
  66860. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  66861. + retval +=
  66862. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  66863. + dwc_otg_module_params.
  66864. + host_support_fs_ls_low_power);
  66865. + }
  66866. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  66867. + retval +=
  66868. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  66869. + dwc_otg_module_params.
  66870. + enable_dynamic_fifo);
  66871. + }
  66872. + if (dwc_otg_module_params.data_fifo_size != -1) {
  66873. + retval +=
  66874. + dwc_otg_set_param_data_fifo_size(core_if,
  66875. + dwc_otg_module_params.
  66876. + data_fifo_size);
  66877. + }
  66878. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  66879. + retval +=
  66880. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  66881. + dwc_otg_module_params.
  66882. + dev_rx_fifo_size);
  66883. + }
  66884. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  66885. + retval +=
  66886. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  66887. + dwc_otg_module_params.
  66888. + dev_nperio_tx_fifo_size);
  66889. + }
  66890. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  66891. + retval +=
  66892. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  66893. + dwc_otg_module_params.host_rx_fifo_size);
  66894. + }
  66895. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  66896. + retval +=
  66897. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  66898. + dwc_otg_module_params.
  66899. + host_nperio_tx_fifo_size);
  66900. + }
  66901. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  66902. + retval +=
  66903. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  66904. + dwc_otg_module_params.
  66905. + host_perio_tx_fifo_size);
  66906. + }
  66907. + if (dwc_otg_module_params.max_transfer_size != -1) {
  66908. + retval +=
  66909. + dwc_otg_set_param_max_transfer_size(core_if,
  66910. + dwc_otg_module_params.
  66911. + max_transfer_size);
  66912. + }
  66913. + if (dwc_otg_module_params.max_packet_count != -1) {
  66914. + retval +=
  66915. + dwc_otg_set_param_max_packet_count(core_if,
  66916. + dwc_otg_module_params.
  66917. + max_packet_count);
  66918. + }
  66919. + if (dwc_otg_module_params.host_channels != -1) {
  66920. + retval +=
  66921. + dwc_otg_set_param_host_channels(core_if,
  66922. + dwc_otg_module_params.
  66923. + host_channels);
  66924. + }
  66925. + if (dwc_otg_module_params.dev_endpoints != -1) {
  66926. + retval +=
  66927. + dwc_otg_set_param_dev_endpoints(core_if,
  66928. + dwc_otg_module_params.
  66929. + dev_endpoints);
  66930. + }
  66931. + if (dwc_otg_module_params.phy_type != -1) {
  66932. + retval +=
  66933. + dwc_otg_set_param_phy_type(core_if,
  66934. + dwc_otg_module_params.phy_type);
  66935. + }
  66936. + if (dwc_otg_module_params.speed != -1) {
  66937. + retval +=
  66938. + dwc_otg_set_param_speed(core_if,
  66939. + dwc_otg_module_params.speed);
  66940. + }
  66941. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  66942. + retval +=
  66943. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  66944. + dwc_otg_module_params.
  66945. + host_ls_low_power_phy_clk);
  66946. + }
  66947. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  66948. + retval +=
  66949. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  66950. + dwc_otg_module_params.
  66951. + phy_ulpi_ddr);
  66952. + }
  66953. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  66954. + retval +=
  66955. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  66956. + dwc_otg_module_params.
  66957. + phy_ulpi_ext_vbus);
  66958. + }
  66959. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  66960. + retval +=
  66961. + dwc_otg_set_param_phy_utmi_width(core_if,
  66962. + dwc_otg_module_params.
  66963. + phy_utmi_width);
  66964. + }
  66965. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  66966. + retval +=
  66967. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  66968. + dwc_otg_module_params.ulpi_fs_ls);
  66969. + }
  66970. + if (dwc_otg_module_params.ts_dline != -1) {
  66971. + retval +=
  66972. + dwc_otg_set_param_ts_dline(core_if,
  66973. + dwc_otg_module_params.ts_dline);
  66974. + }
  66975. + if (dwc_otg_module_params.i2c_enable != -1) {
  66976. + retval +=
  66977. + dwc_otg_set_param_i2c_enable(core_if,
  66978. + dwc_otg_module_params.
  66979. + i2c_enable);
  66980. + }
  66981. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  66982. + retval +=
  66983. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  66984. + dwc_otg_module_params.
  66985. + en_multiple_tx_fifo);
  66986. + }
  66987. + for (i = 0; i < 15; i++) {
  66988. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  66989. + retval +=
  66990. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  66991. + dwc_otg_module_params.
  66992. + dev_perio_tx_fifo_size
  66993. + [i], i);
  66994. + }
  66995. + }
  66996. +
  66997. + for (i = 0; i < 15; i++) {
  66998. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  66999. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  67000. + dwc_otg_module_params.
  67001. + dev_tx_fifo_size
  67002. + [i], i);
  67003. + }
  67004. + }
  67005. + if (dwc_otg_module_params.thr_ctl != -1) {
  67006. + retval +=
  67007. + dwc_otg_set_param_thr_ctl(core_if,
  67008. + dwc_otg_module_params.thr_ctl);
  67009. + }
  67010. + if (dwc_otg_module_params.mpi_enable != -1) {
  67011. + retval +=
  67012. + dwc_otg_set_param_mpi_enable(core_if,
  67013. + dwc_otg_module_params.
  67014. + mpi_enable);
  67015. + }
  67016. + if (dwc_otg_module_params.pti_enable != -1) {
  67017. + retval +=
  67018. + dwc_otg_set_param_pti_enable(core_if,
  67019. + dwc_otg_module_params.
  67020. + pti_enable);
  67021. + }
  67022. + if (dwc_otg_module_params.lpm_enable != -1) {
  67023. + retval +=
  67024. + dwc_otg_set_param_lpm_enable(core_if,
  67025. + dwc_otg_module_params.
  67026. + lpm_enable);
  67027. + }
  67028. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  67029. + retval +=
  67030. + dwc_otg_set_param_ic_usb_cap(core_if,
  67031. + dwc_otg_module_params.
  67032. + ic_usb_cap);
  67033. + }
  67034. + if (dwc_otg_module_params.tx_thr_length != -1) {
  67035. + retval +=
  67036. + dwc_otg_set_param_tx_thr_length(core_if,
  67037. + dwc_otg_module_params.tx_thr_length);
  67038. + }
  67039. + if (dwc_otg_module_params.rx_thr_length != -1) {
  67040. + retval +=
  67041. + dwc_otg_set_param_rx_thr_length(core_if,
  67042. + dwc_otg_module_params.
  67043. + rx_thr_length);
  67044. + }
  67045. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  67046. + retval +=
  67047. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  67048. + dwc_otg_module_params.ahb_thr_ratio);
  67049. + }
  67050. + if (dwc_otg_module_params.power_down != -1) {
  67051. + retval +=
  67052. + dwc_otg_set_param_power_down(core_if,
  67053. + dwc_otg_module_params.power_down);
  67054. + }
  67055. + if (dwc_otg_module_params.reload_ctl != -1) {
  67056. + retval +=
  67057. + dwc_otg_set_param_reload_ctl(core_if,
  67058. + dwc_otg_module_params.reload_ctl);
  67059. + }
  67060. +
  67061. + if (dwc_otg_module_params.dev_out_nak != -1) {
  67062. + retval +=
  67063. + dwc_otg_set_param_dev_out_nak(core_if,
  67064. + dwc_otg_module_params.dev_out_nak);
  67065. + }
  67066. +
  67067. + if (dwc_otg_module_params.cont_on_bna != -1) {
  67068. + retval +=
  67069. + dwc_otg_set_param_cont_on_bna(core_if,
  67070. + dwc_otg_module_params.cont_on_bna);
  67071. + }
  67072. +
  67073. + if (dwc_otg_module_params.ahb_single != -1) {
  67074. + retval +=
  67075. + dwc_otg_set_param_ahb_single(core_if,
  67076. + dwc_otg_module_params.ahb_single);
  67077. + }
  67078. +
  67079. + if (dwc_otg_module_params.otg_ver != -1) {
  67080. + retval +=
  67081. + dwc_otg_set_param_otg_ver(core_if,
  67082. + dwc_otg_module_params.otg_ver);
  67083. + }
  67084. + if (dwc_otg_module_params.adp_enable != -1) {
  67085. + retval +=
  67086. + dwc_otg_set_param_adp_enable(core_if,
  67087. + dwc_otg_module_params.
  67088. + adp_enable);
  67089. + }
  67090. + return retval;
  67091. +}
  67092. +
  67093. +/**
  67094. + * This function is the top level interrupt handler for the Common
  67095. + * (Device and host modes) interrupts.
  67096. + */
  67097. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  67098. +{
  67099. + int32_t retval = IRQ_NONE;
  67100. +
  67101. + retval = dwc_otg_handle_common_intr(dev);
  67102. + if (retval != 0) {
  67103. + S3C2410X_CLEAR_EINTPEND();
  67104. + }
  67105. + return IRQ_RETVAL(retval);
  67106. +}
  67107. +
  67108. +/**
  67109. + * This function is called when a lm_device is unregistered with the
  67110. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  67111. + * executed. The device may or may not be electrically present. If it is
  67112. + * present, the driver stops device processing. Any resources used on behalf
  67113. + * of this device are freed.
  67114. + *
  67115. + * @param _dev
  67116. + */
  67117. +#ifdef LM_INTERFACE
  67118. +#define REM_RETVAL(n)
  67119. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  67120. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  67121. +#elif defined(PCI_INTERFACE)
  67122. +#define REM_RETVAL(n)
  67123. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  67124. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  67125. +#elif defined(PLATFORM_INTERFACE)
  67126. +#define REM_RETVAL(n) n
  67127. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  67128. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  67129. +#endif
  67130. +
  67131. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  67132. +
  67133. + if (!otg_dev) {
  67134. + /* Memory allocation for the dwc_otg_device failed. */
  67135. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  67136. + return REM_RETVAL(-ENOMEM);
  67137. + }
  67138. +#ifndef DWC_DEVICE_ONLY
  67139. + if (otg_dev->hcd) {
  67140. + hcd_remove(_dev);
  67141. + } else {
  67142. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  67143. + return REM_RETVAL(-EINVAL);
  67144. + }
  67145. +#endif
  67146. +
  67147. +#ifndef DWC_HOST_ONLY
  67148. + if (otg_dev->pcd) {
  67149. + pcd_remove(_dev);
  67150. + } else {
  67151. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  67152. + return REM_RETVAL(-EINVAL);
  67153. + }
  67154. +#endif
  67155. + /*
  67156. + * Free the IRQ
  67157. + */
  67158. + if (otg_dev->common_irq_installed) {
  67159. +#ifdef PLATFORM_INTERFACE
  67160. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  67161. +#else
  67162. + free_irq(_dev->irq, otg_dev);
  67163. +#endif
  67164. + } else {
  67165. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  67166. + return REM_RETVAL(-ENXIO);
  67167. + }
  67168. +
  67169. + if (otg_dev->core_if) {
  67170. + dwc_otg_cil_remove(otg_dev->core_if);
  67171. + } else {
  67172. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  67173. + return REM_RETVAL(-ENXIO);
  67174. + }
  67175. +
  67176. + /*
  67177. + * Remove the device attributes
  67178. + */
  67179. + dwc_otg_attr_remove(_dev);
  67180. +
  67181. + /*
  67182. + * Return the memory.
  67183. + */
  67184. + if (otg_dev->os_dep.base) {
  67185. + iounmap(otg_dev->os_dep.base);
  67186. + }
  67187. + DWC_FREE(otg_dev);
  67188. +
  67189. + /*
  67190. + * Clear the drvdata pointer.
  67191. + */
  67192. +#ifdef LM_INTERFACE
  67193. + lm_set_drvdata(_dev, 0);
  67194. +#elif defined(PCI_INTERFACE)
  67195. + release_mem_region(otg_dev->os_dep.rsrc_start,
  67196. + otg_dev->os_dep.rsrc_len);
  67197. + pci_set_drvdata(_dev, 0);
  67198. +#elif defined(PLATFORM_INTERFACE)
  67199. + platform_set_drvdata(_dev, 0);
  67200. +#endif
  67201. + return REM_RETVAL(0);
  67202. +}
  67203. +
  67204. +/**
  67205. + * This function is called when an lm_device is bound to a
  67206. + * dwc_otg_driver. It creates the driver components required to
  67207. + * control the device (CIL, HCD, and PCD) and it initializes the
  67208. + * device. The driver components are stored in a dwc_otg_device
  67209. + * structure. A reference to the dwc_otg_device is saved in the
  67210. + * lm_device. This allows the driver to access the dwc_otg_device
  67211. + * structure on subsequent calls to driver methods for this device.
  67212. + *
  67213. + * @param _dev Bus device
  67214. + */
  67215. +static int dwc_otg_driver_probe(
  67216. +#ifdef LM_INTERFACE
  67217. + struct lm_device *_dev
  67218. +#elif defined(PCI_INTERFACE)
  67219. + struct pci_dev *_dev,
  67220. + const struct pci_device_id *id
  67221. +#elif defined(PLATFORM_INTERFACE)
  67222. + struct platform_device *_dev
  67223. +#endif
  67224. + )
  67225. +{
  67226. + int retval = 0;
  67227. + dwc_otg_device_t *dwc_otg_device;
  67228. + int devirq;
  67229. +
  67230. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  67231. +#ifdef LM_INTERFACE
  67232. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  67233. +#elif defined(PCI_INTERFACE)
  67234. + if (!id) {
  67235. + DWC_ERROR("Invalid pci_device_id %p", id);
  67236. + return -EINVAL;
  67237. + }
  67238. +
  67239. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  67240. + DWC_ERROR("Invalid pci_device %p", _dev);
  67241. + return -ENODEV;
  67242. + }
  67243. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  67244. + /* other stuff needed as well? */
  67245. +
  67246. +#elif defined(PLATFORM_INTERFACE)
  67247. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  67248. + (unsigned)_dev->resource->start,
  67249. + (unsigned)(_dev->resource->end - _dev->resource->start));
  67250. +#endif
  67251. +
  67252. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  67253. +
  67254. + if (!dwc_otg_device) {
  67255. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  67256. + return -ENOMEM;
  67257. + }
  67258. +
  67259. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  67260. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  67261. +
  67262. + /*
  67263. + * Map the DWC_otg Core memory into virtual address space.
  67264. + */
  67265. +#ifdef LM_INTERFACE
  67266. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  67267. +
  67268. + if (!dwc_otg_device->os_dep.base) {
  67269. + dev_err(&_dev->dev, "ioremap() failed\n");
  67270. + DWC_FREE(dwc_otg_device);
  67271. + return -ENOMEM;
  67272. + }
  67273. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  67274. + (unsigned)dwc_otg_device->os_dep.base);
  67275. +#elif defined(PCI_INTERFACE)
  67276. + _dev->current_state = PCI_D0;
  67277. + _dev->dev.power.power_state = PMSG_ON;
  67278. +
  67279. + if (!_dev->irq) {
  67280. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  67281. + pci_name(_dev));
  67282. + iounmap(dwc_otg_device->os_dep.base);
  67283. + DWC_FREE(dwc_otg_device);
  67284. + return -ENODEV;
  67285. + }
  67286. +
  67287. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  67288. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  67289. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  67290. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  67291. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  67292. + if (!request_mem_region
  67293. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  67294. + "dwc_otg")) {
  67295. + dev_dbg(&_dev->dev, "error requesting memory\n");
  67296. + iounmap(dwc_otg_device->os_dep.base);
  67297. + DWC_FREE(dwc_otg_device);
  67298. + return -EFAULT;
  67299. + }
  67300. +
  67301. + dwc_otg_device->os_dep.base =
  67302. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  67303. + dwc_otg_device->os_dep.rsrc_len);
  67304. + if (dwc_otg_device->os_dep.base == NULL) {
  67305. + dev_dbg(&_dev->dev, "error mapping memory\n");
  67306. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  67307. + dwc_otg_device->os_dep.rsrc_len);
  67308. + iounmap(dwc_otg_device->os_dep.base);
  67309. + DWC_FREE(dwc_otg_device);
  67310. + return -EFAULT;
  67311. + }
  67312. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  67313. + dwc_otg_device->os_dep.base);
  67314. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  67315. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  67316. + dwc_otg_device->os_dep.base);
  67317. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  67318. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  67319. + dwc_otg_device->os_dep.base);
  67320. +
  67321. + pci_set_master(_dev);
  67322. + pci_set_drvdata(_dev, dwc_otg_device);
  67323. +#elif defined(PLATFORM_INTERFACE)
  67324. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  67325. + _dev->resource->start,
  67326. + _dev->resource->end - _dev->resource->start + 1);
  67327. +#if 1
  67328. + if (!request_mem_region(_dev->resource[0].start,
  67329. + _dev->resource[0].end - _dev->resource[0].start + 1,
  67330. + "dwc_otg")) {
  67331. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  67332. + retval = -EFAULT;
  67333. + goto fail;
  67334. + }
  67335. +
  67336. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  67337. + _dev->resource[0].end -
  67338. + _dev->resource[0].start+1);
  67339. + if (fiq_fix_enable)
  67340. + {
  67341. + if (!request_mem_region(_dev->resource[1].start,
  67342. + _dev->resource[1].end - _dev->resource[1].start + 1,
  67343. + "dwc_otg")) {
  67344. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  67345. + retval = -EFAULT;
  67346. + goto fail;
  67347. + }
  67348. +
  67349. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  67350. + _dev->resource[1].end -
  67351. + _dev->resource[1].start + 1);
  67352. + dummy_send = (void *) kmalloc(16, GFP_ATOMIC);
  67353. + }
  67354. +
  67355. +#else
  67356. + {
  67357. + struct map_desc desc = {
  67358. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  67359. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  67360. + .length = SZ_128K,
  67361. + .type = MT_DEVICE
  67362. + };
  67363. + iotable_init(&desc, 1);
  67364. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  67365. + }
  67366. +#endif
  67367. + if (!dwc_otg_device->os_dep.base) {
  67368. + dev_err(&_dev->dev, "ioremap() failed\n");
  67369. + retval = -ENOMEM;
  67370. + goto fail;
  67371. + }
  67372. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  67373. + (unsigned)dwc_otg_device->os_dep.base);
  67374. +#endif
  67375. +
  67376. + /*
  67377. + * Initialize driver data to point to the global DWC_otg
  67378. + * Device structure.
  67379. + */
  67380. +#ifdef LM_INTERFACE
  67381. + lm_set_drvdata(_dev, dwc_otg_device);
  67382. +#elif defined(PLATFORM_INTERFACE)
  67383. + platform_set_drvdata(_dev, dwc_otg_device);
  67384. +#endif
  67385. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  67386. +
  67387. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  67388. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  67389. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  67390. +
  67391. + if (!dwc_otg_device->core_if) {
  67392. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  67393. + retval = -ENOMEM;
  67394. + goto fail;
  67395. + }
  67396. +
  67397. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  67398. + /*
  67399. + * Attempt to ensure this device is really a DWC_otg Controller.
  67400. + * Read and verify the SNPSID register contents. The value should be
  67401. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  67402. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  67403. + */
  67404. +
  67405. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  67406. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  67407. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  67408. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  67409. + retval = -EINVAL;
  67410. + goto fail;
  67411. + }
  67412. +
  67413. + /*
  67414. + * Validate parameter values.
  67415. + */
  67416. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  67417. + if (set_parameters(dwc_otg_device->core_if)) {
  67418. + retval = -EINVAL;
  67419. + goto fail;
  67420. + }
  67421. +
  67422. + /*
  67423. + * Create Device Attributes in sysfs
  67424. + */
  67425. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  67426. + dwc_otg_attr_create(_dev);
  67427. +
  67428. + /*
  67429. + * Disable the global interrupt until all the interrupt
  67430. + * handlers are installed.
  67431. + */
  67432. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  67433. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  67434. +
  67435. + /*
  67436. + * Install the interrupt handler for the common interrupts before
  67437. + * enabling common interrupts in core_init below.
  67438. + */
  67439. +
  67440. +#if defined(PLATFORM_INTERFACE)
  67441. + devirq = platform_get_irq(_dev, 0);
  67442. +#else
  67443. + devirq = _dev->irq;
  67444. +#endif
  67445. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  67446. + devirq);
  67447. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  67448. + retval = request_irq(devirq, dwc_otg_common_irq,
  67449. + IRQF_SHARED,
  67450. + "dwc_otg", dwc_otg_device);
  67451. + if (retval) {
  67452. + DWC_ERROR("request of irq%d failed\n", devirq);
  67453. + retval = -EBUSY;
  67454. + goto fail;
  67455. + } else {
  67456. + dwc_otg_device->common_irq_installed = 1;
  67457. + }
  67458. +
  67459. +#ifndef IRQF_TRIGGER_LOW
  67460. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  67461. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  67462. + set_irq_type(devirq,
  67463. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  67464. + IRQT_LOW
  67465. +#else
  67466. + IRQ_TYPE_LEVEL_LOW
  67467. +#endif
  67468. + );
  67469. +#endif
  67470. +#endif /*IRQF_TRIGGER_LOW*/
  67471. +
  67472. + /*
  67473. + * Initialize the DWC_otg core.
  67474. + */
  67475. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  67476. + dwc_otg_core_init(dwc_otg_device->core_if);
  67477. +
  67478. +#ifndef DWC_HOST_ONLY
  67479. + /*
  67480. + * Initialize the PCD
  67481. + */
  67482. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  67483. + retval = pcd_init(_dev);
  67484. + if (retval != 0) {
  67485. + DWC_ERROR("pcd_init failed\n");
  67486. + dwc_otg_device->pcd = NULL;
  67487. + goto fail;
  67488. + }
  67489. +#endif
  67490. +#ifndef DWC_DEVICE_ONLY
  67491. + /*
  67492. + * Initialize the HCD
  67493. + */
  67494. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  67495. + retval = hcd_init(_dev);
  67496. + if (retval != 0) {
  67497. + DWC_ERROR("hcd_init failed\n");
  67498. + dwc_otg_device->hcd = NULL;
  67499. + goto fail;
  67500. + }
  67501. +#endif
  67502. + /* Recover from drvdata having been overwritten by hcd_init() */
  67503. +#ifdef LM_INTERFACE
  67504. + lm_set_drvdata(_dev, dwc_otg_device);
  67505. +#elif defined(PLATFORM_INTERFACE)
  67506. + platform_set_drvdata(_dev, dwc_otg_device);
  67507. +#elif defined(PCI_INTERFACE)
  67508. + pci_set_drvdata(_dev, dwc_otg_device);
  67509. + dwc_otg_device->os_dep.pcidev = _dev;
  67510. +#endif
  67511. +
  67512. + /*
  67513. + * Enable the global interrupt after all the interrupt
  67514. + * handlers are installed if there is no ADP support else
  67515. + * perform initial actions required for Internal ADP logic.
  67516. + */
  67517. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  67518. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  67519. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  67520. + dev_dbg(&_dev->dev, "Done\n");
  67521. + } else
  67522. + dwc_otg_adp_start(dwc_otg_device->core_if,
  67523. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  67524. +
  67525. + return 0;
  67526. +
  67527. +fail:
  67528. + dwc_otg_driver_remove(_dev);
  67529. + return retval;
  67530. +}
  67531. +
  67532. +/**
  67533. + * This structure defines the methods to be called by a bus driver
  67534. + * during the lifecycle of a device on that bus. Both drivers and
  67535. + * devices are registered with a bus driver. The bus driver matches
  67536. + * devices to drivers based on information in the device and driver
  67537. + * structures.
  67538. + *
  67539. + * The probe function is called when the bus driver matches a device
  67540. + * to this driver. The remove function is called when a device is
  67541. + * unregistered with the bus driver.
  67542. + */
  67543. +#ifdef LM_INTERFACE
  67544. +static struct lm_driver dwc_otg_driver = {
  67545. + .drv = {.name = (char *)dwc_driver_name,},
  67546. + .probe = dwc_otg_driver_probe,
  67547. + .remove = dwc_otg_driver_remove,
  67548. + // 'suspend' and 'resume' absent
  67549. +};
  67550. +#elif defined(PCI_INTERFACE)
  67551. +static const struct pci_device_id pci_ids[] = { {
  67552. + PCI_DEVICE(0x16c3, 0xabcd),
  67553. + .driver_data =
  67554. + (unsigned long)0xdeadbeef,
  67555. + }, { /* end: all zeroes */ }
  67556. +};
  67557. +
  67558. +MODULE_DEVICE_TABLE(pci, pci_ids);
  67559. +
  67560. +/* pci driver glue; this is a "new style" PCI driver module */
  67561. +static struct pci_driver dwc_otg_driver = {
  67562. + .name = "dwc_otg",
  67563. + .id_table = pci_ids,
  67564. +
  67565. + .probe = dwc_otg_driver_probe,
  67566. + .remove = dwc_otg_driver_remove,
  67567. +
  67568. + .driver = {
  67569. + .name = (char *)dwc_driver_name,
  67570. + },
  67571. +};
  67572. +#elif defined(PLATFORM_INTERFACE)
  67573. +static struct platform_device_id platform_ids[] = {
  67574. + {
  67575. + .name = "bcm2708_usb",
  67576. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  67577. + },
  67578. + { /* end: all zeroes */ }
  67579. +};
  67580. +MODULE_DEVICE_TABLE(platform, platform_ids);
  67581. +
  67582. +static struct platform_driver dwc_otg_driver = {
  67583. + .driver = {
  67584. + .name = (char *)dwc_driver_name,
  67585. + },
  67586. + .id_table = platform_ids,
  67587. +
  67588. + .probe = dwc_otg_driver_probe,
  67589. + .remove = dwc_otg_driver_remove,
  67590. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  67591. +};
  67592. +#endif
  67593. +
  67594. +/**
  67595. + * This function is called when the dwc_otg_driver is installed with the
  67596. + * insmod command. It registers the dwc_otg_driver structure with the
  67597. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  67598. + * to be called. In addition, the bus driver will automatically expose
  67599. + * attributes defined for the device and driver in the special sysfs file
  67600. + * system.
  67601. + *
  67602. + * @return
  67603. + */
  67604. +static int __init dwc_otg_driver_init(void)
  67605. +{
  67606. + int retval = 0;
  67607. + int error;
  67608. + struct device_driver *drv;
  67609. +
  67610. + if(fiq_split_enable && !fiq_fix_enable) {
  67611. + printk(KERN_WARNING "dwc_otg: fiq_split_enable was set without fiq_fix_enable! Correcting.\n");
  67612. + fiq_fix_enable = 1;
  67613. + }
  67614. +
  67615. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  67616. + DWC_DRIVER_VERSION,
  67617. +#ifdef LM_INTERFACE
  67618. + "logicmodule");
  67619. + retval = lm_driver_register(&dwc_otg_driver);
  67620. + drv = &dwc_otg_driver.drv;
  67621. +#elif defined(PCI_INTERFACE)
  67622. + "pci");
  67623. + retval = pci_register_driver(&dwc_otg_driver);
  67624. + drv = &dwc_otg_driver.driver;
  67625. +#elif defined(PLATFORM_INTERFACE)
  67626. + "platform");
  67627. + retval = platform_driver_register(&dwc_otg_driver);
  67628. + drv = &dwc_otg_driver.driver;
  67629. +#endif
  67630. + if (retval < 0) {
  67631. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  67632. + return retval;
  67633. + }
  67634. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_fix_enable ? "enabled":"disabled");
  67635. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff_enable ? "enabled":"disabled");
  67636. + printk(KERN_DEBUG "dwc_otg: FIQ split fix %s\n", fiq_split_enable ? "enabled":"disabled");
  67637. +
  67638. + error = driver_create_file(drv, &driver_attr_version);
  67639. +#ifdef DEBUG
  67640. + error = driver_create_file(drv, &driver_attr_debuglevel);
  67641. +#endif
  67642. + return retval;
  67643. +}
  67644. +
  67645. +module_init(dwc_otg_driver_init);
  67646. +
  67647. +/**
  67648. + * This function is called when the driver is removed from the kernel
  67649. + * with the rmmod command. The driver unregisters itself with its bus
  67650. + * driver.
  67651. + *
  67652. + */
  67653. +static void __exit dwc_otg_driver_cleanup(void)
  67654. +{
  67655. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  67656. +
  67657. +#ifdef LM_INTERFACE
  67658. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  67659. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  67660. + lm_driver_unregister(&dwc_otg_driver);
  67661. +#elif defined(PCI_INTERFACE)
  67662. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  67663. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  67664. + pci_unregister_driver(&dwc_otg_driver);
  67665. +#elif defined(PLATFORM_INTERFACE)
  67666. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  67667. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  67668. + platform_driver_unregister(&dwc_otg_driver);
  67669. +#endif
  67670. +
  67671. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  67672. +}
  67673. +
  67674. +module_exit(dwc_otg_driver_cleanup);
  67675. +
  67676. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  67677. +MODULE_AUTHOR("Synopsys Inc.");
  67678. +MODULE_LICENSE("GPL");
  67679. +
  67680. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  67681. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  67682. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  67683. +MODULE_PARM_DESC(opt, "OPT Mode");
  67684. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  67685. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  67686. +
  67687. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  67688. + 0444);
  67689. +MODULE_PARM_DESC(dma_desc_enable,
  67690. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  67691. +
  67692. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  67693. + 0444);
  67694. +MODULE_PARM_DESC(dma_burst_size,
  67695. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  67696. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  67697. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  67698. +module_param_named(host_support_fs_ls_low_power,
  67699. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  67700. + 0444);
  67701. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  67702. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  67703. +module_param_named(host_ls_low_power_phy_clk,
  67704. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  67705. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  67706. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  67707. +module_param_named(enable_dynamic_fifo,
  67708. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  67709. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  67710. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  67711. + 0444);
  67712. +MODULE_PARM_DESC(data_fifo_size,
  67713. + "Total number of words in the data FIFO memory 32-32768");
  67714. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  67715. + int, 0444);
  67716. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  67717. +module_param_named(dev_nperio_tx_fifo_size,
  67718. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  67719. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  67720. + "Number of words in the non-periodic Tx FIFO 16-32768");
  67721. +module_param_named(dev_perio_tx_fifo_size_1,
  67722. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  67723. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  67724. + "Number of words in the periodic Tx FIFO 4-768");
  67725. +module_param_named(dev_perio_tx_fifo_size_2,
  67726. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  67727. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  67728. + "Number of words in the periodic Tx FIFO 4-768");
  67729. +module_param_named(dev_perio_tx_fifo_size_3,
  67730. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  67731. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  67732. + "Number of words in the periodic Tx FIFO 4-768");
  67733. +module_param_named(dev_perio_tx_fifo_size_4,
  67734. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  67735. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  67736. + "Number of words in the periodic Tx FIFO 4-768");
  67737. +module_param_named(dev_perio_tx_fifo_size_5,
  67738. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  67739. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  67740. + "Number of words in the periodic Tx FIFO 4-768");
  67741. +module_param_named(dev_perio_tx_fifo_size_6,
  67742. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  67743. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  67744. + "Number of words in the periodic Tx FIFO 4-768");
  67745. +module_param_named(dev_perio_tx_fifo_size_7,
  67746. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  67747. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  67748. + "Number of words in the periodic Tx FIFO 4-768");
  67749. +module_param_named(dev_perio_tx_fifo_size_8,
  67750. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  67751. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  67752. + "Number of words in the periodic Tx FIFO 4-768");
  67753. +module_param_named(dev_perio_tx_fifo_size_9,
  67754. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  67755. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  67756. + "Number of words in the periodic Tx FIFO 4-768");
  67757. +module_param_named(dev_perio_tx_fifo_size_10,
  67758. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  67759. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  67760. + "Number of words in the periodic Tx FIFO 4-768");
  67761. +module_param_named(dev_perio_tx_fifo_size_11,
  67762. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  67763. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  67764. + "Number of words in the periodic Tx FIFO 4-768");
  67765. +module_param_named(dev_perio_tx_fifo_size_12,
  67766. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  67767. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  67768. + "Number of words in the periodic Tx FIFO 4-768");
  67769. +module_param_named(dev_perio_tx_fifo_size_13,
  67770. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  67771. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  67772. + "Number of words in the periodic Tx FIFO 4-768");
  67773. +module_param_named(dev_perio_tx_fifo_size_14,
  67774. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  67775. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  67776. + "Number of words in the periodic Tx FIFO 4-768");
  67777. +module_param_named(dev_perio_tx_fifo_size_15,
  67778. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  67779. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  67780. + "Number of words in the periodic Tx FIFO 4-768");
  67781. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  67782. + int, 0444);
  67783. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  67784. +module_param_named(host_nperio_tx_fifo_size,
  67785. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  67786. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  67787. + "Number of words in the non-periodic Tx FIFO 16-32768");
  67788. +module_param_named(host_perio_tx_fifo_size,
  67789. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  67790. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  67791. + "Number of words in the host periodic Tx FIFO 16-32768");
  67792. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  67793. + int, 0444);
  67794. +/** @todo Set the max to 512K, modify checks */
  67795. +MODULE_PARM_DESC(max_transfer_size,
  67796. + "The maximum transfer size supported in bytes 2047-65535");
  67797. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  67798. + int, 0444);
  67799. +MODULE_PARM_DESC(max_packet_count,
  67800. + "The maximum number of packets in a transfer 15-511");
  67801. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  67802. + 0444);
  67803. +MODULE_PARM_DESC(host_channels,
  67804. + "The number of host channel registers to use 1-16");
  67805. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  67806. + 0444);
  67807. +MODULE_PARM_DESC(dev_endpoints,
  67808. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  67809. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  67810. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  67811. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  67812. + 0444);
  67813. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  67814. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  67815. +MODULE_PARM_DESC(phy_ulpi_ddr,
  67816. + "ULPI at double or single data rate 0=Single 1=Double");
  67817. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  67818. + int, 0444);
  67819. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  67820. + "ULPI PHY using internal or external vbus 0=Internal");
  67821. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  67822. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  67823. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  67824. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  67825. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  67826. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  67827. +module_param_named(debug, g_dbg_lvl, int, 0444);
  67828. +MODULE_PARM_DESC(debug, "");
  67829. +
  67830. +module_param_named(en_multiple_tx_fifo,
  67831. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  67832. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  67833. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  67834. +module_param_named(dev_tx_fifo_size_1,
  67835. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  67836. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  67837. +module_param_named(dev_tx_fifo_size_2,
  67838. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  67839. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  67840. +module_param_named(dev_tx_fifo_size_3,
  67841. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  67842. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  67843. +module_param_named(dev_tx_fifo_size_4,
  67844. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  67845. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  67846. +module_param_named(dev_tx_fifo_size_5,
  67847. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  67848. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  67849. +module_param_named(dev_tx_fifo_size_6,
  67850. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  67851. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  67852. +module_param_named(dev_tx_fifo_size_7,
  67853. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  67854. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  67855. +module_param_named(dev_tx_fifo_size_8,
  67856. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  67857. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  67858. +module_param_named(dev_tx_fifo_size_9,
  67859. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  67860. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  67861. +module_param_named(dev_tx_fifo_size_10,
  67862. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  67863. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  67864. +module_param_named(dev_tx_fifo_size_11,
  67865. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  67866. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  67867. +module_param_named(dev_tx_fifo_size_12,
  67868. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  67869. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  67870. +module_param_named(dev_tx_fifo_size_13,
  67871. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  67872. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  67873. +module_param_named(dev_tx_fifo_size_14,
  67874. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  67875. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  67876. +module_param_named(dev_tx_fifo_size_15,
  67877. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  67878. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  67879. +
  67880. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  67881. +MODULE_PARM_DESC(thr_ctl,
  67882. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  67883. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  67884. + 0444);
  67885. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  67886. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  67887. + 0444);
  67888. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  67889. +
  67890. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  67891. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  67892. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  67893. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  67894. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  67895. +MODULE_PARM_DESC(ic_usb_cap,
  67896. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  67897. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  67898. + 0444);
  67899. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  67900. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  67901. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  67902. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  67903. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  67904. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  67905. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  67906. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  67907. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  67908. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  67909. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  67910. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  67911. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  67912. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  67913. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  67914. +module_param(microframe_schedule, bool, 0444);
  67915. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  67916. +
  67917. +module_param(fiq_fix_enable, bool, 0444);
  67918. +MODULE_PARM_DESC(fiq_fix_enable, "Enable the fiq fix");
  67919. +module_param(nak_holdoff_enable, bool, 0444);
  67920. +MODULE_PARM_DESC(nak_holdoff_enable, "Enable the NAK holdoff");
  67921. +module_param(fiq_split_enable, bool, 0444);
  67922. +MODULE_PARM_DESC(fiq_split_enable, "Enable the FIQ fix on split transactions");
  67923. +
  67924. +/** @page "Module Parameters"
  67925. + *
  67926. + * The following parameters may be specified when starting the module.
  67927. + * These parameters define how the DWC_otg controller should be
  67928. + * configured. Parameter values are passed to the CIL initialization
  67929. + * function dwc_otg_cil_init
  67930. + *
  67931. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  67932. + *
  67933. +
  67934. + <table>
  67935. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  67936. +
  67937. + <tr>
  67938. + <td>otg_cap</td>
  67939. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  67940. + value for this parameter if none is specified.
  67941. + - 0: HNP and SRP capable (default, if available)
  67942. + - 1: SRP Only capable
  67943. + - 2: No HNP/SRP capable
  67944. + </td></tr>
  67945. +
  67946. + <tr>
  67947. + <td>dma_enable</td>
  67948. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  67949. + The driver will automatically detect the value for this parameter if none is
  67950. + specified.
  67951. + - 0: Slave
  67952. + - 1: DMA (default, if available)
  67953. + </td></tr>
  67954. +
  67955. + <tr>
  67956. + <td>dma_burst_size</td>
  67957. + <td>The DMA Burst size (applicable only for External DMA Mode).
  67958. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  67959. + </td></tr>
  67960. +
  67961. + <tr>
  67962. + <td>speed</td>
  67963. + <td>Specifies the maximum speed of operation in host and device mode. The
  67964. + actual speed depends on the speed of the attached device and the value of
  67965. + phy_type.
  67966. + - 0: High Speed (default)
  67967. + - 1: Full Speed
  67968. + </td></tr>
  67969. +
  67970. + <tr>
  67971. + <td>host_support_fs_ls_low_power</td>
  67972. + <td>Specifies whether low power mode is supported when attached to a Full
  67973. + Speed or Low Speed device in host mode.
  67974. + - 0: Don't support low power mode (default)
  67975. + - 1: Support low power mode
  67976. + </td></tr>
  67977. +
  67978. + <tr>
  67979. + <td>host_ls_low_power_phy_clk</td>
  67980. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  67981. + Speed device in host mode. This parameter is applicable only if
  67982. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  67983. + - 0: 48 MHz (default)
  67984. + - 1: 6 MHz
  67985. + </td></tr>
  67986. +
  67987. + <tr>
  67988. + <td>enable_dynamic_fifo</td>
  67989. + <td> Specifies whether FIFOs may be resized by the driver software.
  67990. + - 0: Use cC FIFO size parameters
  67991. + - 1: Allow dynamic FIFO sizing (default)
  67992. + </td></tr>
  67993. +
  67994. + <tr>
  67995. + <td>data_fifo_size</td>
  67996. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  67997. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  67998. + - Values: 32 to 32768 (default 8192)
  67999. +
  68000. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  68001. + </td></tr>
  68002. +
  68003. + <tr>
  68004. + <td>dev_rx_fifo_size</td>
  68005. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  68006. + FIFO sizing is enabled.
  68007. + - Values: 16 to 32768 (default 1064)
  68008. + </td></tr>
  68009. +
  68010. + <tr>
  68011. + <td>dev_nperio_tx_fifo_size</td>
  68012. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  68013. + dynamic FIFO sizing is enabled.
  68014. + - Values: 16 to 32768 (default 1024)
  68015. + </td></tr>
  68016. +
  68017. + <tr>
  68018. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  68019. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  68020. + when dynamic FIFO sizing is enabled.
  68021. + - Values: 4 to 768 (default 256)
  68022. + </td></tr>
  68023. +
  68024. + <tr>
  68025. + <td>host_rx_fifo_size</td>
  68026. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  68027. + sizing is enabled.
  68028. + - Values: 16 to 32768 (default 1024)
  68029. + </td></tr>
  68030. +
  68031. + <tr>
  68032. + <td>host_nperio_tx_fifo_size</td>
  68033. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  68034. + dynamic FIFO sizing is enabled in the core.
  68035. + - Values: 16 to 32768 (default 1024)
  68036. + </td></tr>
  68037. +
  68038. + <tr>
  68039. + <td>host_perio_tx_fifo_size</td>
  68040. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  68041. + sizing is enabled.
  68042. + - Values: 16 to 32768 (default 1024)
  68043. + </td></tr>
  68044. +
  68045. + <tr>
  68046. + <td>max_transfer_size</td>
  68047. + <td>The maximum transfer size supported in bytes.
  68048. + - Values: 2047 to 65,535 (default 65,535)
  68049. + </td></tr>
  68050. +
  68051. + <tr>
  68052. + <td>max_packet_count</td>
  68053. + <td>The maximum number of packets in a transfer.
  68054. + - Values: 15 to 511 (default 511)
  68055. + </td></tr>
  68056. +
  68057. + <tr>
  68058. + <td>host_channels</td>
  68059. + <td>The number of host channel registers to use.
  68060. + - Values: 1 to 16 (default 12)
  68061. +
  68062. + Note: The FPGA configuration supports a maximum of 12 host channels.
  68063. + </td></tr>
  68064. +
  68065. + <tr>
  68066. + <td>dev_endpoints</td>
  68067. + <td>The number of endpoints in addition to EP0 available for device mode
  68068. + operations.
  68069. + - Values: 1 to 15 (default 6 IN and OUT)
  68070. +
  68071. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  68072. + addition to EP0.
  68073. + </td></tr>
  68074. +
  68075. + <tr>
  68076. + <td>phy_type</td>
  68077. + <td>Specifies the type of PHY interface to use. By default, the driver will
  68078. + automatically detect the phy_type.
  68079. + - 0: Full Speed
  68080. + - 1: UTMI+ (default, if available)
  68081. + - 2: ULPI
  68082. + </td></tr>
  68083. +
  68084. + <tr>
  68085. + <td>phy_utmi_width</td>
  68086. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  68087. + phy_type of UTMI+. Also, this parameter is applicable only if the
  68088. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  68089. + core has been configured to work at either data path width.
  68090. + - Values: 8 or 16 bits (default 16)
  68091. + </td></tr>
  68092. +
  68093. + <tr>
  68094. + <td>phy_ulpi_ddr</td>
  68095. + <td>Specifies whether the ULPI operates at double or single data rate. This
  68096. + parameter is only applicable if phy_type is ULPI.
  68097. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  68098. + - 1: double data rate ULPI interface with 4 bit wide data bus
  68099. + </td></tr>
  68100. +
  68101. + <tr>
  68102. + <td>i2c_enable</td>
  68103. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  68104. + parameter is only applicable if PHY_TYPE is FS.
  68105. + - 0: Disabled (default)
  68106. + - 1: Enabled
  68107. + </td></tr>
  68108. +
  68109. + <tr>
  68110. + <td>ulpi_fs_ls</td>
  68111. + <td>Specifies whether to use ULPI FS/LS mode only.
  68112. + - 0: Disabled (default)
  68113. + - 1: Enabled
  68114. + </td></tr>
  68115. +
  68116. + <tr>
  68117. + <td>ts_dline</td>
  68118. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  68119. + - 0: Disabled (default)
  68120. + - 1: Enabled
  68121. + </td></tr>
  68122. +
  68123. + <tr>
  68124. + <td>en_multiple_tx_fifo</td>
  68125. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  68126. + The driver will automatically detect the value for this parameter if none is
  68127. + specified.
  68128. + - 0: Disabled
  68129. + - 1: Enabled (default, if available)
  68130. + </td></tr>
  68131. +
  68132. + <tr>
  68133. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  68134. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  68135. + when dynamic FIFO sizing is enabled.
  68136. + - Values: 4 to 768 (default 256)
  68137. + </td></tr>
  68138. +
  68139. + <tr>
  68140. + <td>tx_thr_length</td>
  68141. + <td>Transmit Threshold length in 32 bit double words
  68142. + - Values: 8 to 128 (default 64)
  68143. + </td></tr>
  68144. +
  68145. + <tr>
  68146. + <td>rx_thr_length</td>
  68147. + <td>Receive Threshold length in 32 bit double words
  68148. + - Values: 8 to 128 (default 64)
  68149. + </td></tr>
  68150. +
  68151. +<tr>
  68152. + <td>thr_ctl</td>
  68153. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  68154. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  68155. + Rx transfers accordingly.
  68156. + The driver will automatically detect the value for this parameter if none is
  68157. + specified.
  68158. + - Values: 0 to 7 (default 0)
  68159. + Bit values indicate:
  68160. + - 0: Thresholding disabled
  68161. + - 1: Thresholding enabled
  68162. + </td></tr>
  68163. +
  68164. +<tr>
  68165. + <td>dma_desc_enable</td>
  68166. + <td>Specifies whether to enable Descriptor DMA mode.
  68167. + The driver will automatically detect the value for this parameter if none is
  68168. + specified.
  68169. + - 0: Descriptor DMA disabled
  68170. + - 1: Descriptor DMA (default, if available)
  68171. + </td></tr>
  68172. +
  68173. +<tr>
  68174. + <td>mpi_enable</td>
  68175. + <td>Specifies whether to enable MPI enhancement mode.
  68176. + The driver will automatically detect the value for this parameter if none is
  68177. + specified.
  68178. + - 0: MPI disabled (default)
  68179. + - 1: MPI enable
  68180. + </td></tr>
  68181. +
  68182. +<tr>
  68183. + <td>pti_enable</td>
  68184. + <td>Specifies whether to enable PTI enhancement support.
  68185. + The driver will automatically detect the value for this parameter if none is
  68186. + specified.
  68187. + - 0: PTI disabled (default)
  68188. + - 1: PTI enable
  68189. + </td></tr>
  68190. +
  68191. +<tr>
  68192. + <td>lpm_enable</td>
  68193. + <td>Specifies whether to enable LPM support.
  68194. + The driver will automatically detect the value for this parameter if none is
  68195. + specified.
  68196. + - 0: LPM disabled
  68197. + - 1: LPM enable (default, if available)
  68198. + </td></tr>
  68199. +
  68200. +<tr>
  68201. + <td>ic_usb_cap</td>
  68202. + <td>Specifies whether to enable IC_USB capability.
  68203. + The driver will automatically detect the value for this parameter if none is
  68204. + specified.
  68205. + - 0: IC_USB disabled (default, if available)
  68206. + - 1: IC_USB enable
  68207. + </td></tr>
  68208. +
  68209. +<tr>
  68210. + <td>ahb_thr_ratio</td>
  68211. + <td>Specifies AHB Threshold ratio.
  68212. + - Values: 0 to 3 (default 0)
  68213. + </td></tr>
  68214. +
  68215. +<tr>
  68216. + <td>power_down</td>
  68217. + <td>Specifies Power Down(Hibernation) Mode.
  68218. + The driver will automatically detect the value for this parameter if none is
  68219. + specified.
  68220. + - 0: Power Down disabled (default)
  68221. + - 2: Power Down enabled
  68222. + </td></tr>
  68223. +
  68224. + <tr>
  68225. + <td>reload_ctl</td>
  68226. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  68227. + run time. The driver will automatically detect the value for this parameter if
  68228. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  68229. + the core might misbehave.
  68230. + - 0: Reload Control disabled (default)
  68231. + - 1: Reload Control enabled
  68232. + </td></tr>
  68233. +
  68234. + <tr>
  68235. + <td>dev_out_nak</td>
  68236. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  68237. + The driver will automatically detect the value for this parameter if
  68238. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  68239. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  68240. + - 1: The core sets NAK after Bulk OUT transfer complete
  68241. + </td></tr>
  68242. +
  68243. + <tr>
  68244. + <td>cont_on_bna</td>
  68245. + <td>Specifies whether Enable Continue on BNA enabled or no.
  68246. + After receiving BNA interrupt the core disables the endpoint,when the
  68247. + endpoint is re-enabled by the application the
  68248. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  68249. + - 1: Core starts processing from the descriptor which received the BNA.
  68250. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  68251. + </td></tr>
  68252. +
  68253. + <tr>
  68254. + <td>ahb_single</td>
  68255. + <td>This bit when programmed supports SINGLE transfers for remainder data
  68256. + in a transfer for DMA mode of operation.
  68257. + - 0: The remainder data will be sent using INCR burst size (default)
  68258. + - 1: The remainder data will be sent using SINGLE burst size.
  68259. + </td></tr>
  68260. +
  68261. +<tr>
  68262. + <td>adp_enable</td>
  68263. + <td>Specifies whether ADP feature is enabled.
  68264. + The driver will automatically detect the value for this parameter if none is
  68265. + specified.
  68266. + - 0: ADP feature disabled (default)
  68267. + - 1: ADP feature enabled
  68268. + </td></tr>
  68269. +
  68270. + <tr>
  68271. + <td>otg_ver</td>
  68272. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  68273. + USB OTG device.
  68274. + - 0: OTG 2.0 support disabled (default)
  68275. + - 1: OTG 2.0 support enabled
  68276. + </td></tr>
  68277. +
  68278. +*/
  68279. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_driver.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  68280. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_driver.h 1970-01-01 01:00:00.000000000 +0100
  68281. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h 2014-04-24 15:35:04.173565776 +0200
  68282. @@ -0,0 +1,86 @@
  68283. +/* ==========================================================================
  68284. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  68285. + * $Revision: #19 $
  68286. + * $Date: 2010/11/15 $
  68287. + * $Change: 1627671 $
  68288. + *
  68289. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  68290. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  68291. + * otherwise expressly agreed to in writing between Synopsys and you.
  68292. + *
  68293. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  68294. + * any End User Software License Agreement or Agreement for Licensed Product
  68295. + * with Synopsys or any supplement thereto. You are permitted to use and
  68296. + * redistribute this Software in source and binary forms, with or without
  68297. + * modification, provided that redistributions of source code must retain this
  68298. + * notice. You may not view, use, disclose, copy or distribute this file or
  68299. + * any information contained herein except pursuant to this license grant from
  68300. + * Synopsys. If you do not agree with this notice, including the disclaimer
  68301. + * below, then you are not authorized to use the Software.
  68302. + *
  68303. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  68304. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68305. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68306. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  68307. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  68308. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68309. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  68310. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  68311. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  68312. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  68313. + * DAMAGE.
  68314. + * ========================================================================== */
  68315. +
  68316. +#ifndef __DWC_OTG_DRIVER_H__
  68317. +#define __DWC_OTG_DRIVER_H__
  68318. +
  68319. +/** @file
  68320. + * This file contains the interface to the Linux driver.
  68321. + */
  68322. +#include "dwc_otg_os_dep.h"
  68323. +#include "dwc_otg_core_if.h"
  68324. +
  68325. +/* Type declarations */
  68326. +struct dwc_otg_pcd;
  68327. +struct dwc_otg_hcd;
  68328. +
  68329. +/**
  68330. + * This structure is a wrapper that encapsulates the driver components used to
  68331. + * manage a single DWC_otg controller.
  68332. + */
  68333. +typedef struct dwc_otg_device {
  68334. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  68335. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  68336. + * require this. */
  68337. + struct os_dependent os_dep;
  68338. +
  68339. + /** Pointer to the core interface structure. */
  68340. + dwc_otg_core_if_t *core_if;
  68341. +
  68342. + /** Pointer to the PCD structure. */
  68343. + struct dwc_otg_pcd *pcd;
  68344. +
  68345. + /** Pointer to the HCD structure. */
  68346. + struct dwc_otg_hcd *hcd;
  68347. +
  68348. + /** Flag to indicate whether the common IRQ handler is installed. */
  68349. + uint8_t common_irq_installed;
  68350. +
  68351. +} dwc_otg_device_t;
  68352. +
  68353. +/*We must clear S3C24XX_EINTPEND external interrupt register
  68354. + * because after clearing in this register trigerred IRQ from
  68355. + * H/W core in kernel interrupt can be occured again before OTG
  68356. + * handlers clear all IRQ sources of Core registers because of
  68357. + * timing latencies and Low Level IRQ Type.
  68358. + */
  68359. +#ifdef CONFIG_MACH_IPMATE
  68360. +#define S3C2410X_CLEAR_EINTPEND() \
  68361. +do { \
  68362. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  68363. +} while (0)
  68364. +#else
  68365. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  68366. +#endif
  68367. +
  68368. +#endif
  68369. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  68370. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 1970-01-01 01:00:00.000000000 +0100
  68371. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 2014-04-24 15:35:04.173565776 +0200
  68372. @@ -0,0 +1,3685 @@
  68373. +
  68374. +/* ==========================================================================
  68375. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  68376. + * $Revision: #104 $
  68377. + * $Date: 2011/10/24 $
  68378. + * $Change: 1871159 $
  68379. + *
  68380. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  68381. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  68382. + * otherwise expressly agreed to in writing between Synopsys and you.
  68383. + *
  68384. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  68385. + * any End User Software License Agreement or Agreement for Licensed Product
  68386. + * with Synopsys or any supplement thereto. You are permitted to use and
  68387. + * redistribute this Software in source and binary forms, with or without
  68388. + * modification, provided that redistributions of source code must retain this
  68389. + * notice. You may not view, use, disclose, copy or distribute this file or
  68390. + * any information contained herein except pursuant to this license grant from
  68391. + * Synopsys. If you do not agree with this notice, including the disclaimer
  68392. + * below, then you are not authorized to use the Software.
  68393. + *
  68394. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  68395. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68396. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68397. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  68398. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  68399. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68400. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  68401. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  68402. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  68403. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  68404. + * DAMAGE.
  68405. + * ========================================================================== */
  68406. +#ifndef DWC_DEVICE_ONLY
  68407. +
  68408. +/** @file
  68409. + * This file implements HCD Core. All code in this file is portable and doesn't
  68410. + * use any OS specific functions.
  68411. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  68412. + * header file.
  68413. + */
  68414. +
  68415. +#include <linux/usb.h>
  68416. +#include <linux/usb/hcd.h>
  68417. +
  68418. +#include "dwc_otg_hcd.h"
  68419. +#include "dwc_otg_regs.h"
  68420. +#include "dwc_otg_mphi_fix.h"
  68421. +
  68422. +extern bool microframe_schedule, nak_holdoff_enable;
  68423. +
  68424. +//#define DEBUG_HOST_CHANNELS
  68425. +#ifdef DEBUG_HOST_CHANNELS
  68426. +static int last_sel_trans_num_per_scheduled = 0;
  68427. +static int last_sel_trans_num_nonper_scheduled = 0;
  68428. +static int last_sel_trans_num_avail_hc_at_start = 0;
  68429. +static int last_sel_trans_num_avail_hc_at_end = 0;
  68430. +#endif /* DEBUG_HOST_CHANNELS */
  68431. +
  68432. +extern int g_next_sched_frame, g_np_count, g_np_sent;
  68433. +
  68434. +extern haint_data_t haint_saved;
  68435. +extern hcintmsk_data_t hcintmsk_saved[MAX_EPS_CHANNELS];
  68436. +extern hcint_data_t hcint_saved[MAX_EPS_CHANNELS];
  68437. +extern gintsts_data_t ginsts_saved;
  68438. +
  68439. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  68440. +{
  68441. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  68442. +}
  68443. +
  68444. +/**
  68445. + * Connection timeout function. An OTG host is required to display a
  68446. + * message if the device does not connect within 10 seconds.
  68447. + */
  68448. +void dwc_otg_hcd_connect_timeout(void *ptr)
  68449. +{
  68450. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  68451. + DWC_PRINTF("Connect Timeout\n");
  68452. + __DWC_ERROR("Device Not Connected/Responding\n");
  68453. +}
  68454. +
  68455. +#if defined(DEBUG)
  68456. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  68457. +{
  68458. + if (qh->channel != NULL) {
  68459. + dwc_hc_t *hc = qh->channel;
  68460. + dwc_list_link_t *item;
  68461. + dwc_otg_qh_t *qh_item;
  68462. + int num_channels = hcd->core_if->core_params->host_channels;
  68463. + int i;
  68464. +
  68465. + dwc_otg_hc_regs_t *hc_regs;
  68466. + hcchar_data_t hcchar;
  68467. + hcsplt_data_t hcsplt;
  68468. + hctsiz_data_t hctsiz;
  68469. + uint32_t hcdma;
  68470. +
  68471. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  68472. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  68473. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  68474. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  68475. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  68476. +
  68477. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  68478. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  68479. + hcsplt.d32);
  68480. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  68481. + hcdma);
  68482. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  68483. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  68484. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  68485. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  68486. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  68487. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  68488. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  68489. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  68490. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  68491. + DWC_PRINTF(" qh: %p\n", hc->qh);
  68492. + DWC_PRINTF(" NP inactive sched:\n");
  68493. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  68494. + qh_item =
  68495. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  68496. + DWC_PRINTF(" %p\n", qh_item);
  68497. + }
  68498. + DWC_PRINTF(" NP active sched:\n");
  68499. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  68500. + qh_item =
  68501. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  68502. + DWC_PRINTF(" %p\n", qh_item);
  68503. + }
  68504. + DWC_PRINTF(" Channels: \n");
  68505. + for (i = 0; i < num_channels; i++) {
  68506. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  68507. + DWC_PRINTF(" %2d: %p\n", i, hc);
  68508. + }
  68509. + }
  68510. +}
  68511. +#else
  68512. +#define dump_channel_info(hcd, qh)
  68513. +#endif /* DEBUG */
  68514. +
  68515. +/**
  68516. + * Work queue function for starting the HCD when A-Cable is connected.
  68517. + * The hcd_start() must be called in a process context.
  68518. + */
  68519. +static void hcd_start_func(void *_vp)
  68520. +{
  68521. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  68522. +
  68523. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  68524. + if (hcd) {
  68525. + hcd->fops->start(hcd);
  68526. + }
  68527. +}
  68528. +
  68529. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  68530. +{
  68531. +#ifdef DEBUG
  68532. + int i;
  68533. + int num_channels = hcd->core_if->core_params->host_channels;
  68534. + for (i = 0; i < num_channels; i++) {
  68535. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  68536. + }
  68537. +#endif
  68538. +}
  68539. +
  68540. +static void del_timers(dwc_otg_hcd_t * hcd)
  68541. +{
  68542. + del_xfer_timers(hcd);
  68543. + DWC_TIMER_CANCEL(hcd->conn_timer);
  68544. +}
  68545. +
  68546. +/**
  68547. + * Processes all the URBs in a single list of QHs. Completes them with
  68548. + * -ESHUTDOWN and frees the QTD.
  68549. + */
  68550. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  68551. +{
  68552. + dwc_list_link_t *qh_item, *qh_tmp;
  68553. + dwc_otg_qh_t *qh;
  68554. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  68555. +
  68556. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  68557. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  68558. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  68559. + &qh->qtd_list, qtd_list_entry) {
  68560. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  68561. + if (qtd->urb != NULL) {
  68562. + hcd->fops->complete(hcd, qtd->urb->priv,
  68563. + qtd->urb, -DWC_E_SHUTDOWN);
  68564. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  68565. + }
  68566. +
  68567. + }
  68568. + if(qh->channel) {
  68569. + /* Using hcchar.chen == 1 is not a reliable test.
  68570. + * It is possible that the channel has already halted
  68571. + * but not yet been through the IRQ handler.
  68572. + */
  68573. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  68574. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  68575. + if(microframe_schedule)
  68576. + hcd->available_host_channels++;
  68577. + qh->channel = NULL;
  68578. + }
  68579. + dwc_otg_hcd_qh_remove(hcd, qh);
  68580. + }
  68581. +}
  68582. +
  68583. +/**
  68584. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  68585. + * and periodic schedules. The QTD associated with each URB is removed from
  68586. + * the schedule and freed. This function may be called when a disconnect is
  68587. + * detected or when the HCD is being stopped.
  68588. + */
  68589. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  68590. +{
  68591. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  68592. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  68593. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  68594. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  68595. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  68596. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  68597. +}
  68598. +
  68599. +/**
  68600. + * Start the connection timer. An OTG host is required to display a
  68601. + * message if the device does not connect within 10 seconds. The
  68602. + * timer is deleted if a port connect interrupt occurs before the
  68603. + * timer expires.
  68604. + */
  68605. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  68606. +{
  68607. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  68608. +}
  68609. +
  68610. +/**
  68611. + * HCD Callback function for disconnect of the HCD.
  68612. + *
  68613. + * @param p void pointer to the <code>struct usb_hcd</code>
  68614. + */
  68615. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  68616. +{
  68617. + dwc_otg_hcd_t *dwc_otg_hcd;
  68618. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  68619. + dwc_otg_hcd = p;
  68620. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  68621. + return 1;
  68622. +}
  68623. +
  68624. +/**
  68625. + * HCD Callback function for starting the HCD when A-Cable is
  68626. + * connected.
  68627. + *
  68628. + * @param p void pointer to the <code>struct usb_hcd</code>
  68629. + */
  68630. +static int32_t dwc_otg_hcd_start_cb(void *p)
  68631. +{
  68632. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68633. + dwc_otg_core_if_t *core_if;
  68634. + hprt0_data_t hprt0;
  68635. +
  68636. + core_if = dwc_otg_hcd->core_if;
  68637. +
  68638. + if (core_if->op_state == B_HOST) {
  68639. + /*
  68640. + * Reset the port. During a HNP mode switch the reset
  68641. + * needs to occur within 1ms and have a duration of at
  68642. + * least 50ms.
  68643. + */
  68644. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  68645. + hprt0.b.prtrst = 1;
  68646. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68647. + }
  68648. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  68649. + hcd_start_func, dwc_otg_hcd, 50,
  68650. + "start hcd");
  68651. +
  68652. + return 1;
  68653. +}
  68654. +
  68655. +/**
  68656. + * HCD Callback function for disconnect of the HCD.
  68657. + *
  68658. + * @param p void pointer to the <code>struct usb_hcd</code>
  68659. + */
  68660. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  68661. +{
  68662. + gintsts_data_t intr;
  68663. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68664. +
  68665. + /*
  68666. + * Set status flags for the hub driver.
  68667. + */
  68668. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  68669. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  68670. + if(fiq_fix_enable)
  68671. + local_fiq_disable();
  68672. + /*
  68673. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  68674. + * interrupt mask and status bits and disabling subsequent host
  68675. + * channel interrupts.
  68676. + */
  68677. + intr.d32 = 0;
  68678. + intr.b.nptxfempty = 1;
  68679. + intr.b.ptxfempty = 1;
  68680. + intr.b.hcintr = 1;
  68681. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  68682. + intr.d32, 0);
  68683. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  68684. + intr.d32, 0);
  68685. +
  68686. + del_timers(dwc_otg_hcd);
  68687. +
  68688. + /*
  68689. + * Turn off the vbus power only if the core has transitioned to device
  68690. + * mode. If still in host mode, need to keep power on to detect a
  68691. + * reconnection.
  68692. + */
  68693. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  68694. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  68695. + hprt0_data_t hprt0 = {.d32 = 0 };
  68696. + DWC_PRINTF("Disconnect: PortPower off\n");
  68697. + hprt0.b.prtpwr = 0;
  68698. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  68699. + hprt0.d32);
  68700. + }
  68701. +
  68702. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  68703. + }
  68704. +
  68705. + /* Respond with an error status to all URBs in the schedule. */
  68706. + kill_all_urbs(dwc_otg_hcd);
  68707. +
  68708. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  68709. + /* Clean up any host channels that were in use. */
  68710. + int num_channels;
  68711. + int i;
  68712. + dwc_hc_t *channel;
  68713. + dwc_otg_hc_regs_t *hc_regs;
  68714. + hcchar_data_t hcchar;
  68715. +
  68716. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  68717. +
  68718. + if (!dwc_otg_hcd->core_if->dma_enable) {
  68719. + /* Flush out any channel requests in slave mode. */
  68720. + for (i = 0; i < num_channels; i++) {
  68721. + channel = dwc_otg_hcd->hc_ptr_array[i];
  68722. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  68723. + (channel, hc_list_entry)) {
  68724. + hc_regs =
  68725. + dwc_otg_hcd->core_if->
  68726. + host_if->hc_regs[i];
  68727. + hcchar.d32 =
  68728. + DWC_READ_REG32(&hc_regs->hcchar);
  68729. + if (hcchar.b.chen) {
  68730. + hcchar.b.chen = 0;
  68731. + hcchar.b.chdis = 1;
  68732. + hcchar.b.epdir = 0;
  68733. + DWC_WRITE_REG32
  68734. + (&hc_regs->hcchar,
  68735. + hcchar.d32);
  68736. + }
  68737. + }
  68738. + }
  68739. + }
  68740. +
  68741. + for (i = 0; i < num_channels; i++) {
  68742. + channel = dwc_otg_hcd->hc_ptr_array[i];
  68743. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  68744. + hc_regs =
  68745. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  68746. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  68747. + if (hcchar.b.chen) {
  68748. + /* Halt the channel. */
  68749. + hcchar.b.chdis = 1;
  68750. + DWC_WRITE_REG32(&hc_regs->hcchar,
  68751. + hcchar.d32);
  68752. + }
  68753. +
  68754. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  68755. + channel);
  68756. + DWC_CIRCLEQ_INSERT_TAIL
  68757. + (&dwc_otg_hcd->free_hc_list, channel,
  68758. + hc_list_entry);
  68759. + /*
  68760. + * Added for Descriptor DMA to prevent channel double cleanup
  68761. + * in release_channel_ddma(). Which called from ep_disable
  68762. + * when device disconnect.
  68763. + */
  68764. + channel->qh = NULL;
  68765. + }
  68766. + }
  68767. + if(fiq_split_enable) {
  68768. + for(i=0; i < 128; i++) {
  68769. + dwc_otg_hcd->hub_port[i] = 0;
  68770. + }
  68771. + haint_saved.d32 = 0;
  68772. + for(i=0; i < MAX_EPS_CHANNELS; i++) {
  68773. + hcint_saved[i].d32 = 0;
  68774. + hcintmsk_saved[i].d32 = 0;
  68775. + }
  68776. + }
  68777. +
  68778. + }
  68779. +
  68780. + if(fiq_fix_enable)
  68781. + local_fiq_enable();
  68782. +
  68783. + if (dwc_otg_hcd->fops->disconnect) {
  68784. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  68785. + }
  68786. +
  68787. + return 1;
  68788. +}
  68789. +
  68790. +/**
  68791. + * HCD Callback function for stopping the HCD.
  68792. + *
  68793. + * @param p void pointer to the <code>struct usb_hcd</code>
  68794. + */
  68795. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  68796. +{
  68797. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68798. +
  68799. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  68800. + dwc_otg_hcd_stop(dwc_otg_hcd);
  68801. + return 1;
  68802. +}
  68803. +
  68804. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68805. +/**
  68806. + * HCD Callback function for sleep of HCD.
  68807. + *
  68808. + * @param p void pointer to the <code>struct usb_hcd</code>
  68809. + */
  68810. +static int dwc_otg_hcd_sleep_cb(void *p)
  68811. +{
  68812. + dwc_otg_hcd_t *hcd = p;
  68813. +
  68814. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  68815. +
  68816. + return 0;
  68817. +}
  68818. +#endif
  68819. +
  68820. +
  68821. +/**
  68822. + * HCD Callback function for Remote Wakeup.
  68823. + *
  68824. + * @param p void pointer to the <code>struct usb_hcd</code>
  68825. + */
  68826. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  68827. +{
  68828. + dwc_otg_hcd_t *hcd = p;
  68829. +
  68830. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  68831. + hcd->flags.b.port_suspend_change = 1;
  68832. + }
  68833. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68834. + else {
  68835. + hcd->flags.b.port_l1_change = 1;
  68836. + }
  68837. +#endif
  68838. + return 0;
  68839. +}
  68840. +
  68841. +/**
  68842. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  68843. + * stopped.
  68844. + */
  68845. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  68846. +{
  68847. + hprt0_data_t hprt0 = {.d32 = 0 };
  68848. +
  68849. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  68850. +
  68851. + /*
  68852. + * The root hub should be disconnected before this function is called.
  68853. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  68854. + * and the QH lists (via ..._hcd_endpoint_disable).
  68855. + */
  68856. +
  68857. + /* Turn off all host-specific interrupts. */
  68858. + dwc_otg_disable_host_interrupts(hcd->core_if);
  68859. +
  68860. + /* Turn off the vbus power */
  68861. + DWC_PRINTF("PortPower off\n");
  68862. + hprt0.b.prtpwr = 0;
  68863. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  68864. + dwc_mdelay(1);
  68865. +}
  68866. +
  68867. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  68868. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  68869. + int atomic_alloc)
  68870. +{
  68871. + int retval = 0;
  68872. + uint8_t needs_scheduling = 0;
  68873. + dwc_otg_transaction_type_e tr_type;
  68874. + dwc_otg_qtd_t *qtd;
  68875. + gintmsk_data_t intr_mask = {.d32 = 0 };
  68876. + hprt0_data_t hprt0 = { .d32 = 0 };
  68877. +
  68878. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68879. + if (NULL == hcd->core_if) {
  68880. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  68881. + /* No longer connected. */
  68882. + return -DWC_E_INVALID;
  68883. + }
  68884. +#endif
  68885. + if (!hcd->flags.b.port_connect_status) {
  68886. + /* No longer connected. */
  68887. + DWC_ERROR("Not connected\n");
  68888. + return -DWC_E_NO_DEVICE;
  68889. + }
  68890. +
  68891. + /* Some core configurations cannot support LS traffic on a FS root port */
  68892. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  68893. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  68894. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  68895. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  68896. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  68897. + return -DWC_E_NO_DEVICE;
  68898. + }
  68899. + }
  68900. +
  68901. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  68902. + if (qtd == NULL) {
  68903. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  68904. + return -DWC_E_NO_MEMORY;
  68905. + }
  68906. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68907. + if (qtd->urb == NULL) {
  68908. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  68909. + return -DWC_E_NO_MEMORY;
  68910. + }
  68911. + if (qtd->urb->priv == NULL) {
  68912. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  68913. + return -DWC_E_NO_MEMORY;
  68914. + }
  68915. +#endif
  68916. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  68917. + if(!intr_mask.b.sofintr) needs_scheduling = 1;
  68918. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  68919. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  68920. + needs_scheduling = 0;
  68921. +
  68922. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  68923. + // creates a new queue in ep_handle if it doesn't exist already
  68924. + if (retval < 0) {
  68925. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  68926. + "Error status %d\n", retval);
  68927. + dwc_otg_hcd_qtd_free(qtd);
  68928. + return retval;
  68929. + }
  68930. +
  68931. + if(needs_scheduling) {
  68932. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  68933. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  68934. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  68935. + }
  68936. + }
  68937. + return retval;
  68938. +}
  68939. +
  68940. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  68941. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  68942. +{
  68943. + dwc_otg_qh_t *qh;
  68944. + dwc_otg_qtd_t *urb_qtd;
  68945. + BUG_ON(!hcd);
  68946. + BUG_ON(!dwc_otg_urb);
  68947. +
  68948. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68949. +
  68950. + if (hcd == NULL) {
  68951. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  68952. + return -DWC_E_INVALID;
  68953. + }
  68954. + if (dwc_otg_urb == NULL) {
  68955. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  68956. + return -DWC_E_INVALID;
  68957. + }
  68958. + if (dwc_otg_urb->qtd == NULL) {
  68959. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  68960. + return -DWC_E_INVALID;
  68961. + }
  68962. + urb_qtd = dwc_otg_urb->qtd;
  68963. + BUG_ON(!urb_qtd);
  68964. + if (urb_qtd->qh == NULL) {
  68965. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  68966. + return -DWC_E_INVALID;
  68967. + }
  68968. +#else
  68969. + urb_qtd = dwc_otg_urb->qtd;
  68970. + BUG_ON(!urb_qtd);
  68971. +#endif
  68972. + qh = urb_qtd->qh;
  68973. + BUG_ON(!qh);
  68974. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  68975. + if (urb_qtd->in_process) {
  68976. + dump_channel_info(hcd, qh);
  68977. + }
  68978. + }
  68979. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68980. + if (hcd->core_if == NULL) {
  68981. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  68982. + return -DWC_E_INVALID;
  68983. + }
  68984. +#endif
  68985. + if (urb_qtd->in_process && qh->channel) {
  68986. + /* The QTD is in process (it has been assigned to a channel). */
  68987. + if (hcd->flags.b.port_connect_status) {
  68988. + /*
  68989. + * If still connected (i.e. in host mode), halt the
  68990. + * channel so it can be used for other transfers. If
  68991. + * no longer connected, the host registers can't be
  68992. + * written to halt the channel since the core is in
  68993. + * device mode.
  68994. + */
  68995. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  68996. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  68997. +
  68998. + dwc_otg_hcd_release_port(hcd, qh);
  68999. + }
  69000. + }
  69001. +
  69002. + /*
  69003. + * Free the QTD and clean up the associated QH. Leave the QH in the
  69004. + * schedule if it has any remaining QTDs.
  69005. + */
  69006. +
  69007. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  69008. + "delete %sQueue handler\n",
  69009. + hcd->core_if->dma_desc_enable?"DMA ":"");
  69010. + if (!hcd->core_if->dma_desc_enable) {
  69011. + uint8_t b = urb_qtd->in_process;
  69012. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  69013. + if (b) {
  69014. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  69015. + qh->channel = NULL;
  69016. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  69017. + dwc_otg_hcd_qh_remove(hcd, qh);
  69018. + }
  69019. + } else {
  69020. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  69021. + }
  69022. + return 0;
  69023. +}
  69024. +
  69025. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  69026. + int retry)
  69027. +{
  69028. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  69029. + int retval = 0;
  69030. + dwc_irqflags_t flags;
  69031. +
  69032. + if (retry < 0) {
  69033. + retval = -DWC_E_INVALID;
  69034. + goto done;
  69035. + }
  69036. +
  69037. + if (!qh) {
  69038. + retval = -DWC_E_INVALID;
  69039. + goto done;
  69040. + }
  69041. +
  69042. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69043. +
  69044. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  69045. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69046. + retry--;
  69047. + dwc_msleep(5);
  69048. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69049. + }
  69050. +
  69051. + dwc_otg_hcd_qh_remove(hcd, qh);
  69052. +
  69053. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69054. + /*
  69055. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  69056. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  69057. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  69058. + * and dwc_otg_hcd_frame_list_alloc().
  69059. + */
  69060. + dwc_otg_hcd_qh_free(hcd, qh);
  69061. +
  69062. +done:
  69063. + return retval;
  69064. +}
  69065. +
  69066. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  69067. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  69068. +{
  69069. + int retval = 0;
  69070. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  69071. + if (!qh)
  69072. + return -DWC_E_INVALID;
  69073. +
  69074. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  69075. + return retval;
  69076. +}
  69077. +#endif
  69078. +
  69079. +/**
  69080. + * HCD Callback structure for handling mode switching.
  69081. + */
  69082. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  69083. + .start = dwc_otg_hcd_start_cb,
  69084. + .stop = dwc_otg_hcd_stop_cb,
  69085. + .disconnect = dwc_otg_hcd_disconnect_cb,
  69086. + .session_start = dwc_otg_hcd_session_start_cb,
  69087. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  69088. +#ifdef CONFIG_USB_DWC_OTG_LPM
  69089. + .sleep = dwc_otg_hcd_sleep_cb,
  69090. +#endif
  69091. + .p = 0,
  69092. +};
  69093. +
  69094. +/**
  69095. + * Reset tasklet function
  69096. + */
  69097. +static void reset_tasklet_func(void *data)
  69098. +{
  69099. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  69100. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  69101. + hprt0_data_t hprt0;
  69102. +
  69103. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  69104. +
  69105. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  69106. + hprt0.b.prtrst = 1;
  69107. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69108. + dwc_mdelay(60);
  69109. +
  69110. + hprt0.b.prtrst = 0;
  69111. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69112. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  69113. +}
  69114. +
  69115. +static void completion_tasklet_func(void *ptr)
  69116. +{
  69117. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  69118. + struct urb *urb;
  69119. + urb_tq_entry_t *item;
  69120. + dwc_irqflags_t flags;
  69121. +
  69122. + /* This could just be spin_lock_irq */
  69123. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69124. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  69125. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  69126. + urb = item->urb;
  69127. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  69128. + urb_tq_entries);
  69129. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69130. + DWC_FREE(item);
  69131. +
  69132. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  69133. +
  69134. + fiq_print(FIQDBG_PORTHUB, "COMPLETE");
  69135. +
  69136. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69137. + }
  69138. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69139. + return;
  69140. +}
  69141. +
  69142. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  69143. +{
  69144. + dwc_list_link_t *item;
  69145. + dwc_otg_qh_t *qh;
  69146. + dwc_irqflags_t flags;
  69147. +
  69148. + if (!qh_list->next) {
  69149. + /* The list hasn't been initialized yet. */
  69150. + return;
  69151. + }
  69152. + /*
  69153. + * Hold spinlock here. Not needed in that case if bellow
  69154. + * function is being called from ISR
  69155. + */
  69156. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  69157. + /* Ensure there are no QTDs or URBs left. */
  69158. + kill_urbs_in_qh_list(hcd, qh_list);
  69159. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  69160. +
  69161. + DWC_LIST_FOREACH(item, qh_list) {
  69162. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  69163. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  69164. + }
  69165. +}
  69166. +
  69167. +/**
  69168. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  69169. + * Device during SRP time by host power up.
  69170. + */
  69171. +void dwc_otg_hcd_power_up(void *ptr)
  69172. +{
  69173. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  69174. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  69175. +
  69176. + DWC_PRINTF("%s called\n", __FUNCTION__);
  69177. +
  69178. + if (!core_if->hibernation_suspend) {
  69179. + DWC_PRINTF("Already exited from Hibernation\n");
  69180. + return;
  69181. + }
  69182. +
  69183. + /* Switch on the voltage to the core */
  69184. + gpwrdn.b.pwrdnswtch = 1;
  69185. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69186. + dwc_udelay(10);
  69187. +
  69188. + /* Reset the core */
  69189. + gpwrdn.d32 = 0;
  69190. + gpwrdn.b.pwrdnrstn = 1;
  69191. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69192. + dwc_udelay(10);
  69193. +
  69194. + /* Disable power clamps */
  69195. + gpwrdn.d32 = 0;
  69196. + gpwrdn.b.pwrdnclmp = 1;
  69197. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69198. +
  69199. + /* Remove reset the core signal */
  69200. + gpwrdn.d32 = 0;
  69201. + gpwrdn.b.pwrdnrstn = 1;
  69202. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  69203. + dwc_udelay(10);
  69204. +
  69205. + /* Disable PMU interrupt */
  69206. + gpwrdn.d32 = 0;
  69207. + gpwrdn.b.pmuintsel = 1;
  69208. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69209. +
  69210. + core_if->hibernation_suspend = 0;
  69211. +
  69212. + /* Disable PMU */
  69213. + gpwrdn.d32 = 0;
  69214. + gpwrdn.b.pmuactv = 1;
  69215. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69216. + dwc_udelay(10);
  69217. +
  69218. + /* Enable VBUS */
  69219. + gpwrdn.d32 = 0;
  69220. + gpwrdn.b.dis_vbus = 1;
  69221. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  69222. +
  69223. + core_if->op_state = A_HOST;
  69224. + dwc_otg_core_init(core_if);
  69225. + dwc_otg_enable_global_interrupts(core_if);
  69226. + cil_hcd_start(core_if);
  69227. +}
  69228. +
  69229. +/**
  69230. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  69231. + * in the struct usb_hcd field.
  69232. + */
  69233. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  69234. +{
  69235. + int i;
  69236. +
  69237. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  69238. +
  69239. + del_timers(dwc_otg_hcd);
  69240. +
  69241. + /* Free memory for QH/QTD lists */
  69242. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  69243. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  69244. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  69245. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  69246. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  69247. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  69248. +
  69249. + /* Free memory for the host channels. */
  69250. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  69251. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  69252. +
  69253. +#ifdef DEBUG
  69254. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  69255. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  69256. + }
  69257. +#endif
  69258. + if (hc != NULL) {
  69259. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  69260. + i, hc);
  69261. + DWC_FREE(hc);
  69262. + }
  69263. + }
  69264. +
  69265. + if (dwc_otg_hcd->core_if->dma_enable) {
  69266. + if (dwc_otg_hcd->status_buf_dma) {
  69267. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  69268. + dwc_otg_hcd->status_buf,
  69269. + dwc_otg_hcd->status_buf_dma);
  69270. + }
  69271. + } else if (dwc_otg_hcd->status_buf != NULL) {
  69272. + DWC_FREE(dwc_otg_hcd->status_buf);
  69273. + }
  69274. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  69275. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  69276. + /* Set core_if's lock pointer to NULL */
  69277. + dwc_otg_hcd->core_if->lock = NULL;
  69278. +
  69279. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  69280. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  69281. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  69282. +
  69283. +#ifdef DWC_DEV_SRPCAP
  69284. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  69285. + dwc_otg_hcd->core_if->pwron_timer) {
  69286. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  69287. + }
  69288. +#endif
  69289. + DWC_FREE(dwc_otg_hcd);
  69290. +}
  69291. +
  69292. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  69293. +
  69294. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  69295. +{
  69296. + int retval = 0;
  69297. + int num_channels;
  69298. + int i;
  69299. + dwc_hc_t *channel;
  69300. +
  69301. + hcd->lock = DWC_SPINLOCK_ALLOC();
  69302. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  69303. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  69304. + hcd, core_if);
  69305. + if (!hcd->lock) {
  69306. + DWC_ERROR("Could not allocate lock for pcd");
  69307. + DWC_FREE(hcd);
  69308. + retval = -DWC_E_NO_MEMORY;
  69309. + goto out;
  69310. + }
  69311. + hcd->core_if = core_if;
  69312. +
  69313. + /* Register the HCD CIL Callbacks */
  69314. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  69315. + &hcd_cil_callbacks, hcd);
  69316. +
  69317. + /* Initialize the non-periodic schedule. */
  69318. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  69319. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  69320. +
  69321. + /* Initialize the periodic schedule. */
  69322. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  69323. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  69324. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  69325. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  69326. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  69327. + /*
  69328. + * Create a host channel descriptor for each host channel implemented
  69329. + * in the controller. Initialize the channel descriptor array.
  69330. + */
  69331. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  69332. + num_channels = hcd->core_if->core_params->host_channels;
  69333. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  69334. + for (i = 0; i < num_channels; i++) {
  69335. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  69336. + if (channel == NULL) {
  69337. + retval = -DWC_E_NO_MEMORY;
  69338. + DWC_ERROR("%s: host channel allocation failed\n",
  69339. + __func__);
  69340. + dwc_otg_hcd_free(hcd);
  69341. + goto out;
  69342. + }
  69343. + channel->hc_num = i;
  69344. + hcd->hc_ptr_array[i] = channel;
  69345. +#ifdef DEBUG
  69346. + hcd->core_if->hc_xfer_timer[i] =
  69347. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  69348. + &hcd->core_if->hc_xfer_info[i]);
  69349. +#endif
  69350. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  69351. + channel);
  69352. + }
  69353. +
  69354. + /* Initialize the Connection timeout timer. */
  69355. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  69356. + dwc_otg_hcd_connect_timeout, 0);
  69357. +
  69358. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  69359. + if (microframe_schedule)
  69360. + init_hcd_usecs(hcd);
  69361. +
  69362. + /* Initialize reset tasklet. */
  69363. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  69364. +
  69365. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  69366. + completion_tasklet_func, hcd);
  69367. +#ifdef DWC_DEV_SRPCAP
  69368. + if (hcd->core_if->power_down == 2) {
  69369. + /* Initialize Power on timer for Host power up in case hibernation */
  69370. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  69371. + dwc_otg_hcd_power_up, core_if);
  69372. + }
  69373. +#endif
  69374. +
  69375. + /*
  69376. + * Allocate space for storing data on status transactions. Normally no
  69377. + * data is sent, but this space acts as a bit bucket. This must be
  69378. + * done after usb_add_hcd since that function allocates the DMA buffer
  69379. + * pool.
  69380. + */
  69381. + if (hcd->core_if->dma_enable) {
  69382. + hcd->status_buf =
  69383. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  69384. + &hcd->status_buf_dma);
  69385. + } else {
  69386. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  69387. + }
  69388. + if (!hcd->status_buf) {
  69389. + retval = -DWC_E_NO_MEMORY;
  69390. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  69391. + dwc_otg_hcd_free(hcd);
  69392. + goto out;
  69393. + }
  69394. +
  69395. + hcd->otg_port = 1;
  69396. + hcd->frame_list = NULL;
  69397. + hcd->frame_list_dma = 0;
  69398. + hcd->periodic_qh_count = 0;
  69399. +
  69400. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  69401. +#ifdef FIQ_DEBUG
  69402. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  69403. +#endif
  69404. +
  69405. +out:
  69406. + return retval;
  69407. +}
  69408. +
  69409. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  69410. +{
  69411. + /* Turn off all host-specific interrupts. */
  69412. + dwc_otg_disable_host_interrupts(hcd->core_if);
  69413. +
  69414. + dwc_otg_hcd_free(hcd);
  69415. +}
  69416. +
  69417. +/**
  69418. + * Initializes dynamic portions of the DWC_otg HCD state.
  69419. + */
  69420. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  69421. +{
  69422. + int num_channels;
  69423. + int i;
  69424. + dwc_hc_t *channel;
  69425. + dwc_hc_t *channel_tmp;
  69426. +
  69427. + hcd->flags.d32 = 0;
  69428. +
  69429. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  69430. + if (!microframe_schedule) {
  69431. + hcd->non_periodic_channels = 0;
  69432. + hcd->periodic_channels = 0;
  69433. + } else {
  69434. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  69435. + }
  69436. + /*
  69437. + * Put all channels in the free channel list and clean up channel
  69438. + * states.
  69439. + */
  69440. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  69441. + &hcd->free_hc_list, hc_list_entry) {
  69442. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  69443. + }
  69444. +
  69445. + num_channels = hcd->core_if->core_params->host_channels;
  69446. + for (i = 0; i < num_channels; i++) {
  69447. + channel = hcd->hc_ptr_array[i];
  69448. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  69449. + hc_list_entry);
  69450. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  69451. + }
  69452. +
  69453. + /* Initialize the DWC core for host mode operation. */
  69454. + dwc_otg_core_host_init(hcd->core_if);
  69455. +
  69456. + /* Set core_if's lock pointer to the hcd->lock */
  69457. + hcd->core_if->lock = hcd->lock;
  69458. +}
  69459. +
  69460. +/**
  69461. + * Assigns transactions from a QTD to a free host channel and initializes the
  69462. + * host channel to perform the transactions. The host channel is removed from
  69463. + * the free list.
  69464. + *
  69465. + * @param hcd The HCD state structure.
  69466. + * @param qh Transactions from the first QTD for this QH are selected and
  69467. + * assigned to a free host channel.
  69468. + */
  69469. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  69470. +{
  69471. + dwc_hc_t *hc;
  69472. + dwc_otg_qtd_t *qtd;
  69473. + dwc_otg_hcd_urb_t *urb;
  69474. + void* ptr = NULL;
  69475. +
  69476. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  69477. +
  69478. + urb = qtd->urb;
  69479. +
  69480. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  69481. +
  69482. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  69483. + urb->actual_length = urb->length;
  69484. +
  69485. +
  69486. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  69487. +
  69488. + /* Remove the host channel from the free list. */
  69489. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  69490. +
  69491. + qh->channel = hc;
  69492. +
  69493. + qtd->in_process = 1;
  69494. +
  69495. + /*
  69496. + * Use usb_pipedevice to determine device address. This address is
  69497. + * 0 before the SET_ADDRESS command and the correct address afterward.
  69498. + */
  69499. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  69500. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  69501. + hc->speed = qh->dev_speed;
  69502. + hc->max_packet = dwc_max_packet(qh->maxp);
  69503. +
  69504. + hc->xfer_started = 0;
  69505. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  69506. + hc->error_state = (qtd->error_count > 0);
  69507. + hc->halt_on_queue = 0;
  69508. + hc->halt_pending = 0;
  69509. + hc->requests = 0;
  69510. +
  69511. + /*
  69512. + * The following values may be modified in the transfer type section
  69513. + * below. The xfer_len value may be reduced when the transfer is
  69514. + * started to accommodate the max widths of the XferSize and PktCnt
  69515. + * fields in the HCTSIZn register.
  69516. + */
  69517. +
  69518. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  69519. + if (hc->ep_is_in) {
  69520. + hc->do_ping = 0;
  69521. + } else {
  69522. + hc->do_ping = qh->ping_state;
  69523. + }
  69524. +
  69525. + hc->data_pid_start = qh->data_toggle;
  69526. + hc->multi_count = 1;
  69527. +
  69528. + if (hcd->core_if->dma_enable) {
  69529. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  69530. +
  69531. + /* For non-dword aligned case */
  69532. + if (((unsigned long)hc->xfer_buff & 0x3)
  69533. + && !hcd->core_if->dma_desc_enable) {
  69534. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  69535. + }
  69536. + } else {
  69537. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  69538. + }
  69539. + hc->xfer_len = urb->length - urb->actual_length;
  69540. + hc->xfer_count = 0;
  69541. +
  69542. + /*
  69543. + * Set the split attributes
  69544. + */
  69545. + hc->do_split = 0;
  69546. + if (qh->do_split) {
  69547. + uint32_t hub_addr, port_addr;
  69548. + hc->do_split = 1;
  69549. + hc->xact_pos = qtd->isoc_split_pos;
  69550. + /* We don't need to do complete splits anymore */
  69551. + if(fiq_split_enable)
  69552. + hc->complete_split = qtd->complete_split = 0;
  69553. + else
  69554. + hc->complete_split = qtd->complete_split;
  69555. +
  69556. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  69557. + hc->hub_addr = (uint8_t) hub_addr;
  69558. + hc->port_addr = (uint8_t) port_addr;
  69559. + }
  69560. +
  69561. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  69562. + case UE_CONTROL:
  69563. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  69564. + switch (qtd->control_phase) {
  69565. + case DWC_OTG_CONTROL_SETUP:
  69566. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  69567. + hc->do_ping = 0;
  69568. + hc->ep_is_in = 0;
  69569. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  69570. + if (hcd->core_if->dma_enable) {
  69571. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  69572. + } else {
  69573. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  69574. + }
  69575. + hc->xfer_len = 8;
  69576. + ptr = NULL;
  69577. + break;
  69578. + case DWC_OTG_CONTROL_DATA:
  69579. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  69580. + hc->data_pid_start = qtd->data_toggle;
  69581. + break;
  69582. + case DWC_OTG_CONTROL_STATUS:
  69583. + /*
  69584. + * Direction is opposite of data direction or IN if no
  69585. + * data.
  69586. + */
  69587. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  69588. + if (urb->length == 0) {
  69589. + hc->ep_is_in = 1;
  69590. + } else {
  69591. + hc->ep_is_in =
  69592. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  69593. + }
  69594. + if (hc->ep_is_in) {
  69595. + hc->do_ping = 0;
  69596. + }
  69597. +
  69598. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  69599. +
  69600. + hc->xfer_len = 0;
  69601. + if (hcd->core_if->dma_enable) {
  69602. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  69603. + } else {
  69604. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  69605. + }
  69606. + ptr = NULL;
  69607. + break;
  69608. + }
  69609. + break;
  69610. + case UE_BULK:
  69611. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  69612. + break;
  69613. + case UE_INTERRUPT:
  69614. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  69615. + break;
  69616. + case UE_ISOCHRONOUS:
  69617. + {
  69618. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  69619. +
  69620. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  69621. +
  69622. + if (hcd->core_if->dma_desc_enable)
  69623. + break;
  69624. +
  69625. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  69626. +
  69627. + frame_desc->status = 0;
  69628. +
  69629. + if (hcd->core_if->dma_enable) {
  69630. + hc->xfer_buff = (uint8_t *) urb->dma;
  69631. + } else {
  69632. + hc->xfer_buff = (uint8_t *) urb->buf;
  69633. + }
  69634. + hc->xfer_buff +=
  69635. + frame_desc->offset + qtd->isoc_split_offset;
  69636. + hc->xfer_len =
  69637. + frame_desc->length - qtd->isoc_split_offset;
  69638. +
  69639. + /* For non-dword aligned buffers */
  69640. + if (((unsigned long)hc->xfer_buff & 0x3)
  69641. + && hcd->core_if->dma_enable) {
  69642. + ptr =
  69643. + (uint8_t *) urb->buf + frame_desc->offset +
  69644. + qtd->isoc_split_offset;
  69645. + } else
  69646. + ptr = NULL;
  69647. +
  69648. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  69649. + if (hc->xfer_len <= 188) {
  69650. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  69651. + } else {
  69652. + hc->xact_pos =
  69653. + DWC_HCSPLIT_XACTPOS_BEGIN;
  69654. + }
  69655. + }
  69656. + }
  69657. + break;
  69658. + }
  69659. + /* non DWORD-aligned buffer case */
  69660. + if (ptr) {
  69661. + uint32_t buf_size;
  69662. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  69663. + buf_size = hcd->core_if->core_params->max_transfer_size;
  69664. + } else {
  69665. + buf_size = 4096;
  69666. + }
  69667. + if (!qh->dw_align_buf) {
  69668. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  69669. + &qh->dw_align_buf_dma);
  69670. + if (!qh->dw_align_buf) {
  69671. + DWC_ERROR
  69672. + ("%s: Failed to allocate memory to handle "
  69673. + "non-dword aligned buffer case\n",
  69674. + __func__);
  69675. + return;
  69676. + }
  69677. + }
  69678. + if (!hc->ep_is_in) {
  69679. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  69680. + }
  69681. + hc->align_buff = qh->dw_align_buf_dma;
  69682. + } else {
  69683. + hc->align_buff = 0;
  69684. + }
  69685. +
  69686. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  69687. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  69688. + /*
  69689. + * This value may be modified when the transfer is started to
  69690. + * reflect the actual transfer length.
  69691. + */
  69692. + hc->multi_count = dwc_hb_mult(qh->maxp);
  69693. + }
  69694. +
  69695. + if (hcd->core_if->dma_desc_enable)
  69696. + hc->desc_list_addr = qh->desc_list_dma;
  69697. +
  69698. + dwc_otg_hc_init(hcd->core_if, hc);
  69699. + hc->qh = qh;
  69700. +}
  69701. +
  69702. +/*
  69703. +** Check the transaction to see if the port / hub has already been assigned for
  69704. +** a split transaction
  69705. +**
  69706. +** Return 0 - Port is already in use
  69707. +*/
  69708. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh)
  69709. +{
  69710. + uint32_t hub_addr, port_addr;
  69711. +
  69712. + if(!fiq_split_enable)
  69713. + return 0;
  69714. +
  69715. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  69716. +
  69717. + if(hcd->hub_port[hub_addr] & (1 << port_addr))
  69718. + {
  69719. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:S%02d", hub_addr, port_addr, qh->skip_count);
  69720. +
  69721. + qh->skip_count++;
  69722. +
  69723. + if(qh->skip_count > 40000)
  69724. + {
  69725. + printk_once(KERN_ERR "Error: Having to skip port allocation");
  69726. + local_fiq_disable();
  69727. + BUG();
  69728. + return 0;
  69729. + }
  69730. + return 1;
  69731. + }
  69732. + else
  69733. + {
  69734. + qh->skip_count = 0;
  69735. + hcd->hub_port[hub_addr] |= 1 << port_addr;
  69736. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:A %d", hub_addr, port_addr, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->pipe_info.ep_num);
  69737. +#ifdef FIQ_DEBUG
  69738. + hcd->hub_port_alloc[hub_addr * 16 + port_addr] = dwc_otg_hcd_get_frame_number(hcd);
  69739. +#endif
  69740. + return 0;
  69741. + }
  69742. +}
  69743. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh)
  69744. +{
  69745. + uint32_t hub_addr, port_addr;
  69746. +
  69747. + if(!fiq_split_enable)
  69748. + return;
  69749. +
  69750. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  69751. +
  69752. + hcd->hub_port[hub_addr] &= ~(1 << port_addr);
  69753. +#ifdef FIQ_DEBUG
  69754. + hcd->hub_port_alloc[hub_addr * 16 + port_addr] = -1;
  69755. +#endif
  69756. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:RO%d", hub_addr, port_addr, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->pipe_info.ep_num);
  69757. +
  69758. +}
  69759. +
  69760. +
  69761. +/**
  69762. + * This function selects transactions from the HCD transfer schedule and
  69763. + * assigns them to available host channels. It is called from HCD interrupt
  69764. + * handler functions.
  69765. + *
  69766. + * @param hcd The HCD state structure.
  69767. + *
  69768. + * @return The types of new transactions that were assigned to host channels.
  69769. + */
  69770. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  69771. +{
  69772. + dwc_list_link_t *qh_ptr;
  69773. + dwc_otg_qh_t *qh;
  69774. + dwc_otg_qtd_t *qtd;
  69775. + int num_channels;
  69776. + dwc_irqflags_t flags;
  69777. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  69778. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  69779. +
  69780. +#ifdef DEBUG_SOF
  69781. + DWC_DEBUGPL(DBG_HCD, " Select Transactions\n");
  69782. +#endif
  69783. +
  69784. +#ifdef DEBUG_HOST_CHANNELS
  69785. + last_sel_trans_num_per_scheduled = 0;
  69786. + last_sel_trans_num_nonper_scheduled = 0;
  69787. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  69788. +#endif /* DEBUG_HOST_CHANNELS */
  69789. +
  69790. + /* Process entries in the periodic ready list. */
  69791. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  69792. +
  69793. + while (qh_ptr != &hcd->periodic_sched_ready &&
  69794. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  69795. +
  69796. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69797. +
  69798. + if(qh->do_split) {
  69799. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  69800. + if(!(qh->ep_type == UE_ISOCHRONOUS &&
  69801. + (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  69802. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END))) {
  69803. + if(dwc_otg_hcd_allocate_port(hcd, qh))
  69804. + {
  69805. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69806. + g_next_sched_frame = dwc_frame_num_inc(dwc_otg_hcd_get_frame_number(hcd), 1);
  69807. + continue;
  69808. + }
  69809. + }
  69810. + }
  69811. +
  69812. + if (microframe_schedule) {
  69813. + // Make sure we leave one channel for non periodic transactions.
  69814. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69815. + if (hcd->available_host_channels <= 1) {
  69816. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69817. + if(qh->do_split) dwc_otg_hcd_release_port(hcd, qh);
  69818. + break;
  69819. + }
  69820. + hcd->available_host_channels--;
  69821. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69822. +#ifdef DEBUG_HOST_CHANNELS
  69823. + last_sel_trans_num_per_scheduled++;
  69824. +#endif /* DEBUG_HOST_CHANNELS */
  69825. + }
  69826. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69827. + assign_and_init_hc(hcd, qh);
  69828. +
  69829. + /*
  69830. + * Move the QH from the periodic ready schedule to the
  69831. + * periodic assigned schedule.
  69832. + */
  69833. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69834. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69835. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  69836. + &qh->qh_list_entry);
  69837. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69838. + }
  69839. +
  69840. + /*
  69841. + * Process entries in the inactive portion of the non-periodic
  69842. + * schedule. Some free host channels may not be used if they are
  69843. + * reserved for periodic transfers.
  69844. + */
  69845. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  69846. + num_channels = hcd->core_if->core_params->host_channels;
  69847. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  69848. + (microframe_schedule || hcd->non_periodic_channels <
  69849. + num_channels - hcd->periodic_channels) &&
  69850. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  69851. +
  69852. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69853. +
  69854. + /*
  69855. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  69856. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  69857. + * cheeky devices that just hold off using NAKs
  69858. + */
  69859. + if (nak_holdoff_enable && qh->do_split) {
  69860. + if (qh->nak_frame != 0xffff &&
  69861. + dwc_full_frame_num(qh->nak_frame) ==
  69862. + dwc_full_frame_num(dwc_otg_hcd_get_frame_number(hcd))) {
  69863. + /*
  69864. + * Revisit: Need to avoid trampling on periodic scheduling.
  69865. + * Currently we are safe because g_np_count != g_np_sent whenever we hit this,
  69866. + * but if this behaviour is changed then periodic endpoints will get a slower
  69867. + * polling rate.
  69868. + */
  69869. + g_next_sched_frame = ((qh->nak_frame + 8) & ~7) & DWC_HFNUM_MAX_FRNUM;
  69870. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69871. + continue;
  69872. + } else {
  69873. + qh->nak_frame = 0xffff;
  69874. + }
  69875. + }
  69876. +
  69877. + if (microframe_schedule) {
  69878. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69879. + if (hcd->available_host_channels < 1) {
  69880. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69881. + break;
  69882. + }
  69883. + hcd->available_host_channels--;
  69884. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69885. +#ifdef DEBUG_HOST_CHANNELS
  69886. + last_sel_trans_num_nonper_scheduled++;
  69887. +#endif /* DEBUG_HOST_CHANNELS */
  69888. + }
  69889. +
  69890. + assign_and_init_hc(hcd, qh);
  69891. +
  69892. + /*
  69893. + * Move the QH from the non-periodic inactive schedule to the
  69894. + * non-periodic active schedule.
  69895. + */
  69896. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69897. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69898. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  69899. + &qh->qh_list_entry);
  69900. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69901. +
  69902. + g_np_sent++;
  69903. +
  69904. + if (!microframe_schedule)
  69905. + hcd->non_periodic_channels++;
  69906. + }
  69907. +
  69908. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  69909. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  69910. +
  69911. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  69912. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  69913. +
  69914. +
  69915. +#ifdef DEBUG_HOST_CHANNELS
  69916. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  69917. +#endif /* DEBUG_HOST_CHANNELS */
  69918. + return ret_val;
  69919. +}
  69920. +
  69921. +/**
  69922. + * Attempts to queue a single transaction request for a host channel
  69923. + * associated with either a periodic or non-periodic transfer. This function
  69924. + * assumes that there is space available in the appropriate request queue. For
  69925. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  69926. + * is available in the appropriate Tx FIFO.
  69927. + *
  69928. + * @param hcd The HCD state structure.
  69929. + * @param hc Host channel descriptor associated with either a periodic or
  69930. + * non-periodic transfer.
  69931. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  69932. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  69933. + * transfers.
  69934. + *
  69935. + * @return 1 if a request is queued and more requests may be needed to
  69936. + * complete the transfer, 0 if no more requests are required for this
  69937. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  69938. + */
  69939. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  69940. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  69941. +{
  69942. + int retval;
  69943. +
  69944. + if (hcd->core_if->dma_enable) {
  69945. + if (hcd->core_if->dma_desc_enable) {
  69946. + if (!hc->xfer_started
  69947. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  69948. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  69949. + hc->qh->ping_state = 0;
  69950. + }
  69951. + } else if (!hc->xfer_started) {
  69952. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69953. + hc->qh->ping_state = 0;
  69954. + }
  69955. + retval = 0;
  69956. + } else if (hc->halt_pending) {
  69957. + /* Don't queue a request if the channel has been halted. */
  69958. + retval = 0;
  69959. + } else if (hc->halt_on_queue) {
  69960. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  69961. + retval = 0;
  69962. + } else if (hc->do_ping) {
  69963. + if (!hc->xfer_started) {
  69964. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69965. + }
  69966. + retval = 0;
  69967. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  69968. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  69969. + if (!hc->xfer_started) {
  69970. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69971. + retval = 1;
  69972. + } else {
  69973. + retval =
  69974. + dwc_otg_hc_continue_transfer(hcd->core_if,
  69975. + hc);
  69976. + }
  69977. + } else {
  69978. + retval = -1;
  69979. + }
  69980. + } else {
  69981. + if (!hc->xfer_started) {
  69982. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69983. + retval = 1;
  69984. + } else {
  69985. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  69986. + }
  69987. + }
  69988. +
  69989. + return retval;
  69990. +}
  69991. +
  69992. +/**
  69993. + * Processes periodic channels for the next frame and queues transactions for
  69994. + * these channels to the DWC_otg controller. After queueing transactions, the
  69995. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  69996. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  69997. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  69998. + */
  69999. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  70000. +{
  70001. + hptxsts_data_t tx_status;
  70002. + dwc_list_link_t *qh_ptr;
  70003. + dwc_otg_qh_t *qh;
  70004. + int status;
  70005. + int no_queue_space = 0;
  70006. + int no_fifo_space = 0;
  70007. +
  70008. + dwc_otg_host_global_regs_t *host_regs;
  70009. + host_regs = hcd->core_if->host_if->host_global_regs;
  70010. +
  70011. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  70012. +#ifdef DEBUG
  70013. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  70014. + DWC_DEBUGPL(DBG_HCDV,
  70015. + " P Tx Req Queue Space Avail (before queue): %d\n",
  70016. + tx_status.b.ptxqspcavail);
  70017. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  70018. + tx_status.b.ptxfspcavail);
  70019. +#endif
  70020. +
  70021. + qh_ptr = hcd->periodic_sched_assigned.next;
  70022. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  70023. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  70024. + if (tx_status.b.ptxqspcavail == 0) {
  70025. + no_queue_space = 1;
  70026. + break;
  70027. + }
  70028. +
  70029. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  70030. +
  70031. + // Do not send a split start transaction any later than frame .6
  70032. + // Note, we have to schedule a periodic in .5 to make it go in .6
  70033. + if(fiq_split_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  70034. + {
  70035. + qh_ptr = qh_ptr->next;
  70036. + g_next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  70037. + continue;
  70038. + }
  70039. +
  70040. + /*
  70041. + * Set a flag if we're queuing high-bandwidth in slave mode.
  70042. + * The flag prevents any halts to get into the request queue in
  70043. + * the middle of multiple high-bandwidth packets getting queued.
  70044. + */
  70045. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  70046. + hcd->core_if->queuing_high_bandwidth = 1;
  70047. + }
  70048. + status =
  70049. + queue_transaction(hcd, qh->channel,
  70050. + tx_status.b.ptxfspcavail);
  70051. + if (status < 0) {
  70052. + no_fifo_space = 1;
  70053. + break;
  70054. + }
  70055. +
  70056. + /*
  70057. + * In Slave mode, stay on the current transfer until there is
  70058. + * nothing more to do or the high-bandwidth request count is
  70059. + * reached. In DMA mode, only need to queue one request. The
  70060. + * controller automatically handles multiple packets for
  70061. + * high-bandwidth transfers.
  70062. + */
  70063. + if (hcd->core_if->dma_enable || status == 0 ||
  70064. + qh->channel->requests == qh->channel->multi_count) {
  70065. + qh_ptr = qh_ptr->next;
  70066. + /*
  70067. + * Move the QH from the periodic assigned schedule to
  70068. + * the periodic queued schedule.
  70069. + */
  70070. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  70071. + &qh->qh_list_entry);
  70072. +
  70073. + /* done queuing high bandwidth */
  70074. + hcd->core_if->queuing_high_bandwidth = 0;
  70075. + }
  70076. + }
  70077. +
  70078. + if (!hcd->core_if->dma_enable) {
  70079. + dwc_otg_core_global_regs_t *global_regs;
  70080. + gintmsk_data_t intr_mask = {.d32 = 0 };
  70081. +
  70082. + global_regs = hcd->core_if->core_global_regs;
  70083. + intr_mask.b.ptxfempty = 1;
  70084. +#ifdef DEBUG
  70085. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  70086. + DWC_DEBUGPL(DBG_HCDV,
  70087. + " P Tx Req Queue Space Avail (after queue): %d\n",
  70088. + tx_status.b.ptxqspcavail);
  70089. + DWC_DEBUGPL(DBG_HCDV,
  70090. + " P Tx FIFO Space Avail (after queue): %d\n",
  70091. + tx_status.b.ptxfspcavail);
  70092. +#endif
  70093. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  70094. + no_queue_space || no_fifo_space) {
  70095. + /*
  70096. + * May need to queue more transactions as the request
  70097. + * queue or Tx FIFO empties. Enable the periodic Tx
  70098. + * FIFO empty interrupt. (Always use the half-empty
  70099. + * level to ensure that new requests are loaded as
  70100. + * soon as possible.)
  70101. + */
  70102. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  70103. + intr_mask.d32);
  70104. + } else {
  70105. + /*
  70106. + * Disable the Tx FIFO empty interrupt since there are
  70107. + * no more transactions that need to be queued right
  70108. + * now. This function is called from interrupt
  70109. + * handlers to queue more transactions as transfer
  70110. + * states change.
  70111. + */
  70112. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  70113. + 0);
  70114. + }
  70115. + }
  70116. +}
  70117. +
  70118. +/**
  70119. + * Processes active non-periodic channels and queues transactions for these
  70120. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  70121. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  70122. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  70123. + * FIFO Empty interrupt is disabled.
  70124. + */
  70125. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  70126. +{
  70127. + gnptxsts_data_t tx_status;
  70128. + dwc_list_link_t *orig_qh_ptr;
  70129. + dwc_otg_qh_t *qh;
  70130. + int status;
  70131. + int no_queue_space = 0;
  70132. + int no_fifo_space = 0;
  70133. + int more_to_do = 0;
  70134. +
  70135. + dwc_otg_core_global_regs_t *global_regs =
  70136. + hcd->core_if->core_global_regs;
  70137. +
  70138. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  70139. +#ifdef DEBUG
  70140. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  70141. + DWC_DEBUGPL(DBG_HCDV,
  70142. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  70143. + tx_status.b.nptxqspcavail);
  70144. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  70145. + tx_status.b.nptxfspcavail);
  70146. +#endif
  70147. + /*
  70148. + * Keep track of the starting point. Skip over the start-of-list
  70149. + * entry.
  70150. + */
  70151. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  70152. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  70153. + }
  70154. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  70155. +
  70156. + /*
  70157. + * Process once through the active list or until no more space is
  70158. + * available in the request queue or the Tx FIFO.
  70159. + */
  70160. + do {
  70161. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  70162. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  70163. + no_queue_space = 1;
  70164. + break;
  70165. + }
  70166. +
  70167. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  70168. + qh_list_entry);
  70169. +
  70170. + // Do not send a split start transaction any later than frame .5
  70171. + // non periodic transactions will start immediately in this uframe
  70172. + if(fiq_split_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  70173. + {
  70174. + g_next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  70175. + break;
  70176. + }
  70177. +
  70178. + status =
  70179. + queue_transaction(hcd, qh->channel,
  70180. + tx_status.b.nptxfspcavail);
  70181. +
  70182. + if (status > 0) {
  70183. + more_to_do = 1;
  70184. + } else if (status < 0) {
  70185. + no_fifo_space = 1;
  70186. + break;
  70187. + }
  70188. +
  70189. + /* Advance to next QH, skipping start-of-list entry. */
  70190. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  70191. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  70192. + hcd->non_periodic_qh_ptr =
  70193. + hcd->non_periodic_qh_ptr->next;
  70194. + }
  70195. +
  70196. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  70197. +
  70198. + if (!hcd->core_if->dma_enable) {
  70199. + gintmsk_data_t intr_mask = {.d32 = 0 };
  70200. + intr_mask.b.nptxfempty = 1;
  70201. +
  70202. +#ifdef DEBUG
  70203. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  70204. + DWC_DEBUGPL(DBG_HCDV,
  70205. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  70206. + tx_status.b.nptxqspcavail);
  70207. + DWC_DEBUGPL(DBG_HCDV,
  70208. + " NP Tx FIFO Space Avail (after queue): %d\n",
  70209. + tx_status.b.nptxfspcavail);
  70210. +#endif
  70211. + if (more_to_do || no_queue_space || no_fifo_space) {
  70212. + /*
  70213. + * May need to queue more transactions as the request
  70214. + * queue or Tx FIFO empties. Enable the non-periodic
  70215. + * Tx FIFO empty interrupt. (Always use the half-empty
  70216. + * level to ensure that new requests are loaded as
  70217. + * soon as possible.)
  70218. + */
  70219. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  70220. + intr_mask.d32);
  70221. + } else {
  70222. + /*
  70223. + * Disable the Tx FIFO empty interrupt since there are
  70224. + * no more transactions that need to be queued right
  70225. + * now. This function is called from interrupt
  70226. + * handlers to queue more transactions as transfer
  70227. + * states change.
  70228. + */
  70229. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  70230. + 0);
  70231. + }
  70232. + }
  70233. +}
  70234. +
  70235. +/**
  70236. + * This function processes the currently active host channels and queues
  70237. + * transactions for these channels to the DWC_otg controller. It is called
  70238. + * from HCD interrupt handler functions.
  70239. + *
  70240. + * @param hcd The HCD state structure.
  70241. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  70242. + * periodic, or both).
  70243. + */
  70244. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  70245. + dwc_otg_transaction_type_e tr_type)
  70246. +{
  70247. +#ifdef DEBUG_SOF
  70248. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  70249. +#endif
  70250. + /* Process host channels associated with periodic transfers. */
  70251. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  70252. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  70253. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  70254. +
  70255. + process_periodic_channels(hcd);
  70256. + }
  70257. +
  70258. + /* Process host channels associated with non-periodic transfers. */
  70259. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  70260. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  70261. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  70262. + process_non_periodic_channels(hcd);
  70263. + } else {
  70264. + /*
  70265. + * Ensure NP Tx FIFO empty interrupt is disabled when
  70266. + * there are no non-periodic transfers to process.
  70267. + */
  70268. + gintmsk_data_t gintmsk = {.d32 = 0 };
  70269. + gintmsk.b.nptxfempty = 1;
  70270. + DWC_MODIFY_REG32(&hcd->core_if->
  70271. + core_global_regs->gintmsk, gintmsk.d32,
  70272. + 0);
  70273. + }
  70274. + }
  70275. +}
  70276. +
  70277. +#ifdef DWC_HS_ELECT_TST
  70278. +/*
  70279. + * Quick and dirty hack to implement the HS Electrical Test
  70280. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  70281. + *
  70282. + * This code was copied from our userspace app "hset". It sends a
  70283. + * Get Device Descriptor control sequence in two parts, first the
  70284. + * Setup packet by itself, followed some time later by the In and
  70285. + * Ack packets. Rather than trying to figure out how to add this
  70286. + * functionality to the normal driver code, we just hijack the
  70287. + * hardware, using these two function to drive the hardware
  70288. + * directly.
  70289. + */
  70290. +
  70291. +static dwc_otg_core_global_regs_t *global_regs;
  70292. +static dwc_otg_host_global_regs_t *hc_global_regs;
  70293. +static dwc_otg_hc_regs_t *hc_regs;
  70294. +static uint32_t *data_fifo;
  70295. +
  70296. +static void do_setup(void)
  70297. +{
  70298. + gintsts_data_t gintsts;
  70299. + hctsiz_data_t hctsiz;
  70300. + hcchar_data_t hcchar;
  70301. + haint_data_t haint;
  70302. + hcint_data_t hcint;
  70303. +
  70304. + /* Enable HAINTs */
  70305. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  70306. +
  70307. + /* Enable HCINTs */
  70308. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  70309. +
  70310. + /* Read GINTSTS */
  70311. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70312. +
  70313. + /* Read HAINT */
  70314. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70315. +
  70316. + /* Read HCINT */
  70317. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70318. +
  70319. + /* Read HCCHAR */
  70320. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70321. +
  70322. + /* Clear HCINT */
  70323. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70324. +
  70325. + /* Clear HAINT */
  70326. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70327. +
  70328. + /* Clear GINTSTS */
  70329. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70330. +
  70331. + /* Read GINTSTS */
  70332. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70333. +
  70334. + /*
  70335. + * Send Setup packet (Get Device Descriptor)
  70336. + */
  70337. +
  70338. + /* Make sure channel is disabled */
  70339. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70340. + if (hcchar.b.chen) {
  70341. + hcchar.b.chdis = 1;
  70342. +// hcchar.b.chen = 1;
  70343. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70344. + //sleep(1);
  70345. + dwc_mdelay(1000);
  70346. +
  70347. + /* Read GINTSTS */
  70348. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70349. +
  70350. + /* Read HAINT */
  70351. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70352. +
  70353. + /* Read HCINT */
  70354. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70355. +
  70356. + /* Read HCCHAR */
  70357. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70358. +
  70359. + /* Clear HCINT */
  70360. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70361. +
  70362. + /* Clear HAINT */
  70363. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70364. +
  70365. + /* Clear GINTSTS */
  70366. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70367. +
  70368. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70369. + }
  70370. +
  70371. + /* Set HCTSIZ */
  70372. + hctsiz.d32 = 0;
  70373. + hctsiz.b.xfersize = 8;
  70374. + hctsiz.b.pktcnt = 1;
  70375. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  70376. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70377. +
  70378. + /* Set HCCHAR */
  70379. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70380. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70381. + hcchar.b.epdir = 0;
  70382. + hcchar.b.epnum = 0;
  70383. + hcchar.b.mps = 8;
  70384. + hcchar.b.chen = 1;
  70385. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70386. +
  70387. + /* Fill FIFO with Setup data for Get Device Descriptor */
  70388. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  70389. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  70390. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  70391. +
  70392. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70393. +
  70394. + /* Wait for host channel interrupt */
  70395. + do {
  70396. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70397. + } while (gintsts.b.hcintr == 0);
  70398. +
  70399. + /* Disable HCINTs */
  70400. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  70401. +
  70402. + /* Disable HAINTs */
  70403. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  70404. +
  70405. + /* Read HAINT */
  70406. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70407. +
  70408. + /* Read HCINT */
  70409. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70410. +
  70411. + /* Read HCCHAR */
  70412. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70413. +
  70414. + /* Clear HCINT */
  70415. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70416. +
  70417. + /* Clear HAINT */
  70418. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70419. +
  70420. + /* Clear GINTSTS */
  70421. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70422. +
  70423. + /* Read GINTSTS */
  70424. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70425. +}
  70426. +
  70427. +static void do_in_ack(void)
  70428. +{
  70429. + gintsts_data_t gintsts;
  70430. + hctsiz_data_t hctsiz;
  70431. + hcchar_data_t hcchar;
  70432. + haint_data_t haint;
  70433. + hcint_data_t hcint;
  70434. + host_grxsts_data_t grxsts;
  70435. +
  70436. + /* Enable HAINTs */
  70437. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  70438. +
  70439. + /* Enable HCINTs */
  70440. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  70441. +
  70442. + /* Read GINTSTS */
  70443. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70444. +
  70445. + /* Read HAINT */
  70446. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70447. +
  70448. + /* Read HCINT */
  70449. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70450. +
  70451. + /* Read HCCHAR */
  70452. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70453. +
  70454. + /* Clear HCINT */
  70455. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70456. +
  70457. + /* Clear HAINT */
  70458. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70459. +
  70460. + /* Clear GINTSTS */
  70461. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70462. +
  70463. + /* Read GINTSTS */
  70464. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70465. +
  70466. + /*
  70467. + * Receive Control In packet
  70468. + */
  70469. +
  70470. + /* Make sure channel is disabled */
  70471. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70472. + if (hcchar.b.chen) {
  70473. + hcchar.b.chdis = 1;
  70474. + hcchar.b.chen = 1;
  70475. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70476. + //sleep(1);
  70477. + dwc_mdelay(1000);
  70478. +
  70479. + /* Read GINTSTS */
  70480. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70481. +
  70482. + /* Read HAINT */
  70483. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70484. +
  70485. + /* Read HCINT */
  70486. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70487. +
  70488. + /* Read HCCHAR */
  70489. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70490. +
  70491. + /* Clear HCINT */
  70492. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70493. +
  70494. + /* Clear HAINT */
  70495. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70496. +
  70497. + /* Clear GINTSTS */
  70498. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70499. +
  70500. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70501. + }
  70502. +
  70503. + /* Set HCTSIZ */
  70504. + hctsiz.d32 = 0;
  70505. + hctsiz.b.xfersize = 8;
  70506. + hctsiz.b.pktcnt = 1;
  70507. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  70508. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70509. +
  70510. + /* Set HCCHAR */
  70511. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70512. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70513. + hcchar.b.epdir = 1;
  70514. + hcchar.b.epnum = 0;
  70515. + hcchar.b.mps = 8;
  70516. + hcchar.b.chen = 1;
  70517. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70518. +
  70519. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70520. +
  70521. + /* Wait for receive status queue interrupt */
  70522. + do {
  70523. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70524. + } while (gintsts.b.rxstsqlvl == 0);
  70525. +
  70526. + /* Read RXSTS */
  70527. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  70528. +
  70529. + /* Clear RXSTSQLVL in GINTSTS */
  70530. + gintsts.d32 = 0;
  70531. + gintsts.b.rxstsqlvl = 1;
  70532. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70533. +
  70534. + switch (grxsts.b.pktsts) {
  70535. + case DWC_GRXSTS_PKTSTS_IN:
  70536. + /* Read the data into the host buffer */
  70537. + if (grxsts.b.bcnt > 0) {
  70538. + int i;
  70539. + int word_count = (grxsts.b.bcnt + 3) / 4;
  70540. +
  70541. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  70542. +
  70543. + for (i = 0; i < word_count; i++) {
  70544. + (void)DWC_READ_REG32(data_fifo++);
  70545. + }
  70546. + }
  70547. + break;
  70548. +
  70549. + default:
  70550. + break;
  70551. + }
  70552. +
  70553. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70554. +
  70555. + /* Wait for receive status queue interrupt */
  70556. + do {
  70557. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70558. + } while (gintsts.b.rxstsqlvl == 0);
  70559. +
  70560. + /* Read RXSTS */
  70561. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  70562. +
  70563. + /* Clear RXSTSQLVL in GINTSTS */
  70564. + gintsts.d32 = 0;
  70565. + gintsts.b.rxstsqlvl = 1;
  70566. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70567. +
  70568. + switch (grxsts.b.pktsts) {
  70569. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  70570. + break;
  70571. +
  70572. + default:
  70573. + break;
  70574. + }
  70575. +
  70576. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70577. +
  70578. + /* Wait for host channel interrupt */
  70579. + do {
  70580. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70581. + } while (gintsts.b.hcintr == 0);
  70582. +
  70583. + /* Read HAINT */
  70584. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70585. +
  70586. + /* Read HCINT */
  70587. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70588. +
  70589. + /* Read HCCHAR */
  70590. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70591. +
  70592. + /* Clear HCINT */
  70593. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70594. +
  70595. + /* Clear HAINT */
  70596. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70597. +
  70598. + /* Clear GINTSTS */
  70599. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70600. +
  70601. + /* Read GINTSTS */
  70602. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70603. +
  70604. +// usleep(100000);
  70605. +// mdelay(100);
  70606. + dwc_mdelay(1);
  70607. +
  70608. + /*
  70609. + * Send handshake packet
  70610. + */
  70611. +
  70612. + /* Read HAINT */
  70613. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70614. +
  70615. + /* Read HCINT */
  70616. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70617. +
  70618. + /* Read HCCHAR */
  70619. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70620. +
  70621. + /* Clear HCINT */
  70622. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70623. +
  70624. + /* Clear HAINT */
  70625. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70626. +
  70627. + /* Clear GINTSTS */
  70628. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70629. +
  70630. + /* Read GINTSTS */
  70631. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70632. +
  70633. + /* Make sure channel is disabled */
  70634. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70635. + if (hcchar.b.chen) {
  70636. + hcchar.b.chdis = 1;
  70637. + hcchar.b.chen = 1;
  70638. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70639. + //sleep(1);
  70640. + dwc_mdelay(1000);
  70641. +
  70642. + /* Read GINTSTS */
  70643. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70644. +
  70645. + /* Read HAINT */
  70646. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70647. +
  70648. + /* Read HCINT */
  70649. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70650. +
  70651. + /* Read HCCHAR */
  70652. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70653. +
  70654. + /* Clear HCINT */
  70655. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70656. +
  70657. + /* Clear HAINT */
  70658. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70659. +
  70660. + /* Clear GINTSTS */
  70661. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70662. +
  70663. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70664. + }
  70665. +
  70666. + /* Set HCTSIZ */
  70667. + hctsiz.d32 = 0;
  70668. + hctsiz.b.xfersize = 0;
  70669. + hctsiz.b.pktcnt = 1;
  70670. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  70671. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70672. +
  70673. + /* Set HCCHAR */
  70674. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70675. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70676. + hcchar.b.epdir = 0;
  70677. + hcchar.b.epnum = 0;
  70678. + hcchar.b.mps = 8;
  70679. + hcchar.b.chen = 1;
  70680. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70681. +
  70682. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70683. +
  70684. + /* Wait for host channel interrupt */
  70685. + do {
  70686. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70687. + } while (gintsts.b.hcintr == 0);
  70688. +
  70689. + /* Disable HCINTs */
  70690. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  70691. +
  70692. + /* Disable HAINTs */
  70693. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  70694. +
  70695. + /* Read HAINT */
  70696. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70697. +
  70698. + /* Read HCINT */
  70699. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70700. +
  70701. + /* Read HCCHAR */
  70702. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70703. +
  70704. + /* Clear HCINT */
  70705. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70706. +
  70707. + /* Clear HAINT */
  70708. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70709. +
  70710. + /* Clear GINTSTS */
  70711. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70712. +
  70713. + /* Read GINTSTS */
  70714. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70715. +}
  70716. +#endif
  70717. +
  70718. +/** Handles hub class-specific requests. */
  70719. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  70720. + uint16_t typeReq,
  70721. + uint16_t wValue,
  70722. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  70723. +{
  70724. + int retval = 0;
  70725. +
  70726. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  70727. + usb_hub_descriptor_t *hub_desc;
  70728. + hprt0_data_t hprt0 = {.d32 = 0 };
  70729. +
  70730. + uint32_t port_status;
  70731. +
  70732. + switch (typeReq) {
  70733. + case UCR_CLEAR_HUB_FEATURE:
  70734. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70735. + "ClearHubFeature 0x%x\n", wValue);
  70736. + switch (wValue) {
  70737. + case UHF_C_HUB_LOCAL_POWER:
  70738. + case UHF_C_HUB_OVER_CURRENT:
  70739. + /* Nothing required here */
  70740. + break;
  70741. + default:
  70742. + retval = -DWC_E_INVALID;
  70743. + DWC_ERROR("DWC OTG HCD - "
  70744. + "ClearHubFeature request %xh unknown\n",
  70745. + wValue);
  70746. + }
  70747. + break;
  70748. + case UCR_CLEAR_PORT_FEATURE:
  70749. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70750. + if (wValue != UHF_PORT_L1)
  70751. +#endif
  70752. + if (!wIndex || wIndex > 1)
  70753. + goto error;
  70754. +
  70755. + switch (wValue) {
  70756. + case UHF_PORT_ENABLE:
  70757. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  70758. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  70759. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70760. + hprt0.b.prtena = 1;
  70761. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70762. + break;
  70763. + case UHF_PORT_SUSPEND:
  70764. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70765. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  70766. +
  70767. + if (core_if->power_down == 2) {
  70768. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  70769. + } else {
  70770. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  70771. + dwc_mdelay(5);
  70772. +
  70773. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70774. + hprt0.b.prtres = 1;
  70775. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70776. + hprt0.b.prtsusp = 0;
  70777. + /* Clear Resume bit */
  70778. + dwc_mdelay(100);
  70779. + hprt0.b.prtres = 0;
  70780. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70781. + }
  70782. + break;
  70783. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70784. + case UHF_PORT_L1:
  70785. + {
  70786. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70787. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  70788. +
  70789. + lpmcfg.d32 =
  70790. + DWC_READ_REG32(&core_if->
  70791. + core_global_regs->glpmcfg);
  70792. + lpmcfg.b.en_utmi_sleep = 0;
  70793. + lpmcfg.b.hird_thres &= (~(1 << 4));
  70794. + lpmcfg.b.prt_sleep_sts = 1;
  70795. + DWC_WRITE_REG32(&core_if->
  70796. + core_global_regs->glpmcfg,
  70797. + lpmcfg.d32);
  70798. +
  70799. + /* Clear Enbl_L1Gating bit. */
  70800. + pcgcctl.b.enbl_sleep_gating = 1;
  70801. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  70802. + 0);
  70803. +
  70804. + dwc_mdelay(5);
  70805. +
  70806. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70807. + hprt0.b.prtres = 1;
  70808. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  70809. + hprt0.d32);
  70810. + /* This bit will be cleared in wakeup interrupt handle */
  70811. + break;
  70812. + }
  70813. +#endif
  70814. + case UHF_PORT_POWER:
  70815. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70816. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  70817. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70818. + hprt0.b.prtpwr = 0;
  70819. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70820. + break;
  70821. + case UHF_PORT_INDICATOR:
  70822. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70823. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  70824. + /* Port inidicator not supported */
  70825. + break;
  70826. + case UHF_C_PORT_CONNECTION:
  70827. + /* Clears drivers internal connect status change
  70828. + * flag */
  70829. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70830. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  70831. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  70832. + break;
  70833. + case UHF_C_PORT_RESET:
  70834. + /* Clears the driver's internal Port Reset Change
  70835. + * flag */
  70836. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70837. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  70838. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  70839. + break;
  70840. + case UHF_C_PORT_ENABLE:
  70841. + /* Clears the driver's internal Port
  70842. + * Enable/Disable Change flag */
  70843. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70844. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  70845. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  70846. + break;
  70847. + case UHF_C_PORT_SUSPEND:
  70848. + /* Clears the driver's internal Port Suspend
  70849. + * Change flag, which is set when resume signaling on
  70850. + * the host port is complete */
  70851. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70852. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  70853. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  70854. + break;
  70855. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70856. + case UHF_C_PORT_L1:
  70857. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  70858. + break;
  70859. +#endif
  70860. + case UHF_C_PORT_OVER_CURRENT:
  70861. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70862. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  70863. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  70864. + break;
  70865. + default:
  70866. + retval = -DWC_E_INVALID;
  70867. + DWC_ERROR("DWC OTG HCD - "
  70868. + "ClearPortFeature request %xh "
  70869. + "unknown or unsupported\n", wValue);
  70870. + }
  70871. + break;
  70872. + case UCR_GET_HUB_DESCRIPTOR:
  70873. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70874. + "GetHubDescriptor\n");
  70875. + hub_desc = (usb_hub_descriptor_t *) buf;
  70876. + hub_desc->bDescLength = 9;
  70877. + hub_desc->bDescriptorType = 0x29;
  70878. + hub_desc->bNbrPorts = 1;
  70879. + USETW(hub_desc->wHubCharacteristics, 0x08);
  70880. + hub_desc->bPwrOn2PwrGood = 1;
  70881. + hub_desc->bHubContrCurrent = 0;
  70882. + hub_desc->DeviceRemovable[0] = 0;
  70883. + hub_desc->DeviceRemovable[1] = 0xff;
  70884. + break;
  70885. + case UCR_GET_HUB_STATUS:
  70886. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70887. + "GetHubStatus\n");
  70888. + DWC_MEMSET(buf, 0, 4);
  70889. + break;
  70890. + case UCR_GET_PORT_STATUS:
  70891. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70892. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  70893. + wIndex, dwc_otg_hcd->flags.d32);
  70894. + if (!wIndex || wIndex > 1)
  70895. + goto error;
  70896. +
  70897. + port_status = 0;
  70898. +
  70899. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  70900. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  70901. +
  70902. + if (dwc_otg_hcd->flags.b.port_enable_change)
  70903. + port_status |= (1 << UHF_C_PORT_ENABLE);
  70904. +
  70905. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  70906. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  70907. +
  70908. + if (dwc_otg_hcd->flags.b.port_l1_change)
  70909. + port_status |= (1 << UHF_C_PORT_L1);
  70910. +
  70911. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  70912. + port_status |= (1 << UHF_C_PORT_RESET);
  70913. + }
  70914. +
  70915. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  70916. + DWC_WARN("Overcurrent change detected\n");
  70917. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  70918. + }
  70919. +
  70920. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  70921. + /*
  70922. + * The port is disconnected, which means the core is
  70923. + * either in device mode or it soon will be. Just
  70924. + * return 0's for the remainder of the port status
  70925. + * since the port register can't be read if the core
  70926. + * is in device mode.
  70927. + */
  70928. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  70929. + break;
  70930. + }
  70931. +
  70932. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  70933. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  70934. +
  70935. + if (hprt0.b.prtconnsts)
  70936. + port_status |= (1 << UHF_PORT_CONNECTION);
  70937. +
  70938. + if (hprt0.b.prtena)
  70939. + port_status |= (1 << UHF_PORT_ENABLE);
  70940. +
  70941. + if (hprt0.b.prtsusp)
  70942. + port_status |= (1 << UHF_PORT_SUSPEND);
  70943. +
  70944. + if (hprt0.b.prtovrcurract)
  70945. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  70946. +
  70947. + if (hprt0.b.prtrst)
  70948. + port_status |= (1 << UHF_PORT_RESET);
  70949. +
  70950. + if (hprt0.b.prtpwr)
  70951. + port_status |= (1 << UHF_PORT_POWER);
  70952. +
  70953. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  70954. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  70955. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  70956. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  70957. +
  70958. + if (hprt0.b.prttstctl)
  70959. + port_status |= (1 << UHF_PORT_TEST);
  70960. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  70961. + port_status |= (1 << UHF_PORT_L1);
  70962. + }
  70963. + /*
  70964. + For Synopsys HW emulation of Power down wkup_control asserts the
  70965. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  70966. + We intentionally tell the software that port is in L2Suspend state.
  70967. + Only for STE.
  70968. + */
  70969. + if ((core_if->power_down == 2)
  70970. + && (core_if->hibernation_suspend == 1)) {
  70971. + port_status |= (1 << UHF_PORT_SUSPEND);
  70972. + }
  70973. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  70974. +
  70975. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  70976. +
  70977. + break;
  70978. + case UCR_SET_HUB_FEATURE:
  70979. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70980. + "SetHubFeature\n");
  70981. + /* No HUB features supported */
  70982. + break;
  70983. + case UCR_SET_PORT_FEATURE:
  70984. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  70985. + goto error;
  70986. +
  70987. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  70988. + /*
  70989. + * The port is disconnected, which means the core is
  70990. + * either in device mode or it soon will be. Just
  70991. + * return without doing anything since the port
  70992. + * register can't be written if the core is in device
  70993. + * mode.
  70994. + */
  70995. + break;
  70996. + }
  70997. +
  70998. + switch (wValue) {
  70999. + case UHF_PORT_SUSPEND:
  71000. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  71001. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  71002. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  71003. + goto error;
  71004. + }
  71005. + if (core_if->power_down == 2) {
  71006. + int timeout = 300;
  71007. + dwc_irqflags_t flags;
  71008. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71009. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71010. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  71011. +#ifdef DWC_DEV_SRPCAP
  71012. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  71013. +#endif
  71014. + DWC_PRINTF("Preparing for complete power-off\n");
  71015. +
  71016. + /* Save registers before hibernation */
  71017. + dwc_otg_save_global_regs(core_if);
  71018. + dwc_otg_save_host_regs(core_if);
  71019. +
  71020. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71021. + hprt0.b.prtsusp = 1;
  71022. + hprt0.b.prtena = 0;
  71023. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71024. + /* Spin hprt0.b.prtsusp to became 1 */
  71025. + do {
  71026. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71027. + if (hprt0.b.prtsusp) {
  71028. + break;
  71029. + }
  71030. + dwc_mdelay(1);
  71031. + } while (--timeout);
  71032. + if (!timeout) {
  71033. + DWC_WARN("Suspend wasn't genereted\n");
  71034. + }
  71035. + dwc_udelay(10);
  71036. +
  71037. + /*
  71038. + * We need to disable interrupts to prevent servicing of any IRQ
  71039. + * during going to hibernation
  71040. + */
  71041. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  71042. + core_if->lx_state = DWC_OTG_L2;
  71043. +#ifdef DWC_DEV_SRPCAP
  71044. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71045. + hprt0.b.prtpwr = 0;
  71046. + hprt0.b.prtena = 0;
  71047. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  71048. + hprt0.d32);
  71049. +#endif
  71050. + gusbcfg.d32 =
  71051. + DWC_READ_REG32(&core_if->core_global_regs->
  71052. + gusbcfg);
  71053. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  71054. + /* ULPI interface */
  71055. + /* Suspend the Phy Clock */
  71056. + pcgcctl.d32 = 0;
  71057. + pcgcctl.b.stoppclk = 1;
  71058. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71059. + pcgcctl.d32);
  71060. + dwc_udelay(10);
  71061. + gpwrdn.b.pmuactv = 1;
  71062. + DWC_MODIFY_REG32(&core_if->
  71063. + core_global_regs->
  71064. + gpwrdn, 0, gpwrdn.d32);
  71065. + } else {
  71066. + /* UTMI+ Interface */
  71067. + gpwrdn.b.pmuactv = 1;
  71068. + DWC_MODIFY_REG32(&core_if->
  71069. + core_global_regs->
  71070. + gpwrdn, 0, gpwrdn.d32);
  71071. + dwc_udelay(10);
  71072. + pcgcctl.b.stoppclk = 1;
  71073. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  71074. + dwc_udelay(10);
  71075. + }
  71076. +#ifdef DWC_DEV_SRPCAP
  71077. + gpwrdn.d32 = 0;
  71078. + gpwrdn.b.dis_vbus = 1;
  71079. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71080. + gpwrdn, 0, gpwrdn.d32);
  71081. +#endif
  71082. + gpwrdn.d32 = 0;
  71083. + gpwrdn.b.pmuintsel = 1;
  71084. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71085. + gpwrdn, 0, gpwrdn.d32);
  71086. + dwc_udelay(10);
  71087. +
  71088. + gpwrdn.d32 = 0;
  71089. +#ifdef DWC_DEV_SRPCAP
  71090. + gpwrdn.b.srp_det_msk = 1;
  71091. +#endif
  71092. + gpwrdn.b.disconn_det_msk = 1;
  71093. + gpwrdn.b.lnstchng_msk = 1;
  71094. + gpwrdn.b.sts_chngint_msk = 1;
  71095. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71096. + gpwrdn, 0, gpwrdn.d32);
  71097. + dwc_udelay(10);
  71098. +
  71099. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  71100. + gpwrdn.d32 = 0;
  71101. + gpwrdn.b.pwrdnclmp = 1;
  71102. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71103. + gpwrdn, 0, gpwrdn.d32);
  71104. + dwc_udelay(10);
  71105. +
  71106. + /* Switch off VDD */
  71107. + gpwrdn.d32 = 0;
  71108. + gpwrdn.b.pwrdnswtch = 1;
  71109. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71110. + gpwrdn, 0, gpwrdn.d32);
  71111. +
  71112. +#ifdef DWC_DEV_SRPCAP
  71113. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  71114. + {
  71115. + core_if->pwron_timer_started = 1;
  71116. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  71117. + }
  71118. +#endif
  71119. + /* Save gpwrdn register for further usage if stschng interrupt */
  71120. + core_if->gr_backup->gpwrdn_local =
  71121. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71122. +
  71123. + /* Set flag to indicate that we are in hibernation */
  71124. + core_if->hibernation_suspend = 1;
  71125. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  71126. +
  71127. + DWC_PRINTF("Host hibernation completed\n");
  71128. + // Exit from case statement
  71129. + break;
  71130. +
  71131. + }
  71132. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  71133. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  71134. + gotgctl_data_t gotgctl = {.d32 = 0 };
  71135. + gotgctl.b.hstsethnpen = 1;
  71136. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71137. + gotgctl, 0, gotgctl.d32);
  71138. + core_if->op_state = A_SUSPEND;
  71139. + }
  71140. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71141. + hprt0.b.prtsusp = 1;
  71142. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71143. + {
  71144. + dwc_irqflags_t flags;
  71145. + /* Update lx_state */
  71146. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  71147. + core_if->lx_state = DWC_OTG_L2;
  71148. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  71149. + }
  71150. + /* Suspend the Phy Clock */
  71151. + {
  71152. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71153. + pcgcctl.b.stoppclk = 1;
  71154. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71155. + pcgcctl.d32);
  71156. + dwc_udelay(10);
  71157. + }
  71158. +
  71159. + /* For HNP the bus must be suspended for at least 200ms. */
  71160. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  71161. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71162. + pcgcctl.b.stoppclk = 1;
  71163. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71164. + dwc_mdelay(200);
  71165. + }
  71166. +
  71167. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  71168. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  71169. + if (core_if->adp_enable) {
  71170. + gotgctl_data_t gotgctl = {.d32 = 0 };
  71171. + gpwrdn_data_t gpwrdn;
  71172. +
  71173. + while (gotgctl.b.asesvld == 1) {
  71174. + gotgctl.d32 =
  71175. + DWC_READ_REG32(&core_if->
  71176. + core_global_regs->
  71177. + gotgctl);
  71178. + dwc_mdelay(100);
  71179. + }
  71180. +
  71181. + /* Enable Power Down Logic */
  71182. + gpwrdn.d32 = 0;
  71183. + gpwrdn.b.pmuactv = 1;
  71184. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71185. + gpwrdn, 0, gpwrdn.d32);
  71186. +
  71187. + /* Unmask SRP detected interrupt from Power Down Logic */
  71188. + gpwrdn.d32 = 0;
  71189. + gpwrdn.b.srp_det_msk = 1;
  71190. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71191. + gpwrdn, 0, gpwrdn.d32);
  71192. +
  71193. + dwc_otg_adp_probe_start(core_if);
  71194. + }
  71195. +#endif
  71196. + break;
  71197. + case UHF_PORT_POWER:
  71198. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  71199. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  71200. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71201. + hprt0.b.prtpwr = 1;
  71202. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71203. + break;
  71204. + case UHF_PORT_RESET:
  71205. + if ((core_if->power_down == 2)
  71206. + && (core_if->hibernation_suspend == 1)) {
  71207. + /* If we are going to exit from Hibernated
  71208. + * state via USB RESET.
  71209. + */
  71210. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  71211. + } else {
  71212. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71213. +
  71214. + DWC_DEBUGPL(DBG_HCD,
  71215. + "DWC OTG HCD HUB CONTROL - "
  71216. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  71217. + {
  71218. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71219. + pcgcctl.b.enbl_sleep_gating = 1;
  71220. + pcgcctl.b.stoppclk = 1;
  71221. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71222. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  71223. + }
  71224. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71225. + {
  71226. + glpmcfg_data_t lpmcfg;
  71227. + lpmcfg.d32 =
  71228. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71229. + if (lpmcfg.b.prt_sleep_sts) {
  71230. + lpmcfg.b.en_utmi_sleep = 0;
  71231. + lpmcfg.b.hird_thres &= (~(1 << 4));
  71232. + DWC_WRITE_REG32
  71233. + (&core_if->core_global_regs->glpmcfg,
  71234. + lpmcfg.d32);
  71235. + dwc_mdelay(1);
  71236. + }
  71237. + }
  71238. +#endif
  71239. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71240. + /* Clear suspend bit if resetting from suspended state. */
  71241. + hprt0.b.prtsusp = 0;
  71242. + /* When B-Host the Port reset bit is set in
  71243. + * the Start HCD Callback function, so that
  71244. + * the reset is started within 1ms of the HNP
  71245. + * success interrupt. */
  71246. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  71247. + hprt0.b.prtpwr = 1;
  71248. + hprt0.b.prtrst = 1;
  71249. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  71250. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  71251. + hprt0.d32);
  71252. + }
  71253. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  71254. + dwc_mdelay(60);
  71255. + hprt0.b.prtrst = 0;
  71256. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71257. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  71258. + }
  71259. + break;
  71260. +#ifdef DWC_HS_ELECT_TST
  71261. + case UHF_PORT_TEST:
  71262. + {
  71263. + uint32_t t;
  71264. + gintmsk_data_t gintmsk;
  71265. +
  71266. + t = (wIndex >> 8); /* MSB wIndex USB */
  71267. + DWC_DEBUGPL(DBG_HCD,
  71268. + "DWC OTG HCD HUB CONTROL - "
  71269. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  71270. + t);
  71271. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  71272. + if (t < 6) {
  71273. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71274. + hprt0.b.prttstctl = t;
  71275. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  71276. + hprt0.d32);
  71277. + } else {
  71278. + /* Setup global vars with reg addresses (quick and
  71279. + * dirty hack, should be cleaned up)
  71280. + */
  71281. + global_regs = core_if->core_global_regs;
  71282. + hc_global_regs =
  71283. + core_if->host_if->host_global_regs;
  71284. + hc_regs =
  71285. + (dwc_otg_hc_regs_t *) ((char *)
  71286. + global_regs +
  71287. + 0x500);
  71288. + data_fifo =
  71289. + (uint32_t *) ((char *)global_regs +
  71290. + 0x1000);
  71291. +
  71292. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  71293. + /* Save current interrupt mask */
  71294. + gintmsk.d32 =
  71295. + DWC_READ_REG32
  71296. + (&global_regs->gintmsk);
  71297. +
  71298. + /* Disable all interrupts while we muck with
  71299. + * the hardware directly
  71300. + */
  71301. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  71302. +
  71303. + /* 15 second delay per the test spec */
  71304. + dwc_mdelay(15000);
  71305. +
  71306. + /* Drive suspend on the root port */
  71307. + hprt0.d32 =
  71308. + dwc_otg_read_hprt0(core_if);
  71309. + hprt0.b.prtsusp = 1;
  71310. + hprt0.b.prtres = 0;
  71311. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71312. +
  71313. + /* 15 second delay per the test spec */
  71314. + dwc_mdelay(15000);
  71315. +
  71316. + /* Drive resume on the root port */
  71317. + hprt0.d32 =
  71318. + dwc_otg_read_hprt0(core_if);
  71319. + hprt0.b.prtsusp = 0;
  71320. + hprt0.b.prtres = 1;
  71321. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71322. + dwc_mdelay(100);
  71323. +
  71324. + /* Clear the resume bit */
  71325. + hprt0.b.prtres = 0;
  71326. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71327. +
  71328. + /* Restore interrupts */
  71329. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  71330. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  71331. + /* Save current interrupt mask */
  71332. + gintmsk.d32 =
  71333. + DWC_READ_REG32
  71334. + (&global_regs->gintmsk);
  71335. +
  71336. + /* Disable all interrupts while we muck with
  71337. + * the hardware directly
  71338. + */
  71339. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  71340. +
  71341. + /* 15 second delay per the test spec */
  71342. + dwc_mdelay(15000);
  71343. +
  71344. + /* Send the Setup packet */
  71345. + do_setup();
  71346. +
  71347. + /* 15 second delay so nothing else happens for awhile */
  71348. + dwc_mdelay(15000);
  71349. +
  71350. + /* Restore interrupts */
  71351. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  71352. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  71353. + /* Save current interrupt mask */
  71354. + gintmsk.d32 =
  71355. + DWC_READ_REG32
  71356. + (&global_regs->gintmsk);
  71357. +
  71358. + /* Disable all interrupts while we muck with
  71359. + * the hardware directly
  71360. + */
  71361. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  71362. +
  71363. + /* Send the Setup packet */
  71364. + do_setup();
  71365. +
  71366. + /* 15 second delay so nothing else happens for awhile */
  71367. + dwc_mdelay(15000);
  71368. +
  71369. + /* Send the In and Ack packets */
  71370. + do_in_ack();
  71371. +
  71372. + /* 15 second delay so nothing else happens for awhile */
  71373. + dwc_mdelay(15000);
  71374. +
  71375. + /* Restore interrupts */
  71376. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  71377. + }
  71378. + }
  71379. + break;
  71380. + }
  71381. +#endif /* DWC_HS_ELECT_TST */
  71382. +
  71383. + case UHF_PORT_INDICATOR:
  71384. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  71385. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  71386. + /* Not supported */
  71387. + break;
  71388. + default:
  71389. + retval = -DWC_E_INVALID;
  71390. + DWC_ERROR("DWC OTG HCD - "
  71391. + "SetPortFeature request %xh "
  71392. + "unknown or unsupported\n", wValue);
  71393. + break;
  71394. + }
  71395. + break;
  71396. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71397. + case UCR_SET_AND_TEST_PORT_FEATURE:
  71398. + if (wValue != UHF_PORT_L1) {
  71399. + goto error;
  71400. + }
  71401. + {
  71402. + int portnum, hird, devaddr, remwake;
  71403. + glpmcfg_data_t lpmcfg;
  71404. + uint32_t time_usecs;
  71405. + gintsts_data_t gintsts;
  71406. + gintmsk_data_t gintmsk;
  71407. +
  71408. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  71409. + goto error;
  71410. + }
  71411. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  71412. + goto error;
  71413. + }
  71414. + /* Check if the port currently is in SLEEP state */
  71415. + lpmcfg.d32 =
  71416. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71417. + if (lpmcfg.b.prt_sleep_sts) {
  71418. + DWC_INFO("Port is already in sleep mode\n");
  71419. + buf[0] = 0; /* Return success */
  71420. + break;
  71421. + }
  71422. +
  71423. + portnum = wIndex & 0xf;
  71424. + hird = (wIndex >> 4) & 0xf;
  71425. + devaddr = (wIndex >> 8) & 0x7f;
  71426. + remwake = (wIndex >> 15);
  71427. +
  71428. + if (portnum != 1) {
  71429. + retval = -DWC_E_INVALID;
  71430. + DWC_WARN
  71431. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  71432. + portnum);
  71433. + break;
  71434. + }
  71435. +
  71436. + DWC_PRINTF
  71437. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  71438. + portnum, hird, devaddr, remwake);
  71439. + /* Disable LPM interrupt */
  71440. + gintmsk.d32 = 0;
  71441. + gintmsk.b.lpmtranrcvd = 1;
  71442. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  71443. + gintmsk.d32, 0);
  71444. +
  71445. + if (dwc_otg_hcd_send_lpm
  71446. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  71447. + retval = -DWC_E_INVALID;
  71448. + break;
  71449. + }
  71450. +
  71451. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  71452. + /* We will consider timeout if time_usecs microseconds pass,
  71453. + * and we don't receive LPM transaction status.
  71454. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  71455. + * core will set lpmtranrcvd bit.
  71456. + */
  71457. + do {
  71458. + gintsts.d32 =
  71459. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  71460. + if (gintsts.b.lpmtranrcvd) {
  71461. + break;
  71462. + }
  71463. + dwc_udelay(1);
  71464. + } while (--time_usecs);
  71465. + /* lpm_int bit will be cleared in LPM interrupt handler */
  71466. +
  71467. + /* Now fill status
  71468. + * 0x00 - Success
  71469. + * 0x10 - NYET
  71470. + * 0x11 - Timeout
  71471. + */
  71472. + if (!gintsts.b.lpmtranrcvd) {
  71473. + buf[0] = 0x3; /* Completion code is Timeout */
  71474. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  71475. + } else {
  71476. + lpmcfg.d32 =
  71477. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71478. + if (lpmcfg.b.lpm_resp == 0x3) {
  71479. + /* ACK responce from the device */
  71480. + buf[0] = 0x00; /* Success */
  71481. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  71482. + /* NYET responce from the device */
  71483. + buf[0] = 0x2;
  71484. + } else {
  71485. + /* Otherwise responce with Timeout */
  71486. + buf[0] = 0x3;
  71487. + }
  71488. + }
  71489. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  71490. + lpmcfg.b.lpm_resp);
  71491. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  71492. + gintmsk.d32);
  71493. +
  71494. + break;
  71495. + }
  71496. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  71497. + default:
  71498. +error:
  71499. + retval = -DWC_E_INVALID;
  71500. + DWC_WARN("DWC OTG HCD - "
  71501. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  71502. + typeReq, wIndex, wValue);
  71503. + break;
  71504. + }
  71505. +
  71506. + return retval;
  71507. +}
  71508. +
  71509. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71510. +/** Returns index of host channel to perform LPM transaction. */
  71511. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  71512. +{
  71513. + dwc_otg_core_if_t *core_if = hcd->core_if;
  71514. + dwc_hc_t *hc;
  71515. + hcchar_data_t hcchar;
  71516. + gintmsk_data_t gintmsk = {.d32 = 0 };
  71517. +
  71518. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  71519. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  71520. + return -1;
  71521. + }
  71522. +
  71523. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  71524. +
  71525. + /* Mask host channel interrupts. */
  71526. + gintmsk.b.hcintr = 1;
  71527. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  71528. +
  71529. + /* Fill fields that core needs for LPM transaction */
  71530. + hcchar.b.devaddr = devaddr;
  71531. + hcchar.b.epnum = 0;
  71532. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  71533. + hcchar.b.mps = 64;
  71534. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  71535. + hcchar.b.epdir = 0; /* OUT */
  71536. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  71537. + hcchar.d32);
  71538. +
  71539. + /* Remove the host channel from the free list. */
  71540. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  71541. +
  71542. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  71543. +
  71544. + return hc->hc_num;
  71545. +}
  71546. +
  71547. +/** Release hc after performing LPM transaction */
  71548. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  71549. +{
  71550. + dwc_hc_t *hc;
  71551. + glpmcfg_data_t lpmcfg;
  71552. + uint8_t hc_num;
  71553. +
  71554. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  71555. + hc_num = lpmcfg.b.lpm_chan_index;
  71556. +
  71557. + hc = hcd->hc_ptr_array[hc_num];
  71558. +
  71559. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  71560. + /* Return host channel to free list */
  71561. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  71562. +}
  71563. +
  71564. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  71565. + uint8_t bRemoteWake)
  71566. +{
  71567. + glpmcfg_data_t lpmcfg;
  71568. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71569. + int channel;
  71570. +
  71571. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  71572. + if (channel < 0) {
  71573. + return channel;
  71574. + }
  71575. +
  71576. + pcgcctl.b.enbl_sleep_gating = 1;
  71577. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  71578. +
  71579. + /* Read LPM config register */
  71580. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  71581. +
  71582. + /* Program LPM transaction fields */
  71583. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  71584. + lpmcfg.b.hird = hird;
  71585. + lpmcfg.b.hird_thres = 0x1c;
  71586. + lpmcfg.b.lpm_chan_index = channel;
  71587. + lpmcfg.b.en_utmi_sleep = 1;
  71588. + /* Program LPM config register */
  71589. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  71590. +
  71591. + /* Send LPM transaction */
  71592. + lpmcfg.b.send_lpm = 1;
  71593. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  71594. +
  71595. + return 0;
  71596. +}
  71597. +
  71598. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  71599. +
  71600. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  71601. +{
  71602. + int retval;
  71603. +
  71604. + if (port != 1) {
  71605. + return -DWC_E_INVALID;
  71606. + }
  71607. +
  71608. + retval = (hcd->flags.b.port_connect_status_change ||
  71609. + hcd->flags.b.port_reset_change ||
  71610. + hcd->flags.b.port_enable_change ||
  71611. + hcd->flags.b.port_suspend_change ||
  71612. + hcd->flags.b.port_over_current_change);
  71613. +#ifdef DEBUG
  71614. + if (retval) {
  71615. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  71616. + " Root port status changed\n");
  71617. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  71618. + hcd->flags.b.port_connect_status_change);
  71619. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  71620. + hcd->flags.b.port_reset_change);
  71621. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  71622. + hcd->flags.b.port_enable_change);
  71623. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  71624. + hcd->flags.b.port_suspend_change);
  71625. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  71626. + hcd->flags.b.port_over_current_change);
  71627. + }
  71628. +#endif
  71629. + return retval;
  71630. +}
  71631. +
  71632. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  71633. +{
  71634. + hfnum_data_t hfnum;
  71635. + hfnum.d32 =
  71636. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  71637. + hfnum);
  71638. +
  71639. +#ifdef DEBUG_SOF
  71640. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  71641. + hfnum.b.frnum);
  71642. +#endif
  71643. + return hfnum.b.frnum;
  71644. +}
  71645. +
  71646. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  71647. + struct dwc_otg_hcd_function_ops *fops)
  71648. +{
  71649. + int retval = 0;
  71650. +
  71651. + hcd->fops = fops;
  71652. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  71653. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  71654. + dwc_otg_hcd_reinit(hcd);
  71655. + } else {
  71656. + retval = -DWC_E_NO_DEVICE;
  71657. + }
  71658. +
  71659. + return retval;
  71660. +}
  71661. +
  71662. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  71663. +{
  71664. + return hcd->priv;
  71665. +}
  71666. +
  71667. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  71668. +{
  71669. + hcd->priv = priv_data;
  71670. +}
  71671. +
  71672. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  71673. +{
  71674. + return hcd->otg_port;
  71675. +}
  71676. +
  71677. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  71678. +{
  71679. + uint32_t is_b_host;
  71680. + if (hcd->core_if->op_state == B_HOST) {
  71681. + is_b_host = 1;
  71682. + } else {
  71683. + is_b_host = 0;
  71684. + }
  71685. +
  71686. + return is_b_host;
  71687. +}
  71688. +
  71689. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  71690. + int iso_desc_count, int atomic_alloc)
  71691. +{
  71692. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  71693. + uint32_t size;
  71694. +
  71695. + size =
  71696. + sizeof(*dwc_otg_urb) +
  71697. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  71698. + if (atomic_alloc)
  71699. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  71700. + else
  71701. + dwc_otg_urb = DWC_ALLOC(size);
  71702. +
  71703. + if (dwc_otg_urb)
  71704. + dwc_otg_urb->packet_count = iso_desc_count;
  71705. + else {
  71706. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  71707. + "%salloc of %db failed\n",
  71708. + atomic_alloc?"atomic ":"", size);
  71709. + }
  71710. + return dwc_otg_urb;
  71711. +}
  71712. +
  71713. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71714. + uint8_t dev_addr, uint8_t ep_num,
  71715. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  71716. +{
  71717. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  71718. + ep_type, ep_dir, mps);
  71719. +#if 0
  71720. + DWC_PRINTF
  71721. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  71722. + dev_addr, ep_num, ep_dir, ep_type, mps);
  71723. +#endif
  71724. +}
  71725. +
  71726. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71727. + void *urb_handle, void *buf, dwc_dma_t dma,
  71728. + uint32_t buflen, void *setup_packet,
  71729. + dwc_dma_t setup_dma, uint32_t flags,
  71730. + uint16_t interval)
  71731. +{
  71732. + dwc_otg_urb->priv = urb_handle;
  71733. + dwc_otg_urb->buf = buf;
  71734. + dwc_otg_urb->dma = dma;
  71735. + dwc_otg_urb->length = buflen;
  71736. + dwc_otg_urb->setup_packet = setup_packet;
  71737. + dwc_otg_urb->setup_dma = setup_dma;
  71738. + dwc_otg_urb->flags = flags;
  71739. + dwc_otg_urb->interval = interval;
  71740. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  71741. +}
  71742. +
  71743. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71744. +{
  71745. + return dwc_otg_urb->status;
  71746. +}
  71747. +
  71748. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71749. +{
  71750. + return dwc_otg_urb->actual_length;
  71751. +}
  71752. +
  71753. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71754. +{
  71755. + return dwc_otg_urb->error_count;
  71756. +}
  71757. +
  71758. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71759. + int desc_num, uint32_t offset,
  71760. + uint32_t length)
  71761. +{
  71762. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  71763. + dwc_otg_urb->iso_descs[desc_num].length = length;
  71764. +}
  71765. +
  71766. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71767. + int desc_num)
  71768. +{
  71769. + return dwc_otg_urb->iso_descs[desc_num].status;
  71770. +}
  71771. +
  71772. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  71773. + dwc_otg_urb, int desc_num)
  71774. +{
  71775. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  71776. +}
  71777. +
  71778. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  71779. +{
  71780. + int allocated = 0;
  71781. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71782. +
  71783. + if (qh) {
  71784. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  71785. + allocated = 1;
  71786. + }
  71787. + }
  71788. + return allocated;
  71789. +}
  71790. +
  71791. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  71792. +{
  71793. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71794. + int freed = 0;
  71795. + DWC_ASSERT(qh, "qh is not allocated\n");
  71796. +
  71797. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  71798. + freed = 1;
  71799. + }
  71800. +
  71801. + return freed;
  71802. +}
  71803. +
  71804. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  71805. +{
  71806. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71807. + DWC_ASSERT(qh, "qh is not allocated\n");
  71808. + return qh->usecs;
  71809. +}
  71810. +
  71811. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  71812. +{
  71813. +#ifdef DEBUG
  71814. + int num_channels;
  71815. + int i;
  71816. + gnptxsts_data_t np_tx_status;
  71817. + hptxsts_data_t p_tx_status;
  71818. +
  71819. + num_channels = hcd->core_if->core_params->host_channels;
  71820. + DWC_PRINTF("\n");
  71821. + DWC_PRINTF
  71822. + ("************************************************************\n");
  71823. + DWC_PRINTF("HCD State:\n");
  71824. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  71825. + for (i = 0; i < num_channels; i++) {
  71826. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  71827. + DWC_PRINTF(" Channel %d:\n", i);
  71828. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  71829. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  71830. + DWC_PRINTF(" speed: %d\n", hc->speed);
  71831. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  71832. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  71833. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  71834. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  71835. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  71836. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  71837. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  71838. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  71839. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  71840. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  71841. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  71842. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  71843. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  71844. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  71845. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  71846. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  71847. + DWC_PRINTF(" requests: %d\n", hc->requests);
  71848. + DWC_PRINTF(" qh: %p\n", hc->qh);
  71849. + if (hc->xfer_started) {
  71850. + hfnum_data_t hfnum;
  71851. + hcchar_data_t hcchar;
  71852. + hctsiz_data_t hctsiz;
  71853. + hcint_data_t hcint;
  71854. + hcintmsk_data_t hcintmsk;
  71855. + hfnum.d32 =
  71856. + DWC_READ_REG32(&hcd->core_if->
  71857. + host_if->host_global_regs->hfnum);
  71858. + hcchar.d32 =
  71859. + DWC_READ_REG32(&hcd->core_if->host_if->
  71860. + hc_regs[i]->hcchar);
  71861. + hctsiz.d32 =
  71862. + DWC_READ_REG32(&hcd->core_if->host_if->
  71863. + hc_regs[i]->hctsiz);
  71864. + hcint.d32 =
  71865. + DWC_READ_REG32(&hcd->core_if->host_if->
  71866. + hc_regs[i]->hcint);
  71867. + hcintmsk.d32 =
  71868. + DWC_READ_REG32(&hcd->core_if->host_if->
  71869. + hc_regs[i]->hcintmsk);
  71870. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  71871. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  71872. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  71873. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  71874. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  71875. + }
  71876. + if (hc->xfer_started && hc->qh) {
  71877. + dwc_otg_qtd_t *qtd;
  71878. + dwc_otg_hcd_urb_t *urb;
  71879. +
  71880. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  71881. + if (!qtd->in_process)
  71882. + break;
  71883. +
  71884. + urb = qtd->urb;
  71885. + DWC_PRINTF(" URB Info:\n");
  71886. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  71887. + if (urb) {
  71888. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  71889. + dwc_otg_hcd_get_dev_addr(&urb->
  71890. + pipe_info),
  71891. + dwc_otg_hcd_get_ep_num(&urb->
  71892. + pipe_info),
  71893. + dwc_otg_hcd_is_pipe_in(&urb->
  71894. + pipe_info) ?
  71895. + "IN" : "OUT");
  71896. + DWC_PRINTF(" Max packet size: %d\n",
  71897. + dwc_otg_hcd_get_mps(&urb->
  71898. + pipe_info));
  71899. + DWC_PRINTF(" transfer_buffer: %p\n",
  71900. + urb->buf);
  71901. + DWC_PRINTF(" transfer_dma: %p\n",
  71902. + (void *)urb->dma);
  71903. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  71904. + urb->length);
  71905. + DWC_PRINTF(" actual_length: %d\n",
  71906. + urb->actual_length);
  71907. + }
  71908. + }
  71909. + }
  71910. + }
  71911. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  71912. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  71913. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  71914. + np_tx_status.d32 =
  71915. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  71916. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  71917. + np_tx_status.b.nptxqspcavail);
  71918. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  71919. + np_tx_status.b.nptxfspcavail);
  71920. + p_tx_status.d32 =
  71921. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  71922. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  71923. + p_tx_status.b.ptxqspcavail);
  71924. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  71925. + dwc_otg_hcd_dump_frrem(hcd);
  71926. + dwc_otg_dump_global_registers(hcd->core_if);
  71927. + dwc_otg_dump_host_registers(hcd->core_if);
  71928. + DWC_PRINTF
  71929. + ("************************************************************\n");
  71930. + DWC_PRINTF("\n");
  71931. +#endif
  71932. +}
  71933. +
  71934. +#ifdef DEBUG
  71935. +void dwc_print_setup_data(uint8_t * setup)
  71936. +{
  71937. + int i;
  71938. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  71939. + DWC_PRINTF("Setup Data = MSB ");
  71940. + for (i = 7; i >= 0; i--)
  71941. + DWC_PRINTF("%02x ", setup[i]);
  71942. + DWC_PRINTF("\n");
  71943. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  71944. + (setup[0] & 0x80) ? "Device-to-Host" :
  71945. + "Host-to-Device");
  71946. + DWC_PRINTF(" bmRequestType Type = ");
  71947. + switch ((setup[0] & 0x60) >> 5) {
  71948. + case 0:
  71949. + DWC_PRINTF("Standard\n");
  71950. + break;
  71951. + case 1:
  71952. + DWC_PRINTF("Class\n");
  71953. + break;
  71954. + case 2:
  71955. + DWC_PRINTF("Vendor\n");
  71956. + break;
  71957. + case 3:
  71958. + DWC_PRINTF("Reserved\n");
  71959. + break;
  71960. + }
  71961. + DWC_PRINTF(" bmRequestType Recipient = ");
  71962. + switch (setup[0] & 0x1f) {
  71963. + case 0:
  71964. + DWC_PRINTF("Device\n");
  71965. + break;
  71966. + case 1:
  71967. + DWC_PRINTF("Interface\n");
  71968. + break;
  71969. + case 2:
  71970. + DWC_PRINTF("Endpoint\n");
  71971. + break;
  71972. + case 3:
  71973. + DWC_PRINTF("Other\n");
  71974. + break;
  71975. + default:
  71976. + DWC_PRINTF("Reserved\n");
  71977. + break;
  71978. + }
  71979. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  71980. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  71981. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  71982. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  71983. + }
  71984. +}
  71985. +#endif
  71986. +
  71987. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  71988. +{
  71989. +#if 0
  71990. + DWC_PRINTF("Frame remaining at SOF:\n");
  71991. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71992. + hcd->frrem_samples, hcd->frrem_accum,
  71993. + (hcd->frrem_samples > 0) ?
  71994. + hcd->frrem_accum / hcd->frrem_samples : 0);
  71995. +
  71996. + DWC_PRINTF("\n");
  71997. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  71998. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71999. + hcd->core_if->hfnum_7_samples,
  72000. + hcd->core_if->hfnum_7_frrem_accum,
  72001. + (hcd->core_if->hfnum_7_samples >
  72002. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  72003. + hcd->core_if->hfnum_7_samples : 0);
  72004. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  72005. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72006. + hcd->core_if->hfnum_0_samples,
  72007. + hcd->core_if->hfnum_0_frrem_accum,
  72008. + (hcd->core_if->hfnum_0_samples >
  72009. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  72010. + hcd->core_if->hfnum_0_samples : 0);
  72011. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  72012. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72013. + hcd->core_if->hfnum_other_samples,
  72014. + hcd->core_if->hfnum_other_frrem_accum,
  72015. + (hcd->core_if->hfnum_other_samples >
  72016. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  72017. + hcd->core_if->hfnum_other_samples : 0);
  72018. +
  72019. + DWC_PRINTF("\n");
  72020. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  72021. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72022. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  72023. + (hcd->hfnum_7_samples_a > 0) ?
  72024. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  72025. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  72026. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72027. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  72028. + (hcd->hfnum_0_samples_a > 0) ?
  72029. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  72030. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  72031. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72032. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  72033. + (hcd->hfnum_other_samples_a > 0) ?
  72034. + hcd->hfnum_other_frrem_accum_a /
  72035. + hcd->hfnum_other_samples_a : 0);
  72036. +
  72037. + DWC_PRINTF("\n");
  72038. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  72039. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72040. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  72041. + (hcd->hfnum_7_samples_b > 0) ?
  72042. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  72043. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  72044. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72045. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  72046. + (hcd->hfnum_0_samples_b > 0) ?
  72047. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  72048. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  72049. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  72050. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  72051. + (hcd->hfnum_other_samples_b > 0) ?
  72052. + hcd->hfnum_other_frrem_accum_b /
  72053. + hcd->hfnum_other_samples_b : 0);
  72054. +#endif
  72055. +}
  72056. +
  72057. +#endif /* DWC_DEVICE_ONLY */
  72058. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  72059. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 1970-01-01 01:00:00.000000000 +0100
  72060. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 2014-04-24 15:35:04.173565776 +0200
  72061. @@ -0,0 +1,1132 @@
  72062. +/*==========================================================================
  72063. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  72064. + * $Revision: #10 $
  72065. + * $Date: 2011/10/20 $
  72066. + * $Change: 1869464 $
  72067. + *
  72068. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72069. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72070. + * otherwise expressly agreed to in writing between Synopsys and you.
  72071. + *
  72072. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72073. + * any End User Software License Agreement or Agreement for Licensed Product
  72074. + * with Synopsys or any supplement thereto. You are permitted to use and
  72075. + * redistribute this Software in source and binary forms, with or without
  72076. + * modification, provided that redistributions of source code must retain this
  72077. + * notice. You may not view, use, disclose, copy or distribute this file or
  72078. + * any information contained herein except pursuant to this license grant from
  72079. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72080. + * below, then you are not authorized to use the Software.
  72081. + *
  72082. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72083. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72084. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72085. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72086. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72087. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72088. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72089. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72090. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72091. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72092. + * DAMAGE.
  72093. + * ========================================================================== */
  72094. +#ifndef DWC_DEVICE_ONLY
  72095. +
  72096. +/** @file
  72097. + * This file contains Descriptor DMA support implementation for host mode.
  72098. + */
  72099. +
  72100. +#include "dwc_otg_hcd.h"
  72101. +#include "dwc_otg_regs.h"
  72102. +
  72103. +extern bool microframe_schedule;
  72104. +
  72105. +static inline uint8_t frame_list_idx(uint16_t frame)
  72106. +{
  72107. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  72108. +}
  72109. +
  72110. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  72111. +{
  72112. + return (idx + inc) &
  72113. + (((speed ==
  72114. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  72115. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  72116. +}
  72117. +
  72118. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  72119. +{
  72120. + return (idx - inc) &
  72121. + (((speed ==
  72122. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  72123. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  72124. +}
  72125. +
  72126. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  72127. +{
  72128. + return (((qh->ep_type == UE_ISOCHRONOUS)
  72129. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  72130. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  72131. +}
  72132. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  72133. +{
  72134. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  72135. + ? ((qh->interval + 8 - 1) / 8)
  72136. + : qh->interval);
  72137. +}
  72138. +
  72139. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  72140. +{
  72141. + int retval = 0;
  72142. +
  72143. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  72144. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  72145. + &qh->desc_list_dma);
  72146. +
  72147. + if (!qh->desc_list) {
  72148. + retval = -DWC_E_NO_MEMORY;
  72149. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  72150. +
  72151. + }
  72152. +
  72153. + dwc_memset(qh->desc_list, 0x00,
  72154. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  72155. +
  72156. + qh->n_bytes =
  72157. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  72158. +
  72159. + if (!qh->n_bytes) {
  72160. + retval = -DWC_E_NO_MEMORY;
  72161. + DWC_ERROR
  72162. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  72163. + __func__);
  72164. +
  72165. + }
  72166. + return retval;
  72167. +
  72168. +}
  72169. +
  72170. +static void desc_list_free(dwc_otg_qh_t * qh)
  72171. +{
  72172. + if (qh->desc_list) {
  72173. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  72174. + qh->desc_list_dma);
  72175. + qh->desc_list = NULL;
  72176. + }
  72177. +
  72178. + if (qh->n_bytes) {
  72179. + DWC_FREE(qh->n_bytes);
  72180. + qh->n_bytes = NULL;
  72181. + }
  72182. +}
  72183. +
  72184. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  72185. +{
  72186. + int retval = 0;
  72187. + if (hcd->frame_list)
  72188. + return 0;
  72189. +
  72190. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  72191. + &hcd->frame_list_dma);
  72192. + if (!hcd->frame_list) {
  72193. + retval = -DWC_E_NO_MEMORY;
  72194. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  72195. + }
  72196. +
  72197. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  72198. +
  72199. + return retval;
  72200. +}
  72201. +
  72202. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  72203. +{
  72204. + if (!hcd->frame_list)
  72205. + return;
  72206. +
  72207. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  72208. + hcd->frame_list = NULL;
  72209. +}
  72210. +
  72211. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  72212. +{
  72213. +
  72214. + hcfg_data_t hcfg;
  72215. +
  72216. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  72217. +
  72218. + if (hcfg.b.perschedena) {
  72219. + /* already enabled */
  72220. + return;
  72221. + }
  72222. +
  72223. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  72224. + hcd->frame_list_dma);
  72225. +
  72226. + switch (fr_list_en) {
  72227. + case 64:
  72228. + hcfg.b.frlisten = 3;
  72229. + break;
  72230. + case 32:
  72231. + hcfg.b.frlisten = 2;
  72232. + break;
  72233. + case 16:
  72234. + hcfg.b.frlisten = 1;
  72235. + break;
  72236. + case 8:
  72237. + hcfg.b.frlisten = 0;
  72238. + break;
  72239. + default:
  72240. + break;
  72241. + }
  72242. +
  72243. + hcfg.b.perschedena = 1;
  72244. +
  72245. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  72246. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  72247. +
  72248. +}
  72249. +
  72250. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  72251. +{
  72252. + hcfg_data_t hcfg;
  72253. +
  72254. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  72255. +
  72256. + if (!hcfg.b.perschedena) {
  72257. + /* already disabled */
  72258. + return;
  72259. + }
  72260. + hcfg.b.perschedena = 0;
  72261. +
  72262. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  72263. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  72264. +}
  72265. +
  72266. +/*
  72267. + * Activates/Deactivates FrameList entries for the channel
  72268. + * based on endpoint servicing period.
  72269. + */
  72270. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  72271. +{
  72272. + uint16_t i, j, inc;
  72273. + dwc_hc_t *hc = NULL;
  72274. +
  72275. + if (!qh->channel) {
  72276. + DWC_ERROR("qh->channel = %p", qh->channel);
  72277. + return;
  72278. + }
  72279. +
  72280. + if (!hcd) {
  72281. + DWC_ERROR("------hcd = %p", hcd);
  72282. + return;
  72283. + }
  72284. +
  72285. + if (!hcd->frame_list) {
  72286. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  72287. + return;
  72288. + }
  72289. +
  72290. + hc = qh->channel;
  72291. + inc = frame_incr_val(qh);
  72292. + if (qh->ep_type == UE_ISOCHRONOUS)
  72293. + i = frame_list_idx(qh->sched_frame);
  72294. + else
  72295. + i = 0;
  72296. +
  72297. + j = i;
  72298. + do {
  72299. + if (enable)
  72300. + hcd->frame_list[j] |= (1 << hc->hc_num);
  72301. + else
  72302. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  72303. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  72304. + }
  72305. + while (j != i);
  72306. + if (!enable)
  72307. + return;
  72308. + hc->schinfo = 0;
  72309. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  72310. + j = 1;
  72311. + /* TODO - check this */
  72312. + inc = (8 + qh->interval - 1) / qh->interval;
  72313. + for (i = 0; i < inc; i++) {
  72314. + hc->schinfo |= j;
  72315. + j = j << qh->interval;
  72316. + }
  72317. + } else {
  72318. + hc->schinfo = 0xff;
  72319. + }
  72320. +}
  72321. +
  72322. +#if 1
  72323. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  72324. +{
  72325. + int i = 0;
  72326. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  72327. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  72328. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  72329. + if (!(i % 8) && i)
  72330. + DWC_PRINTF("\n");
  72331. + }
  72332. + DWC_PRINTF("\n----\n");
  72333. +
  72334. +}
  72335. +#endif
  72336. +
  72337. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72338. +{
  72339. + dwc_irqflags_t flags;
  72340. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  72341. +
  72342. + dwc_hc_t *hc = qh->channel;
  72343. + if (dwc_qh_is_non_per(qh)) {
  72344. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  72345. + if (!microframe_schedule)
  72346. + hcd->non_periodic_channels--;
  72347. + else
  72348. + hcd->available_host_channels++;
  72349. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  72350. + } else
  72351. + update_frame_list(hcd, qh, 0);
  72352. +
  72353. + /*
  72354. + * The condition is added to prevent double cleanup try in case of device
  72355. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  72356. + */
  72357. + if (hc->qh) {
  72358. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  72359. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  72360. + hc->qh = NULL;
  72361. + }
  72362. +
  72363. + qh->channel = NULL;
  72364. + qh->ntd = 0;
  72365. +
  72366. + if (qh->desc_list) {
  72367. + dwc_memset(qh->desc_list, 0x00,
  72368. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  72369. + }
  72370. +}
  72371. +
  72372. +/**
  72373. + * Initializes a QH structure's Descriptor DMA related members.
  72374. + * Allocates memory for descriptor list.
  72375. + * On first periodic QH, allocates memory for FrameList
  72376. + * and enables periodic scheduling.
  72377. + *
  72378. + * @param hcd The HCD state structure for the DWC OTG controller.
  72379. + * @param qh The QH to init.
  72380. + *
  72381. + * @return 0 if successful, negative error code otherwise.
  72382. + */
  72383. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72384. +{
  72385. + int retval = 0;
  72386. +
  72387. + if (qh->do_split) {
  72388. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  72389. + return -1;
  72390. + }
  72391. +
  72392. + retval = desc_list_alloc(qh);
  72393. +
  72394. + if ((retval == 0)
  72395. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  72396. + if (!hcd->frame_list) {
  72397. + retval = frame_list_alloc(hcd);
  72398. + /* Enable periodic schedule on first periodic QH */
  72399. + if (retval == 0)
  72400. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  72401. + }
  72402. + }
  72403. +
  72404. + qh->ntd = 0;
  72405. +
  72406. + return retval;
  72407. +}
  72408. +
  72409. +/**
  72410. + * Frees descriptor list memory associated with the QH.
  72411. + * If QH is periodic and the last, frees FrameList memory
  72412. + * and disables periodic scheduling.
  72413. + *
  72414. + * @param hcd The HCD state structure for the DWC OTG controller.
  72415. + * @param qh The QH to init.
  72416. + */
  72417. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72418. +{
  72419. + desc_list_free(qh);
  72420. +
  72421. + /*
  72422. + * Channel still assigned due to some reasons.
  72423. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  72424. + * ChHalted interrupt to release the channel. Afterwards
  72425. + * when it comes here from endpoint disable routine
  72426. + * channel remains assigned.
  72427. + */
  72428. + if (qh->channel)
  72429. + release_channel_ddma(hcd, qh);
  72430. +
  72431. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  72432. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  72433. +
  72434. + per_sched_disable(hcd);
  72435. + frame_list_free(hcd);
  72436. + }
  72437. +}
  72438. +
  72439. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  72440. +{
  72441. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  72442. + /*
  72443. + * Descriptor set(8 descriptors) index
  72444. + * which is 8-aligned.
  72445. + */
  72446. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  72447. + } else {
  72448. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  72449. + }
  72450. +}
  72451. +
  72452. +/*
  72453. + * Determine starting frame for Isochronous transfer.
  72454. + * Few frames skipped to prevent race condition with HC.
  72455. + */
  72456. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  72457. + uint8_t * skip_frames)
  72458. +{
  72459. + uint16_t frame = 0;
  72460. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  72461. +
  72462. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  72463. +
  72464. + /*
  72465. + * skip_frames is used to limit activated descriptors number
  72466. + * to avoid the situation when HC services the last activated
  72467. + * descriptor firstly.
  72468. + * Example for FS:
  72469. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  72470. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  72471. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  72472. + * list will be fully programmed with Active descriptors and it is possible
  72473. + * case(rare) that the latest descriptor(considering rollback) corresponding
  72474. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  72475. + * up to 11 uframes(16 in the code) may be skipped.
  72476. + */
  72477. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  72478. + /*
  72479. + * Consider uframe counter also, to start xfer asap.
  72480. + * If half of the frame elapsed skip 2 frames otherwise
  72481. + * just 1 frame.
  72482. + * Starting descriptor index must be 8-aligned, so
  72483. + * if the current frame is near to complete the next one
  72484. + * is skipped as well.
  72485. + */
  72486. +
  72487. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  72488. + *skip_frames = 2 * 8;
  72489. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  72490. + } else {
  72491. + *skip_frames = 1 * 8;
  72492. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  72493. + }
  72494. +
  72495. + frame = dwc_full_frame_num(frame);
  72496. + } else {
  72497. + /*
  72498. + * Two frames are skipped for FS - the current and the next.
  72499. + * But for descriptor programming, 1 frame(descriptor) is enough,
  72500. + * see example above.
  72501. + */
  72502. + *skip_frames = 1;
  72503. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  72504. + }
  72505. +
  72506. + return frame;
  72507. +}
  72508. +
  72509. +/*
  72510. + * Calculate initial descriptor index for isochronous transfer
  72511. + * based on scheduled frame.
  72512. + */
  72513. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72514. +{
  72515. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  72516. + uint8_t skip_frames = 0;
  72517. + /*
  72518. + * With current ISOC processing algorithm the channel is being
  72519. + * released when no more QTDs in the list(qh->ntd == 0).
  72520. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  72521. + *
  72522. + * So qh->channel != NULL branch is not used and just not removed from the
  72523. + * source file. It is required for another possible approach which is,
  72524. + * do not disable and release the channel when ISOC session completed,
  72525. + * just move QH to inactive schedule until new QTD arrives.
  72526. + * On new QTD, the QH moved back to 'ready' schedule,
  72527. + * starting frame and therefore starting desc_index are recalculated.
  72528. + * In this case channel is released only on ep_disable.
  72529. + */
  72530. +
  72531. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  72532. + if (qh->channel) {
  72533. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  72534. + /*
  72535. + * Calculate initial descriptor index based on FrameList current bitmap
  72536. + * and servicing period.
  72537. + */
  72538. + fr_idx_tmp = frame_list_idx(frame);
  72539. + fr_idx =
  72540. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  72541. + fr_idx_tmp)
  72542. + % frame_incr_val(qh);
  72543. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  72544. + } else {
  72545. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  72546. + fr_idx = frame_list_idx(qh->sched_frame);
  72547. + }
  72548. +
  72549. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  72550. +
  72551. + return skip_frames;
  72552. +}
  72553. +
  72554. +#define ISOC_URB_GIVEBACK_ASAP
  72555. +
  72556. +#define MAX_ISOC_XFER_SIZE_FS 1023
  72557. +#define MAX_ISOC_XFER_SIZE_HS 3072
  72558. +#define DESCNUM_THRESHOLD 4
  72559. +
  72560. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  72561. + uint8_t skip_frames)
  72562. +{
  72563. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  72564. + dwc_otg_qtd_t *qtd;
  72565. + dwc_otg_host_dma_desc_t *dma_desc;
  72566. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  72567. +
  72568. + idx = qh->td_last;
  72569. + inc = qh->interval;
  72570. + n_desc = 0;
  72571. +
  72572. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  72573. + if (skip_frames && !qh->channel)
  72574. + ntd_max = ntd_max - skip_frames / qh->interval;
  72575. +
  72576. + max_xfer_size =
  72577. + (qh->dev_speed ==
  72578. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  72579. + MAX_ISOC_XFER_SIZE_FS;
  72580. +
  72581. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  72582. + while ((qh->ntd < ntd_max)
  72583. + && (qtd->isoc_frame_index_last <
  72584. + qtd->urb->packet_count)) {
  72585. +
  72586. + dma_desc = &qh->desc_list[idx];
  72587. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  72588. +
  72589. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  72590. +
  72591. + if (frame_desc->length > max_xfer_size)
  72592. + qh->n_bytes[idx] = max_xfer_size;
  72593. + else
  72594. + qh->n_bytes[idx] = frame_desc->length;
  72595. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  72596. + dma_desc->status.b_isoc.a = 1;
  72597. + dma_desc->status.b_isoc.sts = 0;
  72598. +
  72599. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  72600. +
  72601. + qh->ntd++;
  72602. +
  72603. + qtd->isoc_frame_index_last++;
  72604. +
  72605. +#ifdef ISOC_URB_GIVEBACK_ASAP
  72606. + /*
  72607. + * Set IOC for each descriptor corresponding to the
  72608. + * last frame of the URB.
  72609. + */
  72610. + if (qtd->isoc_frame_index_last ==
  72611. + qtd->urb->packet_count)
  72612. + dma_desc->status.b_isoc.ioc = 1;
  72613. +
  72614. +#endif
  72615. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  72616. + n_desc++;
  72617. +
  72618. + }
  72619. + qtd->in_process = 1;
  72620. + }
  72621. +
  72622. + qh->td_last = idx;
  72623. +
  72624. +#ifdef ISOC_URB_GIVEBACK_ASAP
  72625. + /* Set IOC for the last descriptor if descriptor list is full */
  72626. + if (qh->ntd == ntd_max) {
  72627. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  72628. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  72629. + }
  72630. +#else
  72631. + /*
  72632. + * Set IOC bit only for one descriptor.
  72633. + * Always try to be ahead of HW processing,
  72634. + * i.e. on IOC generation driver activates next descriptors but
  72635. + * core continues to process descriptors followed the one with IOC set.
  72636. + */
  72637. +
  72638. + if (n_desc > DESCNUM_THRESHOLD) {
  72639. + /*
  72640. + * Move IOC "up". Required even if there is only one QTD
  72641. + * in the list, cause QTDs migth continue to be queued,
  72642. + * but during the activation it was only one queued.
  72643. + * Actually more than one QTD might be in the list if this function called
  72644. + * from XferCompletion - QTDs was queued during HW processing of the previous
  72645. + * descriptor chunk.
  72646. + */
  72647. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  72648. + } else {
  72649. + /*
  72650. + * Set the IOC for the latest descriptor
  72651. + * if either number of descriptor is not greather than threshold
  72652. + * or no more new descriptors activated.
  72653. + */
  72654. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  72655. + }
  72656. +
  72657. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  72658. +#endif
  72659. +}
  72660. +
  72661. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72662. +{
  72663. +
  72664. + dwc_hc_t *hc;
  72665. + dwc_otg_host_dma_desc_t *dma_desc;
  72666. + dwc_otg_qtd_t *qtd;
  72667. + int num_packets, len, n_desc = 0;
  72668. +
  72669. + hc = qh->channel;
  72670. +
  72671. + /*
  72672. + * Start with hc->xfer_buff initialized in
  72673. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  72674. + * this pointer re-assigned to the buffer of the currently processed QTD.
  72675. + * For non-SG request there is always one QTD active.
  72676. + */
  72677. +
  72678. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  72679. +
  72680. + if (n_desc) {
  72681. + /* SG request - more than 1 QTDs */
  72682. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  72683. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  72684. + }
  72685. +
  72686. + qtd->n_desc = 0;
  72687. +
  72688. + do {
  72689. + dma_desc = &qh->desc_list[n_desc];
  72690. + len = hc->xfer_len;
  72691. +
  72692. + if (len > MAX_DMA_DESC_SIZE)
  72693. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  72694. +
  72695. + if (hc->ep_is_in) {
  72696. + if (len > 0) {
  72697. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  72698. + } else {
  72699. + /* Need 1 packet for transfer length of 0. */
  72700. + num_packets = 1;
  72701. + }
  72702. + /* Always program an integral # of max packets for IN transfers. */
  72703. + len = num_packets * hc->max_packet;
  72704. + }
  72705. +
  72706. + dma_desc->status.b.n_bytes = len;
  72707. +
  72708. + qh->n_bytes[n_desc] = len;
  72709. +
  72710. + if ((qh->ep_type == UE_CONTROL)
  72711. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  72712. + dma_desc->status.b.sup = 1; /* Setup Packet */
  72713. +
  72714. + dma_desc->status.b.a = 1; /* Active descriptor */
  72715. + dma_desc->status.b.sts = 0;
  72716. +
  72717. + dma_desc->buf =
  72718. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  72719. +
  72720. + /*
  72721. + * Last descriptor(or single) of IN transfer
  72722. + * with actual size less than MaxPacket.
  72723. + */
  72724. + if (len > hc->xfer_len) {
  72725. + hc->xfer_len = 0;
  72726. + } else {
  72727. + hc->xfer_buff += len;
  72728. + hc->xfer_len -= len;
  72729. + }
  72730. +
  72731. + qtd->n_desc++;
  72732. + n_desc++;
  72733. + }
  72734. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  72735. +
  72736. +
  72737. + qtd->in_process = 1;
  72738. +
  72739. + if (qh->ep_type == UE_CONTROL)
  72740. + break;
  72741. +
  72742. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  72743. + break;
  72744. + }
  72745. +
  72746. + if (n_desc) {
  72747. + /* Request Transfer Complete interrupt for the last descriptor */
  72748. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  72749. + /* End of List indicator */
  72750. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  72751. +
  72752. + hc->ntd = n_desc;
  72753. + }
  72754. +}
  72755. +
  72756. +/**
  72757. + * For Control and Bulk endpoints initializes descriptor list
  72758. + * and starts the transfer.
  72759. + *
  72760. + * For Interrupt and Isochronous endpoints initializes descriptor list
  72761. + * then updates FrameList, marking appropriate entries as active.
  72762. + * In case of Isochronous, the starting descriptor index is calculated based
  72763. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  72764. + * Then starts the transfer via enabling the channel.
  72765. + * For Isochronous endpoint the channel is not halted on XferComplete
  72766. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  72767. + *
  72768. + * @param hcd The HCD state structure for the DWC OTG controller.
  72769. + * @param qh The QH to init.
  72770. + *
  72771. + * @return 0 if successful, negative error code otherwise.
  72772. + */
  72773. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72774. +{
  72775. + /* Channel is already assigned */
  72776. + dwc_hc_t *hc = qh->channel;
  72777. + uint8_t skip_frames = 0;
  72778. +
  72779. + switch (hc->ep_type) {
  72780. + case DWC_OTG_EP_TYPE_CONTROL:
  72781. + case DWC_OTG_EP_TYPE_BULK:
  72782. + init_non_isoc_dma_desc(hcd, qh);
  72783. +
  72784. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72785. + break;
  72786. + case DWC_OTG_EP_TYPE_INTR:
  72787. + init_non_isoc_dma_desc(hcd, qh);
  72788. +
  72789. + update_frame_list(hcd, qh, 1);
  72790. +
  72791. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72792. + break;
  72793. + case DWC_OTG_EP_TYPE_ISOC:
  72794. +
  72795. + if (!qh->ntd)
  72796. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  72797. +
  72798. + init_isoc_dma_desc(hcd, qh, skip_frames);
  72799. +
  72800. + if (!hc->xfer_started) {
  72801. +
  72802. + update_frame_list(hcd, qh, 1);
  72803. +
  72804. + /*
  72805. + * Always set to max, instead of actual size.
  72806. + * Otherwise ntd will be changed with
  72807. + * channel being enabled. Not recommended.
  72808. + *
  72809. + */
  72810. + hc->ntd = max_desc_num(qh);
  72811. + /* Enable channel only once for ISOC */
  72812. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72813. + }
  72814. +
  72815. + break;
  72816. + default:
  72817. +
  72818. + break;
  72819. + }
  72820. +}
  72821. +
  72822. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  72823. + dwc_hc_t * hc,
  72824. + dwc_otg_hc_regs_t * hc_regs,
  72825. + dwc_otg_halt_status_e halt_status)
  72826. +{
  72827. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  72828. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  72829. + dwc_otg_qh_t *qh;
  72830. + dwc_otg_host_dma_desc_t *dma_desc;
  72831. + uint16_t idx, remain;
  72832. + uint8_t urb_compl;
  72833. +
  72834. + qh = hc->qh;
  72835. + idx = qh->td_first;
  72836. +
  72837. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72838. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  72839. + qtd->in_process = 0;
  72840. + return;
  72841. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  72842. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  72843. + /*
  72844. + * Channel is halted in these error cases.
  72845. + * Considered as serious issues.
  72846. + * Complete all URBs marking all frames as failed,
  72847. + * irrespective whether some of the descriptors(frames) succeeded or no.
  72848. + * Pass error code to completion routine as well, to
  72849. + * update urb->status, some of class drivers might use it to stop
  72850. + * queing transfer requests.
  72851. + */
  72852. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  72853. + ? (-DWC_E_IO)
  72854. + : (-DWC_E_OVERFLOW);
  72855. +
  72856. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72857. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  72858. + frame_desc = &qtd->urb->iso_descs[idx];
  72859. + frame_desc->status = err;
  72860. + }
  72861. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  72862. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72863. + }
  72864. + return;
  72865. + }
  72866. +
  72867. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72868. +
  72869. + if (!qtd->in_process)
  72870. + break;
  72871. +
  72872. + urb_compl = 0;
  72873. +
  72874. + do {
  72875. +
  72876. + dma_desc = &qh->desc_list[idx];
  72877. +
  72878. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  72879. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  72880. +
  72881. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  72882. + /*
  72883. + * XactError or, unable to complete all the transactions
  72884. + * in the scheduled micro-frame/frame,
  72885. + * both indicated by DMA_DESC_STS_PKTERR.
  72886. + */
  72887. + qtd->urb->error_count++;
  72888. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  72889. + frame_desc->status = -DWC_E_PROTOCOL;
  72890. + } else {
  72891. + /* Success */
  72892. +
  72893. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  72894. + frame_desc->status = 0;
  72895. + }
  72896. +
  72897. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  72898. + /*
  72899. + * urb->status is not used for isoc transfers here.
  72900. + * The individual frame_desc status are used instead.
  72901. + */
  72902. +
  72903. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  72904. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72905. +
  72906. + /*
  72907. + * This check is necessary because urb_dequeue can be called
  72908. + * from urb complete callback(sound driver example).
  72909. + * All pending URBs are dequeued there, so no need for
  72910. + * further processing.
  72911. + */
  72912. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72913. + return;
  72914. + }
  72915. +
  72916. + urb_compl = 1;
  72917. +
  72918. + }
  72919. +
  72920. + qh->ntd--;
  72921. +
  72922. + /* Stop if IOC requested descriptor reached */
  72923. + if (dma_desc->status.b_isoc.ioc) {
  72924. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  72925. + goto stop_scan;
  72926. + }
  72927. +
  72928. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  72929. +
  72930. + if (urb_compl)
  72931. + break;
  72932. + }
  72933. + while (idx != qh->td_first);
  72934. + }
  72935. +stop_scan:
  72936. + qh->td_first = idx;
  72937. +}
  72938. +
  72939. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  72940. + dwc_hc_t * hc,
  72941. + dwc_otg_qtd_t * qtd,
  72942. + dwc_otg_host_dma_desc_t * dma_desc,
  72943. + dwc_otg_halt_status_e halt_status,
  72944. + uint32_t n_bytes, uint8_t * xfer_done)
  72945. +{
  72946. +
  72947. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  72948. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  72949. +
  72950. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  72951. + urb->status = -DWC_E_IO;
  72952. + return 1;
  72953. + }
  72954. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  72955. + switch (halt_status) {
  72956. + case DWC_OTG_HC_XFER_STALL:
  72957. + urb->status = -DWC_E_PIPE;
  72958. + break;
  72959. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  72960. + urb->status = -DWC_E_OVERFLOW;
  72961. + break;
  72962. + case DWC_OTG_HC_XFER_XACT_ERR:
  72963. + urb->status = -DWC_E_PROTOCOL;
  72964. + break;
  72965. + default:
  72966. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  72967. + halt_status);
  72968. + break;
  72969. + }
  72970. + return 1;
  72971. + }
  72972. +
  72973. + if (dma_desc->status.b.a == 1) {
  72974. + DWC_DEBUGPL(DBG_HCDV,
  72975. + "Active descriptor encountered on channel %d\n",
  72976. + hc->hc_num);
  72977. + return 0;
  72978. + }
  72979. +
  72980. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  72981. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  72982. + urb->actual_length += n_bytes - remain;
  72983. + if (remain || urb->actual_length == urb->length) {
  72984. + /*
  72985. + * For Control Data stage do not set urb->status=0 to prevent
  72986. + * URB callback. Set it when Status phase done. See below.
  72987. + */
  72988. + *xfer_done = 1;
  72989. + }
  72990. +
  72991. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  72992. + urb->status = 0;
  72993. + *xfer_done = 1;
  72994. + }
  72995. + /* No handling for SETUP stage */
  72996. + } else {
  72997. + /* BULK and INTR */
  72998. + urb->actual_length += n_bytes - remain;
  72999. + if (remain || urb->actual_length == urb->length) {
  73000. + urb->status = 0;
  73001. + *xfer_done = 1;
  73002. + }
  73003. + }
  73004. +
  73005. + return 0;
  73006. +}
  73007. +
  73008. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  73009. + dwc_hc_t * hc,
  73010. + dwc_otg_hc_regs_t * hc_regs,
  73011. + dwc_otg_halt_status_e halt_status)
  73012. +{
  73013. + dwc_otg_hcd_urb_t *urb = NULL;
  73014. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  73015. + dwc_otg_qh_t *qh;
  73016. + dwc_otg_host_dma_desc_t *dma_desc;
  73017. + uint32_t n_bytes, n_desc, i;
  73018. + uint8_t failed = 0, xfer_done;
  73019. +
  73020. + n_desc = 0;
  73021. +
  73022. + qh = hc->qh;
  73023. +
  73024. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  73025. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  73026. + qtd->in_process = 0;
  73027. + }
  73028. + return;
  73029. + }
  73030. +
  73031. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  73032. +
  73033. + urb = qtd->urb;
  73034. +
  73035. + n_bytes = 0;
  73036. + xfer_done = 0;
  73037. +
  73038. + for (i = 0; i < qtd->n_desc; i++) {
  73039. + dma_desc = &qh->desc_list[n_desc];
  73040. +
  73041. + n_bytes = qh->n_bytes[n_desc];
  73042. +
  73043. + failed =
  73044. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  73045. + dma_desc,
  73046. + halt_status, n_bytes,
  73047. + &xfer_done);
  73048. +
  73049. + if (failed
  73050. + || (xfer_done
  73051. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  73052. +
  73053. + hcd->fops->complete(hcd, urb->priv, urb,
  73054. + urb->status);
  73055. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  73056. +
  73057. + if (failed)
  73058. + goto stop_scan;
  73059. + } else if (qh->ep_type == UE_CONTROL) {
  73060. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  73061. + if (urb->length > 0) {
  73062. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  73063. + } else {
  73064. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  73065. + }
  73066. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  73067. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  73068. + if (xfer_done) {
  73069. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  73070. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  73071. + } else if (i + 1 == qtd->n_desc) {
  73072. + /*
  73073. + * Last descriptor for Control data stage which is
  73074. + * not completed yet.
  73075. + */
  73076. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  73077. + }
  73078. + }
  73079. + }
  73080. +
  73081. + n_desc++;
  73082. + }
  73083. +
  73084. + }
  73085. +
  73086. +stop_scan:
  73087. +
  73088. + if (qh->ep_type != UE_CONTROL) {
  73089. + /*
  73090. + * Resetting the data toggle for bulk
  73091. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  73092. + */
  73093. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  73094. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  73095. + else
  73096. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  73097. + }
  73098. +
  73099. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  73100. + hcint_data_t hcint;
  73101. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  73102. + if (hcint.b.nyet) {
  73103. + /*
  73104. + * Got a NYET on the last transaction of the transfer. It
  73105. + * means that the endpoint should be in the PING state at the
  73106. + * beginning of the next transfer.
  73107. + */
  73108. + qh->ping_state = 1;
  73109. + clear_hc_int(hc_regs, nyet);
  73110. + }
  73111. +
  73112. + }
  73113. +
  73114. +}
  73115. +
  73116. +/**
  73117. + * This function is called from interrupt handlers.
  73118. + * Scans the descriptor list, updates URB's status and
  73119. + * calls completion routine for the URB if it's done.
  73120. + * Releases the channel to be used by other transfers.
  73121. + * In case of Isochronous endpoint the channel is not halted until
  73122. + * the end of the session, i.e. QTD list is empty.
  73123. + * If periodic channel released the FrameList is updated accordingly.
  73124. + *
  73125. + * Calls transaction selection routines to activate pending transfers.
  73126. + *
  73127. + * @param hcd The HCD state structure for the DWC OTG controller.
  73128. + * @param hc Host channel, the transfer is completed on.
  73129. + * @param hc_regs Host channel registers.
  73130. + * @param halt_status Reason the channel is being halted,
  73131. + * or just XferComplete for isochronous transfer
  73132. + */
  73133. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  73134. + dwc_hc_t * hc,
  73135. + dwc_otg_hc_regs_t * hc_regs,
  73136. + dwc_otg_halt_status_e halt_status)
  73137. +{
  73138. + uint8_t continue_isoc_xfer = 0;
  73139. + dwc_otg_transaction_type_e tr_type;
  73140. + dwc_otg_qh_t *qh = hc->qh;
  73141. +
  73142. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  73143. +
  73144. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  73145. +
  73146. + /* Release the channel if halted or session completed */
  73147. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  73148. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  73149. +
  73150. + /* Halt the channel if session completed */
  73151. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  73152. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  73153. + }
  73154. +
  73155. + release_channel_ddma(hcd, qh);
  73156. + dwc_otg_hcd_qh_remove(hcd, qh);
  73157. + } else {
  73158. + /* Keep in assigned schedule to continue transfer */
  73159. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  73160. + &qh->qh_list_entry);
  73161. + continue_isoc_xfer = 1;
  73162. +
  73163. + }
  73164. + /** @todo Consider the case when period exceeds FrameList size.
  73165. + * Frame Rollover interrupt should be used.
  73166. + */
  73167. + } else {
  73168. + /* Scan descriptor list to complete the URB(s), then release the channel */
  73169. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  73170. +
  73171. + release_channel_ddma(hcd, qh);
  73172. + dwc_otg_hcd_qh_remove(hcd, qh);
  73173. +
  73174. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  73175. + /* Add back to inactive non-periodic schedule on normal completion */
  73176. + dwc_otg_hcd_qh_add(hcd, qh);
  73177. + }
  73178. +
  73179. + }
  73180. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  73181. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  73182. + if (continue_isoc_xfer) {
  73183. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  73184. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  73185. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  73186. + tr_type = DWC_OTG_TRANSACTION_ALL;
  73187. + }
  73188. + }
  73189. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  73190. + }
  73191. +}
  73192. +
  73193. +#endif /* DWC_DEVICE_ONLY */
  73194. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  73195. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 1970-01-01 01:00:00.000000000 +0100
  73196. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 2014-04-24 15:35:04.173565776 +0200
  73197. @@ -0,0 +1,851 @@
  73198. +/* ==========================================================================
  73199. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  73200. + * $Revision: #58 $
  73201. + * $Date: 2011/09/15 $
  73202. + * $Change: 1846647 $
  73203. + *
  73204. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73205. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73206. + * otherwise expressly agreed to in writing between Synopsys and you.
  73207. + *
  73208. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73209. + * any End User Software License Agreement or Agreement for Licensed Product
  73210. + * with Synopsys or any supplement thereto. You are permitted to use and
  73211. + * redistribute this Software in source and binary forms, with or without
  73212. + * modification, provided that redistributions of source code must retain this
  73213. + * notice. You may not view, use, disclose, copy or distribute this file or
  73214. + * any information contained herein except pursuant to this license grant from
  73215. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73216. + * below, then you are not authorized to use the Software.
  73217. + *
  73218. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73219. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73220. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73221. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73222. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73223. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73224. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73225. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73226. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73227. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73228. + * DAMAGE.
  73229. + * ========================================================================== */
  73230. +#ifndef DWC_DEVICE_ONLY
  73231. +#ifndef __DWC_HCD_H__
  73232. +#define __DWC_HCD_H__
  73233. +
  73234. +#include "dwc_otg_os_dep.h"
  73235. +#include "usb.h"
  73236. +#include "dwc_otg_hcd_if.h"
  73237. +#include "dwc_otg_core_if.h"
  73238. +#include "dwc_list.h"
  73239. +#include "dwc_otg_cil.h"
  73240. +
  73241. +/**
  73242. + * @file
  73243. + *
  73244. + * This file contains the structures, constants, and interfaces for
  73245. + * the Host Contoller Driver (HCD).
  73246. + *
  73247. + * The Host Controller Driver (HCD) is responsible for translating requests
  73248. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  73249. + * It isolates the USBD from the specifics of the controller by providing an
  73250. + * API to the USBD.
  73251. + */
  73252. +
  73253. +struct dwc_otg_hcd_pipe_info {
  73254. + uint8_t dev_addr;
  73255. + uint8_t ep_num;
  73256. + uint8_t pipe_type;
  73257. + uint8_t pipe_dir;
  73258. + uint16_t mps;
  73259. +};
  73260. +
  73261. +struct dwc_otg_hcd_iso_packet_desc {
  73262. + uint32_t offset;
  73263. + uint32_t length;
  73264. + uint32_t actual_length;
  73265. + uint32_t status;
  73266. +};
  73267. +
  73268. +struct dwc_otg_qtd;
  73269. +
  73270. +struct dwc_otg_hcd_urb {
  73271. + void *priv;
  73272. + struct dwc_otg_qtd *qtd;
  73273. + void *buf;
  73274. + dwc_dma_t dma;
  73275. + void *setup_packet;
  73276. + dwc_dma_t setup_dma;
  73277. + uint32_t length;
  73278. + uint32_t actual_length;
  73279. + uint32_t status;
  73280. + uint32_t error_count;
  73281. + uint32_t packet_count;
  73282. + uint32_t flags;
  73283. + uint16_t interval;
  73284. + struct dwc_otg_hcd_pipe_info pipe_info;
  73285. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  73286. +};
  73287. +
  73288. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  73289. +{
  73290. + return pipe->ep_num;
  73291. +}
  73292. +
  73293. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  73294. + *pipe)
  73295. +{
  73296. + return pipe->pipe_type;
  73297. +}
  73298. +
  73299. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  73300. +{
  73301. + return pipe->mps;
  73302. +}
  73303. +
  73304. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  73305. + *pipe)
  73306. +{
  73307. + return pipe->dev_addr;
  73308. +}
  73309. +
  73310. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  73311. + *pipe)
  73312. +{
  73313. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  73314. +}
  73315. +
  73316. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  73317. + *pipe)
  73318. +{
  73319. + return (pipe->pipe_type == UE_INTERRUPT);
  73320. +}
  73321. +
  73322. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  73323. + *pipe)
  73324. +{
  73325. + return (pipe->pipe_type == UE_BULK);
  73326. +}
  73327. +
  73328. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  73329. + *pipe)
  73330. +{
  73331. + return (pipe->pipe_type == UE_CONTROL);
  73332. +}
  73333. +
  73334. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  73335. +{
  73336. + return (pipe->pipe_dir == UE_DIR_IN);
  73337. +}
  73338. +
  73339. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  73340. + *pipe)
  73341. +{
  73342. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  73343. +}
  73344. +
  73345. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  73346. + uint8_t devaddr, uint8_t ep_num,
  73347. + uint8_t pipe_type, uint8_t pipe_dir,
  73348. + uint16_t mps)
  73349. +{
  73350. + pipe->dev_addr = devaddr;
  73351. + pipe->ep_num = ep_num;
  73352. + pipe->pipe_type = pipe_type;
  73353. + pipe->pipe_dir = pipe_dir;
  73354. + pipe->mps = mps;
  73355. +}
  73356. +
  73357. +/**
  73358. + * Phases for control transfers.
  73359. + */
  73360. +typedef enum dwc_otg_control_phase {
  73361. + DWC_OTG_CONTROL_SETUP,
  73362. + DWC_OTG_CONTROL_DATA,
  73363. + DWC_OTG_CONTROL_STATUS
  73364. +} dwc_otg_control_phase_e;
  73365. +
  73366. +/** Transaction types. */
  73367. +typedef enum dwc_otg_transaction_type {
  73368. + DWC_OTG_TRANSACTION_NONE = 0,
  73369. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  73370. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  73371. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  73372. +} dwc_otg_transaction_type_e;
  73373. +
  73374. +struct dwc_otg_qh;
  73375. +
  73376. +/**
  73377. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  73378. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  73379. + * (of one of these types) submitted to the HCD. The transfer associated with
  73380. + * a QTD may require one or multiple transactions.
  73381. + *
  73382. + * A QTD is linked to a Queue Head, which is entered in either the
  73383. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  73384. + * execution, some or all of its transactions may be executed. After
  73385. + * execution, the state of the QTD is updated. The QTD may be retired if all
  73386. + * its transactions are complete or if an error occurred. Otherwise, it
  73387. + * remains in the schedule so more transactions can be executed later.
  73388. + */
  73389. +typedef struct dwc_otg_qtd {
  73390. + /**
  73391. + * Determines the PID of the next data packet for the data phase of
  73392. + * control transfers. Ignored for other transfer types.<br>
  73393. + * One of the following values:
  73394. + * - DWC_OTG_HC_PID_DATA0
  73395. + * - DWC_OTG_HC_PID_DATA1
  73396. + */
  73397. + uint8_t data_toggle;
  73398. +
  73399. + /** Current phase for control transfers (Setup, Data, or Status). */
  73400. + dwc_otg_control_phase_e control_phase;
  73401. +
  73402. + /** Keep track of the current split type
  73403. + * for FS/LS endpoints on a HS Hub */
  73404. + uint8_t complete_split;
  73405. +
  73406. + /** How many bytes transferred during SSPLIT OUT */
  73407. + uint32_t ssplit_out_xfer_count;
  73408. +
  73409. + /**
  73410. + * Holds the number of bus errors that have occurred for a transaction
  73411. + * within this transfer.
  73412. + */
  73413. + uint8_t error_count;
  73414. +
  73415. + /**
  73416. + * Index of the next frame descriptor for an isochronous transfer. A
  73417. + * frame descriptor describes the buffer position and length of the
  73418. + * data to be transferred in the next scheduled (micro)frame of an
  73419. + * isochronous transfer. It also holds status for that transaction.
  73420. + * The frame index starts at 0.
  73421. + */
  73422. + uint16_t isoc_frame_index;
  73423. +
  73424. + /** Position of the ISOC split on full/low speed */
  73425. + uint8_t isoc_split_pos;
  73426. +
  73427. + /** Position of the ISOC split in the buffer for the current frame */
  73428. + uint16_t isoc_split_offset;
  73429. +
  73430. + /** URB for this transfer */
  73431. + struct dwc_otg_hcd_urb *urb;
  73432. +
  73433. + struct dwc_otg_qh *qh;
  73434. +
  73435. + /** This list of QTDs */
  73436. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  73437. +
  73438. + /** Indicates if this QTD is currently processed by HW. */
  73439. + uint8_t in_process;
  73440. +
  73441. + /** Number of DMA descriptors for this QTD */
  73442. + uint8_t n_desc;
  73443. +
  73444. + /**
  73445. + * Last activated frame(packet) index.
  73446. + * Used in Descriptor DMA mode only.
  73447. + */
  73448. + uint16_t isoc_frame_index_last;
  73449. +
  73450. +} dwc_otg_qtd_t;
  73451. +
  73452. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  73453. +
  73454. +/**
  73455. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  73456. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  73457. + * be entered in either the non-periodic or periodic schedule.
  73458. + */
  73459. +typedef struct dwc_otg_qh {
  73460. + /**
  73461. + * Endpoint type.
  73462. + * One of the following values:
  73463. + * - UE_CONTROL
  73464. + * - UE_BULK
  73465. + * - UE_INTERRUPT
  73466. + * - UE_ISOCHRONOUS
  73467. + */
  73468. + uint8_t ep_type;
  73469. + uint8_t ep_is_in;
  73470. +
  73471. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  73472. + uint16_t maxp;
  73473. +
  73474. + /**
  73475. + * Device speed.
  73476. + * One of the following values:
  73477. + * - DWC_OTG_EP_SPEED_LOW
  73478. + * - DWC_OTG_EP_SPEED_FULL
  73479. + * - DWC_OTG_EP_SPEED_HIGH
  73480. + */
  73481. + uint8_t dev_speed;
  73482. +
  73483. + /**
  73484. + * Determines the PID of the next data packet for non-control
  73485. + * transfers. Ignored for control transfers.<br>
  73486. + * One of the following values:
  73487. + * - DWC_OTG_HC_PID_DATA0
  73488. + * - DWC_OTG_HC_PID_DATA1
  73489. + */
  73490. + uint8_t data_toggle;
  73491. +
  73492. + /** Ping state if 1. */
  73493. + uint8_t ping_state;
  73494. +
  73495. + /**
  73496. + * List of QTDs for this QH.
  73497. + */
  73498. + struct dwc_otg_qtd_list qtd_list;
  73499. +
  73500. + /** Host channel currently processing transfers for this QH. */
  73501. + struct dwc_hc *channel;
  73502. +
  73503. + /** Full/low speed endpoint on high-speed hub requires split. */
  73504. + uint8_t do_split;
  73505. +
  73506. + /** @name Periodic schedule information */
  73507. + /** @{ */
  73508. +
  73509. + /** Bandwidth in microseconds per (micro)frame. */
  73510. + uint16_t usecs;
  73511. +
  73512. + /** Interval between transfers in (micro)frames. */
  73513. + uint16_t interval;
  73514. +
  73515. + /**
  73516. + * (micro)frame to initialize a periodic transfer. The transfer
  73517. + * executes in the following (micro)frame.
  73518. + */
  73519. + uint16_t sched_frame;
  73520. +
  73521. + /*
  73522. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  73523. + */
  73524. + uint16_t nak_frame;
  73525. +
  73526. + /** (micro)frame at which last start split was initialized. */
  73527. + uint16_t start_split_frame;
  73528. +
  73529. + /** @} */
  73530. +
  73531. + /**
  73532. + * Used instead of original buffer if
  73533. + * it(physical address) is not dword-aligned.
  73534. + */
  73535. + uint8_t *dw_align_buf;
  73536. + dwc_dma_t dw_align_buf_dma;
  73537. +
  73538. + /** Entry for QH in either the periodic or non-periodic schedule. */
  73539. + dwc_list_link_t qh_list_entry;
  73540. +
  73541. + /** @name Descriptor DMA support */
  73542. + /** @{ */
  73543. +
  73544. + /** Descriptor List. */
  73545. + dwc_otg_host_dma_desc_t *desc_list;
  73546. +
  73547. + /** Descriptor List physical address. */
  73548. + dwc_dma_t desc_list_dma;
  73549. +
  73550. + /**
  73551. + * Xfer Bytes array.
  73552. + * Each element corresponds to a descriptor and indicates
  73553. + * original XferSize size value for the descriptor.
  73554. + */
  73555. + uint32_t *n_bytes;
  73556. +
  73557. + /** Actual number of transfer descriptors in a list. */
  73558. + uint16_t ntd;
  73559. +
  73560. + /** First activated isochronous transfer descriptor index. */
  73561. + uint8_t td_first;
  73562. + /** Last activated isochronous transfer descriptor index. */
  73563. + uint8_t td_last;
  73564. +
  73565. + /** @} */
  73566. +
  73567. +
  73568. + uint16_t speed;
  73569. + uint16_t frame_usecs[8];
  73570. +
  73571. + uint32_t skip_count;
  73572. +} dwc_otg_qh_t;
  73573. +
  73574. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  73575. +
  73576. +typedef struct urb_tq_entry {
  73577. + struct urb *urb;
  73578. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  73579. +} urb_tq_entry_t;
  73580. +
  73581. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  73582. +
  73583. +/**
  73584. + * This structure holds the state of the HCD, including the non-periodic and
  73585. + * periodic schedules.
  73586. + */
  73587. +struct dwc_otg_hcd {
  73588. + /** The DWC otg device pointer */
  73589. + struct dwc_otg_device *otg_dev;
  73590. + /** DWC OTG Core Interface Layer */
  73591. + dwc_otg_core_if_t *core_if;
  73592. +
  73593. + /** Function HCD driver callbacks */
  73594. + struct dwc_otg_hcd_function_ops *fops;
  73595. +
  73596. + /** Internal DWC HCD Flags */
  73597. + volatile union dwc_otg_hcd_internal_flags {
  73598. + uint32_t d32;
  73599. + struct {
  73600. + unsigned port_connect_status_change:1;
  73601. + unsigned port_connect_status:1;
  73602. + unsigned port_reset_change:1;
  73603. + unsigned port_enable_change:1;
  73604. + unsigned port_suspend_change:1;
  73605. + unsigned port_over_current_change:1;
  73606. + unsigned port_l1_change:1;
  73607. + unsigned reserved:26;
  73608. + } b;
  73609. + } flags;
  73610. +
  73611. + /**
  73612. + * Inactive items in the non-periodic schedule. This is a list of
  73613. + * Queue Heads. Transfers associated with these Queue Heads are not
  73614. + * currently assigned to a host channel.
  73615. + */
  73616. + dwc_list_link_t non_periodic_sched_inactive;
  73617. +
  73618. + /**
  73619. + * Active items in the non-periodic schedule. This is a list of
  73620. + * Queue Heads. Transfers associated with these Queue Heads are
  73621. + * currently assigned to a host channel.
  73622. + */
  73623. + dwc_list_link_t non_periodic_sched_active;
  73624. +
  73625. + /**
  73626. + * Pointer to the next Queue Head to process in the active
  73627. + * non-periodic schedule.
  73628. + */
  73629. + dwc_list_link_t *non_periodic_qh_ptr;
  73630. +
  73631. + /**
  73632. + * Inactive items in the periodic schedule. This is a list of QHs for
  73633. + * periodic transfers that are _not_ scheduled for the next frame.
  73634. + * Each QH in the list has an interval counter that determines when it
  73635. + * needs to be scheduled for execution. This scheduling mechanism
  73636. + * allows only a simple calculation for periodic bandwidth used (i.e.
  73637. + * must assume that all periodic transfers may need to execute in the
  73638. + * same frame). However, it greatly simplifies scheduling and should
  73639. + * be sufficient for the vast majority of OTG hosts, which need to
  73640. + * connect to a small number of peripherals at one time.
  73641. + *
  73642. + * Items move from this list to periodic_sched_ready when the QH
  73643. + * interval counter is 0 at SOF.
  73644. + */
  73645. + dwc_list_link_t periodic_sched_inactive;
  73646. +
  73647. + /**
  73648. + * List of periodic QHs that are ready for execution in the next
  73649. + * frame, but have not yet been assigned to host channels.
  73650. + *
  73651. + * Items move from this list to periodic_sched_assigned as host
  73652. + * channels become available during the current frame.
  73653. + */
  73654. + dwc_list_link_t periodic_sched_ready;
  73655. +
  73656. + /**
  73657. + * List of periodic QHs to be executed in the next frame that are
  73658. + * assigned to host channels.
  73659. + *
  73660. + * Items move from this list to periodic_sched_queued as the
  73661. + * transactions for the QH are queued to the DWC_otg controller.
  73662. + */
  73663. + dwc_list_link_t periodic_sched_assigned;
  73664. +
  73665. + /**
  73666. + * List of periodic QHs that have been queued for execution.
  73667. + *
  73668. + * Items move from this list to either periodic_sched_inactive or
  73669. + * periodic_sched_ready when the channel associated with the transfer
  73670. + * is released. If the interval for the QH is 1, the item moves to
  73671. + * periodic_sched_ready because it must be rescheduled for the next
  73672. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  73673. + */
  73674. + dwc_list_link_t periodic_sched_queued;
  73675. +
  73676. + /**
  73677. + * Total bandwidth claimed so far for periodic transfers. This value
  73678. + * is in microseconds per (micro)frame. The assumption is that all
  73679. + * periodic transfers may occur in the same (micro)frame.
  73680. + */
  73681. + uint16_t periodic_usecs;
  73682. +
  73683. + /**
  73684. + * Total bandwidth claimed so far for all periodic transfers
  73685. + * in a frame.
  73686. + * This will include a mixture of HS and FS transfers.
  73687. + * Units are microseconds per (micro)frame.
  73688. + * We have a budget per frame and have to schedule
  73689. + * transactions accordingly.
  73690. + * Watch out for the fact that things are actually scheduled for the
  73691. + * "next frame".
  73692. + */
  73693. + uint16_t frame_usecs[8];
  73694. +
  73695. +
  73696. + /**
  73697. + * Frame number read from the core at SOF. The value ranges from 0 to
  73698. + * DWC_HFNUM_MAX_FRNUM.
  73699. + */
  73700. + uint16_t frame_number;
  73701. +
  73702. + /**
  73703. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  73704. + */
  73705. + uint16_t periodic_qh_count;
  73706. +
  73707. + /**
  73708. + * Free host channels in the controller. This is a list of
  73709. + * dwc_hc_t items.
  73710. + */
  73711. + struct hc_list free_hc_list;
  73712. + /**
  73713. + * Number of host channels assigned to periodic transfers. Currently
  73714. + * assuming that there is a dedicated host channel for each periodic
  73715. + * transaction and at least one host channel available for
  73716. + * non-periodic transactions.
  73717. + */
  73718. + int periodic_channels; /* microframe_schedule==0 */
  73719. +
  73720. + /**
  73721. + * Number of host channels assigned to non-periodic transfers.
  73722. + */
  73723. + int non_periodic_channels; /* microframe_schedule==0 */
  73724. +
  73725. + /**
  73726. + * Number of host channels assigned to non-periodic transfers.
  73727. + */
  73728. + int available_host_channels;
  73729. +
  73730. + /**
  73731. + * Array of pointers to the host channel descriptors. Allows accessing
  73732. + * a host channel descriptor given the host channel number. This is
  73733. + * useful in interrupt handlers.
  73734. + */
  73735. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  73736. +
  73737. + /**
  73738. + * Buffer to use for any data received during the status phase of a
  73739. + * control transfer. Normally no data is transferred during the status
  73740. + * phase. This buffer is used as a bit bucket.
  73741. + */
  73742. + uint8_t *status_buf;
  73743. +
  73744. + /**
  73745. + * DMA address for status_buf.
  73746. + */
  73747. + dma_addr_t status_buf_dma;
  73748. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  73749. +
  73750. + /**
  73751. + * Connection timer. An OTG host must display a message if the device
  73752. + * does not connect. Started when the VBus power is turned on via
  73753. + * sysfs attribute "buspower".
  73754. + */
  73755. + dwc_timer_t *conn_timer;
  73756. +
  73757. + /* Tasket to do a reset */
  73758. + dwc_tasklet_t *reset_tasklet;
  73759. +
  73760. + dwc_tasklet_t *completion_tasklet;
  73761. + struct urb_list completed_urb_list;
  73762. +
  73763. + /* */
  73764. + dwc_spinlock_t *lock;
  73765. + dwc_spinlock_t *channel_lock;
  73766. + /**
  73767. + * Private data that could be used by OS wrapper.
  73768. + */
  73769. + void *priv;
  73770. +
  73771. + uint8_t otg_port;
  73772. +
  73773. + /** Frame List */
  73774. + uint32_t *frame_list;
  73775. +
  73776. + /** Hub - Port assignment */
  73777. + int hub_port[128];
  73778. +#ifdef FIQ_DEBUG
  73779. + int hub_port_alloc[2048];
  73780. +#endif
  73781. +
  73782. + /** Frame List DMA address */
  73783. + dma_addr_t frame_list_dma;
  73784. +
  73785. +#ifdef DEBUG
  73786. + uint32_t frrem_samples;
  73787. + uint64_t frrem_accum;
  73788. +
  73789. + uint32_t hfnum_7_samples_a;
  73790. + uint64_t hfnum_7_frrem_accum_a;
  73791. + uint32_t hfnum_0_samples_a;
  73792. + uint64_t hfnum_0_frrem_accum_a;
  73793. + uint32_t hfnum_other_samples_a;
  73794. + uint64_t hfnum_other_frrem_accum_a;
  73795. +
  73796. + uint32_t hfnum_7_samples_b;
  73797. + uint64_t hfnum_7_frrem_accum_b;
  73798. + uint32_t hfnum_0_samples_b;
  73799. + uint64_t hfnum_0_frrem_accum_b;
  73800. + uint32_t hfnum_other_samples_b;
  73801. + uint64_t hfnum_other_frrem_accum_b;
  73802. +#endif
  73803. +};
  73804. +
  73805. +/** @name Transaction Execution Functions */
  73806. +/** @{ */
  73807. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  73808. + * hcd);
  73809. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  73810. + dwc_otg_transaction_type_e tr_type);
  73811. +
  73812. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  73813. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  73814. +
  73815. +
  73816. +/** @} */
  73817. +
  73818. +/** @name Interrupt Handler Functions */
  73819. +/** @{ */
  73820. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73821. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73822. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  73823. + dwc_otg_hcd);
  73824. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  73825. + dwc_otg_hcd);
  73826. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  73827. + dwc_otg_hcd);
  73828. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  73829. + dwc_otg_hcd);
  73830. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73831. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  73832. + dwc_otg_hcd);
  73833. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73834. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73835. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  73836. + uint32_t num);
  73837. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73838. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  73839. + dwc_otg_hcd);
  73840. +/** @} */
  73841. +
  73842. +/** @name Schedule Queue Functions */
  73843. +/** @{ */
  73844. +
  73845. +/* Implemented in dwc_otg_hcd_queue.c */
  73846. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  73847. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  73848. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73849. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73850. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73851. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  73852. + int sched_csplit);
  73853. +
  73854. +/** Remove and free a QH */
  73855. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  73856. + dwc_otg_qh_t * qh)
  73857. +{
  73858. + dwc_irqflags_t flags;
  73859. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  73860. + dwc_otg_hcd_qh_remove(hcd, qh);
  73861. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  73862. + dwc_otg_hcd_qh_free(hcd, qh);
  73863. +}
  73864. +
  73865. +/** Allocates memory for a QH structure.
  73866. + * @return Returns the memory allocate or NULL on error. */
  73867. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  73868. +{
  73869. + if (atomic_alloc)
  73870. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  73871. + else
  73872. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  73873. +}
  73874. +
  73875. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  73876. + int atomic_alloc);
  73877. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  73878. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  73879. + dwc_otg_qh_t ** qh, int atomic_alloc);
  73880. +
  73881. +/** Allocates memory for a QTD structure.
  73882. + * @return Returns the memory allocate or NULL on error. */
  73883. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  73884. +{
  73885. + if (atomic_alloc)
  73886. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  73887. + else
  73888. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  73889. +}
  73890. +
  73891. +/** Frees the memory for a QTD structure. QTD should already be removed from
  73892. + * list.
  73893. + * @param qtd QTD to free.*/
  73894. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  73895. +{
  73896. + DWC_FREE(qtd);
  73897. +}
  73898. +
  73899. +/** Removes a QTD from list.
  73900. + * @param hcd HCD instance.
  73901. + * @param qtd QTD to remove from list.
  73902. + * @param qh QTD belongs to.
  73903. + */
  73904. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  73905. + dwc_otg_qtd_t * qtd,
  73906. + dwc_otg_qh_t * qh)
  73907. +{
  73908. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  73909. +}
  73910. +
  73911. +/** Remove and free a QTD
  73912. + * Need to disable IRQ and hold hcd lock while calling this function out of
  73913. + * interrupt servicing chain */
  73914. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  73915. + dwc_otg_qtd_t * qtd,
  73916. + dwc_otg_qh_t * qh)
  73917. +{
  73918. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  73919. + dwc_otg_hcd_qtd_free(qtd);
  73920. +}
  73921. +
  73922. +/** @} */
  73923. +
  73924. +/** @name Descriptor DMA Supporting Functions */
  73925. +/** @{ */
  73926. +
  73927. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73928. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  73929. + dwc_hc_t * hc,
  73930. + dwc_otg_hc_regs_t * hc_regs,
  73931. + dwc_otg_halt_status_e halt_status);
  73932. +
  73933. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73934. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73935. +
  73936. +/** @} */
  73937. +
  73938. +/** @name Internal Functions */
  73939. +/** @{ */
  73940. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  73941. +/** @} */
  73942. +
  73943. +#ifdef CONFIG_USB_DWC_OTG_LPM
  73944. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  73945. + uint8_t devaddr);
  73946. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  73947. +#endif
  73948. +
  73949. +/** Gets the QH that contains the list_head */
  73950. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  73951. +
  73952. +/** Gets the QTD that contains the list_head */
  73953. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  73954. +
  73955. +/** Check if QH is non-periodic */
  73956. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  73957. + (_qh_ptr_->ep_type == UE_CONTROL))
  73958. +
  73959. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  73960. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  73961. +
  73962. +/** Packet size for any kind of endpoint descriptor */
  73963. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  73964. +
  73965. +/**
  73966. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  73967. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  73968. + * frame number when the max frame number is reached.
  73969. + */
  73970. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  73971. +{
  73972. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  73973. + (DWC_HFNUM_MAX_FRNUM >> 1);
  73974. +}
  73975. +
  73976. +/**
  73977. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  73978. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  73979. + * number when the max frame number is reached.
  73980. + */
  73981. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  73982. +{
  73983. + return (frame1 != frame2) &&
  73984. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  73985. + (DWC_HFNUM_MAX_FRNUM >> 1));
  73986. +}
  73987. +
  73988. +/**
  73989. + * Increments _frame by the amount specified by _inc. The addition is done
  73990. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  73991. + */
  73992. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  73993. +{
  73994. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  73995. +}
  73996. +
  73997. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  73998. +{
  73999. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  74000. +}
  74001. +
  74002. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  74003. +{
  74004. + return frame & 0x7;
  74005. +}
  74006. +
  74007. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  74008. + dwc_otg_hc_regs_t * hc_regs,
  74009. + dwc_otg_qtd_t * qtd);
  74010. +
  74011. +#ifdef DEBUG
  74012. +/**
  74013. + * Macro to sample the remaining PHY clocks left in the current frame. This
  74014. + * may be used during debugging to determine the average time it takes to
  74015. + * execute sections of code. There are two possible sample points, "a" and
  74016. + * "b", so the _letter argument must be one of these values.
  74017. + *
  74018. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  74019. + * example, "cat /sys/devices/lm0/hcd_frrem".
  74020. + */
  74021. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  74022. +{ \
  74023. + hfnum_data_t hfnum; \
  74024. + dwc_otg_qtd_t *qtd; \
  74025. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  74026. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  74027. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  74028. + switch (hfnum.b.frnum & 0x7) { \
  74029. + case 7: \
  74030. + _hcd->hfnum_7_samples_##_letter++; \
  74031. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  74032. + break; \
  74033. + case 0: \
  74034. + _hcd->hfnum_0_samples_##_letter++; \
  74035. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  74036. + break; \
  74037. + default: \
  74038. + _hcd->hfnum_other_samples_##_letter++; \
  74039. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  74040. + break; \
  74041. + } \
  74042. + } \
  74043. +}
  74044. +#else
  74045. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  74046. +#endif
  74047. +#endif
  74048. +#endif /* DWC_DEVICE_ONLY */
  74049. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  74050. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 1970-01-01 01:00:00.000000000 +0100
  74051. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 2014-04-24 15:35:04.173565776 +0200
  74052. @@ -0,0 +1,417 @@
  74053. +/* ==========================================================================
  74054. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  74055. + * $Revision: #12 $
  74056. + * $Date: 2011/10/26 $
  74057. + * $Change: 1873028 $
  74058. + *
  74059. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74060. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74061. + * otherwise expressly agreed to in writing between Synopsys and you.
  74062. + *
  74063. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74064. + * any End User Software License Agreement or Agreement for Licensed Product
  74065. + * with Synopsys or any supplement thereto. You are permitted to use and
  74066. + * redistribute this Software in source and binary forms, with or without
  74067. + * modification, provided that redistributions of source code must retain this
  74068. + * notice. You may not view, use, disclose, copy or distribute this file or
  74069. + * any information contained herein except pursuant to this license grant from
  74070. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74071. + * below, then you are not authorized to use the Software.
  74072. + *
  74073. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74074. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74075. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74076. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74077. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74078. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74079. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74080. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74081. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74082. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74083. + * DAMAGE.
  74084. + * ========================================================================== */
  74085. +#ifndef DWC_DEVICE_ONLY
  74086. +#ifndef __DWC_HCD_IF_H__
  74087. +#define __DWC_HCD_IF_H__
  74088. +
  74089. +#include "dwc_otg_core_if.h"
  74090. +
  74091. +/** @file
  74092. + * This file defines DWC_OTG HCD Core API.
  74093. + */
  74094. +
  74095. +struct dwc_otg_hcd;
  74096. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  74097. +
  74098. +struct dwc_otg_hcd_urb;
  74099. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  74100. +
  74101. +/** @name HCD Function Driver Callbacks */
  74102. +/** @{ */
  74103. +
  74104. +/** This function is called whenever core switches to host mode. */
  74105. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  74106. +
  74107. +/** This function is called when device has been disconnected */
  74108. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  74109. +
  74110. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  74111. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  74112. + void *urb_handle,
  74113. + uint32_t * hub_addr,
  74114. + uint32_t * port_addr);
  74115. +/** Via this function HCD core gets device speed */
  74116. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  74117. + void *urb_handle);
  74118. +
  74119. +/** This function is called when urb is completed */
  74120. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  74121. + void *urb_handle,
  74122. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  74123. + int32_t status);
  74124. +
  74125. +/** Via this function HCD core gets b_hnp_enable parameter */
  74126. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  74127. +
  74128. +struct dwc_otg_hcd_function_ops {
  74129. + dwc_otg_hcd_start_cb_t start;
  74130. + dwc_otg_hcd_disconnect_cb_t disconnect;
  74131. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  74132. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  74133. + dwc_otg_hcd_complete_urb_cb_t complete;
  74134. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  74135. +};
  74136. +/** @} */
  74137. +
  74138. +/** @name HCD Core API */
  74139. +/** @{ */
  74140. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  74141. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  74142. +
  74143. +/** This function should be called to initiate HCD Core.
  74144. + *
  74145. + * @param hcd The HCD
  74146. + * @param core_if The DWC_OTG Core
  74147. + *
  74148. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  74149. + * Returns 0 on success
  74150. + */
  74151. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  74152. +
  74153. +/** Frees HCD
  74154. + *
  74155. + * @param hcd The HCD
  74156. + */
  74157. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  74158. +
  74159. +/** This function should be called on every hardware interrupt.
  74160. + *
  74161. + * @param dwc_otg_hcd The HCD
  74162. + *
  74163. + * Returns non zero if interrupt is handled
  74164. + * Return 0 if interrupt is not handled
  74165. + */
  74166. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  74167. +
  74168. +/** This function is used to handle the fast interrupt
  74169. + *
  74170. + */
  74171. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  74172. +
  74173. +/**
  74174. + * Returns private data set by
  74175. + * dwc_otg_hcd_set_priv_data function.
  74176. + *
  74177. + * @param hcd The HCD
  74178. + */
  74179. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  74180. +
  74181. +/**
  74182. + * Set private data.
  74183. + *
  74184. + * @param hcd The HCD
  74185. + * @param priv_data pointer to be stored in private data
  74186. + */
  74187. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  74188. +
  74189. +/**
  74190. + * This function initializes the HCD Core.
  74191. + *
  74192. + * @param hcd The HCD
  74193. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  74194. + *
  74195. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  74196. + * Returns 0 on success
  74197. + */
  74198. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  74199. + struct dwc_otg_hcd_function_ops *fops);
  74200. +
  74201. +/**
  74202. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  74203. + * stopped.
  74204. + *
  74205. + * @param hcd The HCD
  74206. + */
  74207. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  74208. +
  74209. +/**
  74210. + * Handles hub class-specific requests.
  74211. + *
  74212. + * @param dwc_otg_hcd The HCD
  74213. + * @param typeReq Request Type
  74214. + * @param wValue wValue from control request
  74215. + * @param wIndex wIndex from control request
  74216. + * @param buf data buffer
  74217. + * @param wLength data buffer length
  74218. + *
  74219. + * Returns -DWC_E_INVALID if invalid argument is passed
  74220. + * Returns 0 on success
  74221. + */
  74222. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  74223. + uint16_t typeReq, uint16_t wValue,
  74224. + uint16_t wIndex, uint8_t * buf,
  74225. + uint16_t wLength);
  74226. +
  74227. +/**
  74228. + * Returns otg port number.
  74229. + *
  74230. + * @param hcd The HCD
  74231. + */
  74232. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  74233. +
  74234. +/**
  74235. + * Returns OTG version - either 1.3 or 2.0.
  74236. + *
  74237. + * @param core_if The core_if structure pointer
  74238. + */
  74239. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  74240. +
  74241. +/**
  74242. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  74243. + *
  74244. + * @param hcd The HCD
  74245. + */
  74246. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  74247. +
  74248. +/**
  74249. + * Returns current frame number.
  74250. + *
  74251. + * @param hcd The HCD
  74252. + */
  74253. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  74254. +
  74255. +/**
  74256. + * Dumps hcd state.
  74257. + *
  74258. + * @param hcd The HCD
  74259. + */
  74260. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  74261. +
  74262. +/**
  74263. + * Dump the average frame remaining at SOF. This can be used to
  74264. + * determine average interrupt latency. Frame remaining is also shown for
  74265. + * start transfer and two additional sample points.
  74266. + * Currently this function is not implemented.
  74267. + *
  74268. + * @param hcd The HCD
  74269. + */
  74270. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  74271. +
  74272. +/**
  74273. + * Sends LPM transaction to the local device.
  74274. + *
  74275. + * @param hcd The HCD
  74276. + * @param devaddr Device Address
  74277. + * @param hird Host initiated resume duration
  74278. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  74279. + *
  74280. + * Returns negative value if sending LPM transaction was not succeeded.
  74281. + * Returns 0 on success.
  74282. + */
  74283. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  74284. + uint8_t hird, uint8_t bRemoteWake);
  74285. +
  74286. +/* URB interface */
  74287. +
  74288. +/**
  74289. + * Allocates memory for dwc_otg_hcd_urb structure.
  74290. + * Allocated memory should be freed by call of DWC_FREE.
  74291. + *
  74292. + * @param hcd The HCD
  74293. + * @param iso_desc_count Count of ISOC descriptors
  74294. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  74295. + */
  74296. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  74297. + int iso_desc_count,
  74298. + int atomic_alloc);
  74299. +
  74300. +/**
  74301. + * Set pipe information in URB.
  74302. + *
  74303. + * @param hcd_urb DWC_OTG URB
  74304. + * @param devaddr Device Address
  74305. + * @param ep_num Endpoint Number
  74306. + * @param ep_type Endpoint Type
  74307. + * @param ep_dir Endpoint Direction
  74308. + * @param mps Max Packet Size
  74309. + */
  74310. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  74311. + uint8_t devaddr, uint8_t ep_num,
  74312. + uint8_t ep_type, uint8_t ep_dir,
  74313. + uint16_t mps);
  74314. +
  74315. +/* Transfer flags */
  74316. +#define URB_GIVEBACK_ASAP 0x1
  74317. +#define URB_SEND_ZERO_PACKET 0x2
  74318. +
  74319. +/**
  74320. + * Sets dwc_otg_hcd_urb parameters.
  74321. + *
  74322. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  74323. + * @param urb_handle Unique handle for request, this will be passed back
  74324. + * to function driver in completion callback.
  74325. + * @param buf The buffer for the data
  74326. + * @param dma The DMA buffer for the data
  74327. + * @param buflen Transfer length
  74328. + * @param sp Buffer for setup data
  74329. + * @param sp_dma DMA address of setup data buffer
  74330. + * @param flags Transfer flags
  74331. + * @param interval Polling interval for interrupt or isochronous transfers.
  74332. + */
  74333. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  74334. + void *urb_handle, void *buf,
  74335. + dwc_dma_t dma, uint32_t buflen, void *sp,
  74336. + dwc_dma_t sp_dma, uint32_t flags,
  74337. + uint16_t interval);
  74338. +
  74339. +/** Gets status from dwc_otg_hcd_urb
  74340. + *
  74341. + * @param dwc_otg_urb DWC_OTG URB
  74342. + */
  74343. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  74344. +
  74345. +/** Gets actual length from dwc_otg_hcd_urb
  74346. + *
  74347. + * @param dwc_otg_urb DWC_OTG URB
  74348. + */
  74349. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  74350. + dwc_otg_urb);
  74351. +
  74352. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  74353. + *
  74354. + * @param dwc_otg_urb DWC_OTG URB
  74355. + */
  74356. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  74357. + dwc_otg_urb);
  74358. +
  74359. +/** Set ISOC descriptor offset and length
  74360. + *
  74361. + * @param dwc_otg_urb DWC_OTG URB
  74362. + * @param desc_num ISOC descriptor number
  74363. + * @param offset Offset from beginig of buffer.
  74364. + * @param length Transaction length
  74365. + */
  74366. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  74367. + int desc_num, uint32_t offset,
  74368. + uint32_t length);
  74369. +
  74370. +/** Get status of ISOC descriptor, specified by desc_num
  74371. + *
  74372. + * @param dwc_otg_urb DWC_OTG URB
  74373. + * @param desc_num ISOC descriptor number
  74374. + */
  74375. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  74376. + dwc_otg_urb, int desc_num);
  74377. +
  74378. +/** Get actual length of ISOC descriptor, specified by desc_num
  74379. + *
  74380. + * @param dwc_otg_urb DWC_OTG URB
  74381. + * @param desc_num ISOC descriptor number
  74382. + */
  74383. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  74384. + dwc_otg_urb,
  74385. + int desc_num);
  74386. +
  74387. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  74388. + *
  74389. + * @param dwc_otg_hcd The HCD
  74390. + * @param dwc_otg_urb DWC_OTG URB
  74391. + * @param ep_handle Out parameter for returning endpoint handle
  74392. + * @param atomic_alloc Flag to do atomic allocation if needed
  74393. + *
  74394. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  74395. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  74396. + * Returns 0 on success.
  74397. + */
  74398. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  74399. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  74400. + void **ep_handle, int atomic_alloc);
  74401. +
  74402. +/** De-queue the specified URB
  74403. + *
  74404. + * @param dwc_otg_hcd The HCD
  74405. + * @param dwc_otg_urb DWC_OTG URB
  74406. + */
  74407. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  74408. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  74409. +
  74410. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  74411. + * Any URBs for the endpoint must already be dequeued.
  74412. + *
  74413. + * @param hcd The HCD
  74414. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  74415. + * @param retry Number of retries if there are queued transfers.
  74416. + *
  74417. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  74418. + * Returns 0 on success
  74419. + */
  74420. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  74421. + int retry);
  74422. +
  74423. +/* Resets the data toggle in qh structure. This function can be called from
  74424. + * usb_clear_halt routine.
  74425. + *
  74426. + * @param hcd The HCD
  74427. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  74428. + *
  74429. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  74430. + * Returns 0 on success
  74431. + */
  74432. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  74433. +
  74434. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  74435. + *
  74436. + * @param hcd The HCD
  74437. + * @param port Port number
  74438. + */
  74439. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  74440. +
  74441. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  74442. + * Only for ISOC and INTERRUPT endpoints.
  74443. + *
  74444. + * @param hcd The HCD
  74445. + * @param ep_handle Endpoint handle
  74446. + */
  74447. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  74448. + void *ep_handle);
  74449. +
  74450. +/** Call this function to check if bandwidth was freed for specified endpoint.
  74451. + *
  74452. + * @param hcd The HCD
  74453. + * @param ep_handle Endpoint handle
  74454. + */
  74455. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  74456. +
  74457. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  74458. + * Only for ISOC and INTERRUPT endpoints.
  74459. + *
  74460. + * @param hcd The HCD
  74461. + * @param ep_handle Endpoint handle
  74462. + */
  74463. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  74464. + void *ep_handle);
  74465. +
  74466. +/** @} */
  74467. +
  74468. +#endif /* __DWC_HCD_IF_H__ */
  74469. +#endif /* DWC_DEVICE_ONLY */
  74470. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  74471. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  74472. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 2014-04-24 15:35:04.177565820 +0200
  74473. @@ -0,0 +1,2741 @@
  74474. +/* ==========================================================================
  74475. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  74476. + * $Revision: #89 $
  74477. + * $Date: 2011/10/20 $
  74478. + * $Change: 1869487 $
  74479. + *
  74480. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74481. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74482. + * otherwise expressly agreed to in writing between Synopsys and you.
  74483. + *
  74484. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74485. + * any End User Software License Agreement or Agreement for Licensed Product
  74486. + * with Synopsys or any supplement thereto. You are permitted to use and
  74487. + * redistribute this Software in source and binary forms, with or without
  74488. + * modification, provided that redistributions of source code must retain this
  74489. + * notice. You may not view, use, disclose, copy or distribute this file or
  74490. + * any information contained herein except pursuant to this license grant from
  74491. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74492. + * below, then you are not authorized to use the Software.
  74493. + *
  74494. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74495. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74496. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74497. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74498. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74499. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74500. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74501. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74502. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74503. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74504. + * DAMAGE.
  74505. + * ========================================================================== */
  74506. +#ifndef DWC_DEVICE_ONLY
  74507. +
  74508. +#include "dwc_otg_hcd.h"
  74509. +#include "dwc_otg_regs.h"
  74510. +#include "dwc_otg_mphi_fix.h"
  74511. +
  74512. +#include <linux/jiffies.h>
  74513. +#include <mach/hardware.h>
  74514. +#include <asm/fiq.h>
  74515. +
  74516. +
  74517. +extern bool microframe_schedule;
  74518. +
  74519. +/** @file
  74520. + * This file contains the implementation of the HCD Interrupt handlers.
  74521. + */
  74522. +
  74523. +/*
  74524. + * Some globals to communicate between the FIQ and INTERRUPT
  74525. + */
  74526. +
  74527. +void * dummy_send;
  74528. +mphi_regs_t c_mphi_regs;
  74529. +volatile void *dwc_regs_base;
  74530. +int fiq_done, int_done;
  74531. +
  74532. +gintsts_data_t gintsts_saved = {.d32 = 0};
  74533. +hcint_data_t hcint_saved[MAX_EPS_CHANNELS];
  74534. +hcintmsk_data_t hcintmsk_saved[MAX_EPS_CHANNELS];
  74535. +int split_out_xfersize[MAX_EPS_CHANNELS];
  74536. +haint_data_t haint_saved;
  74537. +
  74538. +int g_next_sched_frame, g_np_count, g_np_sent;
  74539. +static int mphi_int_count = 0 ;
  74540. +
  74541. +hcchar_data_t nak_hcchar;
  74542. +hctsiz_data_t nak_hctsiz;
  74543. +hcsplt_data_t nak_hcsplt;
  74544. +int nak_count;
  74545. +
  74546. +int complete_sched[MAX_EPS_CHANNELS] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  74547. +int split_start_frame[MAX_EPS_CHANNELS];
  74548. +int queued_port[MAX_EPS_CHANNELS];
  74549. +
  74550. +#ifdef FIQ_DEBUG
  74551. +char buffer[1000*16];
  74552. +int wptr;
  74553. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  74554. +{
  74555. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  74556. + va_list args;
  74557. + char text[17];
  74558. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  74559. + unsigned long flags;
  74560. +
  74561. + local_irq_save(flags);
  74562. + local_fiq_disable();
  74563. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  74564. + {
  74565. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  74566. + va_start(args, fmt);
  74567. + vsnprintf(text+8, 9, fmt, args);
  74568. + va_end(args);
  74569. +
  74570. + memcpy(buffer + wptr, text, 16);
  74571. + wptr = (wptr + 16) % sizeof(buffer);
  74572. + }
  74573. + local_irq_restore(flags);
  74574. +}
  74575. +#endif
  74576. +
  74577. +void notrace fiq_queue_request(int channel, int odd_frame)
  74578. +{
  74579. + hcchar_data_t hcchar = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x0) };
  74580. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x4) };
  74581. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x10) };
  74582. +
  74583. + if(hcsplt.b.spltena == 0)
  74584. + {
  74585. + fiq_print(FIQDBG_ERR, "SPLTENA ");
  74586. + BUG();
  74587. + }
  74588. +
  74589. + if(hcchar.b.epdir == 1)
  74590. + {
  74591. + fiq_print(FIQDBG_SCHED, "IN Ch %d", channel);
  74592. + }
  74593. + else
  74594. + {
  74595. + hctsiz.b.xfersize = 0;
  74596. + fiq_print(FIQDBG_SCHED, "OUT Ch %d", channel);
  74597. + }
  74598. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x10), hctsiz.d32);
  74599. +
  74600. + hcsplt.b.compsplt = 1;
  74601. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x4), hcsplt.d32);
  74602. +
  74603. + // Send the Split complete
  74604. + hcchar.b.chen = 1;
  74605. + hcchar.b.oddfrm = odd_frame ? 1 : 0;
  74606. +
  74607. + // Post this for transmit on the next frame for periodic or this frame for non-periodic
  74608. + fiq_print(FIQDBG_SCHED, "SND_%s", odd_frame ? "ODD " : "EVEN");
  74609. +
  74610. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x0), hcchar.d32);
  74611. +}
  74612. +
  74613. +static int last_sof = -1;
  74614. +
  74615. +/*
  74616. +** Function to handle the start of frame interrupt, choose whether we need to do anything and
  74617. +** therefore trigger the main interrupt
  74618. +**
  74619. +** returns int != 0 - interrupt has been handled
  74620. +*/
  74621. +int diff;
  74622. +
  74623. +int notrace fiq_sof_handle(hfnum_data_t hfnum)
  74624. +{
  74625. + int handled = 0;
  74626. + int i;
  74627. +
  74628. + // Just check that once we're running we don't miss a SOF
  74629. + /*if(last_sof != -1 && (hfnum.b.frnum != ((last_sof + 1) & 0x3fff)))
  74630. + {
  74631. + fiq_print(FIQDBG_ERR, "LASTSOF ");
  74632. + fiq_print(FIQDBG_ERR, "%4d%d ", last_sof / 8, last_sof & 7);
  74633. + fiq_print(FIQDBG_ERR, "%4d%d ", hfnum.b.frnum / 8, hfnum.b.frnum & 7);
  74634. + BUG();
  74635. + }*/
  74636. +
  74637. + // Only start remembering the last sof when the interrupt has been
  74638. + // enabled (we don't check the mask to come in here...)
  74639. + if(last_sof != -1 || FIQ_READ(dwc_regs_base + 0x18) & (1<<3))
  74640. + last_sof = hfnum.b.frnum;
  74641. +
  74642. + for(i = 0; i < MAX_EPS_CHANNELS; i++)
  74643. + {
  74644. + if(complete_sched[i] != -1)
  74645. + {
  74646. + if(complete_sched[i] <= hfnum.b.frnum || (complete_sched[i] > 0x3f00 && hfnum.b.frnum < 0xf0))
  74647. + {
  74648. + fiq_queue_request(i, hfnum.b.frnum & 1);
  74649. + complete_sched[i] = -1;
  74650. + }
  74651. + }
  74652. +
  74653. + if(complete_sched[i] != -1)
  74654. + {
  74655. + // This is because we've seen a split complete occur with no start...
  74656. + // most likely because missed the complete 0x3fff frames ago!
  74657. +
  74658. + diff = (hfnum.b.frnum + 0x3fff - complete_sched[i]) & 0x3fff ;
  74659. + if(diff > 32 && diff < 0x3f00)
  74660. + {
  74661. + fiq_print(FIQDBG_ERR, "SPLTMISS");
  74662. + BUG();
  74663. + }
  74664. + }
  74665. + }
  74666. +
  74667. + if(g_np_count == g_np_sent && dwc_frame_num_gt(g_next_sched_frame, hfnum.b.frnum))
  74668. + {
  74669. + /*
  74670. + * If np_count != np_sent that means we need to queue non-periodic (bulk) packets this packet
  74671. + * g_next_sched_frame is the next frame we have periodic packets for
  74672. + *
  74673. + * if neither of these are required for this frame then just clear the interrupt
  74674. + */
  74675. + handled = 1;
  74676. +
  74677. + }
  74678. +
  74679. + return handled;
  74680. +}
  74681. +
  74682. +int notrace port_id(hcsplt_data_t hcsplt)
  74683. +{
  74684. + return hcsplt.b.prtaddr + (hcsplt.b.hubaddr << 8);
  74685. +}
  74686. +
  74687. +int notrace fiq_hcintr_handle(int channel, hfnum_data_t hfnum)
  74688. +{
  74689. + hcchar_data_t hcchar = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x0) };
  74690. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x4) };
  74691. + hcint_data_t hcint = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x8) };
  74692. + hcintmsk_data_t hcintmsk = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0xc) };
  74693. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x10)};
  74694. +
  74695. + hcint_saved[channel].d32 |= hcint.d32;
  74696. + hcintmsk_saved[channel].d32 = hcintmsk.d32;
  74697. +
  74698. + if(hcsplt.b.spltena)
  74699. + {
  74700. + fiq_print(FIQDBG_PORTHUB, "ph: %4x", port_id(hcsplt));
  74701. + if(hcint.b.chhltd)
  74702. + {
  74703. + fiq_print(FIQDBG_SCHED, "CH HLT %d", channel);
  74704. + fiq_print(FIQDBG_SCHED, "%08x", hcint_saved[channel]);
  74705. + }
  74706. + if(hcint.b.stall || hcint.b.xacterr || hcint.b.bblerr || hcint.b.frmovrun || hcint.b.datatglerr)
  74707. + {
  74708. + queued_port[channel] = 0;
  74709. + fiq_print(FIQDBG_ERR, "CHAN ERR");
  74710. + }
  74711. + if(hcint.b.xfercomp)
  74712. + {
  74713. + // Clear the port allocation and transmit anything also on this port
  74714. + queued_port[channel] = 0;
  74715. + fiq_print(FIQDBG_SCHED, "XFERCOMP");
  74716. + }
  74717. + if(hcint.b.nak)
  74718. + {
  74719. + queued_port[channel] = 0;
  74720. + fiq_print(FIQDBG_SCHED, "NAK");
  74721. + }
  74722. + if(hcint.b.ack && !hcsplt.b.compsplt)
  74723. + {
  74724. + int i;
  74725. +
  74726. + // Do not complete isochronous out transactions
  74727. + if(hcchar.b.eptype == 1 && hcchar.b.epdir == 0)
  74728. + {
  74729. + queued_port[channel] = 0;
  74730. + fiq_print(FIQDBG_SCHED, "ISOC_OUT");
  74731. + }
  74732. + else
  74733. + {
  74734. + // Make sure we check the port / hub combination that we sent this split on.
  74735. + // Do not queue a second request to the same port
  74736. + for(i = 0; i < MAX_EPS_CHANNELS; i++)
  74737. + {
  74738. + if(port_id(hcsplt) == queued_port[i])
  74739. + {
  74740. + fiq_print(FIQDBG_ERR, "PORTERR ");
  74741. + //BUG();
  74742. + }
  74743. + }
  74744. +
  74745. + split_start_frame[channel] = (hfnum.b.frnum + 1) & ~7;
  74746. +
  74747. + // Note, the size of an OUT is in the start split phase, not
  74748. + // the complete split
  74749. + split_out_xfersize[channel] = hctsiz.b.xfersize;
  74750. +
  74751. + hcint_saved[channel].b.chhltd = 0;
  74752. + hcint_saved[channel].b.ack = 0;
  74753. +
  74754. + queued_port[channel] = port_id(hcsplt);
  74755. +
  74756. + if(hcchar.b.eptype & 1)
  74757. + {
  74758. + // Send the periodic complete in the same oddness frame as the ACK went...
  74759. + fiq_queue_request(channel, !(hfnum.b.frnum & 1));
  74760. + // complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 1);
  74761. + }
  74762. + else
  74763. + {
  74764. + // Schedule the split complete to occur later
  74765. + complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 2);
  74766. + fiq_print(FIQDBG_SCHED, "ACK%04d%d", complete_sched[channel]/8, complete_sched[channel]%8);
  74767. + }
  74768. + }
  74769. + }
  74770. + if(hcint.b.nyet)
  74771. + {
  74772. + fiq_print(FIQDBG_ERR, "NYETERR1");
  74773. + //BUG();
  74774. + // Can transmit a split complete up to uframe .0 of the next frame
  74775. + if(hfnum.b.frnum <= dwc_frame_num_inc(split_start_frame[channel], 8))
  74776. + {
  74777. + // Send it next frame
  74778. + if(hcchar.b.eptype & 1) // type 1 & 3 are interrupt & isoc
  74779. + {
  74780. + fiq_print(FIQDBG_SCHED, "NYT:SEND");
  74781. + fiq_queue_request(channel, !(hfnum.b.frnum & 1));
  74782. + }
  74783. + else
  74784. + {
  74785. + // Schedule non-periodic access for next frame (the odd-even bit doesn't effect NP)
  74786. + complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 1);
  74787. + fiq_print(FIQDBG_SCHED, "NYT%04d%d", complete_sched[channel]/8, complete_sched[channel]%8);
  74788. + }
  74789. + hcint_saved[channel].b.chhltd = 0;
  74790. + hcint_saved[channel].b.nyet = 0;
  74791. + }
  74792. + else
  74793. + {
  74794. + queued_port[channel] = 0;
  74795. + fiq_print(FIQDBG_ERR, "NYETERR2");
  74796. + //BUG();
  74797. + }
  74798. + }
  74799. + }
  74800. + else
  74801. + {
  74802. + /*
  74803. + * If we have any of NAK, ACK, Datatlgerr active on a
  74804. + * non-split channel, the sole reason is to reset error
  74805. + * counts for a previously broken transaction. The FIQ
  74806. + * will thrash on NAK IN and ACK OUT in particular so
  74807. + * handle it "once" and allow the IRQ to do the rest.
  74808. + */
  74809. + hcint.d32 &= hcintmsk.d32;
  74810. + if(hcint.b.nak)
  74811. + {
  74812. + hcintmsk.b.nak = 0;
  74813. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0xc), hcintmsk.d32);
  74814. + }
  74815. + if (hcint.b.ack)
  74816. + {
  74817. + hcintmsk.b.ack = 0;
  74818. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0xc), hcintmsk.d32);
  74819. + }
  74820. + }
  74821. +
  74822. + // Clear the interrupt, this will also clear the HAINT bit
  74823. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x8), hcint.d32);
  74824. + return hcint_saved[channel].d32 == 0;
  74825. +}
  74826. +
  74827. +gintsts_data_t gintsts;
  74828. +gintmsk_data_t gintmsk;
  74829. +// triggered: The set of interrupts that were triggered
  74830. +// handled: The set of interrupts that have been handled (no IRQ is
  74831. +// required)
  74832. +// keep: The set of interrupts we want to keep unmasked even though we
  74833. +// want to trigger an IRQ to handle it (SOF and HCINTR)
  74834. +gintsts_data_t triggered, handled, keep;
  74835. +hfnum_data_t hfnum;
  74836. +
  74837. +void __attribute__ ((naked)) notrace dwc_otg_hcd_handle_fiq(void)
  74838. +{
  74839. +
  74840. + /* entry takes care to store registers we will be treading on here */
  74841. + asm __volatile__ (
  74842. + "mov ip, sp ;"
  74843. + /* stash FIQ and normal regs */
  74844. + "stmdb sp!, {r0-r12, lr};"
  74845. + /* !! THIS SETS THE FRAME, adjust to > sizeof locals */
  74846. + "sub fp, ip, #512 ;"
  74847. + );
  74848. +
  74849. + // Cannot put local variables at the beginning of the function
  74850. + // because otherwise 'C' will play with the stack pointer. any locals
  74851. + // need to be inside the following block
  74852. + do
  74853. + {
  74854. + fiq_done++;
  74855. + gintsts.d32 = FIQ_READ(dwc_regs_base + 0x14);
  74856. + gintmsk.d32 = FIQ_READ(dwc_regs_base + 0x18);
  74857. + hfnum.d32 = FIQ_READ(dwc_regs_base + 0x408);
  74858. + triggered.d32 = gintsts.d32 & gintmsk.d32;
  74859. + handled.d32 = 0;
  74860. + keep.d32 = 0;
  74861. + fiq_print(FIQDBG_INT, "FIQ ");
  74862. + fiq_print(FIQDBG_INT, "%08x", gintsts.d32);
  74863. + fiq_print(FIQDBG_INT, "%08x", gintmsk.d32);
  74864. + if(gintsts.d32)
  74865. + {
  74866. + // If port enabled
  74867. + if((FIQ_READ(dwc_regs_base + 0x440) & 0xf) == 0x5)
  74868. + {
  74869. + if(gintsts.b.sofintr)
  74870. + {
  74871. + if(fiq_sof_handle(hfnum))
  74872. + {
  74873. + handled.b.sofintr = 1; /* Handled in FIQ */
  74874. + }
  74875. + else
  74876. + {
  74877. + /* Keer interrupt unmasked */
  74878. + keep.b.sofintr = 1;
  74879. + }
  74880. + {
  74881. + // Need to make sure the read and clearing of the SOF interrupt is as close as possible to avoid the possibility of missing
  74882. + // a start of frame interrupt
  74883. + gintsts_data_t gintsts = { .b.sofintr = 1 };
  74884. + FIQ_WRITE((dwc_regs_base + 0x14), gintsts.d32);
  74885. + }
  74886. + }
  74887. +
  74888. + if(fiq_split_enable && gintsts.b.hcintr)
  74889. + {
  74890. + int i;
  74891. + haint_data_t haint;
  74892. + haintmsk_data_t haintmsk;
  74893. +
  74894. + haint.d32 = FIQ_READ(dwc_regs_base + 0x414);
  74895. + haintmsk.d32 = FIQ_READ(dwc_regs_base + 0x418);
  74896. + haint.d32 &= haintmsk.d32;
  74897. + haint_saved.d32 |= haint.d32;
  74898. +
  74899. + fiq_print(FIQDBG_INT, "hcintr");
  74900. + fiq_print(FIQDBG_INT, "%08x", FIQ_READ(dwc_regs_base + 0x414));
  74901. +
  74902. + // Go through each channel that has an enabled interrupt
  74903. + for(i = 0; i < 16; i++)
  74904. + if((haint.d32 >> i) & 1)
  74905. + if(fiq_hcintr_handle(i, hfnum))
  74906. + haint_saved.d32 &= ~(1 << i); /* this was handled */
  74907. +
  74908. + /* If we've handled all host channel interrupts then don't trigger the interrupt */
  74909. + if(haint_saved.d32 == 0)
  74910. + {
  74911. + handled.b.hcintr = 1;
  74912. + }
  74913. + else
  74914. + {
  74915. + /* Make sure we keep the channel interrupt unmasked when triggering the IRQ */
  74916. + keep.b.hcintr = 1;
  74917. + }
  74918. +
  74919. + {
  74920. + gintsts_data_t gintsts = { .b.hcintr = 1 };
  74921. +
  74922. + // Always clear the channel interrupt
  74923. + FIQ_WRITE((dwc_regs_base + 0x14), gintsts.d32);
  74924. + }
  74925. + }
  74926. + }
  74927. + else
  74928. + {
  74929. + last_sof = -1;
  74930. + }
  74931. + }
  74932. +
  74933. + // Mask out the interrupts triggered - those handled - don't mask out the ones we want to keep
  74934. + gintmsk.d32 = keep.d32 | (gintmsk.d32 & ~(triggered.d32 & ~handled.d32));
  74935. + // Save those that were triggered but not handled
  74936. + gintsts_saved.d32 |= triggered.d32 & ~handled.d32;
  74937. + FIQ_WRITE(dwc_regs_base + 0x18, gintmsk.d32);
  74938. +
  74939. + // Clear and save any unhandled interrupts and trigger the interrupt
  74940. + if(gintsts_saved.d32)
  74941. + {
  74942. + /* To enable the MPHI interrupt (INT 32)
  74943. + */
  74944. + FIQ_WRITE( c_mphi_regs.outdda, (int) dummy_send);
  74945. + FIQ_WRITE( c_mphi_regs.outddb, (1 << 29));
  74946. +
  74947. + mphi_int_count++;
  74948. + }
  74949. + }
  74950. + while(0);
  74951. +
  74952. + mb();
  74953. +
  74954. + /* exit back to normal mode restoring everything */
  74955. + asm __volatile__ (
  74956. + /* return FIQ regs back to pristine state
  74957. + * and get normal regs back
  74958. + */
  74959. + "ldmia sp!, {r0-r12, lr};"
  74960. +
  74961. + /* return */
  74962. + "subs pc, lr, #4;"
  74963. + );
  74964. +}
  74965. +
  74966. +/** This function handles interrupts for the HCD. */
  74967. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74968. +{
  74969. + int retval = 0;
  74970. + static int last_time;
  74971. +
  74972. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  74973. + gintsts_data_t gintsts;
  74974. + gintmsk_data_t gintmsk;
  74975. + hfnum_data_t hfnum;
  74976. +
  74977. +#ifdef DEBUG
  74978. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  74979. +
  74980. +#endif
  74981. +
  74982. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  74983. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  74984. +
  74985. + /* Exit from ISR if core is hibernated */
  74986. + if (core_if->hibernation_suspend == 1) {
  74987. + goto exit_handler_routine;
  74988. + }
  74989. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  74990. + /* Check if HOST Mode */
  74991. + if (dwc_otg_is_host_mode(core_if)) {
  74992. + local_fiq_disable();
  74993. + gintmsk.d32 |= gintsts_saved.d32;
  74994. + gintsts.d32 |= gintsts_saved.d32;
  74995. + gintsts_saved.d32 = 0;
  74996. + local_fiq_enable();
  74997. + if (!gintsts.d32) {
  74998. + goto exit_handler_routine;
  74999. + }
  75000. + gintsts.d32 &= gintmsk.d32;
  75001. +
  75002. +#ifdef DEBUG
  75003. + // We should be OK doing this because the common interrupts should already have been serviced
  75004. + /* Don't print debug message in the interrupt handler on SOF */
  75005. +#ifndef DEBUG_SOF
  75006. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  75007. +#endif
  75008. + DWC_DEBUGPL(DBG_HCDI, "\n");
  75009. +#endif
  75010. +
  75011. +#ifdef DEBUG
  75012. +#ifndef DEBUG_SOF
  75013. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  75014. +#endif
  75015. + DWC_DEBUGPL(DBG_HCDI,
  75016. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  75017. + gintsts.d32, core_if);
  75018. +#endif
  75019. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  75020. + if (gintsts.b.sofintr && g_np_count == g_np_sent && dwc_frame_num_gt(g_next_sched_frame, hfnum.b.frnum))
  75021. + {
  75022. + /* Note, we should never get here if the FIQ is doing it's job properly*/
  75023. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  75024. + }
  75025. + else if (gintsts.b.sofintr) {
  75026. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  75027. + }
  75028. +
  75029. + if (gintsts.b.rxstsqlvl) {
  75030. + retval |=
  75031. + dwc_otg_hcd_handle_rx_status_q_level_intr
  75032. + (dwc_otg_hcd);
  75033. + }
  75034. + if (gintsts.b.nptxfempty) {
  75035. + retval |=
  75036. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  75037. + (dwc_otg_hcd);
  75038. + }
  75039. + if (gintsts.b.i2cintr) {
  75040. + /** @todo Implement i2cintr handler. */
  75041. + }
  75042. + if (gintsts.b.portintr) {
  75043. +
  75044. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  75045. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  75046. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  75047. + }
  75048. + if (gintsts.b.hcintr) {
  75049. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  75050. + }
  75051. + if (gintsts.b.ptxfempty) {
  75052. + retval |=
  75053. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  75054. + (dwc_otg_hcd);
  75055. + }
  75056. +#ifdef DEBUG
  75057. +#ifndef DEBUG_SOF
  75058. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  75059. +#endif
  75060. + {
  75061. + DWC_DEBUGPL(DBG_HCDI,
  75062. + "DWC OTG HCD Finished Servicing Interrupts\n");
  75063. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  75064. + DWC_READ_REG32(&global_regs->gintsts));
  75065. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  75066. + DWC_READ_REG32(&global_regs->gintmsk));
  75067. + }
  75068. +#endif
  75069. +
  75070. +#ifdef DEBUG
  75071. +#ifndef DEBUG_SOF
  75072. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  75073. +#endif
  75074. + DWC_DEBUGPL(DBG_HCDI, "\n");
  75075. +#endif
  75076. +
  75077. + }
  75078. +
  75079. +exit_handler_routine:
  75080. +
  75081. + if (fiq_fix_enable)
  75082. + {
  75083. + local_fiq_disable();
  75084. + // Make sure that we don't clear the interrupt if we've still got pending work to do
  75085. + if(gintsts_saved.d32 == 0)
  75086. + {
  75087. + /* Clear the MPHI interrupt */
  75088. + DWC_WRITE_REG32(c_mphi_regs.intstat, (1<<16));
  75089. + if (mphi_int_count >= 60)
  75090. + {
  75091. + DWC_WRITE_REG32(c_mphi_regs.ctrl, ((1<<31) + (1<<16)));
  75092. + while(!(DWC_READ_REG32(c_mphi_regs.ctrl) & (1 << 17)))
  75093. + ;
  75094. + DWC_WRITE_REG32(c_mphi_regs.ctrl, (1<<31));
  75095. + mphi_int_count = 0;
  75096. + }
  75097. + int_done++;
  75098. + }
  75099. +
  75100. + // Unmask handled interrupts
  75101. + FIQ_WRITE(dwc_regs_base + 0x18, gintmsk.d32);
  75102. + //DWC_MODIFY_REG32((uint32_t *)IO_ADDRESS(USB_BASE + 0x8), 0 , 1);
  75103. +
  75104. + local_fiq_enable();
  75105. +
  75106. + if((jiffies / HZ) > last_time)
  75107. + {
  75108. + /* Once a second output the fiq and irq numbers, useful for debug */
  75109. + last_time = jiffies / HZ;
  75110. + DWC_DEBUGPL(DBG_USER, "int_done = %d fiq_done = %d\n", int_done, fiq_done);
  75111. + }
  75112. + }
  75113. +
  75114. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  75115. + return retval;
  75116. +}
  75117. +
  75118. +#ifdef DWC_TRACK_MISSED_SOFS
  75119. +
  75120. +#warning Compiling code to track missed SOFs
  75121. +#define FRAME_NUM_ARRAY_SIZE 1000
  75122. +/**
  75123. + * This function is for debug only.
  75124. + */
  75125. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  75126. +{
  75127. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  75128. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  75129. + static int frame_num_idx = 0;
  75130. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  75131. + static int dumped_frame_num_array = 0;
  75132. +
  75133. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  75134. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  75135. + curr_frame_number) {
  75136. + frame_num_array[frame_num_idx] = curr_frame_number;
  75137. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  75138. + }
  75139. + } else if (!dumped_frame_num_array) {
  75140. + int i;
  75141. + DWC_PRINTF("Frame Last Frame\n");
  75142. + DWC_PRINTF("----- ----------\n");
  75143. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  75144. + DWC_PRINTF("0x%04x 0x%04x\n",
  75145. + frame_num_array[i], last_frame_num_array[i]);
  75146. + }
  75147. + dumped_frame_num_array = 1;
  75148. + }
  75149. + last_frame_num = curr_frame_number;
  75150. +}
  75151. +#endif
  75152. +
  75153. +/**
  75154. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  75155. + * transactions may be queued to the DWC_otg controller for the current
  75156. + * (micro)frame. Periodic transactions may be queued to the controller for the
  75157. + * next (micro)frame.
  75158. + */
  75159. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  75160. +{
  75161. + hfnum_data_t hfnum;
  75162. + dwc_list_link_t *qh_entry;
  75163. + dwc_otg_qh_t *qh;
  75164. + dwc_otg_transaction_type_e tr_type;
  75165. + int did_something = 0;
  75166. + int32_t next_sched_frame = -1;
  75167. +
  75168. + hfnum.d32 =
  75169. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  75170. +
  75171. +#ifdef DEBUG_SOF
  75172. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  75173. +#endif
  75174. + hcd->frame_number = hfnum.b.frnum;
  75175. +
  75176. +#ifdef DEBUG
  75177. + hcd->frrem_accum += hfnum.b.frrem;
  75178. + hcd->frrem_samples++;
  75179. +#endif
  75180. +
  75181. +#ifdef DWC_TRACK_MISSED_SOFS
  75182. + track_missed_sofs(hcd->frame_number);
  75183. +#endif
  75184. + /* Determine whether any periodic QHs should be executed. */
  75185. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  75186. + while (qh_entry != &hcd->periodic_sched_inactive) {
  75187. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  75188. + qh_entry = qh_entry->next;
  75189. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  75190. +
  75191. + /*
  75192. + * Move QH to the ready list to be executed next
  75193. + * (micro)frame.
  75194. + */
  75195. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  75196. + &qh->qh_list_entry);
  75197. +
  75198. + did_something = 1;
  75199. + }
  75200. + else
  75201. + {
  75202. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  75203. + {
  75204. + next_sched_frame = qh->sched_frame;
  75205. + }
  75206. + }
  75207. + }
  75208. +
  75209. + g_next_sched_frame = next_sched_frame;
  75210. +
  75211. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  75212. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  75213. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  75214. + did_something = 1;
  75215. + }
  75216. +
  75217. + /* Clear interrupt */
  75218. + gintsts.b.sofintr = 1;
  75219. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  75220. +
  75221. + return 1;
  75222. +}
  75223. +
  75224. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  75225. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  75226. + * memory if the DWC_otg controller is operating in Slave mode. */
  75227. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75228. +{
  75229. + host_grxsts_data_t grxsts;
  75230. + dwc_hc_t *hc = NULL;
  75231. +
  75232. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  75233. +
  75234. + grxsts.d32 =
  75235. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  75236. +
  75237. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  75238. + if (!hc) {
  75239. + DWC_ERROR("Unable to get corresponding channel\n");
  75240. + return 0;
  75241. + }
  75242. +
  75243. + /* Packet Status */
  75244. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  75245. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  75246. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  75247. + hc->data_pid_start);
  75248. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  75249. +
  75250. + switch (grxsts.b.pktsts) {
  75251. + case DWC_GRXSTS_PKTSTS_IN:
  75252. + /* Read the data into the host buffer. */
  75253. + if (grxsts.b.bcnt > 0) {
  75254. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  75255. + hc->xfer_buff, grxsts.b.bcnt);
  75256. +
  75257. + /* Update the HC fields for the next packet received. */
  75258. + hc->xfer_count += grxsts.b.bcnt;
  75259. + hc->xfer_buff += grxsts.b.bcnt;
  75260. + }
  75261. +
  75262. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  75263. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  75264. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  75265. + /* Handled in interrupt, just ignore data */
  75266. + break;
  75267. + default:
  75268. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  75269. + grxsts.b.pktsts);
  75270. + break;
  75271. + }
  75272. +
  75273. + return 1;
  75274. +}
  75275. +
  75276. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  75277. + * data packets may be written to the FIFO for OUT transfers. More requests
  75278. + * may be written to the non-periodic request queue for IN transfers. This
  75279. + * interrupt is enabled only in Slave mode. */
  75280. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75281. +{
  75282. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  75283. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  75284. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  75285. + return 1;
  75286. +}
  75287. +
  75288. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  75289. + * packets may be written to the FIFO for OUT transfers. More requests may be
  75290. + * written to the periodic request queue for IN transfers. This interrupt is
  75291. + * enabled only in Slave mode. */
  75292. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75293. +{
  75294. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  75295. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  75296. + DWC_OTG_TRANSACTION_PERIODIC);
  75297. + return 1;
  75298. +}
  75299. +
  75300. +/** There are multiple conditions that can cause a port interrupt. This function
  75301. + * determines which interrupt conditions have occurred and handles them
  75302. + * appropriately. */
  75303. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75304. +{
  75305. + int retval = 0;
  75306. + hprt0_data_t hprt0;
  75307. + hprt0_data_t hprt0_modify;
  75308. +
  75309. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  75310. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  75311. +
  75312. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  75313. + * GINTSTS */
  75314. +
  75315. + hprt0_modify.b.prtena = 0;
  75316. + hprt0_modify.b.prtconndet = 0;
  75317. + hprt0_modify.b.prtenchng = 0;
  75318. + hprt0_modify.b.prtovrcurrchng = 0;
  75319. +
  75320. + /* Port Connect Detected
  75321. + * Set flag and clear if detected */
  75322. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  75323. + // Dont modify port status if we are in hibernation state
  75324. + hprt0_modify.b.prtconndet = 1;
  75325. + hprt0_modify.b.prtenchng = 1;
  75326. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  75327. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  75328. + return retval;
  75329. + }
  75330. +
  75331. + if (hprt0.b.prtconndet) {
  75332. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  75333. + if (dwc_otg_hcd->core_if->adp_enable &&
  75334. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  75335. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  75336. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  75337. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  75338. + /* TODO - check if this is required, as
  75339. + * host initialization was already performed
  75340. + * after initial ADP probing
  75341. + */
  75342. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  75343. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  75344. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  75345. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  75346. + } else {
  75347. +
  75348. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  75349. + "Port Connect Detected--\n", hprt0.d32);
  75350. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  75351. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  75352. + hprt0_modify.b.prtconndet = 1;
  75353. +
  75354. + /* B-Device has connected, Delete the connection timer. */
  75355. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  75356. + }
  75357. + /* The Hub driver asserts a reset when it sees port connect
  75358. + * status change flag */
  75359. + retval |= 1;
  75360. + }
  75361. +
  75362. + /* Port Enable Changed
  75363. + * Clear if detected - Set internal flag if disabled */
  75364. + if (hprt0.b.prtenchng) {
  75365. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  75366. + "Port Enable Changed--\n", hprt0.d32);
  75367. + hprt0_modify.b.prtenchng = 1;
  75368. + if (hprt0.b.prtena == 1) {
  75369. + hfir_data_t hfir;
  75370. + int do_reset = 0;
  75371. + dwc_otg_core_params_t *params =
  75372. + dwc_otg_hcd->core_if->core_params;
  75373. + dwc_otg_core_global_regs_t *global_regs =
  75374. + dwc_otg_hcd->core_if->core_global_regs;
  75375. + dwc_otg_host_if_t *host_if =
  75376. + dwc_otg_hcd->core_if->host_if;
  75377. +
  75378. + /* Every time when port enables calculate
  75379. + * HFIR.FrInterval
  75380. + */
  75381. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  75382. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  75383. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  75384. +
  75385. + /* Check if we need to adjust the PHY clock speed for
  75386. + * low power and adjust it */
  75387. + if (params->host_support_fs_ls_low_power) {
  75388. + gusbcfg_data_t usbcfg;
  75389. +
  75390. + usbcfg.d32 =
  75391. + DWC_READ_REG32(&global_regs->gusbcfg);
  75392. +
  75393. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  75394. + || hprt0.b.prtspd ==
  75395. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  75396. + /*
  75397. + * Low power
  75398. + */
  75399. + hcfg_data_t hcfg;
  75400. + if (usbcfg.b.phylpwrclksel == 0) {
  75401. + /* Set PHY low power clock select for FS/LS devices */
  75402. + usbcfg.b.phylpwrclksel = 1;
  75403. + DWC_WRITE_REG32
  75404. + (&global_regs->gusbcfg,
  75405. + usbcfg.d32);
  75406. + do_reset = 1;
  75407. + }
  75408. +
  75409. + hcfg.d32 =
  75410. + DWC_READ_REG32
  75411. + (&host_if->host_global_regs->hcfg);
  75412. +
  75413. + if (hprt0.b.prtspd ==
  75414. + DWC_HPRT0_PRTSPD_LOW_SPEED
  75415. + && params->host_ls_low_power_phy_clk
  75416. + ==
  75417. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  75418. + {
  75419. + /* 6 MHZ */
  75420. + DWC_DEBUGPL(DBG_CIL,
  75421. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  75422. + if (hcfg.b.fslspclksel !=
  75423. + DWC_HCFG_6_MHZ) {
  75424. + hcfg.b.fslspclksel =
  75425. + DWC_HCFG_6_MHZ;
  75426. + DWC_WRITE_REG32
  75427. + (&host_if->host_global_regs->hcfg,
  75428. + hcfg.d32);
  75429. + do_reset = 1;
  75430. + }
  75431. + } else {
  75432. + /* 48 MHZ */
  75433. + DWC_DEBUGPL(DBG_CIL,
  75434. + "FS_PHY programming HCFG to 48 MHz ()\n");
  75435. + if (hcfg.b.fslspclksel !=
  75436. + DWC_HCFG_48_MHZ) {
  75437. + hcfg.b.fslspclksel =
  75438. + DWC_HCFG_48_MHZ;
  75439. + DWC_WRITE_REG32
  75440. + (&host_if->host_global_regs->hcfg,
  75441. + hcfg.d32);
  75442. + do_reset = 1;
  75443. + }
  75444. + }
  75445. + } else {
  75446. + /*
  75447. + * Not low power
  75448. + */
  75449. + if (usbcfg.b.phylpwrclksel == 1) {
  75450. + usbcfg.b.phylpwrclksel = 0;
  75451. + DWC_WRITE_REG32
  75452. + (&global_regs->gusbcfg,
  75453. + usbcfg.d32);
  75454. + do_reset = 1;
  75455. + }
  75456. + }
  75457. +
  75458. + if (do_reset) {
  75459. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  75460. + }
  75461. + }
  75462. +
  75463. + if (!do_reset) {
  75464. + /* Port has been enabled set the reset change flag */
  75465. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  75466. + }
  75467. + } else {
  75468. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  75469. + }
  75470. + retval |= 1;
  75471. + }
  75472. +
  75473. + /** Overcurrent Change Interrupt */
  75474. + if (hprt0.b.prtovrcurrchng) {
  75475. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  75476. + "Port Overcurrent Changed--\n", hprt0.d32);
  75477. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  75478. + hprt0_modify.b.prtovrcurrchng = 1;
  75479. + retval |= 1;
  75480. + }
  75481. +
  75482. + /* Clear Port Interrupts */
  75483. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  75484. +
  75485. + return retval;
  75486. +}
  75487. +
  75488. +/** This interrupt indicates that one or more host channels has a pending
  75489. + * interrupt. There are multiple conditions that can cause each host channel
  75490. + * interrupt. This function determines which conditions have occurred for each
  75491. + * host channel interrupt and handles them appropriately. */
  75492. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  75493. +{
  75494. + int i;
  75495. + int retval = 0;
  75496. + haint_data_t haint;
  75497. +
  75498. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  75499. + * GINTSTS */
  75500. +
  75501. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  75502. +
  75503. + // Overwrite with saved interrupts from fiq handler
  75504. + if(fiq_split_enable)
  75505. + {
  75506. + local_fiq_disable();
  75507. + haint.d32 = haint_saved.d32;
  75508. + haint_saved.d32 = 0;
  75509. + local_fiq_enable();
  75510. + }
  75511. +
  75512. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  75513. + if (haint.b2.chint & (1 << i)) {
  75514. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  75515. + }
  75516. + }
  75517. +
  75518. + return retval;
  75519. +}
  75520. +
  75521. +/**
  75522. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  75523. + * holds the reason for the halt.
  75524. + *
  75525. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  75526. + * *short_read is set to 1 upon return if less than the requested
  75527. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  75528. + * return. short_read may also be NULL on entry, in which case it remains
  75529. + * unchanged.
  75530. + */
  75531. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  75532. + dwc_otg_hc_regs_t * hc_regs,
  75533. + dwc_otg_qtd_t * qtd,
  75534. + dwc_otg_halt_status_e halt_status,
  75535. + int *short_read)
  75536. +{
  75537. + hctsiz_data_t hctsiz;
  75538. + uint32_t length;
  75539. +
  75540. + if (short_read != NULL) {
  75541. + *short_read = 0;
  75542. + }
  75543. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75544. +
  75545. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  75546. + if (hc->ep_is_in) {
  75547. + length = hc->xfer_len - hctsiz.b.xfersize;
  75548. + if (short_read != NULL) {
  75549. + *short_read = (hctsiz.b.xfersize != 0);
  75550. + }
  75551. + } else if (hc->qh->do_split) {
  75552. + if(fiq_split_enable)
  75553. + length = split_out_xfersize[hc->hc_num];
  75554. + else
  75555. + length = qtd->ssplit_out_xfer_count;
  75556. + } else {
  75557. + length = hc->xfer_len;
  75558. + }
  75559. + } else {
  75560. + /*
  75561. + * Must use the hctsiz.pktcnt field to determine how much data
  75562. + * has been transferred. This field reflects the number of
  75563. + * packets that have been transferred via the USB. This is
  75564. + * always an integral number of packets if the transfer was
  75565. + * halted before its normal completion. (Can't use the
  75566. + * hctsiz.xfersize field because that reflects the number of
  75567. + * bytes transferred via the AHB, not the USB).
  75568. + */
  75569. + length =
  75570. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  75571. + }
  75572. +
  75573. + return length;
  75574. +}
  75575. +
  75576. +/**
  75577. + * Updates the state of the URB after a Transfer Complete interrupt on the
  75578. + * host channel. Updates the actual_length field of the URB based on the
  75579. + * number of bytes transferred via the host channel. Sets the URB status
  75580. + * if the data transfer is finished.
  75581. + *
  75582. + * @return 1 if the data transfer specified by the URB is completely finished,
  75583. + * 0 otherwise.
  75584. + */
  75585. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  75586. + dwc_otg_hc_regs_t * hc_regs,
  75587. + dwc_otg_hcd_urb_t * urb,
  75588. + dwc_otg_qtd_t * qtd)
  75589. +{
  75590. + int xfer_done = 0;
  75591. + int short_read = 0;
  75592. +
  75593. + int xfer_length;
  75594. +
  75595. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  75596. + DWC_OTG_HC_XFER_COMPLETE,
  75597. + &short_read);
  75598. +
  75599. + /* non DWORD-aligned buffer case handling. */
  75600. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  75601. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  75602. + xfer_length);
  75603. + }
  75604. +
  75605. + urb->actual_length += xfer_length;
  75606. +
  75607. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  75608. + (urb->flags & URB_SEND_ZERO_PACKET)
  75609. + && (urb->actual_length == urb->length)
  75610. + && !(urb->length % hc->max_packet)) {
  75611. + xfer_done = 0;
  75612. + } else if (short_read || urb->actual_length >= urb->length) {
  75613. + xfer_done = 1;
  75614. + urb->status = 0;
  75615. + }
  75616. +
  75617. +#ifdef DEBUG
  75618. + {
  75619. + hctsiz_data_t hctsiz;
  75620. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75621. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  75622. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  75623. + hc->hc_num);
  75624. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  75625. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  75626. + hctsiz.b.xfersize);
  75627. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  75628. + urb->length);
  75629. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  75630. + urb->actual_length);
  75631. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  75632. + short_read, xfer_done);
  75633. + }
  75634. +#endif
  75635. +
  75636. + return xfer_done;
  75637. +}
  75638. +
  75639. +/*
  75640. + * Save the starting data toggle for the next transfer. The data toggle is
  75641. + * saved in the QH for non-control transfers and it's saved in the QTD for
  75642. + * control transfers.
  75643. + */
  75644. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  75645. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  75646. +{
  75647. + hctsiz_data_t hctsiz;
  75648. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75649. +
  75650. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  75651. + dwc_otg_qh_t *qh = hc->qh;
  75652. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  75653. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  75654. + } else {
  75655. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  75656. + }
  75657. + } else {
  75658. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  75659. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  75660. + } else {
  75661. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  75662. + }
  75663. + }
  75664. +}
  75665. +
  75666. +/**
  75667. + * Updates the state of an Isochronous URB when the transfer is stopped for
  75668. + * any reason. The fields of the current entry in the frame descriptor array
  75669. + * are set based on the transfer state and the input _halt_status. Completes
  75670. + * the Isochronous URB if all the URB frames have been completed.
  75671. + *
  75672. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  75673. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  75674. + */
  75675. +static dwc_otg_halt_status_e
  75676. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  75677. + dwc_hc_t * hc,
  75678. + dwc_otg_hc_regs_t * hc_regs,
  75679. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  75680. +{
  75681. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  75682. + dwc_otg_halt_status_e ret_val = halt_status;
  75683. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  75684. +
  75685. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  75686. + switch (halt_status) {
  75687. + case DWC_OTG_HC_XFER_COMPLETE:
  75688. + frame_desc->status = 0;
  75689. + frame_desc->actual_length =
  75690. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  75691. +
  75692. + /* non DWORD-aligned buffer case handling. */
  75693. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  75694. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  75695. + hc->qh->dw_align_buf, frame_desc->actual_length);
  75696. + }
  75697. +
  75698. + break;
  75699. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  75700. + urb->error_count++;
  75701. + if (hc->ep_is_in) {
  75702. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  75703. + } else {
  75704. + frame_desc->status = -DWC_E_COMMUNICATION;
  75705. + }
  75706. + frame_desc->actual_length = 0;
  75707. + break;
  75708. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  75709. + urb->error_count++;
  75710. + frame_desc->status = -DWC_E_OVERFLOW;
  75711. + /* Don't need to update actual_length in this case. */
  75712. + break;
  75713. + case DWC_OTG_HC_XFER_XACT_ERR:
  75714. + urb->error_count++;
  75715. + frame_desc->status = -DWC_E_PROTOCOL;
  75716. + frame_desc->actual_length =
  75717. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  75718. +
  75719. + /* non DWORD-aligned buffer case handling. */
  75720. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  75721. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  75722. + hc->qh->dw_align_buf, frame_desc->actual_length);
  75723. + }
  75724. + /* Skip whole frame */
  75725. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  75726. + hc->ep_is_in && hcd->core_if->dma_enable) {
  75727. + qtd->complete_split = 0;
  75728. + qtd->isoc_split_offset = 0;
  75729. + }
  75730. +
  75731. + break;
  75732. + default:
  75733. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  75734. + break;
  75735. + }
  75736. + if (++qtd->isoc_frame_index == urb->packet_count) {
  75737. + /*
  75738. + * urb->status is not used for isoc transfers.
  75739. + * The individual frame_desc statuses are used instead.
  75740. + */
  75741. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  75742. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  75743. + } else {
  75744. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  75745. + }
  75746. + return ret_val;
  75747. +}
  75748. +
  75749. +/**
  75750. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  75751. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  75752. + * still linked to the QH, the QH is added to the end of the inactive
  75753. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  75754. + * schedule if no more QTDs are linked to the QH.
  75755. + */
  75756. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  75757. +{
  75758. + int continue_split = 0;
  75759. + dwc_otg_qtd_t *qtd;
  75760. +
  75761. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  75762. +
  75763. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  75764. +
  75765. + if (qtd->complete_split) {
  75766. + continue_split = 1;
  75767. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  75768. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  75769. + continue_split = 1;
  75770. + }
  75771. +
  75772. + if (free_qtd) {
  75773. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  75774. + continue_split = 0;
  75775. + }
  75776. +
  75777. + qh->channel = NULL;
  75778. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  75779. +}
  75780. +
  75781. +/**
  75782. + * Releases a host channel for use by other transfers. Attempts to select and
  75783. + * queue more transactions since at least one host channel is available.
  75784. + *
  75785. + * @param hcd The HCD state structure.
  75786. + * @param hc The host channel to release.
  75787. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  75788. + * if the transfer is complete or an error has occurred.
  75789. + * @param halt_status Reason the channel is being released. This status
  75790. + * determines the actions taken by this function.
  75791. + */
  75792. +static void release_channel(dwc_otg_hcd_t * hcd,
  75793. + dwc_hc_t * hc,
  75794. + dwc_otg_qtd_t * qtd,
  75795. + dwc_otg_halt_status_e halt_status)
  75796. +{
  75797. + dwc_otg_transaction_type_e tr_type;
  75798. + int free_qtd;
  75799. + dwc_irqflags_t flags;
  75800. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  75801. +#ifdef FIQ_DEBUG
  75802. + int endp = qtd->urb ? qtd->urb->pipe_info.ep_num : 0;
  75803. +#endif
  75804. + int hog_port = 0;
  75805. +
  75806. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  75807. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  75808. +
  75809. + if(fiq_split_enable && hc->do_split) {
  75810. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  75811. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  75812. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  75813. + hog_port = 1;
  75814. + }
  75815. + }
  75816. + }
  75817. +
  75818. + switch (halt_status) {
  75819. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  75820. + free_qtd = 1;
  75821. + break;
  75822. + case DWC_OTG_HC_XFER_AHB_ERR:
  75823. + case DWC_OTG_HC_XFER_STALL:
  75824. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  75825. + free_qtd = 1;
  75826. + break;
  75827. + case DWC_OTG_HC_XFER_XACT_ERR:
  75828. + if (qtd->error_count >= 3) {
  75829. + DWC_DEBUGPL(DBG_HCDV,
  75830. + " Complete URB with transaction error\n");
  75831. + free_qtd = 1;
  75832. + qtd->urb->status = -DWC_E_PROTOCOL;
  75833. + hcd->fops->complete(hcd, qtd->urb->priv,
  75834. + qtd->urb, -DWC_E_PROTOCOL);
  75835. + } else {
  75836. + free_qtd = 0;
  75837. + }
  75838. + break;
  75839. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  75840. + /*
  75841. + * The QTD has already been removed and the QH has been
  75842. + * deactivated. Don't want to do anything except release the
  75843. + * host channel and try to queue more transfers.
  75844. + */
  75845. + goto cleanup;
  75846. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  75847. + free_qtd = 0;
  75848. + break;
  75849. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  75850. + DWC_DEBUGPL(DBG_HCDV,
  75851. + " Complete URB with I/O error\n");
  75852. + free_qtd = 1;
  75853. + qtd->urb->status = -DWC_E_IO;
  75854. + hcd->fops->complete(hcd, qtd->urb->priv,
  75855. + qtd->urb, -DWC_E_IO);
  75856. + break;
  75857. + default:
  75858. + free_qtd = 0;
  75859. + break;
  75860. + }
  75861. +
  75862. + deactivate_qh(hcd, hc->qh, free_qtd);
  75863. +
  75864. +cleanup:
  75865. + /*
  75866. + * Release the host channel for use by other transfers. The cleanup
  75867. + * function clears the channel interrupt enables and conditions, so
  75868. + * there's no need to clear the Channel Halted interrupt separately.
  75869. + */
  75870. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  75871. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  75872. +
  75873. + if (!microframe_schedule) {
  75874. + switch (hc->ep_type) {
  75875. + case DWC_OTG_EP_TYPE_CONTROL:
  75876. + case DWC_OTG_EP_TYPE_BULK:
  75877. + hcd->non_periodic_channels--;
  75878. + break;
  75879. +
  75880. + default:
  75881. + /*
  75882. + * Don't release reservations for periodic channels here.
  75883. + * That's done when a periodic transfer is descheduled (i.e.
  75884. + * when the QH is removed from the periodic schedule).
  75885. + */
  75886. + break;
  75887. + }
  75888. + } else {
  75889. +
  75890. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  75891. + hcd->available_host_channels++;
  75892. + fiq_print(FIQDBG_PORTHUB, "AHC = %d ", hcd->available_host_channels);
  75893. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  75894. + }
  75895. +
  75896. + if(fiq_split_enable && hc->do_split)
  75897. + {
  75898. + if(!(hcd->hub_port[hc->hub_addr] & (1 << hc->port_addr)))
  75899. + {
  75900. + fiq_print(FIQDBG_ERR, "PRTNOTAL");
  75901. + //BUG();
  75902. + }
  75903. + if(!hog_port && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC ||
  75904. + hc->ep_type == DWC_OTG_EP_TYPE_INTR)) {
  75905. + hcd->hub_port[hc->hub_addr] &= ~(1 << hc->port_addr);
  75906. +#ifdef FIQ_DEBUG
  75907. + hcd->hub_port_alloc[hc->hub_addr * 16 + hc->port_addr] = -1;
  75908. +#endif
  75909. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:RR%d", hc->hub_addr, hc->port_addr, endp);
  75910. + }
  75911. + }
  75912. +
  75913. + /* Try to queue more transfers now that there's a free channel. */
  75914. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  75915. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  75916. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  75917. + }
  75918. +}
  75919. +
  75920. +/**
  75921. + * Halts a host channel. If the channel cannot be halted immediately because
  75922. + * the request queue is full, this function ensures that the FIFO empty
  75923. + * interrupt for the appropriate queue is enabled so that the halt request can
  75924. + * be queued when there is space in the request queue.
  75925. + *
  75926. + * This function may also be called in DMA mode. In that case, the channel is
  75927. + * simply released since the core always halts the channel automatically in
  75928. + * DMA mode.
  75929. + */
  75930. +static void halt_channel(dwc_otg_hcd_t * hcd,
  75931. + dwc_hc_t * hc,
  75932. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  75933. +{
  75934. + if (hcd->core_if->dma_enable) {
  75935. + release_channel(hcd, hc, qtd, halt_status);
  75936. + return;
  75937. + }
  75938. +
  75939. + /* Slave mode processing... */
  75940. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  75941. +
  75942. + if (hc->halt_on_queue) {
  75943. + gintmsk_data_t gintmsk = {.d32 = 0 };
  75944. + dwc_otg_core_global_regs_t *global_regs;
  75945. + global_regs = hcd->core_if->core_global_regs;
  75946. +
  75947. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  75948. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  75949. + /*
  75950. + * Make sure the Non-periodic Tx FIFO empty interrupt
  75951. + * is enabled so that the non-periodic schedule will
  75952. + * be processed.
  75953. + */
  75954. + gintmsk.b.nptxfempty = 1;
  75955. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  75956. + } else {
  75957. + /*
  75958. + * Move the QH from the periodic queued schedule to
  75959. + * the periodic assigned schedule. This allows the
  75960. + * halt to be queued when the periodic schedule is
  75961. + * processed.
  75962. + */
  75963. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  75964. + &hc->qh->qh_list_entry);
  75965. +
  75966. + /*
  75967. + * Make sure the Periodic Tx FIFO Empty interrupt is
  75968. + * enabled so that the periodic schedule will be
  75969. + * processed.
  75970. + */
  75971. + gintmsk.b.ptxfempty = 1;
  75972. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  75973. + }
  75974. + }
  75975. +}
  75976. +
  75977. +/**
  75978. + * Performs common cleanup for non-periodic transfers after a Transfer
  75979. + * Complete interrupt. This function should be called after any endpoint type
  75980. + * specific handling is finished to release the host channel.
  75981. + */
  75982. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  75983. + dwc_hc_t * hc,
  75984. + dwc_otg_hc_regs_t * hc_regs,
  75985. + dwc_otg_qtd_t * qtd,
  75986. + dwc_otg_halt_status_e halt_status)
  75987. +{
  75988. + hcint_data_t hcint;
  75989. +
  75990. + qtd->error_count = 0;
  75991. +
  75992. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  75993. + if (hcint.b.nyet) {
  75994. + /*
  75995. + * Got a NYET on the last transaction of the transfer. This
  75996. + * means that the endpoint should be in the PING state at the
  75997. + * beginning of the next transfer.
  75998. + */
  75999. + hc->qh->ping_state = 1;
  76000. + clear_hc_int(hc_regs, nyet);
  76001. + }
  76002. +
  76003. + /*
  76004. + * Always halt and release the host channel to make it available for
  76005. + * more transfers. There may still be more phases for a control
  76006. + * transfer or more data packets for a bulk transfer at this point,
  76007. + * but the host channel is still halted. A channel will be reassigned
  76008. + * to the transfer when the non-periodic schedule is processed after
  76009. + * the channel is released. This allows transactions to be queued
  76010. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  76011. + * Tx FIFO Empty interrupt if necessary.
  76012. + */
  76013. + if (hc->ep_is_in) {
  76014. + /*
  76015. + * IN transfers in Slave mode require an explicit disable to
  76016. + * halt the channel. (In DMA mode, this call simply releases
  76017. + * the channel.)
  76018. + */
  76019. + halt_channel(hcd, hc, qtd, halt_status);
  76020. + } else {
  76021. + /*
  76022. + * The channel is automatically disabled by the core for OUT
  76023. + * transfers in Slave mode.
  76024. + */
  76025. + release_channel(hcd, hc, qtd, halt_status);
  76026. + }
  76027. +}
  76028. +
  76029. +/**
  76030. + * Performs common cleanup for periodic transfers after a Transfer Complete
  76031. + * interrupt. This function should be called after any endpoint type specific
  76032. + * handling is finished to release the host channel.
  76033. + */
  76034. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  76035. + dwc_hc_t * hc,
  76036. + dwc_otg_hc_regs_t * hc_regs,
  76037. + dwc_otg_qtd_t * qtd,
  76038. + dwc_otg_halt_status_e halt_status)
  76039. +{
  76040. + hctsiz_data_t hctsiz;
  76041. + qtd->error_count = 0;
  76042. +
  76043. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76044. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  76045. + /* Core halts channel in these cases. */
  76046. + release_channel(hcd, hc, qtd, halt_status);
  76047. + } else {
  76048. + /* Flush any outstanding requests from the Tx queue. */
  76049. + halt_channel(hcd, hc, qtd, halt_status);
  76050. + }
  76051. +}
  76052. +
  76053. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  76054. + dwc_hc_t * hc,
  76055. + dwc_otg_hc_regs_t * hc_regs,
  76056. + dwc_otg_qtd_t * qtd)
  76057. +{
  76058. + uint32_t len;
  76059. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  76060. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  76061. +
  76062. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  76063. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  76064. +
  76065. + if (!len) {
  76066. + qtd->complete_split = 0;
  76067. + qtd->isoc_split_offset = 0;
  76068. + return 0;
  76069. + }
  76070. + frame_desc->actual_length += len;
  76071. +
  76072. + if (hc->align_buff && len)
  76073. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  76074. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  76075. + qtd->isoc_split_offset += len;
  76076. +
  76077. + if (frame_desc->length == frame_desc->actual_length) {
  76078. + frame_desc->status = 0;
  76079. + qtd->isoc_frame_index++;
  76080. + qtd->complete_split = 0;
  76081. + qtd->isoc_split_offset = 0;
  76082. + }
  76083. +
  76084. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  76085. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  76086. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  76087. + } else {
  76088. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  76089. + }
  76090. +
  76091. + return 1; /* Indicates that channel released */
  76092. +}
  76093. +
  76094. +/**
  76095. + * Handles a host channel Transfer Complete interrupt. This handler may be
  76096. + * called in either DMA mode or Slave mode.
  76097. + */
  76098. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  76099. + dwc_hc_t * hc,
  76100. + dwc_otg_hc_regs_t * hc_regs,
  76101. + dwc_otg_qtd_t * qtd)
  76102. +{
  76103. + int urb_xfer_done;
  76104. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76105. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  76106. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  76107. +
  76108. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76109. + "Transfer Complete--\n", hc->hc_num);
  76110. +
  76111. + if (hcd->core_if->dma_desc_enable) {
  76112. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  76113. + if (pipe_type == UE_ISOCHRONOUS) {
  76114. + /* Do not disable the interrupt, just clear it */
  76115. + clear_hc_int(hc_regs, xfercomp);
  76116. + return 1;
  76117. + }
  76118. + goto handle_xfercomp_done;
  76119. + }
  76120. +
  76121. + /*
  76122. + * Handle xfer complete on CSPLIT.
  76123. + */
  76124. +
  76125. + if (hc->qh->do_split) {
  76126. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  76127. + && hcd->core_if->dma_enable) {
  76128. + if (qtd->complete_split
  76129. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  76130. + qtd))
  76131. + goto handle_xfercomp_done;
  76132. + } else {
  76133. + qtd->complete_split = 0;
  76134. + }
  76135. + }
  76136. +
  76137. + /* Update the QTD and URB states. */
  76138. + switch (pipe_type) {
  76139. + case UE_CONTROL:
  76140. + switch (qtd->control_phase) {
  76141. + case DWC_OTG_CONTROL_SETUP:
  76142. + if (urb->length > 0) {
  76143. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  76144. + } else {
  76145. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  76146. + }
  76147. + DWC_DEBUGPL(DBG_HCDV,
  76148. + " Control setup transaction done\n");
  76149. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76150. + break;
  76151. + case DWC_OTG_CONTROL_DATA:{
  76152. + urb_xfer_done =
  76153. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  76154. + qtd);
  76155. + if (urb_xfer_done) {
  76156. + qtd->control_phase =
  76157. + DWC_OTG_CONTROL_STATUS;
  76158. + DWC_DEBUGPL(DBG_HCDV,
  76159. + " Control data transfer done\n");
  76160. + } else {
  76161. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76162. + }
  76163. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76164. + break;
  76165. + }
  76166. + case DWC_OTG_CONTROL_STATUS:
  76167. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  76168. + if (urb->status == -DWC_E_IN_PROGRESS) {
  76169. + urb->status = 0;
  76170. + }
  76171. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  76172. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  76173. + break;
  76174. + }
  76175. +
  76176. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76177. + break;
  76178. + case UE_BULK:
  76179. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  76180. + urb_xfer_done =
  76181. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  76182. + if (urb_xfer_done) {
  76183. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  76184. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  76185. + } else {
  76186. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76187. + }
  76188. +
  76189. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76190. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76191. + break;
  76192. + case UE_INTERRUPT:
  76193. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  76194. + urb_xfer_done =
  76195. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  76196. +
  76197. + /*
  76198. + * Interrupt URB is done on the first transfer complete
  76199. + * interrupt.
  76200. + */
  76201. + if (urb_xfer_done) {
  76202. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  76203. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  76204. + } else {
  76205. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  76206. + }
  76207. +
  76208. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76209. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76210. + break;
  76211. + case UE_ISOCHRONOUS:
  76212. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  76213. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  76214. + halt_status =
  76215. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76216. + DWC_OTG_HC_XFER_COMPLETE);
  76217. + }
  76218. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  76219. + break;
  76220. + }
  76221. +
  76222. +handle_xfercomp_done:
  76223. + disable_hc_int(hc_regs, xfercompl);
  76224. +
  76225. + return 1;
  76226. +}
  76227. +
  76228. +/**
  76229. + * Handles a host channel STALL interrupt. This handler may be called in
  76230. + * either DMA mode or Slave mode.
  76231. + */
  76232. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  76233. + dwc_hc_t * hc,
  76234. + dwc_otg_hc_regs_t * hc_regs,
  76235. + dwc_otg_qtd_t * qtd)
  76236. +{
  76237. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  76238. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  76239. +
  76240. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  76241. + "STALL Received--\n", hc->hc_num);
  76242. +
  76243. + if (hcd->core_if->dma_desc_enable) {
  76244. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  76245. + goto handle_stall_done;
  76246. + }
  76247. +
  76248. + if (pipe_type == UE_CONTROL) {
  76249. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  76250. + }
  76251. +
  76252. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  76253. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  76254. + /*
  76255. + * USB protocol requires resetting the data toggle for bulk
  76256. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  76257. + * setup command is issued to the endpoint. Anticipate the
  76258. + * CLEAR_FEATURE command since a STALL has occurred and reset
  76259. + * the data toggle now.
  76260. + */
  76261. + hc->qh->data_toggle = 0;
  76262. + }
  76263. +
  76264. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  76265. +
  76266. +handle_stall_done:
  76267. + disable_hc_int(hc_regs, stall);
  76268. +
  76269. + return 1;
  76270. +}
  76271. +
  76272. +/*
  76273. + * Updates the state of the URB when a transfer has been stopped due to an
  76274. + * abnormal condition before the transfer completes. Modifies the
  76275. + * actual_length field of the URB to reflect the number of bytes that have
  76276. + * actually been transferred via the host channel.
  76277. + */
  76278. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  76279. + dwc_otg_hc_regs_t * hc_regs,
  76280. + dwc_otg_hcd_urb_t * urb,
  76281. + dwc_otg_qtd_t * qtd,
  76282. + dwc_otg_halt_status_e halt_status)
  76283. +{
  76284. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  76285. + halt_status, NULL);
  76286. + /* non DWORD-aligned buffer case handling. */
  76287. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  76288. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  76289. + bytes_transferred);
  76290. + }
  76291. +
  76292. + urb->actual_length += bytes_transferred;
  76293. +
  76294. +#ifdef DEBUG
  76295. + {
  76296. + hctsiz_data_t hctsiz;
  76297. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76298. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  76299. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  76300. + hc->hc_num);
  76301. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  76302. + hc->start_pkt_count);
  76303. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  76304. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  76305. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  76306. + bytes_transferred);
  76307. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  76308. + urb->actual_length);
  76309. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  76310. + urb->length);
  76311. + }
  76312. +#endif
  76313. +}
  76314. +
  76315. +/**
  76316. + * Handles a host channel NAK interrupt. This handler may be called in either
  76317. + * DMA mode or Slave mode.
  76318. + */
  76319. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  76320. + dwc_hc_t * hc,
  76321. + dwc_otg_hc_regs_t * hc_regs,
  76322. + dwc_otg_qtd_t * qtd)
  76323. +{
  76324. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76325. + "NAK Received--\n", hc->hc_num);
  76326. +
  76327. + /*
  76328. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  76329. + * the beginning of the next frame
  76330. + */
  76331. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76332. + case UE_BULK:
  76333. + case UE_CONTROL:
  76334. + if (nak_holdoff_enable)
  76335. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  76336. + }
  76337. +
  76338. + /*
  76339. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  76340. + * interrupt. Re-start the SSPLIT transfer.
  76341. + */
  76342. + if (hc->do_split) {
  76343. + if (hc->complete_split) {
  76344. + qtd->error_count = 0;
  76345. + }
  76346. + qtd->complete_split = 0;
  76347. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  76348. + goto handle_nak_done;
  76349. + }
  76350. +
  76351. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76352. + case UE_CONTROL:
  76353. + case UE_BULK:
  76354. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  76355. + /*
  76356. + * NAK interrupts are enabled on bulk/control IN
  76357. + * transfers in DMA mode for the sole purpose of
  76358. + * resetting the error count after a transaction error
  76359. + * occurs. The core will continue transferring data.
  76360. + * Disable other interrupts unmasked for the same
  76361. + * reason.
  76362. + */
  76363. + disable_hc_int(hc_regs, datatglerr);
  76364. + disable_hc_int(hc_regs, ack);
  76365. + qtd->error_count = 0;
  76366. + goto handle_nak_done;
  76367. + }
  76368. +
  76369. + /*
  76370. + * NAK interrupts normally occur during OUT transfers in DMA
  76371. + * or Slave mode. For IN transfers, more requests will be
  76372. + * queued as request queue space is available.
  76373. + */
  76374. + qtd->error_count = 0;
  76375. +
  76376. + if (!hc->qh->ping_state) {
  76377. + update_urb_state_xfer_intr(hc, hc_regs,
  76378. + qtd->urb, qtd,
  76379. + DWC_OTG_HC_XFER_NAK);
  76380. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76381. +
  76382. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  76383. + hc->qh->ping_state = 1;
  76384. + }
  76385. +
  76386. + /*
  76387. + * Halt the channel so the transfer can be re-started from
  76388. + * the appropriate point or the PING protocol will
  76389. + * start/continue.
  76390. + */
  76391. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  76392. + break;
  76393. + case UE_INTERRUPT:
  76394. + qtd->error_count = 0;
  76395. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  76396. + break;
  76397. + case UE_ISOCHRONOUS:
  76398. + /* Should never get called for isochronous transfers. */
  76399. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  76400. + break;
  76401. + }
  76402. +
  76403. +handle_nak_done:
  76404. + disable_hc_int(hc_regs, nak);
  76405. +
  76406. + return 1;
  76407. +}
  76408. +
  76409. +/**
  76410. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  76411. + * performing the PING protocol in Slave mode, when errors occur during
  76412. + * either Slave mode or DMA mode, and during Start Split transactions.
  76413. + */
  76414. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  76415. + dwc_hc_t * hc,
  76416. + dwc_otg_hc_regs_t * hc_regs,
  76417. + dwc_otg_qtd_t * qtd)
  76418. +{
  76419. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76420. + "ACK Received--\n", hc->hc_num);
  76421. +
  76422. + if (hc->do_split) {
  76423. + /*
  76424. + * Handle ACK on SSPLIT.
  76425. + * ACK should not occur in CSPLIT.
  76426. + */
  76427. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  76428. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  76429. + }
  76430. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  76431. + /* Don't need complete for isochronous out transfers. */
  76432. + qtd->complete_split = 1;
  76433. + }
  76434. +
  76435. + /* ISOC OUT */
  76436. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  76437. + switch (hc->xact_pos) {
  76438. + case DWC_HCSPLIT_XACTPOS_ALL:
  76439. + break;
  76440. + case DWC_HCSPLIT_XACTPOS_END:
  76441. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  76442. + qtd->isoc_split_offset = 0;
  76443. + break;
  76444. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  76445. + case DWC_HCSPLIT_XACTPOS_MID:
  76446. + /*
  76447. + * For BEGIN or MID, calculate the length for
  76448. + * the next microframe to determine the correct
  76449. + * SSPLIT token, either MID or END.
  76450. + */
  76451. + {
  76452. + struct dwc_otg_hcd_iso_packet_desc
  76453. + *frame_desc;
  76454. +
  76455. + frame_desc =
  76456. + &qtd->urb->
  76457. + iso_descs[qtd->isoc_frame_index];
  76458. + qtd->isoc_split_offset += 188;
  76459. +
  76460. + if ((frame_desc->length -
  76461. + qtd->isoc_split_offset) <= 188) {
  76462. + qtd->isoc_split_pos =
  76463. + DWC_HCSPLIT_XACTPOS_END;
  76464. + } else {
  76465. + qtd->isoc_split_pos =
  76466. + DWC_HCSPLIT_XACTPOS_MID;
  76467. + }
  76468. +
  76469. + }
  76470. + break;
  76471. + }
  76472. + } else {
  76473. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  76474. + }
  76475. + } else {
  76476. + /*
  76477. + * An unmasked ACK on a non-split DMA transaction is
  76478. + * for the sole purpose of resetting error counts. Disable other
  76479. + * interrupts unmasked for the same reason.
  76480. + */
  76481. + if(hcd->core_if->dma_enable) {
  76482. + disable_hc_int(hc_regs, datatglerr);
  76483. + disable_hc_int(hc_regs, nak);
  76484. + }
  76485. + qtd->error_count = 0;
  76486. +
  76487. + if (hc->qh->ping_state) {
  76488. + hc->qh->ping_state = 0;
  76489. + /*
  76490. + * Halt the channel so the transfer can be re-started
  76491. + * from the appropriate point. This only happens in
  76492. + * Slave mode. In DMA mode, the ping_state is cleared
  76493. + * when the transfer is started because the core
  76494. + * automatically executes the PING, then the transfer.
  76495. + */
  76496. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  76497. + }
  76498. + }
  76499. +
  76500. + /*
  76501. + * If the ACK occurred when _not_ in the PING state, let the channel
  76502. + * continue transferring data after clearing the error count.
  76503. + */
  76504. +
  76505. + disable_hc_int(hc_regs, ack);
  76506. +
  76507. + return 1;
  76508. +}
  76509. +
  76510. +/**
  76511. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  76512. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  76513. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  76514. + * handled in the xfercomp interrupt handler, not here. This handler may be
  76515. + * called in either DMA mode or Slave mode.
  76516. + */
  76517. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  76518. + dwc_hc_t * hc,
  76519. + dwc_otg_hc_regs_t * hc_regs,
  76520. + dwc_otg_qtd_t * qtd)
  76521. +{
  76522. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76523. + "NYET Received--\n", hc->hc_num);
  76524. +
  76525. + /*
  76526. + * NYET on CSPLIT
  76527. + * re-do the CSPLIT immediately on non-periodic
  76528. + */
  76529. + if (hc->do_split && hc->complete_split) {
  76530. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  76531. + && hcd->core_if->dma_enable) {
  76532. + qtd->complete_split = 0;
  76533. + qtd->isoc_split_offset = 0;
  76534. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  76535. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  76536. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  76537. + }
  76538. + else
  76539. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  76540. + goto handle_nyet_done;
  76541. + }
  76542. +
  76543. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  76544. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  76545. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  76546. +
  76547. + // With the FIQ running we only ever see the failed NYET
  76548. + if (dwc_full_frame_num(frnum) !=
  76549. + dwc_full_frame_num(hc->qh->sched_frame) ||
  76550. + fiq_split_enable) {
  76551. + /*
  76552. + * No longer in the same full speed frame.
  76553. + * Treat this as a transaction error.
  76554. + */
  76555. +#if 0
  76556. + /** @todo Fix system performance so this can
  76557. + * be treated as an error. Right now complete
  76558. + * splits cannot be scheduled precisely enough
  76559. + * due to other system activity, so this error
  76560. + * occurs regularly in Slave mode.
  76561. + */
  76562. + qtd->error_count++;
  76563. +#endif
  76564. + qtd->complete_split = 0;
  76565. + halt_channel(hcd, hc, qtd,
  76566. + DWC_OTG_HC_XFER_XACT_ERR);
  76567. + /** @todo add support for isoc release */
  76568. + goto handle_nyet_done;
  76569. + }
  76570. + }
  76571. +
  76572. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  76573. + goto handle_nyet_done;
  76574. + }
  76575. +
  76576. + hc->qh->ping_state = 1;
  76577. + qtd->error_count = 0;
  76578. +
  76579. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  76580. + DWC_OTG_HC_XFER_NYET);
  76581. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76582. +
  76583. + /*
  76584. + * Halt the channel and re-start the transfer so the PING
  76585. + * protocol will start.
  76586. + */
  76587. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  76588. +
  76589. +handle_nyet_done:
  76590. + disable_hc_int(hc_regs, nyet);
  76591. + return 1;
  76592. +}
  76593. +
  76594. +/**
  76595. + * Handles a host channel babble interrupt. This handler may be called in
  76596. + * either DMA mode or Slave mode.
  76597. + */
  76598. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  76599. + dwc_hc_t * hc,
  76600. + dwc_otg_hc_regs_t * hc_regs,
  76601. + dwc_otg_qtd_t * qtd)
  76602. +{
  76603. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76604. + "Babble Error--\n", hc->hc_num);
  76605. +
  76606. + if (hcd->core_if->dma_desc_enable) {
  76607. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76608. + DWC_OTG_HC_XFER_BABBLE_ERR);
  76609. + goto handle_babble_done;
  76610. + }
  76611. +
  76612. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  76613. + hcd->fops->complete(hcd, qtd->urb->priv,
  76614. + qtd->urb, -DWC_E_OVERFLOW);
  76615. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  76616. + } else {
  76617. + dwc_otg_halt_status_e halt_status;
  76618. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76619. + DWC_OTG_HC_XFER_BABBLE_ERR);
  76620. + halt_channel(hcd, hc, qtd, halt_status);
  76621. + }
  76622. +
  76623. +handle_babble_done:
  76624. + disable_hc_int(hc_regs, bblerr);
  76625. + return 1;
  76626. +}
  76627. +
  76628. +/**
  76629. + * Handles a host channel AHB error interrupt. This handler is only called in
  76630. + * DMA mode.
  76631. + */
  76632. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  76633. + dwc_hc_t * hc,
  76634. + dwc_otg_hc_regs_t * hc_regs,
  76635. + dwc_otg_qtd_t * qtd)
  76636. +{
  76637. + hcchar_data_t hcchar;
  76638. + hcsplt_data_t hcsplt;
  76639. + hctsiz_data_t hctsiz;
  76640. + uint32_t hcdma;
  76641. + char *pipetype, *speed;
  76642. +
  76643. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  76644. +
  76645. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76646. + "AHB Error--\n", hc->hc_num);
  76647. +
  76648. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76649. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76650. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76651. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  76652. +
  76653. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  76654. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  76655. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  76656. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  76657. + DWC_ERROR(" Device address: %d\n",
  76658. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  76659. + DWC_ERROR(" Endpoint: %d, %s\n",
  76660. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  76661. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  76662. +
  76663. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  76664. + case UE_CONTROL:
  76665. + pipetype = "CONTROL";
  76666. + break;
  76667. + case UE_BULK:
  76668. + pipetype = "BULK";
  76669. + break;
  76670. + case UE_INTERRUPT:
  76671. + pipetype = "INTERRUPT";
  76672. + break;
  76673. + case UE_ISOCHRONOUS:
  76674. + pipetype = "ISOCHRONOUS";
  76675. + break;
  76676. + default:
  76677. + pipetype = "UNKNOWN";
  76678. + break;
  76679. + }
  76680. +
  76681. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  76682. +
  76683. + switch (hc->speed) {
  76684. + case DWC_OTG_EP_SPEED_HIGH:
  76685. + speed = "HIGH";
  76686. + break;
  76687. + case DWC_OTG_EP_SPEED_FULL:
  76688. + speed = "FULL";
  76689. + break;
  76690. + case DWC_OTG_EP_SPEED_LOW:
  76691. + speed = "LOW";
  76692. + break;
  76693. + default:
  76694. + speed = "UNKNOWN";
  76695. + break;
  76696. + };
  76697. +
  76698. + DWC_ERROR(" Speed: %s\n", speed);
  76699. +
  76700. + DWC_ERROR(" Max packet size: %d\n",
  76701. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  76702. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  76703. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  76704. + urb->buf, (void *)urb->dma);
  76705. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  76706. + urb->setup_packet, (void *)urb->setup_dma);
  76707. + DWC_ERROR(" Interval: %d\n", urb->interval);
  76708. +
  76709. + /* Core haltes the channel for Descriptor DMA mode */
  76710. + if (hcd->core_if->dma_desc_enable) {
  76711. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76712. + DWC_OTG_HC_XFER_AHB_ERR);
  76713. + goto handle_ahberr_done;
  76714. + }
  76715. +
  76716. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  76717. +
  76718. + /*
  76719. + * Force a channel halt. Don't call halt_channel because that won't
  76720. + * write to the HCCHARn register in DMA mode to force the halt.
  76721. + */
  76722. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  76723. +handle_ahberr_done:
  76724. + disable_hc_int(hc_regs, ahberr);
  76725. + return 1;
  76726. +}
  76727. +
  76728. +/**
  76729. + * Handles a host channel transaction error interrupt. This handler may be
  76730. + * called in either DMA mode or Slave mode.
  76731. + */
  76732. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  76733. + dwc_hc_t * hc,
  76734. + dwc_otg_hc_regs_t * hc_regs,
  76735. + dwc_otg_qtd_t * qtd)
  76736. +{
  76737. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76738. + "Transaction Error--\n", hc->hc_num);
  76739. +
  76740. + if (hcd->core_if->dma_desc_enable) {
  76741. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76742. + DWC_OTG_HC_XFER_XACT_ERR);
  76743. + goto handle_xacterr_done;
  76744. + }
  76745. +
  76746. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76747. + case UE_CONTROL:
  76748. + case UE_BULK:
  76749. + qtd->error_count++;
  76750. + if (!hc->qh->ping_state) {
  76751. +
  76752. + update_urb_state_xfer_intr(hc, hc_regs,
  76753. + qtd->urb, qtd,
  76754. + DWC_OTG_HC_XFER_XACT_ERR);
  76755. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76756. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  76757. + hc->qh->ping_state = 1;
  76758. + }
  76759. + }
  76760. +
  76761. + /*
  76762. + * Halt the channel so the transfer can be re-started from
  76763. + * the appropriate point or the PING protocol will start.
  76764. + */
  76765. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76766. + break;
  76767. + case UE_INTERRUPT:
  76768. + qtd->error_count++;
  76769. + if (hc->do_split && hc->complete_split) {
  76770. + qtd->complete_split = 0;
  76771. + }
  76772. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76773. + break;
  76774. + case UE_ISOCHRONOUS:
  76775. + {
  76776. + dwc_otg_halt_status_e halt_status;
  76777. + halt_status =
  76778. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76779. + DWC_OTG_HC_XFER_XACT_ERR);
  76780. +
  76781. + halt_channel(hcd, hc, qtd, halt_status);
  76782. + }
  76783. + break;
  76784. + }
  76785. +handle_xacterr_done:
  76786. + disable_hc_int(hc_regs, xacterr);
  76787. +
  76788. + return 1;
  76789. +}
  76790. +
  76791. +/**
  76792. + * Handles a host channel frame overrun interrupt. This handler may be called
  76793. + * in either DMA mode or Slave mode.
  76794. + */
  76795. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  76796. + dwc_hc_t * hc,
  76797. + dwc_otg_hc_regs_t * hc_regs,
  76798. + dwc_otg_qtd_t * qtd)
  76799. +{
  76800. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76801. + "Frame Overrun--\n", hc->hc_num);
  76802. +
  76803. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76804. + case UE_CONTROL:
  76805. + case UE_BULK:
  76806. + break;
  76807. + case UE_INTERRUPT:
  76808. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  76809. + break;
  76810. + case UE_ISOCHRONOUS:
  76811. + {
  76812. + dwc_otg_halt_status_e halt_status;
  76813. + halt_status =
  76814. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76815. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  76816. +
  76817. + halt_channel(hcd, hc, qtd, halt_status);
  76818. + }
  76819. + break;
  76820. + }
  76821. +
  76822. + disable_hc_int(hc_regs, frmovrun);
  76823. +
  76824. + return 1;
  76825. +}
  76826. +
  76827. +/**
  76828. + * Handles a host channel data toggle error interrupt. This handler may be
  76829. + * called in either DMA mode or Slave mode.
  76830. + */
  76831. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  76832. + dwc_hc_t * hc,
  76833. + dwc_otg_hc_regs_t * hc_regs,
  76834. + dwc_otg_qtd_t * qtd)
  76835. +{
  76836. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76837. + "Data Toggle Error on %s transfer--\n",
  76838. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  76839. +
  76840. + /* Data toggles on split transactions cause the hc to halt.
  76841. + * restart transfer */
  76842. + if(hc->qh->do_split)
  76843. + {
  76844. + qtd->error_count++;
  76845. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76846. + update_urb_state_xfer_intr(hc, hc_regs,
  76847. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76848. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76849. + } else if (hc->ep_is_in) {
  76850. + /* An unmasked data toggle error on a non-split DMA transaction is
  76851. + * for the sole purpose of resetting error counts. Disable other
  76852. + * interrupts unmasked for the same reason.
  76853. + */
  76854. + if(hcd->core_if->dma_enable) {
  76855. + disable_hc_int(hc_regs, ack);
  76856. + disable_hc_int(hc_regs, nak);
  76857. + }
  76858. + qtd->error_count = 0;
  76859. + }
  76860. +
  76861. + disable_hc_int(hc_regs, datatglerr);
  76862. +
  76863. + return 1;
  76864. +}
  76865. +
  76866. +#ifdef DEBUG
  76867. +/**
  76868. + * This function is for debug only. It checks that a valid halt status is set
  76869. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  76870. + * taken and a warning is issued.
  76871. + * @return 1 if halt status is ok, 0 otherwise.
  76872. + */
  76873. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  76874. + dwc_hc_t * hc,
  76875. + dwc_otg_hc_regs_t * hc_regs,
  76876. + dwc_otg_qtd_t * qtd)
  76877. +{
  76878. + hcchar_data_t hcchar;
  76879. + hctsiz_data_t hctsiz;
  76880. + hcint_data_t hcint;
  76881. + hcintmsk_data_t hcintmsk;
  76882. + hcsplt_data_t hcsplt;
  76883. +
  76884. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  76885. + /*
  76886. + * This code is here only as a check. This condition should
  76887. + * never happen. Ignore the halt if it does occur.
  76888. + */
  76889. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76890. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76891. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76892. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76893. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76894. + DWC_WARN
  76895. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  76896. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  76897. + "hcint 0x%08x, hcintmsk 0x%08x, "
  76898. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  76899. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  76900. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  76901. +
  76902. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  76903. + __func__, hc->hc_num);
  76904. + DWC_WARN("\n");
  76905. + clear_hc_int(hc_regs, chhltd);
  76906. + return 0;
  76907. + }
  76908. +
  76909. + /*
  76910. + * This code is here only as a check. hcchar.chdis should
  76911. + * never be set when the halt interrupt occurs. Halt the
  76912. + * channel again if it does occur.
  76913. + */
  76914. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76915. + if (hcchar.b.chdis) {
  76916. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  76917. + "hcchar 0x%08x, trying to halt again\n",
  76918. + __func__, hcchar.d32);
  76919. + clear_hc_int(hc_regs, chhltd);
  76920. + hc->halt_pending = 0;
  76921. + halt_channel(hcd, hc, qtd, hc->halt_status);
  76922. + return 0;
  76923. + }
  76924. +
  76925. + return 1;
  76926. +}
  76927. +#endif
  76928. +
  76929. +/**
  76930. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  76931. + * determines the reason the channel halted and proceeds accordingly.
  76932. + */
  76933. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  76934. + dwc_hc_t * hc,
  76935. + dwc_otg_hc_regs_t * hc_regs,
  76936. + dwc_otg_qtd_t * qtd,
  76937. + hcint_data_t hcint,
  76938. + hcintmsk_data_t hcintmsk)
  76939. +{
  76940. + int out_nak_enh = 0;
  76941. +
  76942. + /* For core with OUT NAK enhancement, the flow for high-
  76943. + * speed CONTROL/BULK OUT is handled a little differently.
  76944. + */
  76945. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  76946. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  76947. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  76948. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  76949. + out_nak_enh = 1;
  76950. + }
  76951. + }
  76952. +
  76953. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  76954. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  76955. + && !hcd->core_if->dma_desc_enable)) {
  76956. + /*
  76957. + * Just release the channel. A dequeue can happen on a
  76958. + * transfer timeout. In the case of an AHB Error, the channel
  76959. + * was forced to halt because there's no way to gracefully
  76960. + * recover.
  76961. + */
  76962. + if (hcd->core_if->dma_desc_enable)
  76963. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76964. + hc->halt_status);
  76965. + else
  76966. + release_channel(hcd, hc, qtd, hc->halt_status);
  76967. + return;
  76968. + }
  76969. +
  76970. + /* Read the HCINTn register to determine the cause for the halt. */
  76971. + if(!fiq_split_enable)
  76972. + {
  76973. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76974. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76975. + }
  76976. +
  76977. + if (hcint.b.xfercomp) {
  76978. + /** @todo This is here because of a possible hardware bug. Spec
  76979. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  76980. + * interrupt w/ACK bit set should occur, but I only see the
  76981. + * XFERCOMP bit, even with it masked out. This is a workaround
  76982. + * for that behavior. Should fix this when hardware is fixed.
  76983. + */
  76984. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  76985. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  76986. + }
  76987. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  76988. + } else if (hcint.b.stall) {
  76989. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  76990. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  76991. + if (out_nak_enh) {
  76992. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  76993. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  76994. + qtd->error_count = 0;
  76995. + } else {
  76996. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  76997. + }
  76998. + }
  76999. +
  77000. + /*
  77001. + * Must handle xacterr before nak or ack. Could get a xacterr
  77002. + * at the same time as either of these on a BULK/CONTROL OUT
  77003. + * that started with a PING. The xacterr takes precedence.
  77004. + */
  77005. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  77006. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  77007. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  77008. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  77009. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  77010. + } else if (hcint.b.bblerr) {
  77011. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  77012. + } else if (hcint.b.frmovrun) {
  77013. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  77014. + } else if (hcint.b.datatglerr) {
  77015. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  77016. + } else if (!out_nak_enh) {
  77017. + if (hcint.b.nyet) {
  77018. + /*
  77019. + * Must handle nyet before nak or ack. Could get a nyet at the
  77020. + * same time as either of those on a BULK/CONTROL OUT that
  77021. + * started with a PING. The nyet takes precedence.
  77022. + */
  77023. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  77024. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  77025. + /*
  77026. + * If nak is not masked, it's because a non-split IN transfer
  77027. + * is in an error state. In that case, the nak is handled by
  77028. + * the nak interrupt handler, not here. Handle nak here for
  77029. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  77030. + * rewinding the buffer pointer.
  77031. + */
  77032. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  77033. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  77034. + /*
  77035. + * If ack is not masked, it's because a non-split IN transfer
  77036. + * is in an error state. In that case, the ack is handled by
  77037. + * the ack interrupt handler, not here. Handle ack here for
  77038. + * split transfers. Start splits halt on ACK.
  77039. + */
  77040. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  77041. + } else {
  77042. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  77043. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  77044. + /*
  77045. + * A periodic transfer halted with no other channel
  77046. + * interrupts set. Assume it was halted by the core
  77047. + * because it could not be completed in its scheduled
  77048. + * (micro)frame.
  77049. + */
  77050. +#ifdef DEBUG
  77051. + DWC_PRINTF
  77052. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  77053. + __func__, hc->hc_num);
  77054. +#endif
  77055. + halt_channel(hcd, hc, qtd,
  77056. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  77057. + } else {
  77058. + DWC_ERROR
  77059. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  77060. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  77061. + __func__, hc->hc_num, hcint.d32,
  77062. + DWC_READ_REG32(&hcd->
  77063. + core_if->core_global_regs->
  77064. + gintsts));
  77065. + /* Failthrough: use 3-strikes rule */
  77066. + qtd->error_count++;
  77067. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  77068. + update_urb_state_xfer_intr(hc, hc_regs,
  77069. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77070. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77071. + }
  77072. +
  77073. + }
  77074. + } else {
  77075. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  77076. + hcint.d32);
  77077. + /* Failthrough: use 3-strikes rule */
  77078. + qtd->error_count++;
  77079. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  77080. + update_urb_state_xfer_intr(hc, hc_regs,
  77081. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77082. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  77083. + }
  77084. +}
  77085. +
  77086. +/**
  77087. + * Handles a host channel Channel Halted interrupt.
  77088. + *
  77089. + * In slave mode, this handler is called only when the driver specifically
  77090. + * requests a halt. This occurs during handling other host channel interrupts
  77091. + * (e.g. nak, xacterr, stall, nyet, etc.).
  77092. + *
  77093. + * In DMA mode, this is the interrupt that occurs when the core has finished
  77094. + * processing a transfer on a channel. Other host channel interrupts (except
  77095. + * ahberr) are disabled in DMA mode.
  77096. + */
  77097. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  77098. + dwc_hc_t * hc,
  77099. + dwc_otg_hc_regs_t * hc_regs,
  77100. + dwc_otg_qtd_t * qtd,
  77101. + hcint_data_t hcint,
  77102. + hcintmsk_data_t hcintmsk)
  77103. +{
  77104. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  77105. + "Channel Halted--\n", hc->hc_num);
  77106. +
  77107. + if (hcd->core_if->dma_enable) {
  77108. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd, hcint, hcintmsk);
  77109. + } else {
  77110. +#ifdef DEBUG
  77111. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  77112. + return 1;
  77113. + }
  77114. +#endif
  77115. + release_channel(hcd, hc, qtd, hc->halt_status);
  77116. + }
  77117. +
  77118. + return 1;
  77119. +}
  77120. +
  77121. +/** Handles interrupt for a specific Host Channel */
  77122. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  77123. +{
  77124. + int retval = 0;
  77125. + hcint_data_t hcint, hcint_orig;
  77126. + hcintmsk_data_t hcintmsk;
  77127. + dwc_hc_t *hc;
  77128. + dwc_otg_hc_regs_t *hc_regs;
  77129. + dwc_otg_qtd_t *qtd;
  77130. +
  77131. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  77132. +
  77133. + hc = dwc_otg_hcd->hc_ptr_array[num];
  77134. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  77135. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  77136. + /* We are responding to a channel disable. Driver
  77137. + * state is cleared - our qtd has gone away.
  77138. + */
  77139. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  77140. + return 1;
  77141. + }
  77142. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  77143. +
  77144. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  77145. + hcint_orig = hcint;
  77146. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  77147. + DWC_DEBUGPL(DBG_HCDV,
  77148. + " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  77149. + hcint.d32, hcintmsk.d32, (hcint.d32 & hcintmsk.d32));
  77150. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  77151. +
  77152. + if(fiq_split_enable)
  77153. + {
  77154. + // replace with the saved interrupts from the fiq handler
  77155. + local_fiq_disable();
  77156. + hcint_orig.d32 = hcint_saved[num].d32;
  77157. + hcint.d32 = hcint_orig.d32 & hcintmsk_saved[num].d32;
  77158. + hcint_saved[num].d32 = 0;
  77159. + local_fiq_enable();
  77160. + }
  77161. +
  77162. + if (!dwc_otg_hcd->core_if->dma_enable) {
  77163. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  77164. + hcint.b.chhltd = 0;
  77165. + }
  77166. + }
  77167. +
  77168. + if (hcint.b.xfercomp) {
  77169. + retval |=
  77170. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77171. + /*
  77172. + * If NYET occurred at same time as Xfer Complete, the NYET is
  77173. + * handled by the Xfer Complete interrupt handler. Don't want
  77174. + * to call the NYET interrupt handler in this case.
  77175. + */
  77176. + hcint.b.nyet = 0;
  77177. + }
  77178. + if (hcint.b.chhltd) {
  77179. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd, hcint_orig, hcintmsk_saved[num]);
  77180. + }
  77181. + if (hcint.b.ahberr) {
  77182. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77183. + }
  77184. + if (hcint.b.stall) {
  77185. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77186. + }
  77187. + if (hcint.b.nak) {
  77188. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77189. + }
  77190. + if (hcint.b.ack) {
  77191. + if(!hcint.b.chhltd)
  77192. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77193. + }
  77194. + if (hcint.b.nyet) {
  77195. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77196. + }
  77197. + if (hcint.b.xacterr) {
  77198. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77199. + }
  77200. + if (hcint.b.bblerr) {
  77201. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77202. + }
  77203. + if (hcint.b.frmovrun) {
  77204. + retval |=
  77205. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77206. + }
  77207. + if (hcint.b.datatglerr) {
  77208. + retval |=
  77209. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  77210. + }
  77211. +
  77212. + return retval;
  77213. +}
  77214. +#endif /* DWC_DEVICE_ONLY */
  77215. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  77216. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  77217. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 2014-04-24 15:35:04.177565820 +0200
  77218. @@ -0,0 +1,972 @@
  77219. +
  77220. +/* ==========================================================================
  77221. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  77222. + * $Revision: #20 $
  77223. + * $Date: 2011/10/26 $
  77224. + * $Change: 1872981 $
  77225. + *
  77226. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  77227. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  77228. + * otherwise expressly agreed to in writing between Synopsys and you.
  77229. + *
  77230. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  77231. + * any End User Software License Agreement or Agreement for Licensed Product
  77232. + * with Synopsys or any supplement thereto. You are permitted to use and
  77233. + * redistribute this Software in source and binary forms, with or without
  77234. + * modification, provided that redistributions of source code must retain this
  77235. + * notice. You may not view, use, disclose, copy or distribute this file or
  77236. + * any information contained herein except pursuant to this license grant from
  77237. + * Synopsys. If you do not agree with this notice, including the disclaimer
  77238. + * below, then you are not authorized to use the Software.
  77239. + *
  77240. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  77241. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  77242. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  77243. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  77244. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  77245. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  77246. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  77247. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  77248. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  77249. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  77250. + * DAMAGE.
  77251. + * ========================================================================== */
  77252. +#ifndef DWC_DEVICE_ONLY
  77253. +
  77254. +/**
  77255. + * @file
  77256. + *
  77257. + * This file contains the implementation of the HCD. In Linux, the HCD
  77258. + * implements the hc_driver API.
  77259. + */
  77260. +#include <linux/kernel.h>
  77261. +#include <linux/module.h>
  77262. +#include <linux/moduleparam.h>
  77263. +#include <linux/init.h>
  77264. +#include <linux/device.h>
  77265. +#include <linux/errno.h>
  77266. +#include <linux/list.h>
  77267. +#include <linux/interrupt.h>
  77268. +#include <linux/string.h>
  77269. +#include <linux/dma-mapping.h>
  77270. +#include <linux/version.h>
  77271. +#include <asm/io.h>
  77272. +#include <asm/fiq.h>
  77273. +#include <linux/usb.h>
  77274. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  77275. +#include <../drivers/usb/core/hcd.h>
  77276. +#else
  77277. +#include <linux/usb/hcd.h>
  77278. +#endif
  77279. +
  77280. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  77281. +#define USB_URB_EP_LINKING 1
  77282. +#else
  77283. +#define USB_URB_EP_LINKING 0
  77284. +#endif
  77285. +
  77286. +#include "dwc_otg_hcd_if.h"
  77287. +#include "dwc_otg_dbg.h"
  77288. +#include "dwc_otg_driver.h"
  77289. +#include "dwc_otg_hcd.h"
  77290. +#include "dwc_otg_mphi_fix.h"
  77291. +
  77292. +/**
  77293. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  77294. + * qualified with its direction (possible 32 endpoints per device).
  77295. + */
  77296. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  77297. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  77298. +
  77299. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  77300. +
  77301. +extern bool fiq_fix_enable;
  77302. +
  77303. +/** @name Linux HC Driver API Functions */
  77304. +/** @{ */
  77305. +/* manage i/o requests, device state */
  77306. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  77307. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77308. + struct usb_host_endpoint *ep,
  77309. +#endif
  77310. + struct urb *urb, gfp_t mem_flags);
  77311. +
  77312. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  77313. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77314. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  77315. +#endif
  77316. +#else /* kernels at or post 2.6.30 */
  77317. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  77318. + struct urb *urb, int status);
  77319. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  77320. +
  77321. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  77322. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  77323. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  77324. +#endif
  77325. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  77326. +extern int hcd_start(struct usb_hcd *hcd);
  77327. +extern void hcd_stop(struct usb_hcd *hcd);
  77328. +static int get_frame_number(struct usb_hcd *hcd);
  77329. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  77330. +extern int hub_control(struct usb_hcd *hcd,
  77331. + u16 typeReq,
  77332. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  77333. +
  77334. +struct wrapper_priv_data {
  77335. + dwc_otg_hcd_t *dwc_otg_hcd;
  77336. +};
  77337. +
  77338. +/** @} */
  77339. +
  77340. +static struct hc_driver dwc_otg_hc_driver = {
  77341. +
  77342. + .description = dwc_otg_hcd_name,
  77343. + .product_desc = "DWC OTG Controller",
  77344. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  77345. +
  77346. + .irq = dwc_otg_hcd_irq,
  77347. +
  77348. + .flags = HCD_MEMORY | HCD_USB2,
  77349. +
  77350. + //.reset =
  77351. + .start = hcd_start,
  77352. + //.suspend =
  77353. + //.resume =
  77354. + .stop = hcd_stop,
  77355. +
  77356. + .urb_enqueue = dwc_otg_urb_enqueue,
  77357. + .urb_dequeue = dwc_otg_urb_dequeue,
  77358. + .endpoint_disable = endpoint_disable,
  77359. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  77360. + .endpoint_reset = endpoint_reset,
  77361. +#endif
  77362. + .get_frame_number = get_frame_number,
  77363. +
  77364. + .hub_status_data = hub_status_data,
  77365. + .hub_control = hub_control,
  77366. + //.bus_suspend =
  77367. + //.bus_resume =
  77368. +};
  77369. +
  77370. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  77371. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  77372. +{
  77373. + struct wrapper_priv_data *p;
  77374. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  77375. + return p->dwc_otg_hcd;
  77376. +}
  77377. +
  77378. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  77379. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  77380. +{
  77381. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  77382. +}
  77383. +
  77384. +/** Gets the usb_host_endpoint associated with an URB. */
  77385. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  77386. +{
  77387. + struct usb_device *dev = urb->dev;
  77388. + int ep_num = usb_pipeendpoint(urb->pipe);
  77389. +
  77390. + if (usb_pipein(urb->pipe))
  77391. + return dev->ep_in[ep_num];
  77392. + else
  77393. + return dev->ep_out[ep_num];
  77394. +}
  77395. +
  77396. +static int _disconnect(dwc_otg_hcd_t * hcd)
  77397. +{
  77398. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  77399. +
  77400. + usb_hcd->self.is_b_host = 0;
  77401. + return 0;
  77402. +}
  77403. +
  77404. +static int _start(dwc_otg_hcd_t * hcd)
  77405. +{
  77406. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  77407. +
  77408. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  77409. + hcd_start(usb_hcd);
  77410. +
  77411. + return 0;
  77412. +}
  77413. +
  77414. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  77415. + uint32_t * port_addr)
  77416. +{
  77417. + struct urb *urb = (struct urb *)urb_handle;
  77418. + struct usb_bus *bus;
  77419. +#if 1 //GRAYG - temporary
  77420. + if (NULL == urb_handle)
  77421. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  77422. + if (NULL == urb->dev)
  77423. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  77424. + if (NULL == port_addr)
  77425. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  77426. +#endif
  77427. + if (urb->dev->tt) {
  77428. + if (NULL == urb->dev->tt->hub) {
  77429. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  77430. + __func__); //GRAYG
  77431. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  77432. + *hub_addr = 0; //GRAYG
  77433. + // we probably shouldn't have a transaction translator if
  77434. + // there's no associated hub?
  77435. + } else {
  77436. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  77437. + if (urb->dev->tt->hub == bus->root_hub)
  77438. + *hub_addr = 0;
  77439. + else
  77440. + *hub_addr = urb->dev->tt->hub->devnum;
  77441. + }
  77442. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  77443. + } else {
  77444. + *hub_addr = 0;
  77445. + *port_addr = urb->dev->ttport;
  77446. + }
  77447. + return 0;
  77448. +}
  77449. +
  77450. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  77451. +{
  77452. + struct urb *urb = (struct urb *)urb_handle;
  77453. + return urb->dev->speed;
  77454. +}
  77455. +
  77456. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  77457. +{
  77458. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  77459. + return usb_hcd->self.b_hnp_enable;
  77460. +}
  77461. +
  77462. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  77463. + struct urb *urb)
  77464. +{
  77465. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  77466. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77467. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  77468. + } else {
  77469. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  77470. + }
  77471. +}
  77472. +
  77473. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  77474. + struct urb *urb)
  77475. +{
  77476. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  77477. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77478. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  77479. + } else {
  77480. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  77481. + }
  77482. +}
  77483. +
  77484. +/**
  77485. + * Sets the final status of an URB and returns it to the device driver. Any
  77486. + * required cleanup of the URB is performed. The HCD lock should be held on
  77487. + * entry.
  77488. + */
  77489. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  77490. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  77491. +{
  77492. + struct urb *urb = (struct urb *)urb_handle;
  77493. + urb_tq_entry_t *new_entry;
  77494. + int rc = 0;
  77495. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77496. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  77497. + __func__, urb, usb_pipedevice(urb->pipe),
  77498. + usb_pipeendpoint(urb->pipe),
  77499. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  77500. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77501. + int i;
  77502. + for (i = 0; i < urb->number_of_packets; i++) {
  77503. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  77504. + i, urb->iso_frame_desc[i].status);
  77505. + }
  77506. + }
  77507. + }
  77508. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  77509. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  77510. + /* Convert status value. */
  77511. + switch (status) {
  77512. + case -DWC_E_PROTOCOL:
  77513. + status = -EPROTO;
  77514. + break;
  77515. + case -DWC_E_IN_PROGRESS:
  77516. + status = -EINPROGRESS;
  77517. + break;
  77518. + case -DWC_E_PIPE:
  77519. + status = -EPIPE;
  77520. + break;
  77521. + case -DWC_E_IO:
  77522. + status = -EIO;
  77523. + break;
  77524. + case -DWC_E_TIMEOUT:
  77525. + status = -ETIMEDOUT;
  77526. + break;
  77527. + case -DWC_E_OVERFLOW:
  77528. + status = -EOVERFLOW;
  77529. + break;
  77530. + case -DWC_E_SHUTDOWN:
  77531. + status = -ESHUTDOWN;
  77532. + break;
  77533. + default:
  77534. + if (status) {
  77535. + DWC_PRINTF("Uknown urb status %d\n", status);
  77536. +
  77537. + }
  77538. + }
  77539. +
  77540. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77541. + int i;
  77542. +
  77543. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  77544. + for (i = 0; i < urb->number_of_packets; ++i) {
  77545. + urb->iso_frame_desc[i].actual_length =
  77546. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  77547. + (dwc_otg_urb, i);
  77548. + urb->iso_frame_desc[i].status =
  77549. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  77550. + }
  77551. + }
  77552. +
  77553. + urb->status = status;
  77554. + urb->hcpriv = NULL;
  77555. + if (!status) {
  77556. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  77557. + (urb->actual_length < urb->transfer_buffer_length)) {
  77558. + urb->status = -EREMOTEIO;
  77559. + }
  77560. + }
  77561. +
  77562. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  77563. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  77564. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  77565. + if (ep) {
  77566. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  77567. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  77568. + ep->hcpriv),
  77569. + urb);
  77570. + }
  77571. + }
  77572. +
  77573. + DWC_FREE(dwc_otg_urb);
  77574. + if (!new_entry) {
  77575. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  77576. + urb->status = -EPROTO;
  77577. + /* don't schedule the tasklet -
  77578. + * directly return the packet here with error. */
  77579. +#if USB_URB_EP_LINKING
  77580. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  77581. +#endif
  77582. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77583. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  77584. +#else
  77585. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  77586. +#endif
  77587. + } else {
  77588. + new_entry->urb = urb;
  77589. +#if USB_URB_EP_LINKING
  77590. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  77591. + if(0 == rc) {
  77592. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  77593. + }
  77594. +#endif
  77595. + if(0 == rc) {
  77596. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  77597. + urb_tq_entries);
  77598. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  77599. + }
  77600. + }
  77601. + return 0;
  77602. +}
  77603. +
  77604. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  77605. + .start = _start,
  77606. + .disconnect = _disconnect,
  77607. + .hub_info = _hub_info,
  77608. + .speed = _speed,
  77609. + .complete = _complete,
  77610. + .get_b_hnp_enable = _get_b_hnp_enable,
  77611. +};
  77612. +
  77613. +static struct fiq_handler fh = {
  77614. + .name = "usb_fiq",
  77615. +};
  77616. +struct fiq_stack_s {
  77617. + int magic1;
  77618. + uint8_t stack[2048];
  77619. + int magic2;
  77620. +} fiq_stack;
  77621. +
  77622. +extern mphi_regs_t c_mphi_regs;
  77623. +/**
  77624. + * Initializes the HCD. This function allocates memory for and initializes the
  77625. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  77626. + * USB bus with the core and calls the hc_driver->start() function. It returns
  77627. + * a negative error on failure.
  77628. + */
  77629. +int hcd_init(dwc_bus_dev_t *_dev)
  77630. +{
  77631. + struct usb_hcd *hcd = NULL;
  77632. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  77633. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  77634. + int retval = 0;
  77635. + u64 dmamask;
  77636. + struct pt_regs regs;
  77637. +
  77638. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  77639. +
  77640. + /* Set device flags indicating whether the HCD supports DMA. */
  77641. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  77642. + dmamask = DMA_BIT_MASK(32);
  77643. + else
  77644. + dmamask = 0;
  77645. +
  77646. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  77647. + dma_set_mask(&_dev->dev, dmamask);
  77648. + dma_set_coherent_mask(&_dev->dev, dmamask);
  77649. +#elif defined(PCI_INTERFACE)
  77650. + pci_set_dma_mask(_dev, dmamask);
  77651. + pci_set_consistent_dma_mask(_dev, dmamask);
  77652. +#endif
  77653. +
  77654. + if (fiq_fix_enable)
  77655. + {
  77656. + // Set up fiq
  77657. + claim_fiq(&fh);
  77658. + set_fiq_handler(__FIQ_Branch, 4);
  77659. + memset(&regs,0,sizeof(regs));
  77660. + regs.ARM_r8 = (long)dwc_otg_hcd_handle_fiq;
  77661. + regs.ARM_r9 = (long)0;
  77662. + regs.ARM_sp = (long)fiq_stack.stack + sizeof(fiq_stack.stack) - 4;
  77663. + set_fiq_regs(&regs);
  77664. + fiq_stack.magic1 = 0xdeadbeef;
  77665. + fiq_stack.magic2 = 0xaa995566;
  77666. + }
  77667. +
  77668. + /*
  77669. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  77670. + * Initialize the base HCD.
  77671. + */
  77672. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  77673. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  77674. +#else
  77675. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  77676. + hcd->has_tt = 1;
  77677. +// hcd->uses_new_polling = 1;
  77678. +// hcd->poll_rh = 0;
  77679. +#endif
  77680. + if (!hcd) {
  77681. + retval = -ENOMEM;
  77682. + goto error1;
  77683. + }
  77684. +
  77685. + hcd->regs = otg_dev->os_dep.base;
  77686. +
  77687. + if (fiq_fix_enable)
  77688. + {
  77689. + volatile extern void *dwc_regs_base;
  77690. +
  77691. + //Set the mphi periph to the required registers
  77692. + c_mphi_regs.base = otg_dev->os_dep.mphi_base;
  77693. + c_mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  77694. + c_mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  77695. + c_mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  77696. + c_mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  77697. +
  77698. + dwc_regs_base = otg_dev->os_dep.base;
  77699. +
  77700. + //Enable mphi peripheral
  77701. + writel((1<<31),c_mphi_regs.ctrl);
  77702. +#ifdef DEBUG
  77703. + if (readl(c_mphi_regs.ctrl) & 0x80000000)
  77704. + DWC_DEBUGPL(DBG_USER, "MPHI periph has been enabled\n");
  77705. + else
  77706. + DWC_DEBUGPL(DBG_USER, "MPHI periph has NOT been enabled\n");
  77707. +#endif
  77708. + // Enable FIQ interrupt from USB peripheral
  77709. + enable_fiq(INTERRUPT_VC_USB);
  77710. + }
  77711. + /* Initialize the DWC OTG HCD. */
  77712. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  77713. + if (!dwc_otg_hcd) {
  77714. + goto error2;
  77715. + }
  77716. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  77717. + dwc_otg_hcd;
  77718. + otg_dev->hcd = dwc_otg_hcd;
  77719. +
  77720. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  77721. + goto error2;
  77722. + }
  77723. +
  77724. + otg_dev->hcd->otg_dev = otg_dev;
  77725. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  77726. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  77727. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  77728. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  77729. +#endif
  77730. + /* Don't support SG list at this point */
  77731. + hcd->self.sg_tablesize = 0;
  77732. +#endif
  77733. + /*
  77734. + * Finish generic HCD initialization and start the HCD. This function
  77735. + * allocates the DMA buffer pool, registers the USB bus, requests the
  77736. + * IRQ line, and calls hcd_start method.
  77737. + */
  77738. +#ifdef PLATFORM_INTERFACE
  77739. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, 0), IRQF_SHARED | IRQF_DISABLED);
  77740. +#else
  77741. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED | IRQF_DISABLED);
  77742. +#endif
  77743. + if (retval < 0) {
  77744. + goto error2;
  77745. + }
  77746. +
  77747. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  77748. + return 0;
  77749. +
  77750. +error2:
  77751. + usb_put_hcd(hcd);
  77752. +error1:
  77753. + return retval;
  77754. +}
  77755. +
  77756. +/**
  77757. + * Removes the HCD.
  77758. + * Frees memory and resources associated with the HCD and deregisters the bus.
  77759. + */
  77760. +void hcd_remove(dwc_bus_dev_t *_dev)
  77761. +{
  77762. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  77763. + dwc_otg_hcd_t *dwc_otg_hcd;
  77764. + struct usb_hcd *hcd;
  77765. +
  77766. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  77767. +
  77768. + if (!otg_dev) {
  77769. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  77770. + return;
  77771. + }
  77772. +
  77773. + dwc_otg_hcd = otg_dev->hcd;
  77774. +
  77775. + if (!dwc_otg_hcd) {
  77776. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  77777. + return;
  77778. + }
  77779. +
  77780. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  77781. +
  77782. + if (!hcd) {
  77783. + DWC_DEBUGPL(DBG_ANY,
  77784. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  77785. + __func__);
  77786. + return;
  77787. + }
  77788. + usb_remove_hcd(hcd);
  77789. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  77790. + dwc_otg_hcd_remove(dwc_otg_hcd);
  77791. + usb_put_hcd(hcd);
  77792. +}
  77793. +
  77794. +/* =========================================================================
  77795. + * Linux HC Driver Functions
  77796. + * ========================================================================= */
  77797. +
  77798. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  77799. + * mode operation. Activates the root port. Returns 0 on success and a negative
  77800. + * error code on failure. */
  77801. +int hcd_start(struct usb_hcd *hcd)
  77802. +{
  77803. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77804. + struct usb_bus *bus;
  77805. +
  77806. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  77807. + bus = hcd_to_bus(hcd);
  77808. +
  77809. + hcd->state = HC_STATE_RUNNING;
  77810. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  77811. + return 0;
  77812. + }
  77813. +
  77814. + /* Initialize and connect root hub if one is not already attached */
  77815. + if (bus->root_hub) {
  77816. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  77817. + /* Inform the HUB driver to resume. */
  77818. + usb_hcd_resume_root_hub(hcd);
  77819. + }
  77820. +
  77821. + return 0;
  77822. +}
  77823. +
  77824. +/**
  77825. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  77826. + * stopped.
  77827. + */
  77828. +void hcd_stop(struct usb_hcd *hcd)
  77829. +{
  77830. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77831. +
  77832. + dwc_otg_hcd_stop(dwc_otg_hcd);
  77833. +}
  77834. +
  77835. +/** Returns the current frame number. */
  77836. +static int get_frame_number(struct usb_hcd *hcd)
  77837. +{
  77838. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77839. +
  77840. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  77841. +}
  77842. +
  77843. +#ifdef DEBUG
  77844. +static void dump_urb_info(struct urb *urb, char *fn_name)
  77845. +{
  77846. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  77847. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  77848. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  77849. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  77850. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  77851. + char *pipetype;
  77852. + switch (usb_pipetype(urb->pipe)) {
  77853. +case PIPE_CONTROL:
  77854. +pipetype = "CONTROL"; break; case PIPE_BULK:
  77855. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  77856. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  77857. +pipetype = "ISOCHRONOUS"; break; default:
  77858. + pipetype = "UNKNOWN"; break;};
  77859. + pipetype;}
  77860. + )) ;
  77861. + DWC_PRINTF(" Speed: %s\n", ( {
  77862. + char *speed; switch (urb->dev->speed) {
  77863. +case USB_SPEED_HIGH:
  77864. +speed = "HIGH"; break; case USB_SPEED_FULL:
  77865. +speed = "FULL"; break; case USB_SPEED_LOW:
  77866. +speed = "LOW"; break; default:
  77867. + speed = "UNKNOWN"; break;};
  77868. + speed;}
  77869. + )) ;
  77870. + DWC_PRINTF(" Max packet size: %d\n",
  77871. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  77872. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  77873. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  77874. + urb->transfer_buffer, (void *)urb->transfer_dma);
  77875. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  77876. + urb->setup_packet, (void *)urb->setup_dma);
  77877. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  77878. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77879. + int i;
  77880. + for (i = 0; i < urb->number_of_packets; i++) {
  77881. + DWC_PRINTF(" ISO Desc %d:\n", i);
  77882. + DWC_PRINTF(" offset: %d, length %d\n",
  77883. + urb->iso_frame_desc[i].offset,
  77884. + urb->iso_frame_desc[i].length);
  77885. + }
  77886. + }
  77887. +}
  77888. +#endif
  77889. +
  77890. +/** Starts processing a USB transfer request specified by a USB Request Block
  77891. + * (URB). mem_flags indicates the type of memory allocation to use while
  77892. + * processing this URB. */
  77893. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  77894. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77895. + struct usb_host_endpoint *ep,
  77896. +#endif
  77897. + struct urb *urb, gfp_t mem_flags)
  77898. +{
  77899. + int retval = 0;
  77900. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  77901. + struct usb_host_endpoint *ep = urb->ep;
  77902. +#endif
  77903. + dwc_irqflags_t irqflags;
  77904. + void **ref_ep_hcpriv = &ep->hcpriv;
  77905. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77906. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  77907. + int i;
  77908. + int alloc_bandwidth = 0;
  77909. + uint8_t ep_type = 0;
  77910. + uint32_t flags = 0;
  77911. + void *buf;
  77912. +
  77913. +#ifdef DEBUG
  77914. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77915. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  77916. + }
  77917. +#endif
  77918. +
  77919. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  77920. + return -EINVAL;
  77921. +
  77922. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  77923. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  77924. + if (!dwc_otg_hcd_is_bandwidth_allocated
  77925. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  77926. + alloc_bandwidth = 1;
  77927. + }
  77928. + }
  77929. +
  77930. + switch (usb_pipetype(urb->pipe)) {
  77931. + case PIPE_CONTROL:
  77932. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  77933. + break;
  77934. + case PIPE_ISOCHRONOUS:
  77935. + ep_type = USB_ENDPOINT_XFER_ISOC;
  77936. + break;
  77937. + case PIPE_BULK:
  77938. + ep_type = USB_ENDPOINT_XFER_BULK;
  77939. + break;
  77940. + case PIPE_INTERRUPT:
  77941. + ep_type = USB_ENDPOINT_XFER_INT;
  77942. + break;
  77943. + default:
  77944. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  77945. + }
  77946. +
  77947. + /* # of packets is often 0 - do we really need to call this then? */
  77948. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  77949. + urb->number_of_packets,
  77950. + mem_flags == GFP_ATOMIC ? 1 : 0);
  77951. +
  77952. + if(dwc_otg_urb == NULL)
  77953. + return -ENOMEM;
  77954. +
  77955. + if (!dwc_otg_urb && urb->number_of_packets)
  77956. + return -ENOMEM;
  77957. +
  77958. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  77959. + usb_pipeendpoint(urb->pipe), ep_type,
  77960. + usb_pipein(urb->pipe),
  77961. + usb_maxpacket(urb->dev, urb->pipe,
  77962. + !(usb_pipein(urb->pipe))));
  77963. +
  77964. + buf = urb->transfer_buffer;
  77965. + if (hcd->self.uses_dma) {
  77966. + /*
  77967. + * Calculate virtual address from physical address,
  77968. + * because some class driver may not fill transfer_buffer.
  77969. + * In Buffer DMA mode virual address is used,
  77970. + * when handling non DWORD aligned buffers.
  77971. + */
  77972. + //buf = phys_to_virt(urb->transfer_dma);
  77973. + // DMA addresses are bus addresses not physical addresses!
  77974. + buf = dma_to_virt(&urb->dev->dev, urb->transfer_dma);
  77975. + }
  77976. +
  77977. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  77978. + flags |= URB_GIVEBACK_ASAP;
  77979. + if (urb->transfer_flags & URB_ZERO_PACKET)
  77980. + flags |= URB_SEND_ZERO_PACKET;
  77981. +
  77982. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  77983. + urb->transfer_dma,
  77984. + urb->transfer_buffer_length,
  77985. + urb->setup_packet,
  77986. + urb->setup_dma, flags, urb->interval);
  77987. +
  77988. + for (i = 0; i < urb->number_of_packets; ++i) {
  77989. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  77990. + urb->
  77991. + iso_frame_desc[i].offset,
  77992. + urb->
  77993. + iso_frame_desc[i].length);
  77994. + }
  77995. +
  77996. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  77997. + urb->hcpriv = dwc_otg_urb;
  77998. +#if USB_URB_EP_LINKING
  77999. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  78000. + if (0 == retval)
  78001. +#endif
  78002. + {
  78003. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  78004. + /*(dwc_otg_qh_t **)*/
  78005. + ref_ep_hcpriv, 1);
  78006. + if (0 == retval) {
  78007. + if (alloc_bandwidth) {
  78008. + allocate_bus_bandwidth(hcd,
  78009. + dwc_otg_hcd_get_ep_bandwidth(
  78010. + dwc_otg_hcd, *ref_ep_hcpriv),
  78011. + urb);
  78012. + }
  78013. + } else {
  78014. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  78015. +#if USB_URB_EP_LINKING
  78016. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  78017. +#endif
  78018. + DWC_FREE(dwc_otg_urb);
  78019. + urb->hcpriv = NULL;
  78020. + if (retval == -DWC_E_NO_DEVICE)
  78021. + retval = -ENODEV;
  78022. + }
  78023. + }
  78024. +#if USB_URB_EP_LINKING
  78025. + else
  78026. + {
  78027. + DWC_FREE(dwc_otg_urb);
  78028. + urb->hcpriv = NULL;
  78029. + }
  78030. +#endif
  78031. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  78032. + return retval;
  78033. +}
  78034. +
  78035. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  78036. + * success. */
  78037. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  78038. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  78039. +#else
  78040. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  78041. +#endif
  78042. +{
  78043. + dwc_irqflags_t flags;
  78044. + dwc_otg_hcd_t *dwc_otg_hcd;
  78045. + int rc;
  78046. +
  78047. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  78048. +
  78049. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78050. +
  78051. +#ifdef DEBUG
  78052. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  78053. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  78054. + }
  78055. +#endif
  78056. +
  78057. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  78058. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  78059. + if (0 == rc) {
  78060. + if(urb->hcpriv != NULL) {
  78061. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  78062. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  78063. +
  78064. + DWC_FREE(urb->hcpriv);
  78065. + urb->hcpriv = NULL;
  78066. + }
  78067. + }
  78068. +
  78069. + if (0 == rc) {
  78070. + /* Higher layer software sets URB status. */
  78071. +#if USB_URB_EP_LINKING
  78072. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  78073. +#endif
  78074. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  78075. +
  78076. +
  78077. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  78078. + usb_hcd_giveback_urb(hcd, urb);
  78079. +#else
  78080. + usb_hcd_giveback_urb(hcd, urb, status);
  78081. +#endif
  78082. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  78083. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  78084. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  78085. + }
  78086. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  78087. + } else {
  78088. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  78089. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  78090. + rc);
  78091. + }
  78092. +
  78093. + return rc;
  78094. +}
  78095. +
  78096. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  78097. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  78098. + * must already be dequeued. */
  78099. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  78100. +{
  78101. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78102. +
  78103. + DWC_DEBUGPL(DBG_HCD,
  78104. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  78105. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  78106. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  78107. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  78108. + ep->hcpriv = NULL;
  78109. +}
  78110. +
  78111. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  78112. +/* Resets endpoint specific parameter values, in current version used to reset
  78113. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  78114. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  78115. +{
  78116. + dwc_irqflags_t flags;
  78117. + struct usb_device *udev = NULL;
  78118. + int epnum = usb_endpoint_num(&ep->desc);
  78119. + int is_out = usb_endpoint_dir_out(&ep->desc);
  78120. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  78121. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78122. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  78123. +
  78124. + if (dev)
  78125. + udev = to_usb_device(dev);
  78126. + else
  78127. + return;
  78128. +
  78129. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  78130. +
  78131. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  78132. + usb_settoggle(udev, epnum, is_out, 0);
  78133. + if (is_control)
  78134. + usb_settoggle(udev, epnum, !is_out, 0);
  78135. +
  78136. + if (ep->hcpriv) {
  78137. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  78138. + }
  78139. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  78140. +}
  78141. +#endif
  78142. +
  78143. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  78144. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  78145. + * interrupt.
  78146. + *
  78147. + * This function is called by the USB core when an interrupt occurs */
  78148. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  78149. +{
  78150. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78151. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  78152. + if (retval != 0) {
  78153. + S3C2410X_CLEAR_EINTPEND();
  78154. + }
  78155. + return IRQ_RETVAL(retval);
  78156. +}
  78157. +
  78158. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  78159. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  78160. + * is the status change indicator for the single root port. Returns 1 if either
  78161. + * change indicator is 1, otherwise returns 0. */
  78162. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  78163. +{
  78164. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  78165. +
  78166. + buf[0] = 0;
  78167. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  78168. +
  78169. + return (buf[0] != 0);
  78170. +}
  78171. +
  78172. +/** Handles hub class-specific requests. */
  78173. +int hub_control(struct usb_hcd *hcd,
  78174. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  78175. +{
  78176. + int retval;
  78177. +
  78178. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  78179. + typeReq, wValue, wIndex, buf, wLength);
  78180. +
  78181. + switch (retval) {
  78182. + case -DWC_E_INVALID:
  78183. + retval = -EINVAL;
  78184. + break;
  78185. + }
  78186. +
  78187. + return retval;
  78188. +}
  78189. +
  78190. +#endif /* DWC_DEVICE_ONLY */
  78191. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  78192. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 1970-01-01 01:00:00.000000000 +0100
  78193. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 2014-04-24 15:35:04.177565820 +0200
  78194. @@ -0,0 +1,959 @@
  78195. +/* ==========================================================================
  78196. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  78197. + * $Revision: #44 $
  78198. + * $Date: 2011/10/26 $
  78199. + * $Change: 1873028 $
  78200. + *
  78201. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  78202. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  78203. + * otherwise expressly agreed to in writing between Synopsys and you.
  78204. + *
  78205. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  78206. + * any End User Software License Agreement or Agreement for Licensed Product
  78207. + * with Synopsys or any supplement thereto. You are permitted to use and
  78208. + * redistribute this Software in source and binary forms, with or without
  78209. + * modification, provided that redistributions of source code must retain this
  78210. + * notice. You may not view, use, disclose, copy or distribute this file or
  78211. + * any information contained herein except pursuant to this license grant from
  78212. + * Synopsys. If you do not agree with this notice, including the disclaimer
  78213. + * below, then you are not authorized to use the Software.
  78214. + *
  78215. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  78216. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  78217. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  78218. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  78219. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78220. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  78221. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  78222. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  78223. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  78224. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  78225. + * DAMAGE.
  78226. + * ========================================================================== */
  78227. +#ifndef DWC_DEVICE_ONLY
  78228. +
  78229. +/**
  78230. + * @file
  78231. + *
  78232. + * This file contains the functions to manage Queue Heads and Queue
  78233. + * Transfer Descriptors.
  78234. + */
  78235. +
  78236. +#include "dwc_otg_hcd.h"
  78237. +#include "dwc_otg_regs.h"
  78238. +#include "dwc_otg_mphi_fix.h"
  78239. +
  78240. +extern bool microframe_schedule;
  78241. +
  78242. +/**
  78243. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  78244. + * removed from a list. QTD list should already be empty if called from URB
  78245. + * Dequeue.
  78246. + *
  78247. + * @param hcd HCD instance.
  78248. + * @param qh The QH to free.
  78249. + */
  78250. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78251. +{
  78252. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  78253. +
  78254. + /* Free each QTD in the QTD list */
  78255. + DWC_SPINLOCK(hcd->lock);
  78256. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  78257. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  78258. + dwc_otg_hcd_qtd_free(qtd);
  78259. + }
  78260. +
  78261. + if (hcd->core_if->dma_desc_enable) {
  78262. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  78263. + } else if (qh->dw_align_buf) {
  78264. + uint32_t buf_size;
  78265. + if (qh->ep_type == UE_ISOCHRONOUS) {
  78266. + buf_size = 4096;
  78267. + } else {
  78268. + buf_size = hcd->core_if->core_params->max_transfer_size;
  78269. + }
  78270. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  78271. + }
  78272. +
  78273. + DWC_FREE(qh);
  78274. + DWC_SPINUNLOCK(hcd->lock);
  78275. + return;
  78276. +}
  78277. +
  78278. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  78279. +#define HS_HOST_DELAY 5 /* nanoseconds */
  78280. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  78281. +#define HUB_LS_SETUP 333 /* nanoseconds */
  78282. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  78283. + /* convert & round nanoseconds to microseconds */
  78284. +
  78285. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  78286. +{
  78287. + unsigned long retval;
  78288. +
  78289. + switch (speed) {
  78290. + case USB_SPEED_HIGH:
  78291. + if (is_isoc) {
  78292. + retval =
  78293. + ((38 * 8 * 2083) +
  78294. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  78295. + HS_HOST_DELAY;
  78296. + } else {
  78297. + retval =
  78298. + ((55 * 8 * 2083) +
  78299. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  78300. + HS_HOST_DELAY;
  78301. + }
  78302. + break;
  78303. + case USB_SPEED_FULL:
  78304. + if (is_isoc) {
  78305. + retval =
  78306. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  78307. + if (is_in) {
  78308. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  78309. + } else {
  78310. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  78311. + }
  78312. + } else {
  78313. + retval =
  78314. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  78315. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  78316. + }
  78317. + break;
  78318. + case USB_SPEED_LOW:
  78319. + if (is_in) {
  78320. + retval =
  78321. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  78322. + 1000;
  78323. + retval =
  78324. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  78325. + retval;
  78326. + } else {
  78327. + retval =
  78328. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  78329. + 1000;
  78330. + retval =
  78331. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  78332. + retval;
  78333. + }
  78334. + break;
  78335. + default:
  78336. + DWC_WARN("Unknown device speed\n");
  78337. + retval = -1;
  78338. + }
  78339. +
  78340. + return NS_TO_US(retval);
  78341. +}
  78342. +
  78343. +/**
  78344. + * Initializes a QH structure.
  78345. + *
  78346. + * @param hcd The HCD state structure for the DWC OTG controller.
  78347. + * @param qh The QH to init.
  78348. + * @param urb Holds the information about the device/endpoint that we need
  78349. + * to initialize the QH.
  78350. + */
  78351. +#define SCHEDULE_SLOP 10
  78352. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  78353. +{
  78354. + char *speed, *type;
  78355. + int dev_speed;
  78356. + uint32_t hub_addr, hub_port;
  78357. +
  78358. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  78359. +
  78360. + /* Initialize QH */
  78361. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  78362. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  78363. +
  78364. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  78365. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  78366. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  78367. + DWC_LIST_INIT(&qh->qh_list_entry);
  78368. + qh->channel = NULL;
  78369. +
  78370. + /* FS/LS Enpoint on HS Hub
  78371. + * NOT virtual root hub */
  78372. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  78373. +
  78374. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  78375. + qh->do_split = 0;
  78376. + if (microframe_schedule)
  78377. + qh->speed = dev_speed;
  78378. +
  78379. + qh->nak_frame = 0xffff;
  78380. +
  78381. + if (((dev_speed == USB_SPEED_LOW) ||
  78382. + (dev_speed == USB_SPEED_FULL)) &&
  78383. + (hub_addr != 0 && hub_addr != 1)) {
  78384. + DWC_DEBUGPL(DBG_HCD,
  78385. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  78386. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  78387. + hub_port);
  78388. + qh->do_split = 1;
  78389. + qh->skip_count = 0;
  78390. + }
  78391. +
  78392. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  78393. + /* Compute scheduling parameters once and save them. */
  78394. + hprt0_data_t hprt;
  78395. +
  78396. + /** @todo Account for split transfers in the bus time. */
  78397. + int bytecount =
  78398. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  78399. +
  78400. + qh->usecs =
  78401. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  78402. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  78403. + bytecount);
  78404. + /* Start in a slightly future (micro)frame. */
  78405. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  78406. + SCHEDULE_SLOP);
  78407. + qh->interval = urb->interval;
  78408. +
  78409. +#if 0
  78410. + /* Increase interrupt polling rate for debugging. */
  78411. + if (qh->ep_type == UE_INTERRUPT) {
  78412. + qh->interval = 8;
  78413. + }
  78414. +#endif
  78415. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  78416. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  78417. + ((dev_speed == USB_SPEED_LOW) ||
  78418. + (dev_speed == USB_SPEED_FULL))) {
  78419. + qh->interval *= 8;
  78420. + qh->sched_frame |= 0x7;
  78421. + qh->start_split_frame = qh->sched_frame;
  78422. + }
  78423. +
  78424. + }
  78425. +
  78426. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  78427. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  78428. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  78429. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  78430. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  78431. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  78432. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  78433. + switch (dev_speed) {
  78434. + case USB_SPEED_LOW:
  78435. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  78436. + speed = "low";
  78437. + break;
  78438. + case USB_SPEED_FULL:
  78439. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  78440. + speed = "full";
  78441. + break;
  78442. + case USB_SPEED_HIGH:
  78443. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  78444. + speed = "high";
  78445. + break;
  78446. + default:
  78447. + speed = "?";
  78448. + break;
  78449. + }
  78450. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  78451. +
  78452. + switch (qh->ep_type) {
  78453. + case UE_ISOCHRONOUS:
  78454. + type = "isochronous";
  78455. + break;
  78456. + case UE_INTERRUPT:
  78457. + type = "interrupt";
  78458. + break;
  78459. + case UE_CONTROL:
  78460. + type = "control";
  78461. + break;
  78462. + case UE_BULK:
  78463. + type = "bulk";
  78464. + break;
  78465. + default:
  78466. + type = "?";
  78467. + break;
  78468. + }
  78469. +
  78470. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  78471. +
  78472. +#ifdef DEBUG
  78473. + if (qh->ep_type == UE_INTERRUPT) {
  78474. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  78475. + qh->usecs);
  78476. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  78477. + qh->interval);
  78478. + }
  78479. +#endif
  78480. +
  78481. +}
  78482. +
  78483. +/**
  78484. + * This function allocates and initializes a QH.
  78485. + *
  78486. + * @param hcd The HCD state structure for the DWC OTG controller.
  78487. + * @param urb Holds the information about the device/endpoint that we need
  78488. + * to initialize the QH.
  78489. + * @param atomic_alloc Flag to do atomic allocation if needed
  78490. + *
  78491. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  78492. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  78493. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  78494. +{
  78495. + dwc_otg_qh_t *qh;
  78496. +
  78497. + /* Allocate memory */
  78498. + /** @todo add memflags argument */
  78499. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  78500. + if (qh == NULL) {
  78501. + DWC_ERROR("qh allocation failed");
  78502. + return NULL;
  78503. + }
  78504. +
  78505. + qh_init(hcd, qh, urb);
  78506. +
  78507. + if (hcd->core_if->dma_desc_enable
  78508. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  78509. + dwc_otg_hcd_qh_free(hcd, qh);
  78510. + return NULL;
  78511. + }
  78512. +
  78513. + return qh;
  78514. +}
  78515. +
  78516. +/* microframe_schedule=0 start */
  78517. +
  78518. +/**
  78519. + * Checks that a channel is available for a periodic transfer.
  78520. + *
  78521. + * @return 0 if successful, negative error code otherise.
  78522. + */
  78523. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  78524. +{
  78525. + /*
  78526. + * Currently assuming that there is a dedicated host channnel for each
  78527. + * periodic transaction plus at least one host channel for
  78528. + * non-periodic transactions.
  78529. + */
  78530. + int status;
  78531. + int num_channels;
  78532. +
  78533. + num_channels = hcd->core_if->core_params->host_channels;
  78534. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  78535. + && (hcd->periodic_channels < num_channels - 1)) {
  78536. + status = 0;
  78537. + } else {
  78538. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  78539. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  78540. + status = -DWC_E_NO_SPACE;
  78541. + }
  78542. +
  78543. + return status;
  78544. +}
  78545. +
  78546. +/**
  78547. + * Checks that there is sufficient bandwidth for the specified QH in the
  78548. + * periodic schedule. For simplicity, this calculation assumes that all the
  78549. + * transfers in the periodic schedule may occur in the same (micro)frame.
  78550. + *
  78551. + * @param hcd The HCD state structure for the DWC OTG controller.
  78552. + * @param qh QH containing periodic bandwidth required.
  78553. + *
  78554. + * @return 0 if successful, negative error code otherwise.
  78555. + */
  78556. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78557. +{
  78558. + int status;
  78559. + int16_t max_claimed_usecs;
  78560. +
  78561. + status = 0;
  78562. +
  78563. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  78564. + /*
  78565. + * High speed mode.
  78566. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  78567. + */
  78568. +
  78569. + max_claimed_usecs = 100 - qh->usecs;
  78570. + } else {
  78571. + /*
  78572. + * Full speed mode.
  78573. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  78574. + */
  78575. + max_claimed_usecs = 900 - qh->usecs;
  78576. + }
  78577. +
  78578. + if (hcd->periodic_usecs > max_claimed_usecs) {
  78579. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  78580. + status = -DWC_E_NO_SPACE;
  78581. + }
  78582. +
  78583. + return status;
  78584. +}
  78585. +
  78586. +/* microframe_schedule=0 end */
  78587. +
  78588. +/**
  78589. + * Microframe scheduler
  78590. + * track the total use in hcd->frame_usecs
  78591. + * keep each qh use in qh->frame_usecs
  78592. + * when surrendering the qh then donate the time back
  78593. + */
  78594. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  78595. +
  78596. +/*
  78597. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  78598. + */
  78599. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  78600. +{
  78601. + int i;
  78602. + for (i=0; i<8; i++) {
  78603. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  78604. + }
  78605. + return 0;
  78606. +}
  78607. +
  78608. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78609. +{
  78610. + int i;
  78611. + unsigned short utime;
  78612. + int t_left;
  78613. + int ret;
  78614. + int done;
  78615. +
  78616. + ret = -1;
  78617. + utime = _qh->usecs;
  78618. + t_left = utime;
  78619. + i = 0;
  78620. + done = 0;
  78621. + while (done == 0) {
  78622. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  78623. + if (utime <= _hcd->frame_usecs[i]) {
  78624. + _hcd->frame_usecs[i] -= utime;
  78625. + _qh->frame_usecs[i] += utime;
  78626. + t_left -= utime;
  78627. + ret = i;
  78628. + done = 1;
  78629. + return ret;
  78630. + } else {
  78631. + i++;
  78632. + if (i == 8) {
  78633. + done = 1;
  78634. + ret = -1;
  78635. + }
  78636. + }
  78637. + }
  78638. + return ret;
  78639. + }
  78640. +
  78641. +/*
  78642. + * use this for FS apps that can span multiple uframes
  78643. + */
  78644. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78645. +{
  78646. + int i;
  78647. + int j;
  78648. + unsigned short utime;
  78649. + int t_left;
  78650. + int ret;
  78651. + int done;
  78652. + unsigned short xtime;
  78653. +
  78654. + ret = -1;
  78655. + utime = _qh->usecs;
  78656. + t_left = utime;
  78657. + i = 0;
  78658. + done = 0;
  78659. +loop:
  78660. + while (done == 0) {
  78661. + if(_hcd->frame_usecs[i] <= 0) {
  78662. + i++;
  78663. + if (i == 8) {
  78664. + done = 1;
  78665. + ret = -1;
  78666. + }
  78667. + goto loop;
  78668. + }
  78669. +
  78670. + /*
  78671. + * we need n consecutive slots
  78672. + * so use j as a start slot j plus j+1 must be enough time (for now)
  78673. + */
  78674. + xtime= _hcd->frame_usecs[i];
  78675. + for (j = i+1 ; j < 8 ; j++ ) {
  78676. + /*
  78677. + * if we add this frame remaining time to xtime we may
  78678. + * be OK, if not we need to test j for a complete frame
  78679. + */
  78680. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  78681. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  78682. + j = 8;
  78683. + ret = -1;
  78684. + continue;
  78685. + }
  78686. + }
  78687. + if (xtime >= utime) {
  78688. + ret = i;
  78689. + j = 8; /* stop loop with a good value ret */
  78690. + continue;
  78691. + }
  78692. + /* add the frame time to x time */
  78693. + xtime += _hcd->frame_usecs[j];
  78694. + /* we must have a fully available next frame or break */
  78695. + if ((xtime < utime)
  78696. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  78697. + ret = -1;
  78698. + j = 8; /* stop loop with a bad value ret */
  78699. + continue;
  78700. + }
  78701. + }
  78702. + if (ret >= 0) {
  78703. + t_left = utime;
  78704. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  78705. + t_left -= _hcd->frame_usecs[j];
  78706. + if ( t_left <= 0 ) {
  78707. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  78708. + _hcd->frame_usecs[j]= -t_left;
  78709. + ret = i;
  78710. + done = 1;
  78711. + } else {
  78712. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  78713. + _hcd->frame_usecs[j] = 0;
  78714. + }
  78715. + }
  78716. + } else {
  78717. + i++;
  78718. + if (i == 8) {
  78719. + done = 1;
  78720. + ret = -1;
  78721. + }
  78722. + }
  78723. + }
  78724. + return ret;
  78725. +}
  78726. +
  78727. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78728. +{
  78729. + int ret;
  78730. + ret = -1;
  78731. +
  78732. + if (_qh->speed == USB_SPEED_HIGH) {
  78733. + /* if this is a hs transaction we need a full frame */
  78734. + ret = find_single_uframe(_hcd, _qh);
  78735. + } else {
  78736. + /* if this is a fs transaction we may need a sequence of frames */
  78737. + ret = find_multi_uframe(_hcd, _qh);
  78738. + }
  78739. + return ret;
  78740. +}
  78741. +
  78742. +/**
  78743. + * Checks that the max transfer size allowed in a host channel is large enough
  78744. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  78745. + * transfer.
  78746. + *
  78747. + * @param hcd The HCD state structure for the DWC OTG controller.
  78748. + * @param qh QH for a periodic endpoint.
  78749. + *
  78750. + * @return 0 if successful, negative error code otherwise.
  78751. + */
  78752. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78753. +{
  78754. + int status;
  78755. + uint32_t max_xfer_size;
  78756. + uint32_t max_channel_xfer_size;
  78757. +
  78758. + status = 0;
  78759. +
  78760. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  78761. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  78762. +
  78763. + if (max_xfer_size > max_channel_xfer_size) {
  78764. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  78765. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  78766. + status = -DWC_E_NO_SPACE;
  78767. + }
  78768. +
  78769. + return status;
  78770. +}
  78771. +
  78772. +
  78773. +extern int g_next_sched_frame, g_np_count, g_np_sent;
  78774. +
  78775. +/**
  78776. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  78777. + *
  78778. + * @param hcd The HCD state structure for the DWC OTG controller.
  78779. + * @param qh QH for the periodic transfer. The QH should already contain the
  78780. + * scheduling information.
  78781. + *
  78782. + * @return 0 if successful, negative error code otherwise.
  78783. + */
  78784. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78785. +{
  78786. + int status = 0;
  78787. +
  78788. + if (microframe_schedule) {
  78789. + int frame;
  78790. + status = find_uframe(hcd, qh);
  78791. + frame = -1;
  78792. + if (status == 0) {
  78793. + frame = 7;
  78794. + } else {
  78795. + if (status > 0 )
  78796. + frame = status-1;
  78797. + }
  78798. +
  78799. + /* Set the new frame up */
  78800. + if (frame > -1) {
  78801. + qh->sched_frame &= ~0x7;
  78802. + qh->sched_frame |= (frame & 7);
  78803. + }
  78804. +
  78805. + if (status != -1)
  78806. + status = 0;
  78807. + } else {
  78808. + status = periodic_channel_available(hcd);
  78809. + if (status) {
  78810. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  78811. + return status;
  78812. + }
  78813. +
  78814. + status = check_periodic_bandwidth(hcd, qh);
  78815. + }
  78816. + if (status) {
  78817. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  78818. + "periodic transfer.\n", __func__);
  78819. + return status;
  78820. + }
  78821. + status = check_max_xfer_size(hcd, qh);
  78822. + if (status) {
  78823. + DWC_INFO("%s: Channel max transfer size too small "
  78824. + "for periodic transfer.\n", __func__);
  78825. + return status;
  78826. + }
  78827. +
  78828. + if (hcd->core_if->dma_desc_enable) {
  78829. + /* Don't rely on SOF and start in ready schedule */
  78830. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  78831. + }
  78832. + else {
  78833. + if(DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, g_next_sched_frame))
  78834. + {
  78835. + g_next_sched_frame = qh->sched_frame;
  78836. +
  78837. + }
  78838. + /* Always start in the inactive schedule. */
  78839. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  78840. + }
  78841. +
  78842. + if (!microframe_schedule) {
  78843. + /* Reserve the periodic channel. */
  78844. + hcd->periodic_channels++;
  78845. + }
  78846. +
  78847. + /* Update claimed usecs per (micro)frame. */
  78848. + hcd->periodic_usecs += qh->usecs;
  78849. +
  78850. + return status;
  78851. +}
  78852. +
  78853. +
  78854. +/**
  78855. + * This function adds a QH to either the non periodic or periodic schedule if
  78856. + * it is not already in the schedule. If the QH is already in the schedule, no
  78857. + * action is taken.
  78858. + *
  78859. + * @return 0 if successful, negative error code otherwise.
  78860. + */
  78861. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78862. +{
  78863. + int status = 0;
  78864. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78865. +
  78866. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  78867. + /* QH already in a schedule. */
  78868. + return status;
  78869. + }
  78870. +
  78871. + /* Add the new QH to the appropriate schedule */
  78872. + if (dwc_qh_is_non_per(qh)) {
  78873. + /* Always start in the inactive schedule. */
  78874. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  78875. + &qh->qh_list_entry);
  78876. + g_np_count++;
  78877. + } else {
  78878. + status = schedule_periodic(hcd, qh);
  78879. + if ( !hcd->periodic_qh_count ) {
  78880. + intr_mask.b.sofintr = 1;
  78881. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  78882. + intr_mask.d32, intr_mask.d32);
  78883. + }
  78884. + hcd->periodic_qh_count++;
  78885. + }
  78886. +
  78887. + return status;
  78888. +}
  78889. +
  78890. +/**
  78891. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  78892. + *
  78893. + * @param hcd The HCD state structure for the DWC OTG controller.
  78894. + * @param qh QH for the periodic transfer.
  78895. + */
  78896. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78897. +{
  78898. + int i;
  78899. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  78900. +
  78901. + /* Update claimed usecs per (micro)frame. */
  78902. + hcd->periodic_usecs -= qh->usecs;
  78903. +
  78904. + if (!microframe_schedule) {
  78905. + /* Release the periodic channel reservation. */
  78906. + hcd->periodic_channels--;
  78907. + } else {
  78908. + for (i = 0; i < 8; i++) {
  78909. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  78910. + qh->frame_usecs[i] = 0;
  78911. + }
  78912. + }
  78913. +}
  78914. +
  78915. +/**
  78916. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  78917. + * not freed.
  78918. + *
  78919. + * @param hcd The HCD state structure.
  78920. + * @param qh QH to remove from schedule. */
  78921. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78922. +{
  78923. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78924. +
  78925. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  78926. + /* QH is not in a schedule. */
  78927. + return;
  78928. + }
  78929. +
  78930. + if (dwc_qh_is_non_per(qh)) {
  78931. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  78932. + hcd->non_periodic_qh_ptr =
  78933. + hcd->non_periodic_qh_ptr->next;
  78934. + }
  78935. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  78936. +
  78937. + // If we've removed the last non-periodic entry then there are none left!
  78938. + g_np_count = g_np_sent;
  78939. + } else {
  78940. + deschedule_periodic(hcd, qh);
  78941. + hcd->periodic_qh_count--;
  78942. + if( !hcd->periodic_qh_count ) {
  78943. + intr_mask.b.sofintr = 1;
  78944. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  78945. + intr_mask.d32, 0);
  78946. + }
  78947. + }
  78948. +}
  78949. +
  78950. +/**
  78951. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  78952. + * non-periodic schedule. The QH is added to the inactive non-periodic
  78953. + * schedule if any QTDs are still attached to the QH.
  78954. + *
  78955. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  78956. + * there are any QTDs still attached to the QH, the QH is added to either the
  78957. + * periodic inactive schedule or the periodic ready schedule and its next
  78958. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  78959. + * the scheduled frame has been reached already. Otherwise it's placed in the
  78960. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  78961. + * completely removed from the periodic schedule.
  78962. + */
  78963. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  78964. + int sched_next_periodic_split)
  78965. +{
  78966. + if (dwc_qh_is_non_per(qh)) {
  78967. +
  78968. + dwc_otg_qh_t *qh_tmp;
  78969. + dwc_list_link_t *qh_list;
  78970. + DWC_LIST_FOREACH(qh_list, &hcd->non_periodic_sched_inactive)
  78971. + {
  78972. + qh_tmp = DWC_LIST_ENTRY(qh_list, struct dwc_otg_qh, qh_list_entry);
  78973. + if(qh_tmp == qh)
  78974. + {
  78975. + /*
  78976. + * FIQ is being disabled because this one nevers gets a np_count increment
  78977. + * This is still not absolutely correct, but it should fix itself with
  78978. + * just an unnecessary extra interrupt
  78979. + */
  78980. + g_np_sent = g_np_count;
  78981. + }
  78982. + }
  78983. +
  78984. +
  78985. + dwc_otg_hcd_qh_remove(hcd, qh);
  78986. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  78987. + /* Add back to inactive non-periodic schedule. */
  78988. + dwc_otg_hcd_qh_add(hcd, qh);
  78989. + }
  78990. + } else {
  78991. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  78992. +
  78993. + if (qh->do_split) {
  78994. + /* Schedule the next continuing periodic split transfer */
  78995. + if (sched_next_periodic_split) {
  78996. +
  78997. + qh->sched_frame = frame_number;
  78998. +
  78999. + if (dwc_frame_num_le(frame_number,
  79000. + dwc_frame_num_inc
  79001. + (qh->start_split_frame,
  79002. + 1))) {
  79003. + /*
  79004. + * Allow one frame to elapse after start
  79005. + * split microframe before scheduling
  79006. + * complete split, but DONT if we are
  79007. + * doing the next start split in the
  79008. + * same frame for an ISOC out.
  79009. + */
  79010. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  79011. + (qh->ep_is_in != 0)) {
  79012. + qh->sched_frame =
  79013. + dwc_frame_num_inc(qh->sched_frame, 1);
  79014. + }
  79015. + }
  79016. + } else {
  79017. + qh->sched_frame =
  79018. + dwc_frame_num_inc(qh->start_split_frame,
  79019. + qh->interval);
  79020. + if (dwc_frame_num_le
  79021. + (qh->sched_frame, frame_number)) {
  79022. + qh->sched_frame = frame_number;
  79023. + }
  79024. + qh->sched_frame |= 0x7;
  79025. + qh->start_split_frame = qh->sched_frame;
  79026. + }
  79027. + } else {
  79028. + qh->sched_frame =
  79029. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  79030. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  79031. + qh->sched_frame = frame_number;
  79032. + }
  79033. + }
  79034. +
  79035. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  79036. + dwc_otg_hcd_qh_remove(hcd, qh);
  79037. + } else {
  79038. + /*
  79039. + * Remove from periodic_sched_queued and move to
  79040. + * appropriate queue.
  79041. + */
  79042. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  79043. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  79044. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  79045. + &qh->qh_list_entry);
  79046. + } else {
  79047. + if(!dwc_frame_num_le(g_next_sched_frame, qh->sched_frame))
  79048. + {
  79049. + g_next_sched_frame = qh->sched_frame;
  79050. + }
  79051. +
  79052. + DWC_LIST_MOVE_HEAD
  79053. + (&hcd->periodic_sched_inactive,
  79054. + &qh->qh_list_entry);
  79055. + }
  79056. + }
  79057. + }
  79058. +}
  79059. +
  79060. +/**
  79061. + * This function allocates and initializes a QTD.
  79062. + *
  79063. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  79064. + * pointing to each other so each pair should have a unique correlation.
  79065. + * @param atomic_alloc Flag to do atomic alloc if needed
  79066. + *
  79067. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  79068. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  79069. +{
  79070. + dwc_otg_qtd_t *qtd;
  79071. +
  79072. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  79073. + if (qtd == NULL) {
  79074. + return NULL;
  79075. + }
  79076. +
  79077. + dwc_otg_hcd_qtd_init(qtd, urb);
  79078. + return qtd;
  79079. +}
  79080. +
  79081. +/**
  79082. + * Initializes a QTD structure.
  79083. + *
  79084. + * @param qtd The QTD to initialize.
  79085. + * @param urb The URB to use for initialization. */
  79086. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  79087. +{
  79088. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  79089. + qtd->urb = urb;
  79090. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  79091. + /*
  79092. + * The only time the QTD data toggle is used is on the data
  79093. + * phase of control transfers. This phase always starts with
  79094. + * DATA1.
  79095. + */
  79096. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  79097. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  79098. + }
  79099. +
  79100. + /* start split */
  79101. + qtd->complete_split = 0;
  79102. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  79103. + qtd->isoc_split_offset = 0;
  79104. + qtd->in_process = 0;
  79105. +
  79106. + /* Store the qtd ptr in the urb to reference what QTD. */
  79107. + urb->qtd = qtd;
  79108. + return;
  79109. +}
  79110. +
  79111. +/**
  79112. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  79113. + * QH to place the QTD into. If it does not find a QH, then it will create a
  79114. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  79115. + * is placed into the proper schedule based on its EP type.
  79116. + * HCD lock must be held and interrupts must be disabled on entry
  79117. + *
  79118. + * @param[in] qtd The QTD to add
  79119. + * @param[in] hcd The DWC HCD structure
  79120. + * @param[out] qh out parameter to return queue head
  79121. + * @param atomic_alloc Flag to do atomic alloc if needed
  79122. + *
  79123. + * @return 0 if successful, negative error code otherwise.
  79124. + */
  79125. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  79126. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  79127. +{
  79128. + int retval = 0;
  79129. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  79130. +
  79131. + /*
  79132. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  79133. + * doesn't exist.
  79134. + */
  79135. + if (*qh == NULL) {
  79136. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  79137. + if (*qh == NULL) {
  79138. + retval = -DWC_E_NO_MEMORY;
  79139. + goto done;
  79140. + }
  79141. + }
  79142. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  79143. + if (retval == 0) {
  79144. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  79145. + qtd_list_entry);
  79146. + qtd->qh = *qh;
  79147. + }
  79148. +done:
  79149. +
  79150. + return retval;
  79151. +}
  79152. +
  79153. +#endif /* DWC_DEVICE_ONLY */
  79154. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c
  79155. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c 1970-01-01 01:00:00.000000000 +0100
  79156. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c 2014-04-24 15:35:04.177565820 +0200
  79157. @@ -0,0 +1,113 @@
  79158. +#include "dwc_otg_regs.h"
  79159. +#include "dwc_otg_dbg.h"
  79160. +
  79161. +void dwc_debug_print_core_int_reg(gintsts_data_t gintsts, const char* function_name)
  79162. +{
  79163. + DWC_DEBUGPL(DBG_USER, "*** Debugging from within the %s function: ***\n"
  79164. + "curmode: %1i Modemismatch: %1i otgintr: %1i sofintr: %1i\n"
  79165. + "rxstsqlvl: %1i nptxfempty : %1i ginnakeff: %1i goutnakeff: %1i\n"
  79166. + "ulpickint: %1i i2cintr: %1i erlysuspend:%1i usbsuspend: %1i\n"
  79167. + "usbreset: %1i enumdone: %1i isooutdrop: %1i eopframe: %1i\n"
  79168. + "restoredone: %1i epmismatch: %1i inepint: %1i outepintr: %1i\n"
  79169. + "incomplisoin:%1i incomplisoout:%1i fetsusp: %1i resetdet: %1i\n"
  79170. + "portintr: %1i hcintr: %1i ptxfempty: %1i lpmtranrcvd:%1i\n"
  79171. + "conidstschng:%1i disconnect: %1i sessreqintr:%1i wkupintr: %1i\n",
  79172. + function_name,
  79173. + gintsts.b.curmode,
  79174. + gintsts.b.modemismatch,
  79175. + gintsts.b.otgintr,
  79176. + gintsts.b.sofintr,
  79177. + gintsts.b.rxstsqlvl,
  79178. + gintsts.b.nptxfempty,
  79179. + gintsts.b.ginnakeff,
  79180. + gintsts.b.goutnakeff,
  79181. + gintsts.b.ulpickint,
  79182. + gintsts.b.i2cintr,
  79183. + gintsts.b.erlysuspend,
  79184. + gintsts.b.usbsuspend,
  79185. + gintsts.b.usbreset,
  79186. + gintsts.b.enumdone,
  79187. + gintsts.b.isooutdrop,
  79188. + gintsts.b.eopframe,
  79189. + gintsts.b.restoredone,
  79190. + gintsts.b.epmismatch,
  79191. + gintsts.b.inepint,
  79192. + gintsts.b.outepintr,
  79193. + gintsts.b.incomplisoin,
  79194. + gintsts.b.incomplisoout,
  79195. + gintsts.b.fetsusp,
  79196. + gintsts.b.resetdet,
  79197. + gintsts.b.portintr,
  79198. + gintsts.b.hcintr,
  79199. + gintsts.b.ptxfempty,
  79200. + gintsts.b.lpmtranrcvd,
  79201. + gintsts.b.conidstschng,
  79202. + gintsts.b.disconnect,
  79203. + gintsts.b.sessreqintr,
  79204. + gintsts.b.wkupintr);
  79205. + return;
  79206. +}
  79207. +
  79208. +void dwc_debug_core_int_mask(gintmsk_data_t gintmsk, const char* function_name)
  79209. +{
  79210. + DWC_DEBUGPL(DBG_USER, "Interrupt Mask status (called from %s) :\n"
  79211. + "modemismatch: %1i otgintr: %1i sofintr: %1i rxstsqlvl: %1i\n"
  79212. + "nptxfempty: %1i ginnakeff: %1i goutnakeff: %1i ulpickint: %1i\n"
  79213. + "i2cintr: %1i erlysuspend:%1i usbsuspend: %1i usbreset: %1i\n"
  79214. + "enumdone: %1i isooutdrop: %1i eopframe: %1i restoredone: %1i\n"
  79215. + "epmismatch: %1i inepintr: %1i outepintr: %1i incomplisoin:%1i\n"
  79216. + "incomplisoout:%1i fetsusp: %1i resetdet: %1i portintr: %1i\n"
  79217. + "hcintr: %1i ptxfempty: %1i lpmtranrcvd:%1i conidstschng:%1i\n"
  79218. + "disconnect: %1i sessreqintr:%1i wkupintr: %1i\n",
  79219. + function_name,
  79220. + gintmsk.b.modemismatch,
  79221. + gintmsk.b.otgintr,
  79222. + gintmsk.b.sofintr,
  79223. + gintmsk.b.rxstsqlvl,
  79224. + gintmsk.b.nptxfempty,
  79225. + gintmsk.b.ginnakeff,
  79226. + gintmsk.b.goutnakeff,
  79227. + gintmsk.b.ulpickint,
  79228. + gintmsk.b.i2cintr,
  79229. + gintmsk.b.erlysuspend,
  79230. + gintmsk.b.usbsuspend,
  79231. + gintmsk.b.usbreset,
  79232. + gintmsk.b.enumdone,
  79233. + gintmsk.b.isooutdrop,
  79234. + gintmsk.b.eopframe,
  79235. + gintmsk.b.restoredone,
  79236. + gintmsk.b.epmismatch,
  79237. + gintmsk.b.inepintr,
  79238. + gintmsk.b.outepintr,
  79239. + gintmsk.b.incomplisoin,
  79240. + gintmsk.b.incomplisoout,
  79241. + gintmsk.b.fetsusp,
  79242. + gintmsk.b.resetdet,
  79243. + gintmsk.b.portintr,
  79244. + gintmsk.b.hcintr,
  79245. + gintmsk.b.ptxfempty,
  79246. + gintmsk.b.lpmtranrcvd,
  79247. + gintmsk.b.conidstschng,
  79248. + gintmsk.b.disconnect,
  79249. + gintmsk.b.sessreqintr,
  79250. + gintmsk.b.wkupintr);
  79251. + return;
  79252. +}
  79253. +
  79254. +void dwc_debug_otg_int(gotgint_data_t gotgint, const char* function_name)
  79255. +{
  79256. + DWC_DEBUGPL(DBG_USER, "otg int register (from %s function):\n"
  79257. + "sesenddet:%1i sesreqsucstschung:%2i hstnegsucstschng:%1i\n"
  79258. + "hstnegdet:%1i adevtoutchng: %2i debdone: %1i\n"
  79259. + "mvic: %1i\n",
  79260. + function_name,
  79261. + gotgint.b.sesenddet,
  79262. + gotgint.b.sesreqsucstschng,
  79263. + gotgint.b.hstnegsucstschng,
  79264. + gotgint.b.hstnegdet,
  79265. + gotgint.b.adevtoutchng,
  79266. + gotgint.b.debdone,
  79267. + gotgint.b.mvic);
  79268. +
  79269. + return;
  79270. +}
  79271. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h
  79272. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h 1970-01-01 01:00:00.000000000 +0100
  79273. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h 2014-04-24 15:35:04.177565820 +0200
  79274. @@ -0,0 +1,48 @@
  79275. +#ifndef __DWC_OTG_MPHI_FIX_H__
  79276. +#define __DWC_OTG_MPHI_FIX_H__
  79277. +#define FIQ_WRITE(_addr_,_data_) (*(volatile uint32_t *) (_addr_) = (_data_))
  79278. +#define FIQ_READ(_addr_) (*(volatile uint32_t *) (_addr_))
  79279. +
  79280. +typedef struct {
  79281. + volatile void* base;
  79282. + volatile void* ctrl;
  79283. + volatile void* outdda;
  79284. + volatile void* outddb;
  79285. + volatile void* intstat;
  79286. +} mphi_regs_t;
  79287. +
  79288. +void dwc_debug_print_core_int_reg(gintsts_data_t gintsts, const char* function_name);
  79289. +void dwc_debug_core_int_mask(gintsts_data_t gintmsk, const char* function_name);
  79290. +void dwc_debug_otg_int(gotgint_data_t gotgint, const char* function_name);
  79291. +
  79292. +extern gintsts_data_t gintsts_saved;
  79293. +
  79294. +#ifdef DEBUG
  79295. +#define DWC_DBG_PRINT_CORE_INT(_arg_) dwc_debug_print_core_int_reg(_arg_,__func__)
  79296. +#define DWC_DBG_PRINT_CORE_INT_MASK(_arg_) dwc_debug_core_int_mask(_arg_,__func__)
  79297. +#define DWC_DBG_PRINT_OTG_INT(_arg_) dwc_debug_otg_int(_arg_,__func__)
  79298. +
  79299. +#else
  79300. +#define DWC_DBG_PRINT_CORE_INT(_arg_)
  79301. +#define DWC_DBG_PRINT_CORE_INT_MASK(_arg_)
  79302. +#define DWC_DBG_PRINT_OTG_INT(_arg_)
  79303. +
  79304. +#endif
  79305. +
  79306. +typedef enum {
  79307. + FIQDBG_SCHED = (1 << 0),
  79308. + FIQDBG_INT = (1 << 1),
  79309. + FIQDBG_ERR = (1 << 2),
  79310. + FIQDBG_PORTHUB = (1 << 3),
  79311. +} FIQDBG_T;
  79312. +
  79313. +void _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...);
  79314. +#ifdef FIQ_DEBUG
  79315. +#define fiq_print _fiq_print
  79316. +#else
  79317. +#define fiq_print(x, y, ...)
  79318. +#endif
  79319. +
  79320. +extern bool fiq_fix_enable, nak_holdoff_enable, fiq_split_enable;
  79321. +
  79322. +#endif
  79323. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  79324. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 1970-01-01 01:00:00.000000000 +0100
  79325. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 2014-04-24 15:35:04.177565820 +0200
  79326. @@ -0,0 +1,188 @@
  79327. +#ifndef _DWC_OS_DEP_H_
  79328. +#define _DWC_OS_DEP_H_
  79329. +
  79330. +/**
  79331. + * @file
  79332. + *
  79333. + * This file contains OS dependent structures.
  79334. + *
  79335. + */
  79336. +
  79337. +#include <linux/kernel.h>
  79338. +#include <linux/module.h>
  79339. +#include <linux/moduleparam.h>
  79340. +#include <linux/init.h>
  79341. +#include <linux/device.h>
  79342. +#include <linux/errno.h>
  79343. +#include <linux/types.h>
  79344. +#include <linux/slab.h>
  79345. +#include <linux/list.h>
  79346. +#include <linux/interrupt.h>
  79347. +#include <linux/ctype.h>
  79348. +#include <linux/string.h>
  79349. +#include <linux/dma-mapping.h>
  79350. +#include <linux/jiffies.h>
  79351. +#include <linux/delay.h>
  79352. +#include <linux/timer.h>
  79353. +#include <linux/workqueue.h>
  79354. +#include <linux/stat.h>
  79355. +#include <linux/pci.h>
  79356. +
  79357. +#include <linux/version.h>
  79358. +
  79359. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  79360. +# include <linux/irq.h>
  79361. +#endif
  79362. +
  79363. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  79364. +# include <linux/usb/ch9.h>
  79365. +#else
  79366. +# include <linux/usb_ch9.h>
  79367. +#endif
  79368. +
  79369. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  79370. +# include <linux/usb/gadget.h>
  79371. +#else
  79372. +# include <linux/usb_gadget.h>
  79373. +#endif
  79374. +
  79375. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  79376. +# include <asm/irq.h>
  79377. +#endif
  79378. +
  79379. +#ifdef PCI_INTERFACE
  79380. +# include <asm/io.h>
  79381. +#endif
  79382. +
  79383. +#ifdef LM_INTERFACE
  79384. +# include <asm/unaligned.h>
  79385. +# include <asm/sizes.h>
  79386. +# include <asm/param.h>
  79387. +# include <asm/io.h>
  79388. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  79389. +# include <asm/arch/hardware.h>
  79390. +# include <asm/arch/lm.h>
  79391. +# include <asm/arch/irqs.h>
  79392. +# include <asm/arch/regs-irq.h>
  79393. +# else
  79394. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  79395. + here we assume that the machine architecture provides definitions
  79396. + in its own header
  79397. +*/
  79398. +# include <mach/lm.h>
  79399. +# include <mach/hardware.h>
  79400. +# endif
  79401. +#endif
  79402. +
  79403. +#ifdef PLATFORM_INTERFACE
  79404. +#include <linux/platform_device.h>
  79405. +#include <asm/mach/map.h>
  79406. +#endif
  79407. +
  79408. +/** The OS page size */
  79409. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  79410. +
  79411. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  79412. +typedef int gfp_t;
  79413. +#endif
  79414. +
  79415. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  79416. +# define IRQF_SHARED SA_SHIRQ
  79417. +#endif
  79418. +
  79419. +typedef struct os_dependent {
  79420. + /** Base address returned from ioremap() */
  79421. + void *base;
  79422. +
  79423. + /** Register offset for Diagnostic API */
  79424. + uint32_t reg_offset;
  79425. +
  79426. + /** Base address for MPHI peripheral */
  79427. + void *mphi_base;
  79428. +
  79429. +#ifdef LM_INTERFACE
  79430. + struct lm_device *lmdev;
  79431. +#elif defined(PCI_INTERFACE)
  79432. + struct pci_dev *pcidev;
  79433. +
  79434. + /** Start address of a PCI region */
  79435. + resource_size_t rsrc_start;
  79436. +
  79437. + /** Length address of a PCI region */
  79438. + resource_size_t rsrc_len;
  79439. +#elif defined(PLATFORM_INTERFACE)
  79440. + struct platform_device *platformdev;
  79441. +#endif
  79442. +
  79443. +} os_dependent_t;
  79444. +
  79445. +#ifdef __cplusplus
  79446. +}
  79447. +#endif
  79448. +
  79449. +
  79450. +
  79451. +/* Type for the our device on the chosen bus */
  79452. +#if defined(LM_INTERFACE)
  79453. +typedef struct lm_device dwc_bus_dev_t;
  79454. +#elif defined(PCI_INTERFACE)
  79455. +typedef struct pci_dev dwc_bus_dev_t;
  79456. +#elif defined(PLATFORM_INTERFACE)
  79457. +typedef struct platform_device dwc_bus_dev_t;
  79458. +#endif
  79459. +
  79460. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  79461. +#if defined(LM_INTERFACE)
  79462. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  79463. +#elif defined(PCI_INTERFACE)
  79464. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  79465. +#elif defined(PLATFORM_INTERFACE)
  79466. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  79467. +#endif
  79468. +
  79469. +/**
  79470. + * Helper macro returning the otg_device structure of a given struct device
  79471. + *
  79472. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  79473. + */
  79474. +#ifdef LM_INTERFACE
  79475. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  79476. + struct lm_device *lm_dev = \
  79477. + container_of(_dev, struct lm_device, dev); \
  79478. + _var = lm_get_drvdata(lm_dev); \
  79479. + } while (0)
  79480. +
  79481. +#elif defined(PCI_INTERFACE)
  79482. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  79483. + _var = dev_get_drvdata(_dev); \
  79484. + } while (0)
  79485. +
  79486. +#elif defined(PLATFORM_INTERFACE)
  79487. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  79488. + struct platform_device *platform_dev = \
  79489. + container_of(_dev, struct platform_device, dev); \
  79490. + _var = platform_get_drvdata(platform_dev); \
  79491. + } while (0)
  79492. +#endif
  79493. +
  79494. +
  79495. +/**
  79496. + * Helper macro returning the struct dev of the given struct os_dependent
  79497. + *
  79498. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  79499. + */
  79500. +#ifdef LM_INTERFACE
  79501. +#define DWC_OTG_OS_GETDEV(_osdep) \
  79502. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  79503. +#elif defined(PCI_INTERFACE)
  79504. +#define DWC_OTG_OS_GETDEV(_osdep) \
  79505. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  79506. +#elif defined(PLATFORM_INTERFACE)
  79507. +#define DWC_OTG_OS_GETDEV(_osdep) \
  79508. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  79509. +#endif
  79510. +
  79511. +
  79512. +
  79513. +
  79514. +#endif /* _DWC_OS_DEP_H_ */
  79515. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  79516. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 1970-01-01 01:00:00.000000000 +0100
  79517. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 2014-04-24 15:35:04.177565820 +0200
  79518. @@ -0,0 +1,2708 @@
  79519. +/* ==========================================================================
  79520. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  79521. + * $Revision: #101 $
  79522. + * $Date: 2012/08/10 $
  79523. + * $Change: 2047372 $
  79524. + *
  79525. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  79526. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  79527. + * otherwise expressly agreed to in writing between Synopsys and you.
  79528. + *
  79529. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  79530. + * any End User Software License Agreement or Agreement for Licensed Product
  79531. + * with Synopsys or any supplement thereto. You are permitted to use and
  79532. + * redistribute this Software in source and binary forms, with or without
  79533. + * modification, provided that redistributions of source code must retain this
  79534. + * notice. You may not view, use, disclose, copy or distribute this file or
  79535. + * any information contained herein except pursuant to this license grant from
  79536. + * Synopsys. If you do not agree with this notice, including the disclaimer
  79537. + * below, then you are not authorized to use the Software.
  79538. + *
  79539. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  79540. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  79541. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  79542. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  79543. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  79544. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  79545. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  79546. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  79547. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  79548. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  79549. + * DAMAGE.
  79550. + * ========================================================================== */
  79551. +#ifndef DWC_HOST_ONLY
  79552. +
  79553. +/** @file
  79554. + * This file implements PCD Core. All code in this file is portable and doesn't
  79555. + * use any OS specific functions.
  79556. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  79557. + * header file, which can be used to implement OS specific PCD interface.
  79558. + *
  79559. + * An important function of the PCD is managing interrupts generated
  79560. + * by the DWC_otg controller. The implementation of the DWC_otg device
  79561. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  79562. + *
  79563. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  79564. + * @todo Does it work when the request size is greater than DEPTSIZ
  79565. + * transfer size
  79566. + *
  79567. + */
  79568. +
  79569. +#include "dwc_otg_pcd.h"
  79570. +
  79571. +#ifdef DWC_UTE_CFI
  79572. +#include "dwc_otg_cfi.h"
  79573. +
  79574. +extern int init_cfi(cfiobject_t * cfiobj);
  79575. +#endif
  79576. +
  79577. +/**
  79578. + * Choose endpoint from ep arrays using usb_ep structure.
  79579. + */
  79580. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  79581. +{
  79582. + int i;
  79583. + if (pcd->ep0.priv == handle) {
  79584. + return &pcd->ep0;
  79585. + }
  79586. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  79587. + if (pcd->in_ep[i].priv == handle)
  79588. + return &pcd->in_ep[i];
  79589. + if (pcd->out_ep[i].priv == handle)
  79590. + return &pcd->out_ep[i];
  79591. + }
  79592. +
  79593. + return NULL;
  79594. +}
  79595. +
  79596. +/**
  79597. + * This function completes a request. It call's the request call back.
  79598. + */
  79599. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  79600. + int32_t status)
  79601. +{
  79602. + unsigned stopped = ep->stopped;
  79603. +
  79604. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  79605. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  79606. +
  79607. + /* don't modify queue heads during completion callback */
  79608. + ep->stopped = 1;
  79609. + /* spin_unlock/spin_lock now done in fops->complete() */
  79610. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  79611. + req->actual);
  79612. +
  79613. + if (ep->pcd->request_pending > 0) {
  79614. + --ep->pcd->request_pending;
  79615. + }
  79616. +
  79617. + ep->stopped = stopped;
  79618. + DWC_FREE(req);
  79619. +}
  79620. +
  79621. +/**
  79622. + * This function terminates all the requsts in the EP request queue.
  79623. + */
  79624. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  79625. +{
  79626. + dwc_otg_pcd_request_t *req;
  79627. +
  79628. + ep->stopped = 1;
  79629. +
  79630. + /* called with irqs blocked?? */
  79631. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  79632. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  79633. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  79634. + }
  79635. +}
  79636. +
  79637. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  79638. + const struct dwc_otg_pcd_function_ops *fops)
  79639. +{
  79640. + pcd->fops = fops;
  79641. +}
  79642. +
  79643. +/**
  79644. + * PCD Callback function for initializing the PCD when switching to
  79645. + * device mode.
  79646. + *
  79647. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79648. + */
  79649. +static int32_t dwc_otg_pcd_start_cb(void *p)
  79650. +{
  79651. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79652. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  79653. +
  79654. + /*
  79655. + * Initialized the Core for Device mode.
  79656. + */
  79657. + if (dwc_otg_is_device_mode(core_if)) {
  79658. + dwc_otg_core_dev_init(core_if);
  79659. + /* Set core_if's lock pointer to the pcd->lock */
  79660. + core_if->lock = pcd->lock;
  79661. + }
  79662. + return 1;
  79663. +}
  79664. +
  79665. +/** CFI-specific buffer allocation function for EP */
  79666. +#ifdef DWC_UTE_CFI
  79667. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  79668. + size_t buflen, int flags)
  79669. +{
  79670. + dwc_otg_pcd_ep_t *ep;
  79671. + ep = get_ep_from_handle(pcd, pep);
  79672. + if (!ep) {
  79673. + DWC_WARN("bad ep\n");
  79674. + return -DWC_E_INVALID;
  79675. + }
  79676. +
  79677. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  79678. + flags);
  79679. +}
  79680. +#else
  79681. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  79682. + size_t buflen, int flags);
  79683. +#endif
  79684. +
  79685. +/**
  79686. + * PCD Callback function for notifying the PCD when resuming from
  79687. + * suspend.
  79688. + *
  79689. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79690. + */
  79691. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  79692. +{
  79693. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79694. +
  79695. + if (pcd->fops->resume) {
  79696. + pcd->fops->resume(pcd);
  79697. + }
  79698. +
  79699. + /* Stop the SRP timeout timer. */
  79700. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  79701. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  79702. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  79703. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  79704. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  79705. + }
  79706. + }
  79707. + return 1;
  79708. +}
  79709. +
  79710. +/**
  79711. + * PCD Callback function for notifying the PCD device is suspended.
  79712. + *
  79713. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79714. + */
  79715. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  79716. +{
  79717. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79718. +
  79719. + if (pcd->fops->suspend) {
  79720. + DWC_SPINUNLOCK(pcd->lock);
  79721. + pcd->fops->suspend(pcd);
  79722. + DWC_SPINLOCK(pcd->lock);
  79723. + }
  79724. +
  79725. + return 1;
  79726. +}
  79727. +
  79728. +/**
  79729. + * PCD Callback function for stopping the PCD when switching to Host
  79730. + * mode.
  79731. + *
  79732. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79733. + */
  79734. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  79735. +{
  79736. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79737. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  79738. +
  79739. + dwc_otg_pcd_stop(pcd);
  79740. + return 1;
  79741. +}
  79742. +
  79743. +/**
  79744. + * PCD Callback structure for handling mode switching.
  79745. + */
  79746. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  79747. + .start = dwc_otg_pcd_start_cb,
  79748. + .stop = dwc_otg_pcd_stop_cb,
  79749. + .suspend = dwc_otg_pcd_suspend_cb,
  79750. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  79751. + .p = 0, /* Set at registration */
  79752. +};
  79753. +
  79754. +/**
  79755. + * This function allocates a DMA Descriptor chain for the Endpoint
  79756. + * buffer to be used for a transfer to/from the specified endpoint.
  79757. + */
  79758. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  79759. + uint32_t count)
  79760. +{
  79761. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  79762. + dma_desc_addr);
  79763. +}
  79764. +
  79765. +/**
  79766. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  79767. + */
  79768. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  79769. + uint32_t dma_desc_addr, uint32_t count)
  79770. +{
  79771. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  79772. + dma_desc_addr);
  79773. +}
  79774. +
  79775. +#ifdef DWC_EN_ISOC
  79776. +
  79777. +/**
  79778. + * This function initializes a descriptor chain for Isochronous transfer
  79779. + *
  79780. + * @param core_if Programming view of DWC_otg controller.
  79781. + * @param dwc_ep The EP to start the transfer on.
  79782. + *
  79783. + */
  79784. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  79785. + dwc_ep_t * dwc_ep)
  79786. +{
  79787. +
  79788. + dsts_data_t dsts = {.d32 = 0 };
  79789. + depctl_data_t depctl = {.d32 = 0 };
  79790. + volatile uint32_t *addr;
  79791. + int i, j;
  79792. + uint32_t len;
  79793. +
  79794. + if (dwc_ep->is_in)
  79795. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  79796. + else
  79797. + dwc_ep->desc_cnt =
  79798. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  79799. + dwc_ep->bInterval;
  79800. +
  79801. + /** Allocate descriptors for double buffering */
  79802. + dwc_ep->iso_desc_addr =
  79803. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  79804. + dwc_ep->desc_cnt * 2);
  79805. + if (dwc_ep->desc_addr) {
  79806. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  79807. + return;
  79808. + }
  79809. +
  79810. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79811. +
  79812. + /** ISO OUT EP */
  79813. + if (dwc_ep->is_in == 0) {
  79814. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  79815. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  79816. + dma_addr_t dma_ad;
  79817. + uint32_t data_per_desc;
  79818. + dwc_otg_dev_out_ep_regs_t *out_regs =
  79819. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  79820. + int offset;
  79821. +
  79822. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  79823. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  79824. +
  79825. + /** Buffer 0 descriptors setup */
  79826. + dma_ad = dwc_ep->dma_addr0;
  79827. +
  79828. + sts.b_iso_out.bs = BS_HOST_READY;
  79829. + sts.b_iso_out.rxsts = 0;
  79830. + sts.b_iso_out.l = 0;
  79831. + sts.b_iso_out.sp = 0;
  79832. + sts.b_iso_out.ioc = 0;
  79833. + sts.b_iso_out.pid = 0;
  79834. + sts.b_iso_out.framenum = 0;
  79835. +
  79836. + offset = 0;
  79837. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79838. + i += dwc_ep->pkt_per_frm) {
  79839. +
  79840. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  79841. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79842. + if (len > dwc_ep->data_per_frame)
  79843. + data_per_desc =
  79844. + dwc_ep->data_per_frame -
  79845. + j * dwc_ep->maxpacket;
  79846. + else
  79847. + data_per_desc = dwc_ep->maxpacket;
  79848. + len = data_per_desc % 4;
  79849. + if (len)
  79850. + data_per_desc += 4 - len;
  79851. +
  79852. + sts.b_iso_out.rxbytes = data_per_desc;
  79853. + dma_desc->buf = dma_ad;
  79854. + dma_desc->status.d32 = sts.d32;
  79855. +
  79856. + offset += data_per_desc;
  79857. + dma_desc++;
  79858. + dma_ad += data_per_desc;
  79859. + }
  79860. + }
  79861. +
  79862. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  79863. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79864. + if (len > dwc_ep->data_per_frame)
  79865. + data_per_desc =
  79866. + dwc_ep->data_per_frame -
  79867. + j * dwc_ep->maxpacket;
  79868. + else
  79869. + data_per_desc = dwc_ep->maxpacket;
  79870. + len = data_per_desc % 4;
  79871. + if (len)
  79872. + data_per_desc += 4 - len;
  79873. + sts.b_iso_out.rxbytes = data_per_desc;
  79874. + dma_desc->buf = dma_ad;
  79875. + dma_desc->status.d32 = sts.d32;
  79876. +
  79877. + offset += data_per_desc;
  79878. + dma_desc++;
  79879. + dma_ad += data_per_desc;
  79880. + }
  79881. +
  79882. + sts.b_iso_out.ioc = 1;
  79883. + len = (j + 1) * dwc_ep->maxpacket;
  79884. + if (len > dwc_ep->data_per_frame)
  79885. + data_per_desc =
  79886. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  79887. + else
  79888. + data_per_desc = dwc_ep->maxpacket;
  79889. + len = data_per_desc % 4;
  79890. + if (len)
  79891. + data_per_desc += 4 - len;
  79892. + sts.b_iso_out.rxbytes = data_per_desc;
  79893. +
  79894. + dma_desc->buf = dma_ad;
  79895. + dma_desc->status.d32 = sts.d32;
  79896. + dma_desc++;
  79897. +
  79898. + /** Buffer 1 descriptors setup */
  79899. + sts.b_iso_out.ioc = 0;
  79900. + dma_ad = dwc_ep->dma_addr1;
  79901. +
  79902. + offset = 0;
  79903. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79904. + i += dwc_ep->pkt_per_frm) {
  79905. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  79906. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79907. + if (len > dwc_ep->data_per_frame)
  79908. + data_per_desc =
  79909. + dwc_ep->data_per_frame -
  79910. + j * dwc_ep->maxpacket;
  79911. + else
  79912. + data_per_desc = dwc_ep->maxpacket;
  79913. + len = data_per_desc % 4;
  79914. + if (len)
  79915. + data_per_desc += 4 - len;
  79916. +
  79917. + data_per_desc =
  79918. + sts.b_iso_out.rxbytes = data_per_desc;
  79919. + dma_desc->buf = dma_ad;
  79920. + dma_desc->status.d32 = sts.d32;
  79921. +
  79922. + offset += data_per_desc;
  79923. + dma_desc++;
  79924. + dma_ad += data_per_desc;
  79925. + }
  79926. + }
  79927. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  79928. + data_per_desc =
  79929. + ((j + 1) * dwc_ep->maxpacket >
  79930. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  79931. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  79932. + data_per_desc +=
  79933. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  79934. + sts.b_iso_out.rxbytes = data_per_desc;
  79935. + dma_desc->buf = dma_ad;
  79936. + dma_desc->status.d32 = sts.d32;
  79937. +
  79938. + offset += data_per_desc;
  79939. + dma_desc++;
  79940. + dma_ad += data_per_desc;
  79941. + }
  79942. +
  79943. + sts.b_iso_out.ioc = 1;
  79944. + sts.b_iso_out.l = 1;
  79945. + data_per_desc =
  79946. + ((j + 1) * dwc_ep->maxpacket >
  79947. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  79948. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  79949. + data_per_desc +=
  79950. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  79951. + sts.b_iso_out.rxbytes = data_per_desc;
  79952. +
  79953. + dma_desc->buf = dma_ad;
  79954. + dma_desc->status.d32 = sts.d32;
  79955. +
  79956. + dwc_ep->next_frame = 0;
  79957. +
  79958. + /** Write dma_ad into DOEPDMA register */
  79959. + DWC_WRITE_REG32(&(out_regs->doepdma),
  79960. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  79961. +
  79962. + }
  79963. + /** ISO IN EP */
  79964. + else {
  79965. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  79966. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  79967. + dma_addr_t dma_ad;
  79968. + dwc_otg_dev_in_ep_regs_t *in_regs =
  79969. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  79970. + unsigned int frmnumber;
  79971. + fifosize_data_t txfifosize, rxfifosize;
  79972. +
  79973. + txfifosize.d32 =
  79974. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  79975. + dtxfsts);
  79976. + rxfifosize.d32 =
  79977. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  79978. +
  79979. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  79980. +
  79981. + dma_ad = dwc_ep->dma_addr0;
  79982. +
  79983. + dsts.d32 =
  79984. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79985. +
  79986. + sts.b_iso_in.bs = BS_HOST_READY;
  79987. + sts.b_iso_in.txsts = 0;
  79988. + sts.b_iso_in.sp =
  79989. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  79990. + sts.b_iso_in.ioc = 0;
  79991. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  79992. +
  79993. + frmnumber = dwc_ep->next_frame;
  79994. +
  79995. + sts.b_iso_in.framenum = frmnumber;
  79996. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  79997. + sts.b_iso_in.l = 0;
  79998. +
  79999. + /** Buffer 0 descriptors setup */
  80000. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  80001. + dma_desc->buf = dma_ad;
  80002. + dma_desc->status.d32 = sts.d32;
  80003. + dma_desc++;
  80004. +
  80005. + dma_ad += dwc_ep->data_per_frame;
  80006. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  80007. + }
  80008. +
  80009. + sts.b_iso_in.ioc = 1;
  80010. + dma_desc->buf = dma_ad;
  80011. + dma_desc->status.d32 = sts.d32;
  80012. + ++dma_desc;
  80013. +
  80014. + /** Buffer 1 descriptors setup */
  80015. + sts.b_iso_in.ioc = 0;
  80016. + dma_ad = dwc_ep->dma_addr1;
  80017. +
  80018. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  80019. + i += dwc_ep->pkt_per_frm) {
  80020. + dma_desc->buf = dma_ad;
  80021. + dma_desc->status.d32 = sts.d32;
  80022. + dma_desc++;
  80023. +
  80024. + dma_ad += dwc_ep->data_per_frame;
  80025. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  80026. +
  80027. + sts.b_iso_in.ioc = 0;
  80028. + }
  80029. + sts.b_iso_in.ioc = 1;
  80030. + sts.b_iso_in.l = 1;
  80031. +
  80032. + dma_desc->buf = dma_ad;
  80033. + dma_desc->status.d32 = sts.d32;
  80034. +
  80035. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  80036. +
  80037. + /** Write dma_ad into diepdma register */
  80038. + DWC_WRITE_REG32(&(in_regs->diepdma),
  80039. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  80040. + }
  80041. + /** Enable endpoint, clear nak */
  80042. + depctl.d32 = 0;
  80043. + depctl.b.epena = 1;
  80044. + depctl.b.usbactep = 1;
  80045. + depctl.b.cnak = 1;
  80046. +
  80047. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  80048. + depctl.d32 = DWC_READ_REG32(addr);
  80049. +}
  80050. +
  80051. +/**
  80052. + * This function initializes a descriptor chain for Isochronous transfer
  80053. + *
  80054. + * @param core_if Programming view of DWC_otg controller.
  80055. + * @param ep The EP to start the transfer on.
  80056. + *
  80057. + */
  80058. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  80059. + dwc_ep_t * ep)
  80060. +{
  80061. + depctl_data_t depctl = {.d32 = 0 };
  80062. + volatile uint32_t *addr;
  80063. +
  80064. + if (ep->is_in) {
  80065. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  80066. + } else {
  80067. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  80068. + }
  80069. +
  80070. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  80071. + return;
  80072. + } else {
  80073. + deptsiz_data_t deptsiz = {.d32 = 0 };
  80074. +
  80075. + ep->xfer_len =
  80076. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  80077. + ep->pkt_cnt =
  80078. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  80079. + ep->xfer_count = 0;
  80080. + ep->xfer_buff =
  80081. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  80082. + ep->dma_addr =
  80083. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  80084. +
  80085. + if (ep->is_in) {
  80086. + /* Program the transfer size and packet count
  80087. + * as follows: xfersize = N * maxpacket +
  80088. + * short_packet pktcnt = N + (short_packet
  80089. + * exist ? 1 : 0)
  80090. + */
  80091. + deptsiz.b.mc = ep->pkt_per_frm;
  80092. + deptsiz.b.xfersize = ep->xfer_len;
  80093. + deptsiz.b.pktcnt =
  80094. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  80095. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  80096. + dieptsiz, deptsiz.d32);
  80097. +
  80098. + /* Write the DMA register */
  80099. + DWC_WRITE_REG32(&
  80100. + (core_if->dev_if->in_ep_regs[ep->num]->
  80101. + diepdma), (uint32_t) ep->dma_addr);
  80102. +
  80103. + } else {
  80104. + deptsiz.b.pktcnt =
  80105. + (ep->xfer_len + (ep->maxpacket - 1)) /
  80106. + ep->maxpacket;
  80107. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  80108. +
  80109. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  80110. + doeptsiz, deptsiz.d32);
  80111. +
  80112. + /* Write the DMA register */
  80113. + DWC_WRITE_REG32(&
  80114. + (core_if->dev_if->out_ep_regs[ep->num]->
  80115. + doepdma), (uint32_t) ep->dma_addr);
  80116. +
  80117. + }
  80118. + /** Enable endpoint, clear nak */
  80119. + depctl.d32 = 0;
  80120. + depctl.b.epena = 1;
  80121. + depctl.b.cnak = 1;
  80122. +
  80123. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  80124. + }
  80125. +}
  80126. +
  80127. +/**
  80128. + * This function does the setup for a data transfer for an EP and
  80129. + * starts the transfer. For an IN transfer, the packets will be
  80130. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  80131. + * the packets are unloaded from the Rx FIFO in the ISR.
  80132. + *
  80133. + * @param core_if Programming view of DWC_otg controller.
  80134. + * @param ep The EP to start the transfer on.
  80135. + */
  80136. +
  80137. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  80138. + dwc_ep_t * ep)
  80139. +{
  80140. + if (core_if->dma_enable) {
  80141. + if (core_if->dma_desc_enable) {
  80142. + if (ep->is_in) {
  80143. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  80144. + } else {
  80145. + ep->desc_cnt = ep->pkt_cnt;
  80146. + }
  80147. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  80148. + } else {
  80149. + if (core_if->pti_enh_enable) {
  80150. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  80151. + } else {
  80152. + ep->cur_pkt_addr =
  80153. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  80154. + xfer_buff0;
  80155. + ep->cur_pkt_dma_addr =
  80156. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  80157. + dma_addr0;
  80158. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  80159. + }
  80160. + }
  80161. + } else {
  80162. + ep->cur_pkt_addr =
  80163. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  80164. + ep->cur_pkt_dma_addr =
  80165. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  80166. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  80167. + }
  80168. +}
  80169. +
  80170. +/**
  80171. + * This function stops transfer for an EP and
  80172. + * resets the ep's variables.
  80173. + *
  80174. + * @param core_if Programming view of DWC_otg controller.
  80175. + * @param ep The EP to start the transfer on.
  80176. + */
  80177. +
  80178. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  80179. +{
  80180. + depctl_data_t depctl = {.d32 = 0 };
  80181. + volatile uint32_t *addr;
  80182. +
  80183. + if (ep->is_in == 1) {
  80184. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  80185. + } else {
  80186. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  80187. + }
  80188. +
  80189. + /* disable the ep */
  80190. + depctl.d32 = DWC_READ_REG32(addr);
  80191. +
  80192. + depctl.b.epdis = 1;
  80193. + depctl.b.snak = 1;
  80194. +
  80195. + DWC_WRITE_REG32(addr, depctl.d32);
  80196. +
  80197. + if (core_if->dma_desc_enable &&
  80198. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  80199. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  80200. + ep->iso_dma_desc_addr,
  80201. + ep->desc_cnt * 2);
  80202. + }
  80203. +
  80204. + /* reset varibales */
  80205. + ep->dma_addr0 = 0;
  80206. + ep->dma_addr1 = 0;
  80207. + ep->xfer_buff0 = 0;
  80208. + ep->xfer_buff1 = 0;
  80209. + ep->data_per_frame = 0;
  80210. + ep->data_pattern_frame = 0;
  80211. + ep->sync_frame = 0;
  80212. + ep->buf_proc_intrvl = 0;
  80213. + ep->bInterval = 0;
  80214. + ep->proc_buf_num = 0;
  80215. + ep->pkt_per_frm = 0;
  80216. + ep->pkt_per_frm = 0;
  80217. + ep->desc_cnt = 0;
  80218. + ep->iso_desc_addr = 0;
  80219. + ep->iso_dma_desc_addr = 0;
  80220. +}
  80221. +
  80222. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  80223. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  80224. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  80225. + int data_per_frame, int start_frame,
  80226. + int buf_proc_intrvl, void *req_handle,
  80227. + int atomic_alloc)
  80228. +{
  80229. + dwc_otg_pcd_ep_t *ep;
  80230. + dwc_irqflags_t flags = 0;
  80231. + dwc_ep_t *dwc_ep;
  80232. + int32_t frm_data;
  80233. + dsts_data_t dsts;
  80234. + dwc_otg_core_if_t *core_if;
  80235. +
  80236. + ep = get_ep_from_handle(pcd, ep_handle);
  80237. +
  80238. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  80239. + DWC_WARN("bad ep\n");
  80240. + return -DWC_E_INVALID;
  80241. + }
  80242. +
  80243. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80244. + core_if = GET_CORE_IF(pcd);
  80245. + dwc_ep = &ep->dwc_ep;
  80246. +
  80247. + if (ep->iso_req_handle) {
  80248. + DWC_WARN("ISO request in progress\n");
  80249. + }
  80250. +
  80251. + dwc_ep->dma_addr0 = dma0;
  80252. + dwc_ep->dma_addr1 = dma1;
  80253. +
  80254. + dwc_ep->xfer_buff0 = buf0;
  80255. + dwc_ep->xfer_buff1 = buf1;
  80256. +
  80257. + dwc_ep->data_per_frame = data_per_frame;
  80258. +
  80259. + /** @todo - pattern data support is to be implemented in the future */
  80260. + dwc_ep->data_pattern_frame = dp_frame;
  80261. + dwc_ep->sync_frame = sync_frame;
  80262. +
  80263. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  80264. +
  80265. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  80266. +
  80267. + dwc_ep->proc_buf_num = 0;
  80268. +
  80269. + dwc_ep->pkt_per_frm = 0;
  80270. + frm_data = ep->dwc_ep.data_per_frame;
  80271. + while (frm_data > 0) {
  80272. + dwc_ep->pkt_per_frm++;
  80273. + frm_data -= ep->dwc_ep.maxpacket;
  80274. + }
  80275. +
  80276. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  80277. +
  80278. + if (start_frame == -1) {
  80279. + dwc_ep->next_frame = dsts.b.soffn + 1;
  80280. + if (dwc_ep->bInterval != 1) {
  80281. + dwc_ep->next_frame =
  80282. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  80283. + dwc_ep->next_frame %
  80284. + dwc_ep->bInterval);
  80285. + }
  80286. + } else {
  80287. + dwc_ep->next_frame = start_frame;
  80288. + }
  80289. +
  80290. + if (!core_if->pti_enh_enable) {
  80291. + dwc_ep->pkt_cnt =
  80292. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  80293. + dwc_ep->bInterval;
  80294. + } else {
  80295. + dwc_ep->pkt_cnt =
  80296. + (dwc_ep->data_per_frame *
  80297. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  80298. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  80299. + }
  80300. +
  80301. + if (core_if->dma_desc_enable) {
  80302. + dwc_ep->desc_cnt =
  80303. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  80304. + dwc_ep->bInterval;
  80305. + }
  80306. +
  80307. + if (atomic_alloc) {
  80308. + dwc_ep->pkt_info =
  80309. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  80310. + } else {
  80311. + dwc_ep->pkt_info =
  80312. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  80313. + }
  80314. + if (!dwc_ep->pkt_info) {
  80315. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80316. + return -DWC_E_NO_MEMORY;
  80317. + }
  80318. + if (core_if->pti_enh_enable) {
  80319. + dwc_memset(dwc_ep->pkt_info, 0,
  80320. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  80321. + }
  80322. +
  80323. + dwc_ep->cur_pkt = 0;
  80324. + ep->iso_req_handle = req_handle;
  80325. +
  80326. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80327. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  80328. + return 0;
  80329. +}
  80330. +
  80331. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  80332. + void *req_handle)
  80333. +{
  80334. + dwc_irqflags_t flags = 0;
  80335. + dwc_otg_pcd_ep_t *ep;
  80336. + dwc_ep_t *dwc_ep;
  80337. +
  80338. + ep = get_ep_from_handle(pcd, ep_handle);
  80339. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  80340. + DWC_WARN("bad ep\n");
  80341. + return -DWC_E_INVALID;
  80342. + }
  80343. + dwc_ep = &ep->dwc_ep;
  80344. +
  80345. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  80346. +
  80347. + DWC_FREE(dwc_ep->pkt_info);
  80348. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80349. + if (ep->iso_req_handle != req_handle) {
  80350. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80351. + return -DWC_E_INVALID;
  80352. + }
  80353. +
  80354. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80355. +
  80356. + ep->iso_req_handle = 0;
  80357. + return 0;
  80358. +}
  80359. +
  80360. +/**
  80361. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  80362. + * for Isochronous EPs
  80363. + *
  80364. + * - Every time a sync period completes this function is called to
  80365. + * perform data exchange between PCD and gadget
  80366. + */
  80367. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  80368. + void *req_handle)
  80369. +{
  80370. + int i;
  80371. + dwc_ep_t *dwc_ep;
  80372. +
  80373. + dwc_ep = &ep->dwc_ep;
  80374. +
  80375. + DWC_SPINUNLOCK(ep->pcd->lock);
  80376. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  80377. + dwc_ep->proc_buf_num ^ 0x1);
  80378. + DWC_SPINLOCK(ep->pcd->lock);
  80379. +
  80380. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  80381. + dwc_ep->pkt_info[i].status = 0;
  80382. + dwc_ep->pkt_info[i].offset = 0;
  80383. + dwc_ep->pkt_info[i].length = 0;
  80384. + }
  80385. +}
  80386. +
  80387. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  80388. + void *iso_req_handle)
  80389. +{
  80390. + dwc_otg_pcd_ep_t *ep;
  80391. + dwc_ep_t *dwc_ep;
  80392. +
  80393. + ep = get_ep_from_handle(pcd, ep_handle);
  80394. + if (!ep->desc || ep->dwc_ep.num == 0) {
  80395. + DWC_WARN("bad ep\n");
  80396. + return -DWC_E_INVALID;
  80397. + }
  80398. + dwc_ep = &ep->dwc_ep;
  80399. +
  80400. + return dwc_ep->pkt_cnt;
  80401. +}
  80402. +
  80403. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  80404. + void *iso_req_handle, int packet,
  80405. + int *status, int *actual, int *offset)
  80406. +{
  80407. + dwc_otg_pcd_ep_t *ep;
  80408. + dwc_ep_t *dwc_ep;
  80409. +
  80410. + ep = get_ep_from_handle(pcd, ep_handle);
  80411. + if (!ep)
  80412. + DWC_WARN("bad ep\n");
  80413. +
  80414. + dwc_ep = &ep->dwc_ep;
  80415. +
  80416. + *status = dwc_ep->pkt_info[packet].status;
  80417. + *actual = dwc_ep->pkt_info[packet].length;
  80418. + *offset = dwc_ep->pkt_info[packet].offset;
  80419. +}
  80420. +
  80421. +#endif /* DWC_EN_ISOC */
  80422. +
  80423. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  80424. + uint32_t is_in, uint32_t ep_num)
  80425. +{
  80426. + /* Init EP structure */
  80427. + pcd_ep->desc = 0;
  80428. + pcd_ep->pcd = pcd;
  80429. + pcd_ep->stopped = 1;
  80430. + pcd_ep->queue_sof = 0;
  80431. +
  80432. + /* Init DWC ep structure */
  80433. + pcd_ep->dwc_ep.is_in = is_in;
  80434. + pcd_ep->dwc_ep.num = ep_num;
  80435. + pcd_ep->dwc_ep.active = 0;
  80436. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  80437. + /* Control until ep is actvated */
  80438. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  80439. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  80440. + pcd_ep->dwc_ep.dma_addr = 0;
  80441. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  80442. + pcd_ep->dwc_ep.xfer_buff = 0;
  80443. + pcd_ep->dwc_ep.xfer_len = 0;
  80444. + pcd_ep->dwc_ep.xfer_count = 0;
  80445. + pcd_ep->dwc_ep.sent_zlp = 0;
  80446. + pcd_ep->dwc_ep.total_len = 0;
  80447. + pcd_ep->dwc_ep.desc_addr = 0;
  80448. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  80449. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  80450. +}
  80451. +
  80452. +/**
  80453. + * Initialize ep's
  80454. + */
  80455. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  80456. +{
  80457. + int i;
  80458. + uint32_t hwcfg1;
  80459. + dwc_otg_pcd_ep_t *ep;
  80460. + int in_ep_cntr, out_ep_cntr;
  80461. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  80462. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  80463. +
  80464. + /**
  80465. + * Initialize the EP0 structure.
  80466. + */
  80467. + ep = &pcd->ep0;
  80468. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  80469. +
  80470. + in_ep_cntr = 0;
  80471. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  80472. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  80473. + if ((hwcfg1 & 0x1) == 0) {
  80474. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  80475. + in_ep_cntr++;
  80476. + /**
  80477. + * @todo NGS: Add direction to EP, based on contents
  80478. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  80479. + * sprintf(";r
  80480. + */
  80481. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  80482. +
  80483. + DWC_CIRCLEQ_INIT(&ep->queue);
  80484. + }
  80485. + hwcfg1 >>= 2;
  80486. + }
  80487. +
  80488. + out_ep_cntr = 0;
  80489. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  80490. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  80491. + if ((hwcfg1 & 0x1) == 0) {
  80492. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  80493. + out_ep_cntr++;
  80494. + /**
  80495. + * @todo NGS: Add direction to EP, based on contents
  80496. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  80497. + * sprintf(";r
  80498. + */
  80499. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  80500. + DWC_CIRCLEQ_INIT(&ep->queue);
  80501. + }
  80502. + hwcfg1 >>= 2;
  80503. + }
  80504. +
  80505. + pcd->ep0state = EP0_DISCONNECT;
  80506. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  80507. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  80508. +}
  80509. +
  80510. +/**
  80511. + * This function is called when the SRP timer expires. The SRP should
  80512. + * complete within 6 seconds.
  80513. + */
  80514. +static void srp_timeout(void *ptr)
  80515. +{
  80516. + gotgctl_data_t gotgctl;
  80517. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  80518. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  80519. +
  80520. + gotgctl.d32 = DWC_READ_REG32(addr);
  80521. +
  80522. + core_if->srp_timer_started = 0;
  80523. +
  80524. + if (core_if->adp_enable) {
  80525. + if (gotgctl.b.bsesvld == 0) {
  80526. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  80527. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  80528. + /* Power off the core */
  80529. + if (core_if->power_down == 2) {
  80530. + gpwrdn.b.pwrdnswtch = 1;
  80531. + DWC_MODIFY_REG32(&core_if->
  80532. + core_global_regs->gpwrdn,
  80533. + gpwrdn.d32, 0);
  80534. + }
  80535. +
  80536. + gpwrdn.d32 = 0;
  80537. + gpwrdn.b.pmuintsel = 1;
  80538. + gpwrdn.b.pmuactv = 1;
  80539. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  80540. + gpwrdn.d32);
  80541. + dwc_otg_adp_probe_start(core_if);
  80542. + } else {
  80543. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  80544. + core_if->op_state = B_PERIPHERAL;
  80545. + dwc_otg_core_init(core_if);
  80546. + dwc_otg_enable_global_interrupts(core_if);
  80547. + cil_pcd_start(core_if);
  80548. + }
  80549. + }
  80550. +
  80551. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  80552. + (core_if->core_params->i2c_enable)) {
  80553. + DWC_PRINTF("SRP Timeout\n");
  80554. +
  80555. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  80556. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  80557. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  80558. + }
  80559. +
  80560. + /* Clear Session Request */
  80561. + gotgctl.d32 = 0;
  80562. + gotgctl.b.sesreq = 1;
  80563. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  80564. + gotgctl.d32, 0);
  80565. +
  80566. + core_if->srp_success = 0;
  80567. + } else {
  80568. + __DWC_ERROR("Device not connected/responding\n");
  80569. + gotgctl.b.sesreq = 0;
  80570. + DWC_WRITE_REG32(addr, gotgctl.d32);
  80571. + }
  80572. + } else if (gotgctl.b.sesreq) {
  80573. + DWC_PRINTF("SRP Timeout\n");
  80574. +
  80575. + __DWC_ERROR("Device not connected/responding\n");
  80576. + gotgctl.b.sesreq = 0;
  80577. + DWC_WRITE_REG32(addr, gotgctl.d32);
  80578. + } else {
  80579. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  80580. + }
  80581. +}
  80582. +
  80583. +/**
  80584. + * Tasklet
  80585. + *
  80586. + */
  80587. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  80588. +
  80589. +static void start_xfer_tasklet_func(void *data)
  80590. +{
  80591. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  80592. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80593. +
  80594. + int i;
  80595. + depctl_data_t diepctl;
  80596. +
  80597. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  80598. +
  80599. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  80600. +
  80601. + if (pcd->ep0.queue_sof) {
  80602. + pcd->ep0.queue_sof = 0;
  80603. + start_next_request(&pcd->ep0);
  80604. + // break;
  80605. + }
  80606. +
  80607. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  80608. + depctl_data_t diepctl;
  80609. + diepctl.d32 =
  80610. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  80611. +
  80612. + if (pcd->in_ep[i].queue_sof) {
  80613. + pcd->in_ep[i].queue_sof = 0;
  80614. + start_next_request(&pcd->in_ep[i]);
  80615. + // break;
  80616. + }
  80617. + }
  80618. +
  80619. + return;
  80620. +}
  80621. +
  80622. +/**
  80623. + * This function initialized the PCD portion of the driver.
  80624. + *
  80625. + */
  80626. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  80627. +{
  80628. + dwc_otg_pcd_t *pcd = NULL;
  80629. + dwc_otg_dev_if_t *dev_if;
  80630. + int i;
  80631. +
  80632. + /*
  80633. + * Allocate PCD structure
  80634. + */
  80635. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  80636. +
  80637. + if (pcd == NULL) {
  80638. + return NULL;
  80639. + }
  80640. +
  80641. + pcd->lock = DWC_SPINLOCK_ALLOC();
  80642. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  80643. + pcd, core_if);//GRAYG
  80644. + if (!pcd->lock) {
  80645. + DWC_ERROR("Could not allocate lock for pcd");
  80646. + DWC_FREE(pcd);
  80647. + return NULL;
  80648. + }
  80649. + /* Set core_if's lock pointer to hcd->lock */
  80650. + core_if->lock = pcd->lock;
  80651. + pcd->core_if = core_if;
  80652. +
  80653. + dev_if = core_if->dev_if;
  80654. + dev_if->isoc_ep = NULL;
  80655. +
  80656. + if (core_if->hwcfg4.b.ded_fifo_en) {
  80657. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  80658. + } else {
  80659. + DWC_PRINTF("Shared Tx FIFO mode\n");
  80660. + }
  80661. +
  80662. + /*
  80663. + * Initialized the Core for Device mode here if there is nod ADP support.
  80664. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  80665. + */
  80666. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  80667. + dwc_otg_core_dev_init(core_if);
  80668. + }
  80669. +
  80670. + /*
  80671. + * Register the PCD Callbacks.
  80672. + */
  80673. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  80674. +
  80675. + /*
  80676. + * Initialize the DMA buffer for SETUP packets
  80677. + */
  80678. + if (GET_CORE_IF(pcd)->dma_enable) {
  80679. + pcd->setup_pkt =
  80680. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  80681. + &pcd->setup_pkt_dma_handle);
  80682. + if (pcd->setup_pkt == NULL) {
  80683. + DWC_FREE(pcd);
  80684. + return NULL;
  80685. + }
  80686. +
  80687. + pcd->status_buf =
  80688. + DWC_DMA_ALLOC(sizeof(uint16_t),
  80689. + &pcd->status_buf_dma_handle);
  80690. + if (pcd->status_buf == NULL) {
  80691. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  80692. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  80693. + DWC_FREE(pcd);
  80694. + return NULL;
  80695. + }
  80696. +
  80697. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80698. + dev_if->setup_desc_addr[0] =
  80699. + dwc_otg_ep_alloc_desc_chain
  80700. + (&dev_if->dma_setup_desc_addr[0], 1);
  80701. + dev_if->setup_desc_addr[1] =
  80702. + dwc_otg_ep_alloc_desc_chain
  80703. + (&dev_if->dma_setup_desc_addr[1], 1);
  80704. + dev_if->in_desc_addr =
  80705. + dwc_otg_ep_alloc_desc_chain
  80706. + (&dev_if->dma_in_desc_addr, 1);
  80707. + dev_if->out_desc_addr =
  80708. + dwc_otg_ep_alloc_desc_chain
  80709. + (&dev_if->dma_out_desc_addr, 1);
  80710. + pcd->data_terminated = 0;
  80711. +
  80712. + if (dev_if->setup_desc_addr[0] == 0
  80713. + || dev_if->setup_desc_addr[1] == 0
  80714. + || dev_if->in_desc_addr == 0
  80715. + || dev_if->out_desc_addr == 0) {
  80716. +
  80717. + if (dev_if->out_desc_addr)
  80718. + dwc_otg_ep_free_desc_chain
  80719. + (dev_if->out_desc_addr,
  80720. + dev_if->dma_out_desc_addr, 1);
  80721. + if (dev_if->in_desc_addr)
  80722. + dwc_otg_ep_free_desc_chain
  80723. + (dev_if->in_desc_addr,
  80724. + dev_if->dma_in_desc_addr, 1);
  80725. + if (dev_if->setup_desc_addr[1])
  80726. + dwc_otg_ep_free_desc_chain
  80727. + (dev_if->setup_desc_addr[1],
  80728. + dev_if->dma_setup_desc_addr[1], 1);
  80729. + if (dev_if->setup_desc_addr[0])
  80730. + dwc_otg_ep_free_desc_chain
  80731. + (dev_if->setup_desc_addr[0],
  80732. + dev_if->dma_setup_desc_addr[0], 1);
  80733. +
  80734. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  80735. + pcd->setup_pkt,
  80736. + pcd->setup_pkt_dma_handle);
  80737. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  80738. + pcd->status_buf,
  80739. + pcd->status_buf_dma_handle);
  80740. +
  80741. + DWC_FREE(pcd);
  80742. +
  80743. + return NULL;
  80744. + }
  80745. + }
  80746. + } else {
  80747. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  80748. + if (pcd->setup_pkt == NULL) {
  80749. + DWC_FREE(pcd);
  80750. + return NULL;
  80751. + }
  80752. +
  80753. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  80754. + if (pcd->status_buf == NULL) {
  80755. + DWC_FREE(pcd->setup_pkt);
  80756. + DWC_FREE(pcd);
  80757. + return NULL;
  80758. + }
  80759. + }
  80760. +
  80761. + dwc_otg_pcd_reinit(pcd);
  80762. +
  80763. + /* Allocate the cfi object for the PCD */
  80764. +#ifdef DWC_UTE_CFI
  80765. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  80766. + if (NULL == pcd->cfi)
  80767. + goto fail;
  80768. + if (init_cfi(pcd->cfi)) {
  80769. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  80770. + goto fail;
  80771. + }
  80772. +#endif
  80773. +
  80774. + /* Initialize tasklets */
  80775. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  80776. + start_xfer_tasklet_func, pcd);
  80777. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  80778. + do_test_mode, pcd);
  80779. +
  80780. + /* Initialize SRP timer */
  80781. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  80782. +
  80783. + if (core_if->core_params->dev_out_nak) {
  80784. + /**
  80785. + * Initialize xfer timeout timer. Implemented for
  80786. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  80787. + */
  80788. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  80789. + pcd->core_if->ep_xfer_timer[i] =
  80790. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  80791. + &pcd->core_if->ep_xfer_info[i]);
  80792. + }
  80793. + }
  80794. +
  80795. + return pcd;
  80796. +#ifdef DWC_UTE_CFI
  80797. +fail:
  80798. +#endif
  80799. + if (pcd->setup_pkt)
  80800. + DWC_FREE(pcd->setup_pkt);
  80801. + if (pcd->status_buf)
  80802. + DWC_FREE(pcd->status_buf);
  80803. +#ifdef DWC_UTE_CFI
  80804. + if (pcd->cfi)
  80805. + DWC_FREE(pcd->cfi);
  80806. +#endif
  80807. + if (pcd)
  80808. + DWC_FREE(pcd);
  80809. + return NULL;
  80810. +
  80811. +}
  80812. +
  80813. +/**
  80814. + * Remove PCD specific data
  80815. + */
  80816. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  80817. +{
  80818. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  80819. + int i;
  80820. + if (pcd->core_if->core_params->dev_out_nak) {
  80821. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  80822. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  80823. + pcd->core_if->ep_xfer_info[i].state = 0;
  80824. + }
  80825. + }
  80826. +
  80827. + if (GET_CORE_IF(pcd)->dma_enable) {
  80828. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  80829. + pcd->setup_pkt_dma_handle);
  80830. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  80831. + pcd->status_buf_dma_handle);
  80832. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80833. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  80834. + dev_if->dma_setup_desc_addr
  80835. + [0], 1);
  80836. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  80837. + dev_if->dma_setup_desc_addr
  80838. + [1], 1);
  80839. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  80840. + dev_if->dma_in_desc_addr, 1);
  80841. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  80842. + dev_if->dma_out_desc_addr,
  80843. + 1);
  80844. + }
  80845. + } else {
  80846. + DWC_FREE(pcd->setup_pkt);
  80847. + DWC_FREE(pcd->status_buf);
  80848. + }
  80849. + DWC_SPINLOCK_FREE(pcd->lock);
  80850. + /* Set core_if's lock pointer to NULL */
  80851. + pcd->core_if->lock = NULL;
  80852. +
  80853. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  80854. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  80855. + if (pcd->core_if->core_params->dev_out_nak) {
  80856. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  80857. + if (pcd->core_if->ep_xfer_timer[i]) {
  80858. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  80859. + }
  80860. + }
  80861. + }
  80862. +
  80863. +/* Release the CFI object's dynamic memory */
  80864. +#ifdef DWC_UTE_CFI
  80865. + if (pcd->cfi->ops.release) {
  80866. + pcd->cfi->ops.release(pcd->cfi);
  80867. + }
  80868. +#endif
  80869. +
  80870. + DWC_FREE(pcd);
  80871. +}
  80872. +
  80873. +/**
  80874. + * Returns whether registered pcd is dual speed or not
  80875. + */
  80876. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  80877. +{
  80878. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80879. +
  80880. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  80881. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  80882. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  80883. + (core_if->core_params->ulpi_fs_ls))) {
  80884. + return 0;
  80885. + }
  80886. +
  80887. + return 1;
  80888. +}
  80889. +
  80890. +/**
  80891. + * Returns whether registered pcd is OTG capable or not
  80892. + */
  80893. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  80894. +{
  80895. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80896. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  80897. +
  80898. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  80899. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  80900. + return 0;
  80901. + }
  80902. +
  80903. + return 1;
  80904. +}
  80905. +
  80906. +/**
  80907. + * This function assigns periodic Tx FIFO to an periodic EP
  80908. + * in shared Tx FIFO mode
  80909. + */
  80910. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  80911. +{
  80912. + uint32_t TxMsk = 1;
  80913. + int i;
  80914. +
  80915. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  80916. + if ((TxMsk & core_if->tx_msk) == 0) {
  80917. + core_if->tx_msk |= TxMsk;
  80918. + return i + 1;
  80919. + }
  80920. + TxMsk <<= 1;
  80921. + }
  80922. + return 0;
  80923. +}
  80924. +
  80925. +/**
  80926. + * This function assigns periodic Tx FIFO to an periodic EP
  80927. + * in shared Tx FIFO mode
  80928. + */
  80929. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  80930. +{
  80931. + uint32_t PerTxMsk = 1;
  80932. + int i;
  80933. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  80934. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  80935. + core_if->p_tx_msk |= PerTxMsk;
  80936. + return i + 1;
  80937. + }
  80938. + PerTxMsk <<= 1;
  80939. + }
  80940. + return 0;
  80941. +}
  80942. +
  80943. +/**
  80944. + * This function releases periodic Tx FIFO
  80945. + * in shared Tx FIFO mode
  80946. + */
  80947. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  80948. + uint32_t fifo_num)
  80949. +{
  80950. + core_if->p_tx_msk =
  80951. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  80952. +}
  80953. +
  80954. +/**
  80955. + * This function releases periodic Tx FIFO
  80956. + * in shared Tx FIFO mode
  80957. + */
  80958. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  80959. +{
  80960. + core_if->tx_msk =
  80961. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  80962. +}
  80963. +
  80964. +/**
  80965. + * This function is being called from gadget
  80966. + * to enable PCD endpoint.
  80967. + */
  80968. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  80969. + const uint8_t * ep_desc, void *usb_ep)
  80970. +{
  80971. + int num, dir;
  80972. + dwc_otg_pcd_ep_t *ep = NULL;
  80973. + const usb_endpoint_descriptor_t *desc;
  80974. + dwc_irqflags_t flags;
  80975. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  80976. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  80977. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  80978. + int retval = 0;
  80979. + int i, epcount;
  80980. +
  80981. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  80982. +
  80983. + if (!desc) {
  80984. + pcd->ep0.priv = usb_ep;
  80985. + ep = &pcd->ep0;
  80986. + retval = -DWC_E_INVALID;
  80987. + goto out;
  80988. + }
  80989. +
  80990. + num = UE_GET_ADDR(desc->bEndpointAddress);
  80991. + dir = UE_GET_DIR(desc->bEndpointAddress);
  80992. +
  80993. + if (!desc->wMaxPacketSize) {
  80994. + DWC_WARN("bad maxpacketsize\n");
  80995. + retval = -DWC_E_INVALID;
  80996. + goto out;
  80997. + }
  80998. +
  80999. + if (dir == UE_DIR_IN) {
  81000. + epcount = pcd->core_if->dev_if->num_in_eps;
  81001. + for (i = 0; i < epcount; i++) {
  81002. + if (num == pcd->in_ep[i].dwc_ep.num) {
  81003. + ep = &pcd->in_ep[i];
  81004. + break;
  81005. + }
  81006. + }
  81007. + } else {
  81008. + epcount = pcd->core_if->dev_if->num_out_eps;
  81009. + for (i = 0; i < epcount; i++) {
  81010. + if (num == pcd->out_ep[i].dwc_ep.num) {
  81011. + ep = &pcd->out_ep[i];
  81012. + break;
  81013. + }
  81014. + }
  81015. + }
  81016. +
  81017. + if (!ep) {
  81018. + DWC_WARN("bad address\n");
  81019. + retval = -DWC_E_INVALID;
  81020. + goto out;
  81021. + }
  81022. +
  81023. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81024. +
  81025. + ep->desc = desc;
  81026. + ep->priv = usb_ep;
  81027. +
  81028. + /*
  81029. + * Activate the EP
  81030. + */
  81031. + ep->stopped = 0;
  81032. +
  81033. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  81034. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  81035. +
  81036. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  81037. +
  81038. + if (ep->dwc_ep.is_in) {
  81039. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81040. + ep->dwc_ep.tx_fifo_num = 0;
  81041. +
  81042. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  81043. + /*
  81044. + * if ISOC EP then assign a Periodic Tx FIFO.
  81045. + */
  81046. + ep->dwc_ep.tx_fifo_num =
  81047. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  81048. + }
  81049. + } else {
  81050. + /*
  81051. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  81052. + */
  81053. + ep->dwc_ep.tx_fifo_num =
  81054. + assign_tx_fifo(GET_CORE_IF(pcd));
  81055. + }
  81056. +
  81057. + /* Calculating EP info controller base address */
  81058. + if (ep->dwc_ep.tx_fifo_num
  81059. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81060. + gdfifocfg.d32 =
  81061. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81062. + core_global_regs->gdfifocfg);
  81063. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  81064. + dptxfsiz.d32 =
  81065. + (DWC_READ_REG32
  81066. + (&GET_CORE_IF(pcd)->core_global_regs->
  81067. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  81068. + gdfifocfg.b.epinfobase =
  81069. + gdfifocfgbase.d32 + dptxfsiz.d32;
  81070. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  81071. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  81072. + core_global_regs->gdfifocfg,
  81073. + gdfifocfg.d32);
  81074. + }
  81075. + }
  81076. + }
  81077. + /* Set initial data PID. */
  81078. + if (ep->dwc_ep.type == UE_BULK) {
  81079. + ep->dwc_ep.data_pid_start = 0;
  81080. + }
  81081. +
  81082. + /* Alloc DMA Descriptors */
  81083. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81084. +#ifndef DWC_UTE_PER_IO
  81085. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  81086. +#endif
  81087. + ep->dwc_ep.desc_addr =
  81088. + dwc_otg_ep_alloc_desc_chain(&ep->
  81089. + dwc_ep.dma_desc_addr,
  81090. + MAX_DMA_DESC_CNT);
  81091. + if (!ep->dwc_ep.desc_addr) {
  81092. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  81093. + __func__);
  81094. + retval = -DWC_E_SHUTDOWN;
  81095. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81096. + goto out;
  81097. + }
  81098. +#ifndef DWC_UTE_PER_IO
  81099. + }
  81100. +#endif
  81101. + }
  81102. +
  81103. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  81104. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  81105. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  81106. +#ifdef DWC_UTE_PER_IO
  81107. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  81108. +#endif
  81109. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  81110. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  81111. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  81112. + }
  81113. +
  81114. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  81115. +
  81116. +#ifdef DWC_UTE_CFI
  81117. + if (pcd->cfi->ops.ep_enable) {
  81118. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  81119. + }
  81120. +#endif
  81121. +
  81122. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81123. +
  81124. +out:
  81125. + return retval;
  81126. +}
  81127. +
  81128. +/**
  81129. + * This function is being called from gadget
  81130. + * to disable PCD endpoint.
  81131. + */
  81132. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  81133. +{
  81134. + dwc_otg_pcd_ep_t *ep;
  81135. + dwc_irqflags_t flags;
  81136. + dwc_otg_dev_dma_desc_t *desc_addr;
  81137. + dwc_dma_t dma_desc_addr;
  81138. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  81139. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  81140. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  81141. +
  81142. + ep = get_ep_from_handle(pcd, ep_handle);
  81143. +
  81144. + if (!ep || !ep->desc) {
  81145. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  81146. + return -DWC_E_INVALID;
  81147. + }
  81148. +
  81149. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81150. +
  81151. + dwc_otg_request_nuke(ep);
  81152. +
  81153. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  81154. + if (pcd->core_if->core_params->dev_out_nak) {
  81155. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  81156. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  81157. + }
  81158. + ep->desc = NULL;
  81159. + ep->stopped = 1;
  81160. +
  81161. + gdfifocfg.d32 =
  81162. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  81163. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  81164. +
  81165. + if (ep->dwc_ep.is_in) {
  81166. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81167. + /* Flush the Tx FIFO */
  81168. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  81169. + ep->dwc_ep.tx_fifo_num);
  81170. + }
  81171. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  81172. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  81173. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  81174. + /* Decreasing EPinfo Base Addr */
  81175. + dptxfsiz.d32 =
  81176. + (DWC_READ_REG32
  81177. + (&GET_CORE_IF(pcd)->
  81178. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  81179. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  81180. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  81181. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  81182. + gdfifocfg.d32);
  81183. + }
  81184. + }
  81185. + }
  81186. +
  81187. + /* Free DMA Descriptors */
  81188. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81189. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  81190. + desc_addr = ep->dwc_ep.desc_addr;
  81191. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  81192. +
  81193. + /* Cannot call dma_free_coherent() with IRQs disabled */
  81194. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81195. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  81196. + MAX_DMA_DESC_CNT);
  81197. +
  81198. + goto out_unlocked;
  81199. + }
  81200. + }
  81201. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81202. +
  81203. +out_unlocked:
  81204. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  81205. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81206. + return 0;
  81207. +
  81208. +}
  81209. +
  81210. +/******************************************************************************/
  81211. +#ifdef DWC_UTE_PER_IO
  81212. +
  81213. +/**
  81214. + * Free the request and its extended parts
  81215. + *
  81216. + */
  81217. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  81218. +{
  81219. + DWC_FREE(req->ext_req.per_io_frame_descs);
  81220. + DWC_FREE(req);
  81221. +}
  81222. +
  81223. +/**
  81224. + * Start the next request in the endpoint's queue.
  81225. + *
  81226. + */
  81227. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  81228. + dwc_otg_pcd_ep_t * ep)
  81229. +{
  81230. + int i;
  81231. + dwc_otg_pcd_request_t *req = NULL;
  81232. + dwc_ep_t *dwcep = NULL;
  81233. + struct dwc_iso_xreq_port *ereq = NULL;
  81234. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  81235. + uint16_t nat;
  81236. + depctl_data_t diepctl;
  81237. +
  81238. + dwcep = &ep->dwc_ep;
  81239. +
  81240. + if (dwcep->xiso_active_xfers > 0) {
  81241. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  81242. + DWC_WARN("There are currently active transfers for EP%d \
  81243. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  81244. + dwcep->xiso_queued_xfers);
  81245. +#endif
  81246. + return 0;
  81247. + }
  81248. +
  81249. + nat = UGETW(ep->desc->wMaxPacketSize);
  81250. + nat = (nat >> 11) & 0x03;
  81251. +
  81252. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81253. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  81254. + ereq = &req->ext_req;
  81255. + ep->stopped = 0;
  81256. +
  81257. + /* Get the frame number */
  81258. + dwcep->xiso_frame_num =
  81259. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  81260. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  81261. +
  81262. + ddesc_iso = ereq->per_io_frame_descs;
  81263. +
  81264. + if (dwcep->is_in) {
  81265. + /* Setup DMA Descriptor chain for IN Isoc request */
  81266. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81267. + //if ((i % (nat + 1)) == 0)
  81268. + if ( i > 0 )
  81269. + dwcep->xiso_frame_num =
  81270. + (dwcep->xiso_bInterval +
  81271. + dwcep->xiso_frame_num) & 0x3FFF;
  81272. + dwcep->desc_addr[i].buf =
  81273. + req->dma + ddesc_iso[i].offset;
  81274. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  81275. + ddesc_iso[i].length;
  81276. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  81277. + dwcep->xiso_frame_num;
  81278. + dwcep->desc_addr[i].status.b_iso_in.bs =
  81279. + BS_HOST_READY;
  81280. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  81281. + dwcep->desc_addr[i].status.b_iso_in.sp =
  81282. + (ddesc_iso[i].length %
  81283. + dwcep->maxpacket) ? 1 : 0;
  81284. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  81285. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  81286. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  81287. +
  81288. + /* Process the last descriptor */
  81289. + if (i == ereq->pio_pkt_count - 1) {
  81290. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  81291. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  81292. + }
  81293. + }
  81294. +
  81295. + /* Setup and start the transfer for this endpoint */
  81296. + dwcep->xiso_active_xfers++;
  81297. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  81298. + in_ep_regs[dwcep->num]->diepdma,
  81299. + dwcep->dma_desc_addr);
  81300. + diepctl.d32 = 0;
  81301. + diepctl.b.epena = 1;
  81302. + diepctl.b.cnak = 1;
  81303. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  81304. + in_ep_regs[dwcep->num]->diepctl, 0,
  81305. + diepctl.d32);
  81306. + } else {
  81307. + /* Setup DMA Descriptor chain for OUT Isoc request */
  81308. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81309. + //if ((i % (nat + 1)) == 0)
  81310. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  81311. + dwcep->xiso_frame_num) & 0x3FFF;
  81312. + dwcep->desc_addr[i].buf =
  81313. + req->dma + ddesc_iso[i].offset;
  81314. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  81315. + ddesc_iso[i].length;
  81316. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  81317. + dwcep->xiso_frame_num;
  81318. + dwcep->desc_addr[i].status.b_iso_out.bs =
  81319. + BS_HOST_READY;
  81320. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  81321. + dwcep->desc_addr[i].status.b_iso_out.sp =
  81322. + (ddesc_iso[i].length %
  81323. + dwcep->maxpacket) ? 1 : 0;
  81324. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  81325. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  81326. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  81327. +
  81328. + /* Process the last descriptor */
  81329. + if (i == ereq->pio_pkt_count - 1) {
  81330. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  81331. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  81332. + }
  81333. + }
  81334. +
  81335. + /* Setup and start the transfer for this endpoint */
  81336. + dwcep->xiso_active_xfers++;
  81337. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  81338. + dev_if->out_ep_regs[dwcep->num]->
  81339. + doepdma, dwcep->dma_desc_addr);
  81340. + diepctl.d32 = 0;
  81341. + diepctl.b.epena = 1;
  81342. + diepctl.b.cnak = 1;
  81343. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81344. + dev_if->out_ep_regs[dwcep->num]->
  81345. + doepctl, 0, diepctl.d32);
  81346. + }
  81347. +
  81348. + } else {
  81349. + ep->stopped = 1;
  81350. + }
  81351. +
  81352. + return 0;
  81353. +}
  81354. +
  81355. +/**
  81356. + * - Remove the request from the queue
  81357. + */
  81358. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  81359. +{
  81360. + dwc_otg_pcd_request_t *req = NULL;
  81361. + struct dwc_iso_xreq_port *ereq = NULL;
  81362. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  81363. + dwc_ep_t *dwcep = NULL;
  81364. + int i;
  81365. +
  81366. + //DWC_DEBUG();
  81367. + dwcep = &ep->dwc_ep;
  81368. +
  81369. + /* Get the first pending request from the queue */
  81370. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81371. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  81372. + if (!req) {
  81373. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  81374. + return;
  81375. + }
  81376. + dwcep->xiso_active_xfers--;
  81377. + dwcep->xiso_queued_xfers--;
  81378. + /* Remove this request from the queue */
  81379. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  81380. + } else {
  81381. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  81382. + return;
  81383. + }
  81384. +
  81385. + ep->stopped = 1;
  81386. + ereq = &req->ext_req;
  81387. + ddesc_iso = ereq->per_io_frame_descs;
  81388. +
  81389. + if (dwcep->xiso_active_xfers < 0) {
  81390. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  81391. + dwcep->xiso_active_xfers);
  81392. + }
  81393. +
  81394. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  81395. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81396. + if (dwcep->is_in) { /* IN endpoints */
  81397. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  81398. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  81399. + ddesc_iso[i].status =
  81400. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  81401. + } else { /* OUT endpoints */
  81402. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  81403. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  81404. + ddesc_iso[i].status =
  81405. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  81406. + }
  81407. + }
  81408. +
  81409. + DWC_SPINUNLOCK(ep->pcd->lock);
  81410. +
  81411. + /* Call the completion function in the non-portable logic */
  81412. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  81413. + &req->ext_req);
  81414. +
  81415. + DWC_SPINLOCK(ep->pcd->lock);
  81416. +
  81417. + /* Free the request - specific freeing needed for extended request object */
  81418. + dwc_pcd_xiso_ereq_free(ep, req);
  81419. +
  81420. + /* Start the next request */
  81421. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  81422. +
  81423. + return;
  81424. +}
  81425. +
  81426. +/**
  81427. + * Create and initialize the Isoc pkt descriptors of the extended request.
  81428. + *
  81429. + */
  81430. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  81431. + void *ereq_nonport,
  81432. + int atomic_alloc)
  81433. +{
  81434. + struct dwc_iso_xreq_port *ereq = NULL;
  81435. + struct dwc_iso_xreq_port *req_mapped = NULL;
  81436. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  81437. + uint32_t pkt_count;
  81438. + int i;
  81439. +
  81440. + ereq = &req->ext_req;
  81441. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  81442. + pkt_count = req_mapped->pio_pkt_count;
  81443. +
  81444. + /* Create the isoc descs */
  81445. + if (atomic_alloc) {
  81446. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  81447. + } else {
  81448. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  81449. + }
  81450. +
  81451. + if (!ipds) {
  81452. + DWC_ERROR("Failed to allocate isoc descriptors");
  81453. + return -DWC_E_NO_MEMORY;
  81454. + }
  81455. +
  81456. + /* Initialize the extended request fields */
  81457. + ereq->per_io_frame_descs = ipds;
  81458. + ereq->error_count = 0;
  81459. + ereq->pio_alloc_pkt_count = pkt_count;
  81460. + ereq->pio_pkt_count = pkt_count;
  81461. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  81462. +
  81463. + /* Init the Isoc descriptors */
  81464. + for (i = 0; i < pkt_count; i++) {
  81465. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  81466. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  81467. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  81468. + ipds[i].actual_length =
  81469. + req_mapped->per_io_frame_descs[i].actual_length;
  81470. + }
  81471. +
  81472. + return 0;
  81473. +}
  81474. +
  81475. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  81476. +{
  81477. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  81478. + int i;
  81479. +
  81480. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  81481. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  81482. + DWC_DEBUG("error_count=%d", ereq->error_count);
  81483. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  81484. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  81485. + DWC_DEBUG("res=%d", ereq->res);
  81486. +
  81487. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  81488. + xfd = &ereq->per_io_frame_descs[0];
  81489. + DWC_DEBUG("FD #%d", i);
  81490. +
  81491. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  81492. + DWC_DEBUG("xfd->length=%d", xfd->length);
  81493. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  81494. + DWC_DEBUG("xfd->status=%d", xfd->status);
  81495. + }
  81496. +}
  81497. +
  81498. +/**
  81499. + *
  81500. + */
  81501. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81502. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  81503. + int zero, void *req_handle, int atomic_alloc,
  81504. + void *ereq_nonport)
  81505. +{
  81506. + dwc_otg_pcd_request_t *req = NULL;
  81507. + dwc_otg_pcd_ep_t *ep;
  81508. + dwc_irqflags_t flags;
  81509. + int res;
  81510. +
  81511. + ep = get_ep_from_handle(pcd, ep_handle);
  81512. + if (!ep) {
  81513. + DWC_WARN("bad ep\n");
  81514. + return -DWC_E_INVALID;
  81515. + }
  81516. +
  81517. + /* We support this extension only for DDMA mode */
  81518. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  81519. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  81520. + return -DWC_E_INVALID;
  81521. +
  81522. + /* Create a dwc_otg_pcd_request_t object */
  81523. + if (atomic_alloc) {
  81524. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  81525. + } else {
  81526. + req = DWC_ALLOC(sizeof(*req));
  81527. + }
  81528. +
  81529. + if (!req) {
  81530. + return -DWC_E_NO_MEMORY;
  81531. + }
  81532. +
  81533. + /* Create the Isoc descs for this request which shall be the exact match
  81534. + * of the structure sent to us from the non-portable logic */
  81535. + res =
  81536. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  81537. + if (res) {
  81538. + DWC_WARN("Failed to init the Isoc descriptors");
  81539. + DWC_FREE(req);
  81540. + return res;
  81541. + }
  81542. +
  81543. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81544. +
  81545. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  81546. + req->buf = buf;
  81547. + req->dma = dma_buf;
  81548. + req->length = buflen;
  81549. + req->sent_zlp = zero;
  81550. + req->priv = req_handle;
  81551. +
  81552. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81553. + ep->dwc_ep.dma_addr = dma_buf;
  81554. + ep->dwc_ep.start_xfer_buff = buf;
  81555. + ep->dwc_ep.xfer_buff = buf;
  81556. + ep->dwc_ep.xfer_len = 0;
  81557. + ep->dwc_ep.xfer_count = 0;
  81558. + ep->dwc_ep.sent_zlp = 0;
  81559. + ep->dwc_ep.total_len = buflen;
  81560. +
  81561. + /* Add this request to the tail */
  81562. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81563. + ep->dwc_ep.xiso_queued_xfers++;
  81564. +
  81565. +//DWC_DEBUG("CP_0");
  81566. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  81567. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  81568. +//prn_ext_request(&req->ext_req);
  81569. +
  81570. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81571. +
  81572. + /* If the req->status == ASAP then check if there is any active transfer
  81573. + * for this endpoint. If no active transfers, then get the first entry
  81574. + * from the queue and start that transfer
  81575. + */
  81576. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  81577. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  81578. + if (res) {
  81579. + DWC_WARN("Failed to start the next Isoc transfer");
  81580. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81581. + DWC_FREE(req);
  81582. + return res;
  81583. + }
  81584. + }
  81585. +
  81586. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81587. + return 0;
  81588. +}
  81589. +
  81590. +#endif
  81591. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  81592. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81593. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  81594. + int zero, void *req_handle, int atomic_alloc)
  81595. +{
  81596. + dwc_irqflags_t flags;
  81597. + dwc_otg_pcd_request_t *req;
  81598. + dwc_otg_pcd_ep_t *ep;
  81599. + uint32_t max_transfer;
  81600. +
  81601. + ep = get_ep_from_handle(pcd, ep_handle);
  81602. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  81603. + DWC_WARN("bad ep\n");
  81604. + return -DWC_E_INVALID;
  81605. + }
  81606. +
  81607. + if (atomic_alloc) {
  81608. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  81609. + } else {
  81610. + req = DWC_ALLOC(sizeof(*req));
  81611. + }
  81612. +
  81613. + if (!req) {
  81614. + return -DWC_E_NO_MEMORY;
  81615. + }
  81616. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  81617. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  81618. + if (ep->dwc_ep.num != 0) {
  81619. + DWC_ERROR("queue req %p, len %d buf %p\n",
  81620. + req_handle, buflen, buf);
  81621. + }
  81622. + }
  81623. +
  81624. + req->buf = buf;
  81625. + req->dma = dma_buf;
  81626. + req->length = buflen;
  81627. + req->sent_zlp = zero;
  81628. + req->priv = req_handle;
  81629. + req->dw_align_buf = NULL;
  81630. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  81631. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  81632. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  81633. + &req->dw_align_buf_dma);
  81634. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81635. +
  81636. + /*
  81637. + * After adding request to the queue for IN ISOC wait for In Token Received
  81638. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  81639. + * Received when EP is disabled interrupt to obtain starting microframe
  81640. + * (odd/even) start transfer
  81641. + */
  81642. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  81643. + if (req != 0) {
  81644. + depctl_data_t depctl = {.d32 =
  81645. + DWC_READ_REG32(&pcd->core_if->dev_if->
  81646. + in_ep_regs[ep->dwc_ep.num]->
  81647. + diepctl) };
  81648. + ++pcd->request_pending;
  81649. +
  81650. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81651. + if (ep->dwc_ep.is_in) {
  81652. + depctl.b.cnak = 1;
  81653. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  81654. + in_ep_regs[ep->dwc_ep.num]->
  81655. + diepctl, depctl.d32);
  81656. + }
  81657. +
  81658. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81659. + }
  81660. + return 0;
  81661. + }
  81662. +
  81663. + /*
  81664. + * For EP0 IN without premature status, zlp is required?
  81665. + */
  81666. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  81667. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  81668. + //_req->zero = 1;
  81669. + }
  81670. +
  81671. + /* Start the transfer */
  81672. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  81673. + /* EP0 Transfer? */
  81674. + if (ep->dwc_ep.num == 0) {
  81675. + switch (pcd->ep0state) {
  81676. + case EP0_IN_DATA_PHASE:
  81677. + DWC_DEBUGPL(DBG_PCD,
  81678. + "%s ep0: EP0_IN_DATA_PHASE\n",
  81679. + __func__);
  81680. + break;
  81681. +
  81682. + case EP0_OUT_DATA_PHASE:
  81683. + DWC_DEBUGPL(DBG_PCD,
  81684. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  81685. + __func__);
  81686. + if (pcd->request_config) {
  81687. + /* Complete STATUS PHASE */
  81688. + ep->dwc_ep.is_in = 1;
  81689. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  81690. + }
  81691. + break;
  81692. +
  81693. + case EP0_IN_STATUS_PHASE:
  81694. + DWC_DEBUGPL(DBG_PCD,
  81695. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  81696. + __func__);
  81697. + break;
  81698. +
  81699. + default:
  81700. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  81701. + pcd->ep0state);
  81702. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81703. + return -DWC_E_SHUTDOWN;
  81704. + }
  81705. +
  81706. + ep->dwc_ep.dma_addr = dma_buf;
  81707. + ep->dwc_ep.start_xfer_buff = buf;
  81708. + ep->dwc_ep.xfer_buff = buf;
  81709. + ep->dwc_ep.xfer_len = buflen;
  81710. + ep->dwc_ep.xfer_count = 0;
  81711. + ep->dwc_ep.sent_zlp = 0;
  81712. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  81713. +
  81714. + if (zero) {
  81715. + if ((ep->dwc_ep.xfer_len %
  81716. + ep->dwc_ep.maxpacket == 0)
  81717. + && (ep->dwc_ep.xfer_len != 0)) {
  81718. + ep->dwc_ep.sent_zlp = 1;
  81719. + }
  81720. +
  81721. + }
  81722. +
  81723. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  81724. + &ep->dwc_ep);
  81725. + } // non-ep0 endpoints
  81726. + else {
  81727. +#ifdef DWC_UTE_CFI
  81728. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  81729. + /* store the request length */
  81730. + ep->dwc_ep.cfi_req_len = buflen;
  81731. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  81732. + ep, req);
  81733. + } else {
  81734. +#endif
  81735. + max_transfer =
  81736. + GET_CORE_IF(ep->pcd)->core_params->
  81737. + max_transfer_size;
  81738. +
  81739. + /* Setup and start the Transfer */
  81740. + if (req->dw_align_buf){
  81741. + if (ep->dwc_ep.is_in)
  81742. + dwc_memcpy(req->dw_align_buf,
  81743. + buf, buflen);
  81744. + ep->dwc_ep.dma_addr =
  81745. + req->dw_align_buf_dma;
  81746. + ep->dwc_ep.start_xfer_buff =
  81747. + req->dw_align_buf;
  81748. + ep->dwc_ep.xfer_buff =
  81749. + req->dw_align_buf;
  81750. + } else {
  81751. + ep->dwc_ep.dma_addr = dma_buf;
  81752. + ep->dwc_ep.start_xfer_buff = buf;
  81753. + ep->dwc_ep.xfer_buff = buf;
  81754. + }
  81755. + ep->dwc_ep.xfer_len = 0;
  81756. + ep->dwc_ep.xfer_count = 0;
  81757. + ep->dwc_ep.sent_zlp = 0;
  81758. + ep->dwc_ep.total_len = buflen;
  81759. +
  81760. + ep->dwc_ep.maxxfer = max_transfer;
  81761. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81762. + uint32_t out_max_xfer =
  81763. + DDMA_MAX_TRANSFER_SIZE -
  81764. + (DDMA_MAX_TRANSFER_SIZE % 4);
  81765. + if (ep->dwc_ep.is_in) {
  81766. + if (ep->dwc_ep.maxxfer >
  81767. + DDMA_MAX_TRANSFER_SIZE) {
  81768. + ep->dwc_ep.maxxfer =
  81769. + DDMA_MAX_TRANSFER_SIZE;
  81770. + }
  81771. + } else {
  81772. + if (ep->dwc_ep.maxxfer >
  81773. + out_max_xfer) {
  81774. + ep->dwc_ep.maxxfer =
  81775. + out_max_xfer;
  81776. + }
  81777. + }
  81778. + }
  81779. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  81780. + ep->dwc_ep.maxxfer -=
  81781. + (ep->dwc_ep.maxxfer %
  81782. + ep->dwc_ep.maxpacket);
  81783. + }
  81784. +
  81785. + if (zero) {
  81786. + if ((ep->dwc_ep.total_len %
  81787. + ep->dwc_ep.maxpacket == 0)
  81788. + && (ep->dwc_ep.total_len != 0)) {
  81789. + ep->dwc_ep.sent_zlp = 1;
  81790. + }
  81791. + }
  81792. +#ifdef DWC_UTE_CFI
  81793. + }
  81794. +#endif
  81795. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  81796. + &ep->dwc_ep);
  81797. + }
  81798. + }
  81799. +
  81800. + if (req != 0) {
  81801. + ++pcd->request_pending;
  81802. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81803. + if (ep->dwc_ep.is_in && ep->stopped
  81804. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  81805. + /** @todo NGS Create a function for this. */
  81806. + diepmsk_data_t diepmsk = {.d32 = 0 };
  81807. + diepmsk.b.intktxfemp = 1;
  81808. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  81809. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81810. + dev_if->dev_global_regs->diepeachintmsk
  81811. + [ep->dwc_ep.num], 0,
  81812. + diepmsk.d32);
  81813. + } else {
  81814. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81815. + dev_if->dev_global_regs->
  81816. + diepmsk, 0, diepmsk.d32);
  81817. + }
  81818. +
  81819. + }
  81820. + }
  81821. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81822. +
  81823. + return 0;
  81824. +}
  81825. +
  81826. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81827. + void *req_handle)
  81828. +{
  81829. + dwc_irqflags_t flags;
  81830. + dwc_otg_pcd_request_t *req;
  81831. + dwc_otg_pcd_ep_t *ep;
  81832. +
  81833. + ep = get_ep_from_handle(pcd, ep_handle);
  81834. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  81835. + DWC_WARN("bad argument\n");
  81836. + return -DWC_E_INVALID;
  81837. + }
  81838. +
  81839. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81840. +
  81841. + /* make sure it's actually queued on this endpoint */
  81842. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  81843. + if (req->priv == (void *)req_handle) {
  81844. + break;
  81845. + }
  81846. + }
  81847. +
  81848. + if (req->priv != (void *)req_handle) {
  81849. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81850. + return -DWC_E_INVALID;
  81851. + }
  81852. +
  81853. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  81854. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  81855. + } else {
  81856. + req = NULL;
  81857. + }
  81858. +
  81859. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81860. +
  81861. + return req ? 0 : -DWC_E_SHUTDOWN;
  81862. +
  81863. +}
  81864. +
  81865. +/**
  81866. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  81867. + *
  81868. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  81869. + * requests. If the gadget driver clears the halt status, it will
  81870. + * automatically unwedge the endpoint.
  81871. + *
  81872. + * Returns zero on success, else negative DWC error code.
  81873. + */
  81874. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  81875. +{
  81876. + dwc_otg_pcd_ep_t *ep;
  81877. + dwc_irqflags_t flags;
  81878. + int retval = 0;
  81879. +
  81880. + ep = get_ep_from_handle(pcd, ep_handle);
  81881. +
  81882. + if ((!ep->desc && ep != &pcd->ep0) ||
  81883. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  81884. + DWC_WARN("%s, bad ep\n", __func__);
  81885. + return -DWC_E_INVALID;
  81886. + }
  81887. +
  81888. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81889. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81890. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  81891. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81892. + retval = -DWC_E_AGAIN;
  81893. + } else {
  81894. + /* This code needs to be reviewed */
  81895. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  81896. + dtxfsts_data_t txstatus;
  81897. + fifosize_data_t txfifosize;
  81898. +
  81899. + txfifosize.d32 =
  81900. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81901. + core_global_regs->dtxfsiz[ep->dwc_ep.
  81902. + tx_fifo_num]);
  81903. + txstatus.d32 =
  81904. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81905. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  81906. + dtxfsts);
  81907. +
  81908. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  81909. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  81910. + retval = -DWC_E_AGAIN;
  81911. + } else {
  81912. + if (ep->dwc_ep.num == 0) {
  81913. + pcd->ep0state = EP0_STALL;
  81914. + }
  81915. +
  81916. + ep->stopped = 1;
  81917. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  81918. + &ep->dwc_ep);
  81919. + }
  81920. + } else {
  81921. + if (ep->dwc_ep.num == 0) {
  81922. + pcd->ep0state = EP0_STALL;
  81923. + }
  81924. +
  81925. + ep->stopped = 1;
  81926. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81927. + }
  81928. + }
  81929. +
  81930. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81931. +
  81932. + return retval;
  81933. +}
  81934. +
  81935. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  81936. +{
  81937. + dwc_otg_pcd_ep_t *ep;
  81938. + dwc_irqflags_t flags;
  81939. + int retval = 0;
  81940. +
  81941. + ep = get_ep_from_handle(pcd, ep_handle);
  81942. +
  81943. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  81944. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  81945. + DWC_WARN("%s, bad ep\n", __func__);
  81946. + return -DWC_E_INVALID;
  81947. + }
  81948. +
  81949. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81950. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81951. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  81952. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81953. + retval = -DWC_E_AGAIN;
  81954. + } else if (value == 0) {
  81955. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81956. + } else if (value == 1) {
  81957. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  81958. + dtxfsts_data_t txstatus;
  81959. + fifosize_data_t txfifosize;
  81960. +
  81961. + txfifosize.d32 =
  81962. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  81963. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  81964. + txstatus.d32 =
  81965. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  81966. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  81967. +
  81968. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  81969. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  81970. + retval = -DWC_E_AGAIN;
  81971. + } else {
  81972. + if (ep->dwc_ep.num == 0) {
  81973. + pcd->ep0state = EP0_STALL;
  81974. + }
  81975. +
  81976. + ep->stopped = 1;
  81977. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  81978. + &ep->dwc_ep);
  81979. + }
  81980. + } else {
  81981. + if (ep->dwc_ep.num == 0) {
  81982. + pcd->ep0state = EP0_STALL;
  81983. + }
  81984. +
  81985. + ep->stopped = 1;
  81986. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81987. + }
  81988. + } else if (value == 2) {
  81989. + ep->dwc_ep.stall_clear_flag = 0;
  81990. + } else if (value == 3) {
  81991. + ep->dwc_ep.stall_clear_flag = 1;
  81992. + }
  81993. +
  81994. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81995. +
  81996. + return retval;
  81997. +}
  81998. +
  81999. +/**
  82000. + * This function initiates remote wakeup of the host from suspend state.
  82001. + */
  82002. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  82003. +{
  82004. + dctl_data_t dctl = { 0 };
  82005. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82006. + dsts_data_t dsts;
  82007. +
  82008. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  82009. + if (!dsts.b.suspsts) {
  82010. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  82011. + }
  82012. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  82013. + if (pcd->remote_wakeup_enable) {
  82014. + if (set) {
  82015. +
  82016. + if (core_if->adp_enable) {
  82017. + gpwrdn_data_t gpwrdn;
  82018. +
  82019. + dwc_otg_adp_probe_stop(core_if);
  82020. +
  82021. + /* Mask SRP detected interrupt from Power Down Logic */
  82022. + gpwrdn.d32 = 0;
  82023. + gpwrdn.b.srp_det_msk = 1;
  82024. + DWC_MODIFY_REG32(&core_if->
  82025. + core_global_regs->gpwrdn,
  82026. + gpwrdn.d32, 0);
  82027. +
  82028. + /* Disable Power Down Logic */
  82029. + gpwrdn.d32 = 0;
  82030. + gpwrdn.b.pmuactv = 1;
  82031. + DWC_MODIFY_REG32(&core_if->
  82032. + core_global_regs->gpwrdn,
  82033. + gpwrdn.d32, 0);
  82034. +
  82035. + /*
  82036. + * Initialize the Core for Device mode.
  82037. + */
  82038. + core_if->op_state = B_PERIPHERAL;
  82039. + dwc_otg_core_init(core_if);
  82040. + dwc_otg_enable_global_interrupts(core_if);
  82041. + cil_pcd_start(core_if);
  82042. +
  82043. + dwc_otg_initiate_srp(core_if);
  82044. + }
  82045. +
  82046. + dctl.b.rmtwkupsig = 1;
  82047. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  82048. + dctl, 0, dctl.d32);
  82049. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  82050. +
  82051. + dwc_mdelay(2);
  82052. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  82053. + dctl, dctl.d32, 0);
  82054. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  82055. + }
  82056. + } else {
  82057. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  82058. + }
  82059. +}
  82060. +
  82061. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82062. +/**
  82063. + * This function initiates remote wakeup of the host from L1 sleep state.
  82064. + */
  82065. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  82066. +{
  82067. + glpmcfg_data_t lpmcfg;
  82068. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82069. +
  82070. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  82071. +
  82072. + /* Check if we are in L1 state */
  82073. + if (!lpmcfg.b.prt_sleep_sts) {
  82074. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  82075. + return;
  82076. + }
  82077. +
  82078. + /* Check if host allows remote wakeup */
  82079. + if (!lpmcfg.b.rem_wkup_en) {
  82080. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  82081. + return;
  82082. + }
  82083. +
  82084. + /* Check if Resume OK */
  82085. + if (!lpmcfg.b.sleep_state_resumeok) {
  82086. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  82087. + return;
  82088. + }
  82089. +
  82090. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  82091. + lpmcfg.b.en_utmi_sleep = 0;
  82092. + lpmcfg.b.hird_thres &= (~(1 << 4));
  82093. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  82094. +
  82095. + if (set) {
  82096. + dctl_data_t dctl = {.d32 = 0 };
  82097. + dctl.b.rmtwkupsig = 1;
  82098. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  82099. + * Hardware will automatically clear this bit.
  82100. + */
  82101. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  82102. + 0, dctl.d32);
  82103. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  82104. + }
  82105. +
  82106. +}
  82107. +#endif
  82108. +
  82109. +/**
  82110. + * Performs remote wakeup.
  82111. + */
  82112. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  82113. +{
  82114. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82115. + dwc_irqflags_t flags;
  82116. + if (dwc_otg_is_device_mode(core_if)) {
  82117. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  82118. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82119. + if (core_if->lx_state == DWC_OTG_L1) {
  82120. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  82121. + } else {
  82122. +#endif
  82123. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  82124. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82125. + }
  82126. +#endif
  82127. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  82128. + }
  82129. + return;
  82130. +}
  82131. +
  82132. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  82133. +{
  82134. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82135. + dctl_data_t dctl = { 0 };
  82136. +
  82137. + if (dwc_otg_is_device_mode(core_if)) {
  82138. + dctl.b.sftdiscon = 1;
  82139. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  82140. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  82141. + dwc_udelay(no_of_usecs);
  82142. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  82143. +
  82144. + } else{
  82145. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  82146. + }
  82147. + return;
  82148. +
  82149. +}
  82150. +
  82151. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  82152. +{
  82153. + dsts_data_t dsts;
  82154. + gotgctl_data_t gotgctl;
  82155. +
  82156. + /*
  82157. + * This function starts the Protocol if no session is in progress. If
  82158. + * a session is already in progress, but the device is suspended,
  82159. + * remote wakeup signaling is started.
  82160. + */
  82161. +
  82162. + /* Check if valid session */
  82163. + gotgctl.d32 =
  82164. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  82165. + if (gotgctl.b.bsesvld) {
  82166. + /* Check if suspend state */
  82167. + dsts.d32 =
  82168. + DWC_READ_REG32(&
  82169. + (GET_CORE_IF(pcd)->dev_if->
  82170. + dev_global_regs->dsts));
  82171. + if (dsts.b.suspsts) {
  82172. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  82173. + }
  82174. + } else {
  82175. + dwc_otg_pcd_initiate_srp(pcd);
  82176. + }
  82177. +
  82178. + return 0;
  82179. +
  82180. +}
  82181. +
  82182. +/**
  82183. + * Start the SRP timer to detect when the SRP does not complete within
  82184. + * 6 seconds.
  82185. + *
  82186. + * @param pcd the pcd structure.
  82187. + */
  82188. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  82189. +{
  82190. + dwc_irqflags_t flags;
  82191. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  82192. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  82193. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  82194. +}
  82195. +
  82196. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  82197. +{
  82198. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  82199. +}
  82200. +
  82201. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  82202. +{
  82203. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  82204. +}
  82205. +
  82206. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  82207. +{
  82208. + return pcd->b_hnp_enable;
  82209. +}
  82210. +
  82211. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  82212. +{
  82213. + return pcd->a_hnp_support;
  82214. +}
  82215. +
  82216. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  82217. +{
  82218. + return pcd->a_alt_hnp_support;
  82219. +}
  82220. +
  82221. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  82222. +{
  82223. + return pcd->remote_wakeup_enable;
  82224. +}
  82225. +
  82226. +#endif /* DWC_HOST_ONLY */
  82227. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  82228. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 1970-01-01 01:00:00.000000000 +0100
  82229. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 2014-04-24 15:35:04.177565820 +0200
  82230. @@ -0,0 +1,266 @@
  82231. +/* ==========================================================================
  82232. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  82233. + * $Revision: #48 $
  82234. + * $Date: 2012/08/10 $
  82235. + * $Change: 2047372 $
  82236. + *
  82237. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82238. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82239. + * otherwise expressly agreed to in writing between Synopsys and you.
  82240. + *
  82241. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82242. + * any End User Software License Agreement or Agreement for Licensed Product
  82243. + * with Synopsys or any supplement thereto. You are permitted to use and
  82244. + * redistribute this Software in source and binary forms, with or without
  82245. + * modification, provided that redistributions of source code must retain this
  82246. + * notice. You may not view, use, disclose, copy or distribute this file or
  82247. + * any information contained herein except pursuant to this license grant from
  82248. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82249. + * below, then you are not authorized to use the Software.
  82250. + *
  82251. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82252. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82253. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82254. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82255. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82256. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82257. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82258. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82259. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82260. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82261. + * DAMAGE.
  82262. + * ========================================================================== */
  82263. +#ifndef DWC_HOST_ONLY
  82264. +#if !defined(__DWC_PCD_H__)
  82265. +#define __DWC_PCD_H__
  82266. +
  82267. +#include "dwc_otg_os_dep.h"
  82268. +#include "usb.h"
  82269. +#include "dwc_otg_cil.h"
  82270. +#include "dwc_otg_pcd_if.h"
  82271. +struct cfiobject;
  82272. +
  82273. +/**
  82274. + * @file
  82275. + *
  82276. + * This file contains the structures, constants, and interfaces for
  82277. + * the Perpherial Contoller Driver (PCD).
  82278. + *
  82279. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  82280. + * Gadget API, so that the existing Gadget drivers can be used. For
  82281. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  82282. + * (FBS) driver will be used. The FBS driver supports the
  82283. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  82284. + * transports.
  82285. + *
  82286. + */
  82287. +
  82288. +/** Invalid DMA Address */
  82289. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  82290. +
  82291. +/** Max Transfer size for any EP */
  82292. +#define DDMA_MAX_TRANSFER_SIZE 65535
  82293. +
  82294. +/**
  82295. + * Get the pointer to the core_if from the pcd pointer.
  82296. + */
  82297. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  82298. +
  82299. +/**
  82300. + * States of EP0.
  82301. + */
  82302. +typedef enum ep0_state {
  82303. + EP0_DISCONNECT, /* no host */
  82304. + EP0_IDLE,
  82305. + EP0_IN_DATA_PHASE,
  82306. + EP0_OUT_DATA_PHASE,
  82307. + EP0_IN_STATUS_PHASE,
  82308. + EP0_OUT_STATUS_PHASE,
  82309. + EP0_STALL,
  82310. +} ep0state_e;
  82311. +
  82312. +/** Fordward declaration.*/
  82313. +struct dwc_otg_pcd;
  82314. +
  82315. +/** DWC_otg iso request structure.
  82316. + *
  82317. + */
  82318. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  82319. +
  82320. +#ifdef DWC_UTE_PER_IO
  82321. +
  82322. +/**
  82323. + * This shall be the exact analogy of the same type structure defined in the
  82324. + * usb_gadget.h. Each descriptor contains
  82325. + */
  82326. +struct dwc_iso_pkt_desc_port {
  82327. + uint32_t offset;
  82328. + uint32_t length; /* expected length */
  82329. + uint32_t actual_length;
  82330. + uint32_t status;
  82331. +};
  82332. +
  82333. +struct dwc_iso_xreq_port {
  82334. + /** transfer/submission flag */
  82335. + uint32_t tr_sub_flags;
  82336. + /** Start the request ASAP */
  82337. +#define DWC_EREQ_TF_ASAP 0x00000002
  82338. + /** Just enqueue the request w/o initiating a transfer */
  82339. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  82340. +
  82341. + /**
  82342. + * count of ISO packets attached to this request - shall
  82343. + * not exceed the pio_alloc_pkt_count
  82344. + */
  82345. + uint32_t pio_pkt_count;
  82346. + /** count of ISO packets allocated for this request */
  82347. + uint32_t pio_alloc_pkt_count;
  82348. + /** number of ISO packet errors */
  82349. + uint32_t error_count;
  82350. + /** reserved for future extension */
  82351. + uint32_t res;
  82352. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  82353. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  82354. +};
  82355. +#endif
  82356. +/** DWC_otg request structure.
  82357. + * This structure is a list of requests.
  82358. + */
  82359. +typedef struct dwc_otg_pcd_request {
  82360. + void *priv;
  82361. + void *buf;
  82362. + dwc_dma_t dma;
  82363. + uint32_t length;
  82364. + uint32_t actual;
  82365. + unsigned sent_zlp:1;
  82366. + /**
  82367. + * Used instead of original buffer if
  82368. + * it(physical address) is not dword-aligned.
  82369. + **/
  82370. + uint8_t *dw_align_buf;
  82371. + dwc_dma_t dw_align_buf_dma;
  82372. +
  82373. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  82374. +#ifdef DWC_UTE_PER_IO
  82375. + struct dwc_iso_xreq_port ext_req;
  82376. + //void *priv_ereq_nport; /* */
  82377. +#endif
  82378. +} dwc_otg_pcd_request_t;
  82379. +
  82380. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  82381. +
  82382. +/** PCD EP structure.
  82383. + * This structure describes an EP, there is an array of EPs in the PCD
  82384. + * structure.
  82385. + */
  82386. +typedef struct dwc_otg_pcd_ep {
  82387. + /** USB EP Descriptor */
  82388. + const usb_endpoint_descriptor_t *desc;
  82389. +
  82390. + /** queue of dwc_otg_pcd_requests. */
  82391. + struct req_list queue;
  82392. + unsigned stopped:1;
  82393. + unsigned disabling:1;
  82394. + unsigned dma:1;
  82395. + unsigned queue_sof:1;
  82396. +
  82397. +#ifdef DWC_EN_ISOC
  82398. + /** ISOC req handle passed */
  82399. + void *iso_req_handle;
  82400. +#endif //_EN_ISOC_
  82401. +
  82402. + /** DWC_otg ep data. */
  82403. + dwc_ep_t dwc_ep;
  82404. +
  82405. + /** Pointer to PCD */
  82406. + struct dwc_otg_pcd *pcd;
  82407. +
  82408. + void *priv;
  82409. +} dwc_otg_pcd_ep_t;
  82410. +
  82411. +/** DWC_otg PCD Structure.
  82412. + * This structure encapsulates the data for the dwc_otg PCD.
  82413. + */
  82414. +struct dwc_otg_pcd {
  82415. + const struct dwc_otg_pcd_function_ops *fops;
  82416. + /** The DWC otg device pointer */
  82417. + struct dwc_otg_device *otg_dev;
  82418. + /** Core Interface */
  82419. + dwc_otg_core_if_t *core_if;
  82420. + /** State of EP0 */
  82421. + ep0state_e ep0state;
  82422. + /** EP0 Request is pending */
  82423. + unsigned ep0_pending:1;
  82424. + /** Indicates when SET CONFIGURATION Request is in process */
  82425. + unsigned request_config:1;
  82426. + /** The state of the Remote Wakeup Enable. */
  82427. + unsigned remote_wakeup_enable:1;
  82428. + /** The state of the B-Device HNP Enable. */
  82429. + unsigned b_hnp_enable:1;
  82430. + /** The state of A-Device HNP Support. */
  82431. + unsigned a_hnp_support:1;
  82432. + /** The state of the A-Device Alt HNP support. */
  82433. + unsigned a_alt_hnp_support:1;
  82434. + /** Count of pending Requests */
  82435. + unsigned request_pending;
  82436. +
  82437. + /** SETUP packet for EP0
  82438. + * This structure is allocated as a DMA buffer on PCD initialization
  82439. + * with enough space for up to 3 setup packets.
  82440. + */
  82441. + union {
  82442. + usb_device_request_t req;
  82443. + uint32_t d32[2];
  82444. + } *setup_pkt;
  82445. +
  82446. + dwc_dma_t setup_pkt_dma_handle;
  82447. +
  82448. + /* Additional buffer and flag for CTRL_WR premature case */
  82449. + uint8_t *backup_buf;
  82450. + unsigned data_terminated;
  82451. +
  82452. + /** 2-byte dma buffer used to return status from GET_STATUS */
  82453. + uint16_t *status_buf;
  82454. + dwc_dma_t status_buf_dma_handle;
  82455. +
  82456. + /** EP0 */
  82457. + dwc_otg_pcd_ep_t ep0;
  82458. +
  82459. + /** Array of IN EPs. */
  82460. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  82461. + /** Array of OUT EPs. */
  82462. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  82463. + /** number of valid EPs in the above array. */
  82464. +// unsigned num_eps : 4;
  82465. + dwc_spinlock_t *lock;
  82466. +
  82467. + /** Tasklet to defer starting of TEST mode transmissions until
  82468. + * Status Phase has been completed.
  82469. + */
  82470. + dwc_tasklet_t *test_mode_tasklet;
  82471. +
  82472. + /** Tasklet to delay starting of xfer in DMA mode */
  82473. + dwc_tasklet_t *start_xfer_tasklet;
  82474. +
  82475. + /** The test mode to enter when the tasklet is executed. */
  82476. + unsigned test_mode;
  82477. + /** The cfi_api structure that implements most of the CFI API
  82478. + * and OTG specific core configuration functionality
  82479. + */
  82480. +#ifdef DWC_UTE_CFI
  82481. + struct cfiobject *cfi;
  82482. +#endif
  82483. +
  82484. +};
  82485. +
  82486. +//FIXME this functions should be static, and this prototypes should be removed
  82487. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  82488. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  82489. + dwc_otg_pcd_request_t * req, int32_t status);
  82490. +
  82491. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  82492. + void *req_handle);
  82493. +
  82494. +extern void do_test_mode(void *data);
  82495. +#endif
  82496. +#endif /* DWC_HOST_ONLY */
  82497. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  82498. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 1970-01-01 01:00:00.000000000 +0100
  82499. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 2014-04-24 15:35:04.177565820 +0200
  82500. @@ -0,0 +1,360 @@
  82501. +/* ==========================================================================
  82502. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  82503. + * $Revision: #11 $
  82504. + * $Date: 2011/10/26 $
  82505. + * $Change: 1873028 $
  82506. + *
  82507. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82508. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82509. + * otherwise expressly agreed to in writing between Synopsys and you.
  82510. + *
  82511. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82512. + * any End User Software License Agreement or Agreement for Licensed Product
  82513. + * with Synopsys or any supplement thereto. You are permitted to use and
  82514. + * redistribute this Software in source and binary forms, with or without
  82515. + * modification, provided that redistributions of source code must retain this
  82516. + * notice. You may not view, use, disclose, copy or distribute this file or
  82517. + * any information contained herein except pursuant to this license grant from
  82518. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82519. + * below, then you are not authorized to use the Software.
  82520. + *
  82521. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82522. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82523. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82524. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82525. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82526. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82527. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82528. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82529. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82530. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82531. + * DAMAGE.
  82532. + * ========================================================================== */
  82533. +#ifndef DWC_HOST_ONLY
  82534. +
  82535. +#if !defined(__DWC_PCD_IF_H__)
  82536. +#define __DWC_PCD_IF_H__
  82537. +
  82538. +//#include "dwc_os.h"
  82539. +#include "dwc_otg_core_if.h"
  82540. +
  82541. +/** @file
  82542. + * This file defines DWC_OTG PCD Core API.
  82543. + */
  82544. +
  82545. +struct dwc_otg_pcd;
  82546. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  82547. +
  82548. +/** Maxpacket size for EP0 */
  82549. +#define MAX_EP0_SIZE 64
  82550. +/** Maxpacket size for any EP */
  82551. +#define MAX_PACKET_SIZE 1024
  82552. +
  82553. +/** @name Function Driver Callbacks */
  82554. +/** @{ */
  82555. +
  82556. +/** This function will be called whenever a previously queued request has
  82557. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  82558. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  82559. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  82560. + * parameters. */
  82561. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  82562. + void *req_handle, int32_t status,
  82563. + uint32_t actual);
  82564. +/**
  82565. + * This function will be called whenever a previousle queued ISOC request has
  82566. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  82567. + * function.
  82568. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  82569. + * functions.
  82570. + */
  82571. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  82572. + void *req_handle, int proc_buf_num);
  82573. +/** This function should handle any SETUP request that cannot be handled by the
  82574. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  82575. + * class-specific requests, etc. The function must non-blocking.
  82576. + *
  82577. + * Returns 0 on success.
  82578. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  82579. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  82580. + * Returns -DWC_E_SHUTDOWN on any other error. */
  82581. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  82582. +/** This is called whenever the device has been disconnected. The function
  82583. + * driver should take appropriate action to clean up all pending requests in the
  82584. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  82585. + * state. */
  82586. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  82587. +/** This function is called when device has been connected. */
  82588. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  82589. +/** This function is called when device has been suspended */
  82590. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  82591. +/** This function is called when device has received LPM tokens, i.e.
  82592. + * device has been sent to sleep state. */
  82593. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  82594. +/** This function is called when device has been resumed
  82595. + * from suspend(L2) or L1 sleep state. */
  82596. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  82597. +/** This function is called whenever hnp params has been changed.
  82598. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  82599. + * to get hnp parameters. */
  82600. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  82601. +/** This function is called whenever USB RESET is detected. */
  82602. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  82603. +
  82604. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  82605. +
  82606. +/**
  82607. + *
  82608. + * @param ep_handle Void pointer to the usb_ep structure
  82609. + * @param ereq_port Pointer to the extended request structure created in the
  82610. + * portable part.
  82611. + */
  82612. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  82613. + void *req_handle, int32_t status,
  82614. + void *ereq_port);
  82615. +/** Function Driver Ops Data Structure */
  82616. +struct dwc_otg_pcd_function_ops {
  82617. + dwc_connect_cb_t connect;
  82618. + dwc_disconnect_cb_t disconnect;
  82619. + dwc_setup_cb_t setup;
  82620. + dwc_completion_cb_t complete;
  82621. + dwc_isoc_completion_cb_t isoc_complete;
  82622. + dwc_suspend_cb_t suspend;
  82623. + dwc_sleep_cb_t sleep;
  82624. + dwc_resume_cb_t resume;
  82625. + dwc_reset_cb_t reset;
  82626. + dwc_hnp_params_changed_cb_t hnp_changed;
  82627. + cfi_setup_cb_t cfi_setup;
  82628. +#ifdef DWC_UTE_PER_IO
  82629. + xiso_completion_cb_t xisoc_complete;
  82630. +#endif
  82631. +};
  82632. +/** @} */
  82633. +
  82634. +/** @name Function Driver Functions */
  82635. +/** @{ */
  82636. +
  82637. +/** Call this function to get pointer on dwc_otg_pcd_t,
  82638. + * this pointer will be used for all PCD API functions.
  82639. + *
  82640. + * @param core_if The DWC_OTG Core
  82641. + */
  82642. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  82643. +
  82644. +/** Frees PCD allocated by dwc_otg_pcd_init
  82645. + *
  82646. + * @param pcd The PCD
  82647. + */
  82648. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  82649. +
  82650. +/** Call this to bind the function driver to the PCD Core.
  82651. + *
  82652. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  82653. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  82654. + */
  82655. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  82656. + const struct dwc_otg_pcd_function_ops *fops);
  82657. +
  82658. +/** Enables an endpoint for use. This function enables an endpoint in
  82659. + * the PCD. The endpoint is described by the ep_desc which has the
  82660. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  82661. + * to the endpoint from other API functions and in callbacks. Normally this
  82662. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  82663. + * core for that interface.
  82664. + *
  82665. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82666. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82667. + * Returns 0 on success.
  82668. + *
  82669. + * @param pcd The PCD
  82670. + * @param ep_desc Endpoint descriptor
  82671. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  82672. + */
  82673. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  82674. + const uint8_t * ep_desc, void *usb_ep);
  82675. +
  82676. +/** Disable the endpoint referenced by ep_handle.
  82677. + *
  82678. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82679. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  82680. + * Returns 0 on success. */
  82681. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  82682. +
  82683. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  82684. + * After the transfer is completes, the complete callback will be called with
  82685. + * the request status.
  82686. + *
  82687. + * @param pcd The PCD
  82688. + * @param ep_handle The handle of the endpoint
  82689. + * @param buf The buffer for the data
  82690. + * @param dma_buf The DMA buffer for the data
  82691. + * @param buflen The length of the data transfer
  82692. + * @param zero Specifies whether to send zero length last packet.
  82693. + * @param req_handle Set this handle to any value to use to reference this
  82694. + * request in the ep_dequeue function or from the complete callback
  82695. + * @param atomic_alloc If driver need to perform atomic allocations
  82696. + * for internal data structures.
  82697. + *
  82698. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82699. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82700. + * Returns 0 on success. */
  82701. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82702. + uint8_t * buf, dwc_dma_t dma_buf,
  82703. + uint32_t buflen, int zero, void *req_handle,
  82704. + int atomic_alloc);
  82705. +#ifdef DWC_UTE_PER_IO
  82706. +/**
  82707. + *
  82708. + * @param ereq_nonport Pointer to the extended request part of the
  82709. + * usb_request structure defined in usb_gadget.h file.
  82710. + */
  82711. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82712. + uint8_t * buf, dwc_dma_t dma_buf,
  82713. + uint32_t buflen, int zero,
  82714. + void *req_handle, int atomic_alloc,
  82715. + void *ereq_nonport);
  82716. +
  82717. +#endif
  82718. +
  82719. +/** De-queue the specified data transfer that has not yet completed.
  82720. + *
  82721. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82722. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82723. + * Returns 0 on success. */
  82724. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82725. + void *req_handle);
  82726. +
  82727. +/** Halt (STALL) an endpoint or clear it.
  82728. + *
  82729. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82730. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82731. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  82732. + * Returns 0 on success. */
  82733. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  82734. +
  82735. +/** This function */
  82736. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  82737. +
  82738. +/** This function should be called on every hardware interrupt */
  82739. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  82740. +
  82741. +/** This function returns current frame number */
  82742. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  82743. +
  82744. +/**
  82745. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  82746. + * For isochronous transfers duble buffering is used.
  82747. + * After processing each of buffers comlete callback will be called with
  82748. + * status for each transaction.
  82749. + *
  82750. + * @param pcd The PCD
  82751. + * @param ep_handle The handle of the endpoint
  82752. + * @param buf0 The virtual address of first data buffer
  82753. + * @param buf1 The virtual address of second data buffer
  82754. + * @param dma0 The DMA address of first data buffer
  82755. + * @param dma1 The DMA address of second data buffer
  82756. + * @param sync_frame Data pattern frame number
  82757. + * @param dp_frame Data size for pattern frame
  82758. + * @param data_per_frame Data size for regular frame
  82759. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  82760. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  82761. + * @param req_handle Handle of ISOC request
  82762. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  82763. + * internal data structures.
  82764. + *
  82765. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  82766. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  82767. + * Returns -DW_E_SHUTDOWN for any other error.
  82768. + * Returns 0 on success
  82769. + */
  82770. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  82771. + uint8_t * buf0, uint8_t * buf1,
  82772. + dwc_dma_t dma0, dwc_dma_t dma1,
  82773. + int sync_frame, int dp_frame,
  82774. + int data_per_frame, int start_frame,
  82775. + int buf_proc_intrvl, void *req_handle,
  82776. + int atomic_alloc);
  82777. +
  82778. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  82779. + *
  82780. + * @param pcd The PCD
  82781. + * @param ep_handle The handle of the endpoint
  82782. + * @param req_handle Handle of ISOC request
  82783. + *
  82784. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  82785. + * Returns 0 on success
  82786. + */
  82787. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  82788. + void *req_handle);
  82789. +
  82790. +/** Get ISOC packet status.
  82791. + *
  82792. + * @param pcd The PCD
  82793. + * @param ep_handle The handle of the endpoint
  82794. + * @param iso_req_handle Isochronoush request handle
  82795. + * @param packet Number of packet
  82796. + * @param status Out parameter for returning status
  82797. + * @param actual Out parameter for returning actual length
  82798. + * @param offset Out parameter for returning offset
  82799. + *
  82800. + */
  82801. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  82802. + void *ep_handle,
  82803. + void *iso_req_handle, int packet,
  82804. + int *status, int *actual,
  82805. + int *offset);
  82806. +
  82807. +/** Get ISOC packet count.
  82808. + *
  82809. + * @param pcd The PCD
  82810. + * @param ep_handle The handle of the endpoint
  82811. + * @param iso_req_handle
  82812. + */
  82813. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  82814. + void *ep_handle,
  82815. + void *iso_req_handle);
  82816. +
  82817. +/** This function starts the SRP Protocol if no session is in progress. If
  82818. + * a session is already in progress, but the device is suspended,
  82819. + * remote wakeup signaling is started.
  82820. + */
  82821. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  82822. +
  82823. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  82824. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  82825. +
  82826. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  82827. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  82828. +
  82829. +/** Initiate SRP */
  82830. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  82831. +
  82832. +/** Starts remote wakeup signaling. */
  82833. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  82834. +
  82835. +/** Starts micorsecond soft disconnect. */
  82836. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  82837. +/** This function returns whether device is dualspeed.*/
  82838. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  82839. +
  82840. +/** This function returns whether device is otg. */
  82841. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  82842. +
  82843. +/** These functions allow to get hnp parameters */
  82844. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  82845. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  82846. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  82847. +
  82848. +/** CFI specific Interface functions */
  82849. +/** Allocate a cfi buffer */
  82850. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  82851. + dwc_dma_t * addr, size_t buflen,
  82852. + int flags);
  82853. +
  82854. +/******************************************************************************/
  82855. +
  82856. +/** @} */
  82857. +
  82858. +#endif /* __DWC_PCD_IF_H__ */
  82859. +
  82860. +#endif /* DWC_HOST_ONLY */
  82861. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  82862. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  82863. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 2014-04-24 15:35:04.177565820 +0200
  82864. @@ -0,0 +1,5147 @@
  82865. +/* ==========================================================================
  82866. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  82867. + * $Revision: #116 $
  82868. + * $Date: 2012/08/10 $
  82869. + * $Change: 2047372 $
  82870. + *
  82871. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82872. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82873. + * otherwise expressly agreed to in writing between Synopsys and you.
  82874. + *
  82875. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82876. + * any End User Software License Agreement or Agreement for Licensed Product
  82877. + * with Synopsys or any supplement thereto. You are permitted to use and
  82878. + * redistribute this Software in source and binary forms, with or without
  82879. + * modification, provided that redistributions of source code must retain this
  82880. + * notice. You may not view, use, disclose, copy or distribute this file or
  82881. + * any information contained herein except pursuant to this license grant from
  82882. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82883. + * below, then you are not authorized to use the Software.
  82884. + *
  82885. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82886. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82887. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82888. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82889. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82890. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82891. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82892. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82893. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82894. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82895. + * DAMAGE.
  82896. + * ========================================================================== */
  82897. +#ifndef DWC_HOST_ONLY
  82898. +
  82899. +#include "dwc_otg_pcd.h"
  82900. +
  82901. +#ifdef DWC_UTE_CFI
  82902. +#include "dwc_otg_cfi.h"
  82903. +#endif
  82904. +
  82905. +#ifdef DWC_UTE_PER_IO
  82906. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  82907. +#endif
  82908. +//#define PRINT_CFI_DMA_DESCS
  82909. +
  82910. +#define DEBUG_EP0
  82911. +
  82912. +/**
  82913. + * This function updates OTG.
  82914. + */
  82915. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  82916. +{
  82917. +
  82918. + if (reset) {
  82919. + pcd->b_hnp_enable = 0;
  82920. + pcd->a_hnp_support = 0;
  82921. + pcd->a_alt_hnp_support = 0;
  82922. + }
  82923. +
  82924. + if (pcd->fops->hnp_changed) {
  82925. + pcd->fops->hnp_changed(pcd);
  82926. + }
  82927. +}
  82928. +
  82929. +/** @file
  82930. + * This file contains the implementation of the PCD Interrupt handlers.
  82931. + *
  82932. + * The PCD handles the device interrupts. Many conditions can cause a
  82933. + * device interrupt. When an interrupt occurs, the device interrupt
  82934. + * service routine determines the cause of the interrupt and
  82935. + * dispatches handling to the appropriate function. These interrupt
  82936. + * handling functions are described below.
  82937. + * All interrupt registers are processed from LSB to MSB.
  82938. + */
  82939. +
  82940. +/**
  82941. + * This function prints the ep0 state for debug purposes.
  82942. + */
  82943. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  82944. +{
  82945. +#ifdef DEBUG
  82946. + char str[40];
  82947. +
  82948. + switch (pcd->ep0state) {
  82949. + case EP0_DISCONNECT:
  82950. + dwc_strcpy(str, "EP0_DISCONNECT");
  82951. + break;
  82952. + case EP0_IDLE:
  82953. + dwc_strcpy(str, "EP0_IDLE");
  82954. + break;
  82955. + case EP0_IN_DATA_PHASE:
  82956. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  82957. + break;
  82958. + case EP0_OUT_DATA_PHASE:
  82959. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  82960. + break;
  82961. + case EP0_IN_STATUS_PHASE:
  82962. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  82963. + break;
  82964. + case EP0_OUT_STATUS_PHASE:
  82965. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  82966. + break;
  82967. + case EP0_STALL:
  82968. + dwc_strcpy(str, "EP0_STALL");
  82969. + break;
  82970. + default:
  82971. + dwc_strcpy(str, "EP0_INVALID");
  82972. + }
  82973. +
  82974. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  82975. +#endif
  82976. +}
  82977. +
  82978. +/**
  82979. + * This function calculate the size of the payload in the memory
  82980. + * for out endpoints and prints size for debug purposes(used in
  82981. + * 2.93a DevOutNak feature).
  82982. + */
  82983. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  82984. +{
  82985. +#ifdef DEBUG
  82986. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  82987. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  82988. + int pack_num;
  82989. + unsigned payload;
  82990. +
  82991. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  82992. + deptsiz_updt.d32 =
  82993. + DWC_READ_REG32(&pcd->core_if->dev_if->
  82994. + out_ep_regs[ep->num]->doeptsiz);
  82995. + /* Payload will be */
  82996. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  82997. + /* Packet count is decremented every time a packet
  82998. + * is written to the RxFIFO not in to the external memory
  82999. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  83000. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  83001. + DWC_DEBUGPL(DBG_PCDV,
  83002. + "Payload for EP%d-%s\n",
  83003. + ep->num, (ep->is_in ? "IN" : "OUT"));
  83004. + DWC_DEBUGPL(DBG_PCDV,
  83005. + "Number of transfered bytes = 0x%08x\n", payload);
  83006. + DWC_DEBUGPL(DBG_PCDV,
  83007. + "Number of transfered packets = %d\n", pack_num);
  83008. +#endif
  83009. +}
  83010. +
  83011. +
  83012. +#ifdef DWC_UTE_CFI
  83013. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  83014. + const uint8_t * epname, int descnum)
  83015. +{
  83016. + CFI_INFO
  83017. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  83018. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  83019. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  83020. + ddesc->status.b.bs);
  83021. +}
  83022. +#endif
  83023. +
  83024. +/**
  83025. + * This function returns pointer to in ep struct with number ep_num
  83026. + */
  83027. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  83028. +{
  83029. + int i;
  83030. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  83031. + if (ep_num == 0) {
  83032. + return &pcd->ep0;
  83033. + } else {
  83034. + for (i = 0; i < num_in_eps; ++i) {
  83035. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  83036. + return &pcd->in_ep[i];
  83037. + }
  83038. + return 0;
  83039. + }
  83040. +}
  83041. +
  83042. +/**
  83043. + * This function returns pointer to out ep struct with number ep_num
  83044. + */
  83045. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  83046. +{
  83047. + int i;
  83048. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  83049. + if (ep_num == 0) {
  83050. + return &pcd->ep0;
  83051. + } else {
  83052. + for (i = 0; i < num_out_eps; ++i) {
  83053. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  83054. + return &pcd->out_ep[i];
  83055. + }
  83056. + return 0;
  83057. + }
  83058. +}
  83059. +
  83060. +/**
  83061. + * This functions gets a pointer to an EP from the wIndex address
  83062. + * value of the control request.
  83063. + */
  83064. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  83065. +{
  83066. + dwc_otg_pcd_ep_t *ep;
  83067. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  83068. +
  83069. + if (ep_num == 0) {
  83070. + ep = &pcd->ep0;
  83071. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  83072. + ep = &pcd->in_ep[ep_num - 1];
  83073. + } else {
  83074. + ep = &pcd->out_ep[ep_num - 1];
  83075. + }
  83076. +
  83077. + return ep;
  83078. +}
  83079. +
  83080. +/**
  83081. + * This function checks the EP request queue, if the queue is not
  83082. + * empty the next request is started.
  83083. + */
  83084. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  83085. +{
  83086. + dwc_otg_pcd_request_t *req = 0;
  83087. + uint32_t max_transfer =
  83088. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  83089. +
  83090. +#ifdef DWC_UTE_CFI
  83091. + struct dwc_otg_pcd *pcd;
  83092. + pcd = ep->pcd;
  83093. +#endif
  83094. +
  83095. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  83096. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  83097. +
  83098. +#ifdef DWC_UTE_CFI
  83099. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  83100. + ep->dwc_ep.cfi_req_len = req->length;
  83101. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  83102. + } else {
  83103. +#endif
  83104. + /* Setup and start the Transfer */
  83105. + if (req->dw_align_buf) {
  83106. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  83107. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  83108. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  83109. + } else {
  83110. + ep->dwc_ep.dma_addr = req->dma;
  83111. + ep->dwc_ep.start_xfer_buff = req->buf;
  83112. + ep->dwc_ep.xfer_buff = req->buf;
  83113. + }
  83114. + ep->dwc_ep.sent_zlp = 0;
  83115. + ep->dwc_ep.total_len = req->length;
  83116. + ep->dwc_ep.xfer_len = 0;
  83117. + ep->dwc_ep.xfer_count = 0;
  83118. +
  83119. + ep->dwc_ep.maxxfer = max_transfer;
  83120. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  83121. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  83122. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  83123. + if (ep->dwc_ep.is_in) {
  83124. + if (ep->dwc_ep.maxxfer >
  83125. + DDMA_MAX_TRANSFER_SIZE) {
  83126. + ep->dwc_ep.maxxfer =
  83127. + DDMA_MAX_TRANSFER_SIZE;
  83128. + }
  83129. + } else {
  83130. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  83131. + ep->dwc_ep.maxxfer =
  83132. + out_max_xfer;
  83133. + }
  83134. + }
  83135. + }
  83136. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  83137. + ep->dwc_ep.maxxfer -=
  83138. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  83139. + }
  83140. + if (req->sent_zlp) {
  83141. + if ((ep->dwc_ep.total_len %
  83142. + ep->dwc_ep.maxpacket == 0)
  83143. + && (ep->dwc_ep.total_len != 0)) {
  83144. + ep->dwc_ep.sent_zlp = 1;
  83145. + }
  83146. +
  83147. + }
  83148. +#ifdef DWC_UTE_CFI
  83149. + }
  83150. +#endif
  83151. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  83152. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  83153. + DWC_PRINTF("There are no more ISOC requests \n");
  83154. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  83155. + }
  83156. +}
  83157. +
  83158. +/**
  83159. + * This function handles the SOF Interrupts. At this time the SOF
  83160. + * Interrupt is disabled.
  83161. + */
  83162. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  83163. +{
  83164. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83165. +
  83166. + gintsts_data_t gintsts;
  83167. +
  83168. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  83169. +
  83170. + /* Clear interrupt */
  83171. + gintsts.d32 = 0;
  83172. + gintsts.b.sofintr = 1;
  83173. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83174. +
  83175. + return 1;
  83176. +}
  83177. +
  83178. +/**
  83179. + * This function handles the Rx Status Queue Level Interrupt, which
  83180. + * indicates that there is a least one packet in the Rx FIFO. The
  83181. + * packets are moved from the FIFO to memory, where they will be
  83182. + * processed when the Endpoint Interrupt Register indicates Transfer
  83183. + * Complete or SETUP Phase Done.
  83184. + *
  83185. + * Repeat the following until the Rx Status Queue is empty:
  83186. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  83187. + * info
  83188. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  83189. + * and exit
  83190. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  83191. + * SETUP data to the buffer
  83192. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  83193. + * to the destination buffer
  83194. + */
  83195. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  83196. +{
  83197. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83198. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  83199. + gintmsk_data_t gintmask = {.d32 = 0 };
  83200. + device_grxsts_data_t status;
  83201. + dwc_otg_pcd_ep_t *ep;
  83202. + gintsts_data_t gintsts;
  83203. +#ifdef DEBUG
  83204. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  83205. +#endif
  83206. +
  83207. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  83208. + /* Disable the Rx Status Queue Level interrupt */
  83209. + gintmask.b.rxstsqlvl = 1;
  83210. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  83211. +
  83212. + /* Get the Status from the top of the FIFO */
  83213. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  83214. +
  83215. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  83216. + "pktsts:%x Frame:%d(0x%0x)\n",
  83217. + status.b.epnum, status.b.bcnt,
  83218. + dpid_str[status.b.dpid],
  83219. + status.b.pktsts, status.b.fn, status.b.fn);
  83220. + /* Get pointer to EP structure */
  83221. + ep = get_out_ep(pcd, status.b.epnum);
  83222. +
  83223. + switch (status.b.pktsts) {
  83224. + case DWC_DSTS_GOUT_NAK:
  83225. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  83226. + break;
  83227. + case DWC_STS_DATA_UPDT:
  83228. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  83229. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  83230. + /** @todo NGS Check for buffer overflow? */
  83231. + dwc_otg_read_packet(core_if,
  83232. + ep->dwc_ep.xfer_buff,
  83233. + status.b.bcnt);
  83234. + ep->dwc_ep.xfer_count += status.b.bcnt;
  83235. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  83236. + }
  83237. + break;
  83238. + case DWC_STS_XFER_COMP:
  83239. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  83240. + break;
  83241. + case DWC_DSTS_SETUP_COMP:
  83242. +#ifdef DEBUG_EP0
  83243. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  83244. +#endif
  83245. + break;
  83246. + case DWC_DSTS_SETUP_UPDT:
  83247. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  83248. +#ifdef DEBUG_EP0
  83249. + DWC_DEBUGPL(DBG_PCD,
  83250. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  83251. + pcd->setup_pkt->req.bmRequestType,
  83252. + pcd->setup_pkt->req.bRequest,
  83253. + UGETW(pcd->setup_pkt->req.wValue),
  83254. + UGETW(pcd->setup_pkt->req.wIndex),
  83255. + UGETW(pcd->setup_pkt->req.wLength));
  83256. +#endif
  83257. + ep->dwc_ep.xfer_count += status.b.bcnt;
  83258. + break;
  83259. + default:
  83260. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  83261. + status.b.pktsts);
  83262. + break;
  83263. + }
  83264. +
  83265. + /* Enable the Rx Status Queue Level interrupt */
  83266. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  83267. + /* Clear interrupt */
  83268. + gintsts.d32 = 0;
  83269. + gintsts.b.rxstsqlvl = 1;
  83270. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  83271. +
  83272. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  83273. + return 1;
  83274. +}
  83275. +
  83276. +/**
  83277. + * This function examines the Device IN Token Learning Queue to
  83278. + * determine the EP number of the last IN token received. This
  83279. + * implementation is for the Mass Storage device where there are only
  83280. + * 2 IN EPs (Control-IN and BULK-IN).
  83281. + *
  83282. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  83283. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  83284. + *
  83285. + * @param core_if Programming view of DWC_otg controller.
  83286. + *
  83287. + */
  83288. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  83289. +{
  83290. + dwc_otg_device_global_regs_t *dev_global_regs =
  83291. + core_if->dev_if->dev_global_regs;
  83292. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  83293. + /* Number of Token Queue Registers */
  83294. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  83295. + dtknq1_data_t dtknqr1;
  83296. + uint32_t in_tkn_epnums[4];
  83297. + int ndx = 0;
  83298. + int i = 0;
  83299. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  83300. + int epnum = 0;
  83301. +
  83302. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  83303. +
  83304. + /* Read the DTKNQ Registers */
  83305. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  83306. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  83307. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  83308. + in_tkn_epnums[i]);
  83309. + if (addr == &dev_global_regs->dvbusdis) {
  83310. + addr = &dev_global_regs->dtknqr3_dthrctl;
  83311. + } else {
  83312. + ++addr;
  83313. + }
  83314. +
  83315. + }
  83316. +
  83317. + /* Copy the DTKNQR1 data to the bit field. */
  83318. + dtknqr1.d32 = in_tkn_epnums[0];
  83319. + /* Get the EP numbers */
  83320. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  83321. + ndx = dtknqr1.b.intknwptr - 1;
  83322. +
  83323. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  83324. + if (ndx == -1) {
  83325. + /** @todo Find a simpler way to calculate the max
  83326. + * queue position.*/
  83327. + int cnt = TOKEN_Q_DEPTH;
  83328. + if (TOKEN_Q_DEPTH <= 6) {
  83329. + cnt = TOKEN_Q_DEPTH - 1;
  83330. + } else if (TOKEN_Q_DEPTH <= 14) {
  83331. + cnt = TOKEN_Q_DEPTH - 7;
  83332. + } else if (TOKEN_Q_DEPTH <= 22) {
  83333. + cnt = TOKEN_Q_DEPTH - 15;
  83334. + } else {
  83335. + cnt = TOKEN_Q_DEPTH - 23;
  83336. + }
  83337. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  83338. + } else {
  83339. + if (ndx <= 5) {
  83340. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  83341. + } else if (ndx <= 13) {
  83342. + ndx -= 6;
  83343. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  83344. + } else if (ndx <= 21) {
  83345. + ndx -= 14;
  83346. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  83347. + } else if (ndx <= 29) {
  83348. + ndx -= 22;
  83349. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  83350. + }
  83351. + }
  83352. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  83353. + return epnum;
  83354. +}
  83355. +
  83356. +/**
  83357. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  83358. + * The active request is checked for the next packet to be loaded into
  83359. + * the non-periodic Tx FIFO.
  83360. + */
  83361. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  83362. +{
  83363. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83364. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  83365. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  83366. + gnptxsts_data_t txstatus = {.d32 = 0 };
  83367. + gintsts_data_t gintsts;
  83368. +
  83369. + int epnum = 0;
  83370. + dwc_otg_pcd_ep_t *ep = 0;
  83371. + uint32_t len = 0;
  83372. + int dwords;
  83373. +
  83374. + /* Get the epnum from the IN Token Learning Queue. */
  83375. + epnum = get_ep_of_last_in_token(core_if);
  83376. + ep = get_in_ep(pcd, epnum);
  83377. +
  83378. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  83379. +
  83380. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  83381. +
  83382. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83383. + if (len > ep->dwc_ep.maxpacket) {
  83384. + len = ep->dwc_ep.maxpacket;
  83385. + }
  83386. + dwords = (len + 3) / 4;
  83387. +
  83388. + /* While there is space in the queue and space in the FIFO and
  83389. + * More data to tranfer, Write packets to the Tx FIFO */
  83390. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  83391. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  83392. +
  83393. + while (txstatus.b.nptxqspcavail > 0 &&
  83394. + txstatus.b.nptxfspcavail > dwords &&
  83395. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  83396. + /* Write the FIFO */
  83397. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  83398. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83399. +
  83400. + if (len > ep->dwc_ep.maxpacket) {
  83401. + len = ep->dwc_ep.maxpacket;
  83402. + }
  83403. +
  83404. + dwords = (len + 3) / 4;
  83405. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  83406. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  83407. + }
  83408. +
  83409. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  83410. + DWC_READ_REG32(&global_regs->gnptxsts));
  83411. +
  83412. + /* Clear interrupt */
  83413. + gintsts.d32 = 0;
  83414. + gintsts.b.nptxfempty = 1;
  83415. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  83416. +
  83417. + return 1;
  83418. +}
  83419. +
  83420. +/**
  83421. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  83422. + * The active request is checked for the next packet to be loaded into
  83423. + * apropriate Tx FIFO.
  83424. + */
  83425. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  83426. +{
  83427. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83428. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83429. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  83430. + dtxfsts_data_t txstatus = {.d32 = 0 };
  83431. + dwc_otg_pcd_ep_t *ep = 0;
  83432. + uint32_t len = 0;
  83433. + int dwords;
  83434. +
  83435. + ep = get_in_ep(pcd, epnum);
  83436. +
  83437. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  83438. +
  83439. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  83440. +
  83441. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83442. +
  83443. + if (len > ep->dwc_ep.maxpacket) {
  83444. + len = ep->dwc_ep.maxpacket;
  83445. + }
  83446. +
  83447. + dwords = (len + 3) / 4;
  83448. +
  83449. + /* While there is space in the queue and space in the FIFO and
  83450. + * More data to tranfer, Write packets to the Tx FIFO */
  83451. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  83452. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  83453. +
  83454. + while (txstatus.b.txfspcavail > dwords &&
  83455. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  83456. + ep->dwc_ep.xfer_len != 0) {
  83457. + /* Write the FIFO */
  83458. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  83459. +
  83460. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  83461. + if (len > ep->dwc_ep.maxpacket) {
  83462. + len = ep->dwc_ep.maxpacket;
  83463. + }
  83464. +
  83465. + dwords = (len + 3) / 4;
  83466. + txstatus.d32 =
  83467. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  83468. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  83469. + txstatus.d32);
  83470. + }
  83471. +
  83472. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  83473. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  83474. +
  83475. + return 1;
  83476. +}
  83477. +
  83478. +/**
  83479. + * This function is called when the Device is disconnected. It stops
  83480. + * any active requests and informs the Gadget driver of the
  83481. + * disconnect.
  83482. + */
  83483. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  83484. +{
  83485. + int i, num_in_eps, num_out_eps;
  83486. + dwc_otg_pcd_ep_t *ep;
  83487. +
  83488. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83489. +
  83490. + DWC_SPINLOCK(pcd->lock);
  83491. +
  83492. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  83493. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  83494. +
  83495. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  83496. + /* don't disconnect drivers more than once */
  83497. + if (pcd->ep0state == EP0_DISCONNECT) {
  83498. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  83499. + DWC_SPINUNLOCK(pcd->lock);
  83500. + return;
  83501. + }
  83502. + pcd->ep0state = EP0_DISCONNECT;
  83503. +
  83504. + /* Reset the OTG state. */
  83505. + dwc_otg_pcd_update_otg(pcd, 1);
  83506. +
  83507. + /* Disable the NP Tx Fifo Empty Interrupt. */
  83508. + intr_mask.b.nptxfempty = 1;
  83509. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  83510. + intr_mask.d32, 0);
  83511. +
  83512. + /* Flush the FIFOs */
  83513. + /**@todo NGS Flush Periodic FIFOs */
  83514. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  83515. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  83516. +
  83517. + /* prevent new request submissions, kill any outstanding requests */
  83518. + ep = &pcd->ep0;
  83519. + dwc_otg_request_nuke(ep);
  83520. + /* prevent new request submissions, kill any outstanding requests */
  83521. + for (i = 0; i < num_in_eps; i++) {
  83522. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  83523. + dwc_otg_request_nuke(ep);
  83524. + }
  83525. + /* prevent new request submissions, kill any outstanding requests */
  83526. + for (i = 0; i < num_out_eps; i++) {
  83527. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  83528. + dwc_otg_request_nuke(ep);
  83529. + }
  83530. +
  83531. + /* report disconnect; the driver is already quiesced */
  83532. + if (pcd->fops->disconnect) {
  83533. + DWC_SPINUNLOCK(pcd->lock);
  83534. + pcd->fops->disconnect(pcd);
  83535. + DWC_SPINLOCK(pcd->lock);
  83536. + }
  83537. + DWC_SPINUNLOCK(pcd->lock);
  83538. +}
  83539. +
  83540. +/**
  83541. + * This interrupt indicates that ...
  83542. + */
  83543. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  83544. +{
  83545. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83546. + gintsts_data_t gintsts;
  83547. +
  83548. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  83549. + intr_mask.b.i2cintr = 1;
  83550. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  83551. + intr_mask.d32, 0);
  83552. +
  83553. + /* Clear interrupt */
  83554. + gintsts.d32 = 0;
  83555. + gintsts.b.i2cintr = 1;
  83556. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83557. + gintsts.d32);
  83558. + return 1;
  83559. +}
  83560. +
  83561. +/**
  83562. + * This interrupt indicates that ...
  83563. + */
  83564. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  83565. +{
  83566. + gintsts_data_t gintsts;
  83567. +#if defined(VERBOSE)
  83568. + DWC_PRINTF("Early Suspend Detected\n");
  83569. +#endif
  83570. +
  83571. + /* Clear interrupt */
  83572. + gintsts.d32 = 0;
  83573. + gintsts.b.erlysuspend = 1;
  83574. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83575. + gintsts.d32);
  83576. + return 1;
  83577. +}
  83578. +
  83579. +/**
  83580. + * This function configures EPO to receive SETUP packets.
  83581. + *
  83582. + * @todo NGS: Update the comments from the HW FS.
  83583. + *
  83584. + * -# Program the following fields in the endpoint specific registers
  83585. + * for Control OUT EP 0, in order to receive a setup packet
  83586. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  83587. + * setup packets)
  83588. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  83589. + * to back setup packets)
  83590. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  83591. + * store any setup packets received
  83592. + *
  83593. + * @param core_if Programming view of DWC_otg controller.
  83594. + * @param pcd Programming view of the PCD.
  83595. + */
  83596. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  83597. + dwc_otg_pcd_t * pcd)
  83598. +{
  83599. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83600. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  83601. + dwc_otg_dev_dma_desc_t *dma_desc;
  83602. + depctl_data_t doepctl = {.d32 = 0 };
  83603. +
  83604. +#ifdef VERBOSE
  83605. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  83606. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  83607. +#endif
  83608. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  83609. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  83610. + if (doepctl.b.epena) {
  83611. + return;
  83612. + }
  83613. + }
  83614. +
  83615. + doeptsize0.b.supcnt = 3;
  83616. + doeptsize0.b.pktcnt = 1;
  83617. + doeptsize0.b.xfersize = 8 * 3;
  83618. +
  83619. + if (core_if->dma_enable) {
  83620. + if (!core_if->dma_desc_enable) {
  83621. + /** put here as for Hermes mode deptisz register should not be written */
  83622. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  83623. + doeptsize0.d32);
  83624. +
  83625. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  83626. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  83627. + pcd->setup_pkt_dma_handle);
  83628. + } else {
  83629. + dev_if->setup_desc_index =
  83630. + (dev_if->setup_desc_index + 1) & 1;
  83631. + dma_desc =
  83632. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  83633. +
  83634. + /** DMA Descriptor Setup */
  83635. + dma_desc->status.b.bs = BS_HOST_BUSY;
  83636. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  83637. + dma_desc->status.b.sr = 0;
  83638. + dma_desc->status.b.mtrf = 0;
  83639. + }
  83640. + dma_desc->status.b.l = 1;
  83641. + dma_desc->status.b.ioc = 1;
  83642. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  83643. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  83644. + dma_desc->status.b.sts = 0;
  83645. + dma_desc->status.b.bs = BS_HOST_READY;
  83646. +
  83647. + /** DOEPDMA0 Register write */
  83648. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  83649. + dev_if->dma_setup_desc_addr
  83650. + [dev_if->setup_desc_index]);
  83651. + }
  83652. +
  83653. + } else {
  83654. + /** put here as for Hermes mode deptisz register should not be written */
  83655. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  83656. + doeptsize0.d32);
  83657. + }
  83658. +
  83659. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  83660. + doepctl.d32 = 0;
  83661. + doepctl.b.epena = 1;
  83662. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  83663. + doepctl.b.cnak = 1;
  83664. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  83665. + } else {
  83666. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  83667. + }
  83668. +
  83669. +#ifdef VERBOSE
  83670. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  83671. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  83672. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  83673. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  83674. +#endif
  83675. +}
  83676. +
  83677. +/**
  83678. + * This interrupt occurs when a USB Reset is detected. When the USB
  83679. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  83680. + * EP0 state is set to IDLE.
  83681. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  83682. + * -# Unmask the following interrupt bits
  83683. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  83684. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  83685. + * - DOEPMSK.SETUP = 1
  83686. + * - DOEPMSK.XferCompl = 1
  83687. + * - DIEPMSK.XferCompl = 1
  83688. + * - DIEPMSK.TimeOut = 1
  83689. + * -# Program the following fields in the endpoint specific registers
  83690. + * for Control OUT EP 0, in order to receive a setup packet
  83691. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  83692. + * setup packets)
  83693. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  83694. + * to back setup packets)
  83695. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  83696. + * store any setup packets received
  83697. + * At this point, all the required initialization, except for enabling
  83698. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  83699. + */
  83700. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  83701. +{
  83702. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83703. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83704. + depctl_data_t doepctl = {.d32 = 0 };
  83705. + depctl_data_t diepctl = {.d32 = 0 };
  83706. + daint_data_t daintmsk = {.d32 = 0 };
  83707. + doepmsk_data_t doepmsk = {.d32 = 0 };
  83708. + diepmsk_data_t diepmsk = {.d32 = 0 };
  83709. + dcfg_data_t dcfg = {.d32 = 0 };
  83710. + grstctl_t resetctl = {.d32 = 0 };
  83711. + dctl_data_t dctl = {.d32 = 0 };
  83712. + int i = 0;
  83713. + gintsts_data_t gintsts;
  83714. + pcgcctl_data_t power = {.d32 = 0 };
  83715. +
  83716. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  83717. + if (power.b.stoppclk) {
  83718. + power.d32 = 0;
  83719. + power.b.stoppclk = 1;
  83720. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83721. +
  83722. + power.b.pwrclmp = 1;
  83723. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83724. +
  83725. + power.b.rstpdwnmodule = 1;
  83726. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83727. + }
  83728. +
  83729. + core_if->lx_state = DWC_OTG_L0;
  83730. +
  83731. + DWC_PRINTF("USB RESET\n");
  83732. +#ifdef DWC_EN_ISOC
  83733. + for (i = 1; i < 16; ++i) {
  83734. + dwc_otg_pcd_ep_t *ep;
  83735. + dwc_ep_t *dwc_ep;
  83736. + ep = get_in_ep(pcd, i);
  83737. + if (ep != 0) {
  83738. + dwc_ep = &ep->dwc_ep;
  83739. + dwc_ep->next_frame = 0xffffffff;
  83740. + }
  83741. + }
  83742. +#endif /* DWC_EN_ISOC */
  83743. +
  83744. + /* reset the HNP settings */
  83745. + dwc_otg_pcd_update_otg(pcd, 1);
  83746. +
  83747. + /* Clear the Remote Wakeup Signalling */
  83748. + dctl.b.rmtwkupsig = 1;
  83749. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  83750. +
  83751. + /* Set NAK for all OUT EPs */
  83752. + doepctl.b.snak = 1;
  83753. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  83754. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  83755. + }
  83756. +
  83757. + /* Flush the NP Tx FIFO */
  83758. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  83759. + /* Flush the Learning Queue */
  83760. + resetctl.b.intknqflsh = 1;
  83761. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  83762. +
  83763. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  83764. + core_if->start_predict = 0;
  83765. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  83766. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  83767. + }
  83768. + core_if->nextep_seq[0] = 0;
  83769. + core_if->first_in_nextep_seq = 0;
  83770. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  83771. + diepctl.b.nextep = 0;
  83772. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  83773. +
  83774. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  83775. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  83776. + dcfg.b.epmscnt = 2;
  83777. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  83778. +
  83779. + DWC_DEBUGPL(DBG_PCDV,
  83780. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  83781. + __func__, core_if->first_in_nextep_seq);
  83782. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  83783. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  83784. + }
  83785. + }
  83786. +
  83787. + if (core_if->multiproc_int_enable) {
  83788. + daintmsk.b.inep0 = 1;
  83789. + daintmsk.b.outep0 = 1;
  83790. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  83791. + daintmsk.d32);
  83792. +
  83793. + doepmsk.b.setup = 1;
  83794. + doepmsk.b.xfercompl = 1;
  83795. + doepmsk.b.ahberr = 1;
  83796. + doepmsk.b.epdisabled = 1;
  83797. +
  83798. + if ((core_if->dma_desc_enable) ||
  83799. + (core_if->dma_enable
  83800. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  83801. + doepmsk.b.stsphsercvd = 1;
  83802. + }
  83803. + if (core_if->dma_desc_enable)
  83804. + doepmsk.b.bna = 1;
  83805. +/*
  83806. + doepmsk.b.babble = 1;
  83807. + doepmsk.b.nyet = 1;
  83808. +
  83809. + if (core_if->dma_enable) {
  83810. + doepmsk.b.nak = 1;
  83811. + }
  83812. +*/
  83813. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  83814. + doepmsk.d32);
  83815. +
  83816. + diepmsk.b.xfercompl = 1;
  83817. + diepmsk.b.timeout = 1;
  83818. + diepmsk.b.epdisabled = 1;
  83819. + diepmsk.b.ahberr = 1;
  83820. + diepmsk.b.intknepmis = 1;
  83821. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  83822. + diepmsk.b.intknepmis = 0;
  83823. +
  83824. +/* if (core_if->dma_desc_enable) {
  83825. + diepmsk.b.bna = 1;
  83826. + }
  83827. +*/
  83828. +/*
  83829. + if (core_if->dma_enable) {
  83830. + diepmsk.b.nak = 1;
  83831. + }
  83832. +*/
  83833. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  83834. + diepmsk.d32);
  83835. + } else {
  83836. + daintmsk.b.inep0 = 1;
  83837. + daintmsk.b.outep0 = 1;
  83838. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  83839. + daintmsk.d32);
  83840. +
  83841. + doepmsk.b.setup = 1;
  83842. + doepmsk.b.xfercompl = 1;
  83843. + doepmsk.b.ahberr = 1;
  83844. + doepmsk.b.epdisabled = 1;
  83845. +
  83846. + if ((core_if->dma_desc_enable) ||
  83847. + (core_if->dma_enable
  83848. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  83849. + doepmsk.b.stsphsercvd = 1;
  83850. + }
  83851. + if (core_if->dma_desc_enable)
  83852. + doepmsk.b.bna = 1;
  83853. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  83854. +
  83855. + diepmsk.b.xfercompl = 1;
  83856. + diepmsk.b.timeout = 1;
  83857. + diepmsk.b.epdisabled = 1;
  83858. + diepmsk.b.ahberr = 1;
  83859. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  83860. + diepmsk.b.intknepmis = 0;
  83861. +/*
  83862. + if (core_if->dma_desc_enable) {
  83863. + diepmsk.b.bna = 1;
  83864. + }
  83865. +*/
  83866. +
  83867. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  83868. + }
  83869. +
  83870. + /* Reset Device Address */
  83871. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  83872. + dcfg.b.devaddr = 0;
  83873. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  83874. +
  83875. + /* setup EP0 to receive SETUP packets */
  83876. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  83877. + ep0_out_start(core_if, pcd);
  83878. +
  83879. + /* Clear interrupt */
  83880. + gintsts.d32 = 0;
  83881. + gintsts.b.usbreset = 1;
  83882. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83883. +
  83884. + return 1;
  83885. +}
  83886. +
  83887. +/**
  83888. + * Get the device speed from the device status register and convert it
  83889. + * to USB speed constant.
  83890. + *
  83891. + * @param core_if Programming view of DWC_otg controller.
  83892. + */
  83893. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  83894. +{
  83895. + dsts_data_t dsts;
  83896. + int speed = 0;
  83897. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  83898. +
  83899. + switch (dsts.b.enumspd) {
  83900. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  83901. + speed = USB_SPEED_HIGH;
  83902. + break;
  83903. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  83904. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  83905. + speed = USB_SPEED_FULL;
  83906. + break;
  83907. +
  83908. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  83909. + speed = USB_SPEED_LOW;
  83910. + break;
  83911. + }
  83912. +
  83913. + return speed;
  83914. +}
  83915. +
  83916. +/**
  83917. + * Read the device status register and set the device speed in the
  83918. + * data structure.
  83919. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  83920. + */
  83921. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  83922. +{
  83923. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83924. + gintsts_data_t gintsts;
  83925. + gusbcfg_data_t gusbcfg;
  83926. + dwc_otg_core_global_regs_t *global_regs =
  83927. + GET_CORE_IF(pcd)->core_global_regs;
  83928. + uint8_t utmi16b, utmi8b;
  83929. + int speed;
  83930. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  83931. +
  83932. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  83933. + utmi16b = 6; //vahrama old value was 6;
  83934. + utmi8b = 9;
  83935. + } else {
  83936. + utmi16b = 4;
  83937. + utmi8b = 8;
  83938. + }
  83939. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83940. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  83941. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  83942. + }
  83943. +
  83944. +#ifdef DEBUG_EP0
  83945. + print_ep0_state(pcd);
  83946. +#endif
  83947. +
  83948. + if (pcd->ep0state == EP0_DISCONNECT) {
  83949. + pcd->ep0state = EP0_IDLE;
  83950. + } else if (pcd->ep0state == EP0_STALL) {
  83951. + pcd->ep0state = EP0_IDLE;
  83952. + }
  83953. +
  83954. + pcd->ep0state = EP0_IDLE;
  83955. +
  83956. + ep0->stopped = 0;
  83957. +
  83958. + speed = get_device_speed(GET_CORE_IF(pcd));
  83959. + pcd->fops->connect(pcd, speed);
  83960. +
  83961. + /* Set USB turnaround time based on device speed and PHY interface. */
  83962. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  83963. + if (speed == USB_SPEED_HIGH) {
  83964. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83965. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  83966. + /* ULPI interface */
  83967. + gusbcfg.b.usbtrdtim = 9;
  83968. + }
  83969. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83970. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  83971. + /* UTMI+ interface */
  83972. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  83973. + gusbcfg.b.usbtrdtim = utmi8b;
  83974. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  83975. + b.utmi_phy_data_width == 1) {
  83976. + gusbcfg.b.usbtrdtim = utmi16b;
  83977. + } else if (GET_CORE_IF(pcd)->
  83978. + core_params->phy_utmi_width == 8) {
  83979. + gusbcfg.b.usbtrdtim = utmi8b;
  83980. + } else {
  83981. + gusbcfg.b.usbtrdtim = utmi16b;
  83982. + }
  83983. + }
  83984. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83985. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  83986. + /* UTMI+ OR ULPI interface */
  83987. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  83988. + /* ULPI interface */
  83989. + gusbcfg.b.usbtrdtim = 9;
  83990. + } else {
  83991. + /* UTMI+ interface */
  83992. + if (GET_CORE_IF(pcd)->
  83993. + core_params->phy_utmi_width == 16) {
  83994. + gusbcfg.b.usbtrdtim = utmi16b;
  83995. + } else {
  83996. + gusbcfg.b.usbtrdtim = utmi8b;
  83997. + }
  83998. + }
  83999. + }
  84000. + } else {
  84001. + /* Full or low speed */
  84002. + gusbcfg.b.usbtrdtim = 9;
  84003. + }
  84004. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  84005. +
  84006. + /* Clear interrupt */
  84007. + gintsts.d32 = 0;
  84008. + gintsts.b.enumdone = 1;
  84009. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  84010. + gintsts.d32);
  84011. + return 1;
  84012. +}
  84013. +
  84014. +/**
  84015. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  84016. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  84017. + * read all the data from the Rx FIFO.
  84018. + */
  84019. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  84020. +{
  84021. + gintmsk_data_t intr_mask = {.d32 = 0 };
  84022. + gintsts_data_t gintsts;
  84023. +
  84024. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  84025. + "ISOC Out Dropped");
  84026. +
  84027. + intr_mask.b.isooutdrop = 1;
  84028. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  84029. + intr_mask.d32, 0);
  84030. +
  84031. + /* Clear interrupt */
  84032. + gintsts.d32 = 0;
  84033. + gintsts.b.isooutdrop = 1;
  84034. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  84035. + gintsts.d32);
  84036. +
  84037. + return 1;
  84038. +}
  84039. +
  84040. +/**
  84041. + * This interrupt indicates the end of the portion of the micro-frame
  84042. + * for periodic transactions. If there is a periodic transaction for
  84043. + * the next frame, load the packets into the EP periodic Tx FIFO.
  84044. + */
  84045. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  84046. +{
  84047. + gintmsk_data_t intr_mask = {.d32 = 0 };
  84048. + gintsts_data_t gintsts;
  84049. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  84050. +
  84051. + intr_mask.b.eopframe = 1;
  84052. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  84053. + intr_mask.d32, 0);
  84054. +
  84055. + /* Clear interrupt */
  84056. + gintsts.d32 = 0;
  84057. + gintsts.b.eopframe = 1;
  84058. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  84059. + gintsts.d32);
  84060. +
  84061. + return 1;
  84062. +}
  84063. +
  84064. +/**
  84065. + * This interrupt indicates that EP of the packet on the top of the
  84066. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  84067. + *
  84068. + * The "Device IN Token Queue" Registers are read to determine the
  84069. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  84070. + * is flushed, so it can be reloaded in the order seen in the IN Token
  84071. + * Queue.
  84072. + */
  84073. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  84074. +{
  84075. + gintsts_data_t gintsts;
  84076. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84077. + dctl_data_t dctl;
  84078. + gintmsk_data_t intr_mask = {.d32 = 0 };
  84079. +
  84080. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  84081. + core_if->start_predict = 1;
  84082. +
  84083. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  84084. +
  84085. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  84086. + if (!gintsts.b.ginnakeff) {
  84087. + /* Disable EP Mismatch interrupt */
  84088. + intr_mask.d32 = 0;
  84089. + intr_mask.b.epmismatch = 1;
  84090. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  84091. + /* Enable the Global IN NAK Effective Interrupt */
  84092. + intr_mask.d32 = 0;
  84093. + intr_mask.b.ginnakeff = 1;
  84094. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  84095. + /* Set the global non-periodic IN NAK handshake */
  84096. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  84097. + dctl.b.sgnpinnak = 1;
  84098. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  84099. + } else {
  84100. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  84101. + }
  84102. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  84103. + * handler after Global IN NAK Effective interrupt will be asserted */
  84104. + }
  84105. + /* Clear interrupt */
  84106. + gintsts.d32 = 0;
  84107. + gintsts.b.epmismatch = 1;
  84108. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  84109. +
  84110. + return 1;
  84111. +}
  84112. +
  84113. +/**
  84114. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  84115. + * core has stopped fetching data for IN endpoints due to the unavailability of
  84116. + * TxFIFO space or Request Queue space. This interrupt is used by the
  84117. + * application for an endpoint mismatch algorithm.
  84118. + *
  84119. + * @param pcd The PCD
  84120. + */
  84121. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  84122. +{
  84123. + gintsts_data_t gintsts;
  84124. + gintmsk_data_t gintmsk_data;
  84125. + dctl_data_t dctl;
  84126. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84127. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  84128. +
  84129. + /* Clear the global non-periodic IN NAK handshake */
  84130. + dctl.d32 = 0;
  84131. + dctl.b.cgnpinnak = 1;
  84132. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  84133. +
  84134. + /* Mask GINTSTS.FETSUSP interrupt */
  84135. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  84136. + gintmsk_data.b.fetsusp = 0;
  84137. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  84138. +
  84139. + /* Clear interrupt */
  84140. + gintsts.d32 = 0;
  84141. + gintsts.b.fetsusp = 1;
  84142. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  84143. +
  84144. + return 1;
  84145. +}
  84146. +/**
  84147. + * This funcion stalls EP0.
  84148. + */
  84149. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  84150. +{
  84151. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84152. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  84153. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  84154. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  84155. +
  84156. + ep0->dwc_ep.is_in = 1;
  84157. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84158. + pcd->ep0.stopped = 1;
  84159. + pcd->ep0state = EP0_IDLE;
  84160. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  84161. +}
  84162. +
  84163. +/**
  84164. + * This functions delegates the setup command to the gadget driver.
  84165. + */
  84166. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  84167. + usb_device_request_t * ctrl)
  84168. +{
  84169. + int ret = 0;
  84170. + DWC_SPINUNLOCK(pcd->lock);
  84171. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  84172. + DWC_SPINLOCK(pcd->lock);
  84173. + if (ret < 0) {
  84174. + ep0_do_stall(pcd, ret);
  84175. + }
  84176. +
  84177. + /** @todo This is a g_file_storage gadget driver specific
  84178. + * workaround: a DELAYED_STATUS result from the fsg_setup
  84179. + * routine will result in the gadget queueing a EP0 IN status
  84180. + * phase for a two-stage control transfer. Exactly the same as
  84181. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  84182. + * specific request. Need a generic way to know when the gadget
  84183. + * driver will queue the status phase. Can we assume when we
  84184. + * call the gadget driver setup() function that it will always
  84185. + * queue and require the following flag? Need to look into
  84186. + * this.
  84187. + */
  84188. +
  84189. + if (ret == 256 + 999) {
  84190. + pcd->request_config = 1;
  84191. + }
  84192. +}
  84193. +
  84194. +#ifdef DWC_UTE_CFI
  84195. +/**
  84196. + * This functions delegates the CFI setup commands to the gadget driver.
  84197. + * This function will return a negative value to indicate a failure.
  84198. + */
  84199. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  84200. + struct cfi_usb_ctrlrequest *ctrl_req)
  84201. +{
  84202. + int ret = 0;
  84203. +
  84204. + if (pcd->fops && pcd->fops->cfi_setup) {
  84205. + DWC_SPINUNLOCK(pcd->lock);
  84206. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  84207. + DWC_SPINLOCK(pcd->lock);
  84208. + if (ret < 0) {
  84209. + ep0_do_stall(pcd, ret);
  84210. + return ret;
  84211. + }
  84212. + }
  84213. +
  84214. + return ret;
  84215. +}
  84216. +#endif
  84217. +
  84218. +/**
  84219. + * This function starts the Zero-Length Packet for the IN status phase
  84220. + * of a 2 stage control transfer.
  84221. + */
  84222. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  84223. +{
  84224. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84225. + if (pcd->ep0state == EP0_STALL) {
  84226. + return;
  84227. + }
  84228. +
  84229. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  84230. +
  84231. + /* Prepare for more SETUP Packets */
  84232. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  84233. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  84234. + && (pcd->core_if->dma_desc_enable)
  84235. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  84236. + DWC_DEBUGPL(DBG_PCDV,
  84237. + "Data terminated wait next packet in out_desc_addr\n");
  84238. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  84239. + pcd->data_terminated = 1;
  84240. + }
  84241. + ep0->dwc_ep.xfer_len = 0;
  84242. + ep0->dwc_ep.xfer_count = 0;
  84243. + ep0->dwc_ep.is_in = 1;
  84244. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  84245. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84246. +
  84247. + /* Prepare for more SETUP Packets */
  84248. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  84249. +}
  84250. +
  84251. +/**
  84252. + * This function starts the Zero-Length Packet for the OUT status phase
  84253. + * of a 2 stage control transfer.
  84254. + */
  84255. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  84256. +{
  84257. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84258. + if (pcd->ep0state == EP0_STALL) {
  84259. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  84260. + return;
  84261. + }
  84262. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  84263. +
  84264. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  84265. + ep0->dwc_ep.xfer_len = 0;
  84266. + ep0->dwc_ep.xfer_count = 0;
  84267. + ep0->dwc_ep.is_in = 0;
  84268. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  84269. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84270. +
  84271. + /* Prepare for more SETUP Packets */
  84272. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  84273. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  84274. + }
  84275. +}
  84276. +
  84277. +/**
  84278. + * Clear the EP halt (STALL) and if pending requests start the
  84279. + * transfer.
  84280. + */
  84281. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  84282. +{
  84283. + if (ep->dwc_ep.stall_clear_flag == 0)
  84284. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  84285. +
  84286. + /* Reactive the EP */
  84287. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  84288. + if (ep->stopped) {
  84289. + ep->stopped = 0;
  84290. + /* If there is a request in the EP queue start it */
  84291. +
  84292. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  84293. + * epmismatch not yet implemented. */
  84294. +
  84295. + /*
  84296. + * Above fixme is solved by implmenting a tasklet to call the
  84297. + * start_next_request(), outside of interrupt context at some
  84298. + * time after the current time, after a clear-halt setup packet.
  84299. + * Still need to implement ep mismatch in the future if a gadget
  84300. + * ever uses more than one endpoint at once
  84301. + */
  84302. + ep->queue_sof = 1;
  84303. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  84304. + }
  84305. + /* Start Control Status Phase */
  84306. + do_setup_in_status_phase(pcd);
  84307. +}
  84308. +
  84309. +/**
  84310. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  84311. + * is sent from the host. The Device Control register is written with
  84312. + * the Test Mode bits set to the specified Test Mode. This is done as
  84313. + * a tasklet so that the "Status" phase of the control transfer
  84314. + * completes before transmitting the TEST packets.
  84315. + *
  84316. + * @todo This has not been tested since the tasklet struct was put
  84317. + * into the PCD struct!
  84318. + *
  84319. + */
  84320. +void do_test_mode(void *data)
  84321. +{
  84322. + dctl_data_t dctl;
  84323. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  84324. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84325. + int test_mode = pcd->test_mode;
  84326. +
  84327. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  84328. +
  84329. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  84330. + switch (test_mode) {
  84331. + case 1: // TEST_J
  84332. + dctl.b.tstctl = 1;
  84333. + break;
  84334. +
  84335. + case 2: // TEST_K
  84336. + dctl.b.tstctl = 2;
  84337. + break;
  84338. +
  84339. + case 3: // TEST_SE0_NAK
  84340. + dctl.b.tstctl = 3;
  84341. + break;
  84342. +
  84343. + case 4: // TEST_PACKET
  84344. + dctl.b.tstctl = 4;
  84345. + break;
  84346. +
  84347. + case 5: // TEST_FORCE_ENABLE
  84348. + dctl.b.tstctl = 5;
  84349. + break;
  84350. + }
  84351. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  84352. +}
  84353. +
  84354. +/**
  84355. + * This function process the GET_STATUS Setup Commands.
  84356. + */
  84357. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  84358. +{
  84359. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84360. + dwc_otg_pcd_ep_t *ep;
  84361. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84362. + uint16_t *status = pcd->status_buf;
  84363. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84364. +
  84365. +#ifdef DEBUG_EP0
  84366. + DWC_DEBUGPL(DBG_PCD,
  84367. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  84368. + ctrl.bmRequestType, ctrl.bRequest,
  84369. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84370. + UGETW(ctrl.wLength));
  84371. +#endif
  84372. +
  84373. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  84374. + case UT_DEVICE:
  84375. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  84376. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  84377. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  84378. + DWC_PRINTF("OTG CAP - %d, %d\n",
  84379. + core_if->core_params->otg_cap,
  84380. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  84381. + if (core_if->otg_ver == 1
  84382. + && core_if->core_params->otg_cap ==
  84383. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84384. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  84385. + *otgsts = (core_if->otg_sts & 0x1);
  84386. + pcd->ep0_pending = 1;
  84387. + ep0->dwc_ep.start_xfer_buff =
  84388. + (uint8_t *) otgsts;
  84389. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  84390. + ep0->dwc_ep.dma_addr =
  84391. + pcd->status_buf_dma_handle;
  84392. + ep0->dwc_ep.xfer_len = 1;
  84393. + ep0->dwc_ep.xfer_count = 0;
  84394. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  84395. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  84396. + &ep0->dwc_ep);
  84397. + return;
  84398. + } else {
  84399. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84400. + return;
  84401. + }
  84402. + break;
  84403. + } else {
  84404. + *status = 0x1; /* Self powered */
  84405. + *status |= pcd->remote_wakeup_enable << 1;
  84406. + break;
  84407. + }
  84408. + case UT_INTERFACE:
  84409. + *status = 0;
  84410. + break;
  84411. +
  84412. + case UT_ENDPOINT:
  84413. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  84414. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  84415. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84416. + return;
  84417. + }
  84418. + /** @todo check for EP stall */
  84419. + *status = ep->stopped;
  84420. + break;
  84421. + }
  84422. + pcd->ep0_pending = 1;
  84423. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  84424. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  84425. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  84426. + ep0->dwc_ep.xfer_len = 2;
  84427. + ep0->dwc_ep.xfer_count = 0;
  84428. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  84429. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  84430. +}
  84431. +
  84432. +/**
  84433. + * This function process the SET_FEATURE Setup Commands.
  84434. + */
  84435. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  84436. +{
  84437. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84438. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  84439. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84440. + dwc_otg_pcd_ep_t *ep = 0;
  84441. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  84442. + gotgctl_data_t gotgctl = {.d32 = 0 };
  84443. +
  84444. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  84445. + ctrl.bmRequestType, ctrl.bRequest,
  84446. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84447. + UGETW(ctrl.wLength));
  84448. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  84449. +
  84450. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  84451. + case UT_DEVICE:
  84452. + switch (UGETW(ctrl.wValue)) {
  84453. + case UF_DEVICE_REMOTE_WAKEUP:
  84454. + pcd->remote_wakeup_enable = 1;
  84455. + break;
  84456. +
  84457. + case UF_TEST_MODE:
  84458. + /* Setup the Test Mode tasklet to do the Test
  84459. + * Packet generation after the SETUP Status
  84460. + * phase has completed. */
  84461. +
  84462. + /** @todo This has not been tested since the
  84463. + * tasklet struct was put into the PCD
  84464. + * struct! */
  84465. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  84466. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  84467. + break;
  84468. +
  84469. + case UF_DEVICE_B_HNP_ENABLE:
  84470. + DWC_DEBUGPL(DBG_PCDV,
  84471. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  84472. +
  84473. + /* dev may initiate HNP */
  84474. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84475. + pcd->b_hnp_enable = 1;
  84476. + dwc_otg_pcd_update_otg(pcd, 0);
  84477. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  84478. + /**@todo Is the gotgctl.devhnpen cleared
  84479. + * by a USB Reset? */
  84480. + gotgctl.b.devhnpen = 1;
  84481. + gotgctl.b.hnpreq = 1;
  84482. + DWC_WRITE_REG32(&global_regs->gotgctl,
  84483. + gotgctl.d32);
  84484. + } else {
  84485. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84486. + return;
  84487. + }
  84488. + break;
  84489. +
  84490. + case UF_DEVICE_A_HNP_SUPPORT:
  84491. + /* RH port supports HNP */
  84492. + DWC_DEBUGPL(DBG_PCDV,
  84493. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  84494. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84495. + pcd->a_hnp_support = 1;
  84496. + dwc_otg_pcd_update_otg(pcd, 0);
  84497. + } else {
  84498. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84499. + return;
  84500. + }
  84501. + break;
  84502. +
  84503. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  84504. + /* other RH port does */
  84505. + DWC_DEBUGPL(DBG_PCDV,
  84506. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  84507. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  84508. + pcd->a_alt_hnp_support = 1;
  84509. + dwc_otg_pcd_update_otg(pcd, 0);
  84510. + } else {
  84511. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84512. + return;
  84513. + }
  84514. + break;
  84515. +
  84516. + default:
  84517. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84518. + return;
  84519. +
  84520. + }
  84521. + do_setup_in_status_phase(pcd);
  84522. + break;
  84523. +
  84524. + case UT_INTERFACE:
  84525. + do_gadget_setup(pcd, &ctrl);
  84526. + break;
  84527. +
  84528. + case UT_ENDPOINT:
  84529. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  84530. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  84531. + if (ep == 0) {
  84532. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84533. + return;
  84534. + }
  84535. + ep->stopped = 1;
  84536. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  84537. + }
  84538. + do_setup_in_status_phase(pcd);
  84539. + break;
  84540. + }
  84541. +}
  84542. +
  84543. +/**
  84544. + * This function process the CLEAR_FEATURE Setup Commands.
  84545. + */
  84546. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  84547. +{
  84548. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84549. + dwc_otg_pcd_ep_t *ep = 0;
  84550. +
  84551. + DWC_DEBUGPL(DBG_PCD,
  84552. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  84553. + ctrl.bmRequestType, ctrl.bRequest,
  84554. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84555. + UGETW(ctrl.wLength));
  84556. +
  84557. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  84558. + case UT_DEVICE:
  84559. + switch (UGETW(ctrl.wValue)) {
  84560. + case UF_DEVICE_REMOTE_WAKEUP:
  84561. + pcd->remote_wakeup_enable = 0;
  84562. + break;
  84563. +
  84564. + case UF_TEST_MODE:
  84565. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  84566. + break;
  84567. +
  84568. + default:
  84569. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84570. + return;
  84571. + }
  84572. + do_setup_in_status_phase(pcd);
  84573. + break;
  84574. +
  84575. + case UT_ENDPOINT:
  84576. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  84577. + if (ep == 0) {
  84578. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  84579. + return;
  84580. + }
  84581. +
  84582. + pcd_clear_halt(pcd, ep);
  84583. +
  84584. + break;
  84585. + }
  84586. +}
  84587. +
  84588. +/**
  84589. + * This function process the SET_ADDRESS Setup Commands.
  84590. + */
  84591. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  84592. +{
  84593. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  84594. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84595. +
  84596. + if (ctrl.bmRequestType == UT_DEVICE) {
  84597. + dcfg_data_t dcfg = {.d32 = 0 };
  84598. +
  84599. +#ifdef DEBUG_EP0
  84600. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  84601. +#endif
  84602. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  84603. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  84604. + do_setup_in_status_phase(pcd);
  84605. + }
  84606. +}
  84607. +
  84608. +/**
  84609. + * This function processes SETUP commands. In Linux, the USB Command
  84610. + * processing is done in two places - the first being the PCD and the
  84611. + * second in the Gadget Driver (for example, the File-Backed Storage
  84612. + * Gadget Driver).
  84613. + *
  84614. + * <table>
  84615. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  84616. + *
  84617. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  84618. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  84619. + * </td></tr>
  84620. + *
  84621. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  84622. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  84623. + * interface requests are ignored.</td></tr>
  84624. + *
  84625. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  84626. + * requests are processed by the PCD. Interface requests are passed
  84627. + * to the Gadget Driver.</td></tr>
  84628. + *
  84629. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  84630. + * with device address received </td></tr>
  84631. + *
  84632. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  84633. + * requested descriptor</td></tr>
  84634. + *
  84635. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  84636. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  84637. + *
  84638. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  84639. + * all EPs and enable EPs for new configuration.</td></tr>
  84640. + *
  84641. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  84642. + * the current configuration</td></tr>
  84643. + *
  84644. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  84645. + * EPs and enable EPs for new configuration.</td></tr>
  84646. + *
  84647. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  84648. + * current interface.</td></tr>
  84649. + *
  84650. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  84651. + * message.</td></tr>
  84652. + * </table>
  84653. + *
  84654. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  84655. + * processed by pcd_setup. Calling the Function Driver's setup function from
  84656. + * pcd_setup processes the gadget SETUP commands.
  84657. + */
  84658. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  84659. +{
  84660. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  84661. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84662. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84663. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84664. +
  84665. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  84666. +
  84667. +#ifdef DWC_UTE_CFI
  84668. + int retval = 0;
  84669. + struct cfi_usb_ctrlrequest cfi_req;
  84670. +#endif
  84671. +
  84672. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  84673. +
  84674. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  84675. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  84676. + && (doeptsize0.b.supcnt < 2)
  84677. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  84678. + DWC_ERROR
  84679. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  84680. + }
  84681. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  84682. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  84683. + ctrl =
  84684. + (pcd->setup_pkt +
  84685. + (3 - doeptsize0.b.supcnt - 1 +
  84686. + ep0->dwc_ep.stp_rollover))->req;
  84687. + }
  84688. +#ifdef DEBUG_EP0
  84689. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  84690. + ctrl.bmRequestType, ctrl.bRequest,
  84691. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84692. + UGETW(ctrl.wLength));
  84693. +#endif
  84694. +
  84695. + /* Clean up the request queue */
  84696. + dwc_otg_request_nuke(ep0);
  84697. + ep0->stopped = 0;
  84698. +
  84699. + if (ctrl.bmRequestType & UE_DIR_IN) {
  84700. + ep0->dwc_ep.is_in = 1;
  84701. + pcd->ep0state = EP0_IN_DATA_PHASE;
  84702. + } else {
  84703. + ep0->dwc_ep.is_in = 0;
  84704. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  84705. + }
  84706. +
  84707. + if (UGETW(ctrl.wLength) == 0) {
  84708. + ep0->dwc_ep.is_in = 1;
  84709. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  84710. + }
  84711. +
  84712. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  84713. +
  84714. +#ifdef DWC_UTE_CFI
  84715. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  84716. +
  84717. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  84718. + ctrl.bRequestType, ctrl.bRequest);
  84719. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  84720. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  84721. + retval = cfi_setup(pcd, &cfi_req);
  84722. + if (retval < 0) {
  84723. + ep0_do_stall(pcd, retval);
  84724. + pcd->ep0_pending = 0;
  84725. + return;
  84726. + }
  84727. +
  84728. + /* if need gadget setup then call it and check the retval */
  84729. + if (pcd->cfi->need_gadget_att) {
  84730. + retval =
  84731. + cfi_gadget_setup(pcd,
  84732. + &pcd->
  84733. + cfi->ctrl_req);
  84734. + if (retval < 0) {
  84735. + pcd->ep0_pending = 0;
  84736. + return;
  84737. + }
  84738. + }
  84739. +
  84740. + if (pcd->cfi->need_status_in_complete) {
  84741. + do_setup_in_status_phase(pcd);
  84742. + }
  84743. + return;
  84744. + }
  84745. + }
  84746. +#endif
  84747. +
  84748. + /* handle non-standard (class/vendor) requests in the gadget driver */
  84749. + do_gadget_setup(pcd, &ctrl);
  84750. + return;
  84751. + }
  84752. +
  84753. + /** @todo NGS: Handle bad setup packet? */
  84754. +
  84755. +///////////////////////////////////////////
  84756. +//// --- Standard Request handling --- ////
  84757. +
  84758. + switch (ctrl.bRequest) {
  84759. + case UR_GET_STATUS:
  84760. + do_get_status(pcd);
  84761. + break;
  84762. +
  84763. + case UR_CLEAR_FEATURE:
  84764. + do_clear_feature(pcd);
  84765. + break;
  84766. +
  84767. + case UR_SET_FEATURE:
  84768. + do_set_feature(pcd);
  84769. + break;
  84770. +
  84771. + case UR_SET_ADDRESS:
  84772. + do_set_address(pcd);
  84773. + break;
  84774. +
  84775. + case UR_SET_INTERFACE:
  84776. + case UR_SET_CONFIG:
  84777. +// _pcd->request_config = 1; /* Configuration changed */
  84778. + do_gadget_setup(pcd, &ctrl);
  84779. + break;
  84780. +
  84781. + case UR_SYNCH_FRAME:
  84782. + do_gadget_setup(pcd, &ctrl);
  84783. + break;
  84784. +
  84785. + default:
  84786. + /* Call the Gadget Driver's setup functions */
  84787. + do_gadget_setup(pcd, &ctrl);
  84788. + break;
  84789. + }
  84790. +}
  84791. +
  84792. +/**
  84793. + * This function completes the ep0 control transfer.
  84794. + */
  84795. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  84796. +{
  84797. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  84798. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84799. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  84800. + dev_if->in_ep_regs[ep->dwc_ep.num];
  84801. +#ifdef DEBUG_EP0
  84802. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  84803. + dev_if->out_ep_regs[ep->dwc_ep.num];
  84804. +#endif
  84805. + deptsiz0_data_t deptsiz;
  84806. + dev_dma_desc_sts_t desc_sts;
  84807. + dwc_otg_pcd_request_t *req;
  84808. + int is_last = 0;
  84809. + dwc_otg_pcd_t *pcd = ep->pcd;
  84810. +
  84811. +#ifdef DWC_UTE_CFI
  84812. + struct cfi_usb_ctrlrequest *ctrlreq;
  84813. + int retval = -DWC_E_NOT_SUPPORTED;
  84814. +#endif
  84815. +
  84816. + desc_sts.b.bytes = 0;
  84817. +
  84818. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84819. + if (ep->dwc_ep.is_in) {
  84820. +#ifdef DEBUG_EP0
  84821. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  84822. +#endif
  84823. + do_setup_out_status_phase(pcd);
  84824. + } else {
  84825. +#ifdef DEBUG_EP0
  84826. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  84827. +#endif
  84828. +
  84829. +#ifdef DWC_UTE_CFI
  84830. + ctrlreq = &pcd->cfi->ctrl_req;
  84831. +
  84832. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  84833. + if (ctrlreq->bRequest > 0xB0
  84834. + && ctrlreq->bRequest < 0xBF) {
  84835. +
  84836. + /* Return if the PCD failed to handle the request */
  84837. + if ((retval =
  84838. + pcd->cfi->ops.
  84839. + ctrl_write_complete(pcd->cfi,
  84840. + pcd)) < 0) {
  84841. + CFI_INFO
  84842. + ("ERROR setting a new value in the PCD(%d)\n",
  84843. + retval);
  84844. + ep0_do_stall(pcd, retval);
  84845. + pcd->ep0_pending = 0;
  84846. + return 0;
  84847. + }
  84848. +
  84849. + /* If the gadget needs to be notified on the request */
  84850. + if (pcd->cfi->need_gadget_att == 1) {
  84851. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  84852. + retval =
  84853. + cfi_gadget_setup(pcd,
  84854. + &pcd->cfi->
  84855. + ctrl_req);
  84856. +
  84857. + /* Return from the function if the gadget failed to process
  84858. + * the request properly - this should never happen !!!
  84859. + */
  84860. + if (retval < 0) {
  84861. + CFI_INFO
  84862. + ("ERROR setting a new value in the gadget(%d)\n",
  84863. + retval);
  84864. + pcd->ep0_pending = 0;
  84865. + return 0;
  84866. + }
  84867. + }
  84868. +
  84869. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  84870. + retval);
  84871. + /* If we hit here then the PCD and the gadget has properly
  84872. + * handled the request - so send the ZLP IN to the host.
  84873. + */
  84874. + /* @todo: MAS - decide whether we need to start the setup
  84875. + * stage based on the need_setup value of the cfi object
  84876. + */
  84877. + do_setup_in_status_phase(pcd);
  84878. + pcd->ep0_pending = 0;
  84879. + return 1;
  84880. + }
  84881. + }
  84882. +#endif
  84883. +
  84884. + do_setup_in_status_phase(pcd);
  84885. + }
  84886. + pcd->ep0_pending = 0;
  84887. + return 1;
  84888. + }
  84889. +
  84890. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84891. + return 0;
  84892. + }
  84893. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  84894. +
  84895. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  84896. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  84897. + is_last = 1;
  84898. + } else if (ep->dwc_ep.is_in) {
  84899. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  84900. + if (core_if->dma_desc_enable != 0)
  84901. + desc_sts = dev_if->in_desc_addr->status;
  84902. +#ifdef DEBUG_EP0
  84903. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  84904. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  84905. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84906. +#endif
  84907. +
  84908. + if (((core_if->dma_desc_enable == 0)
  84909. + && (deptsiz.b.xfersize == 0))
  84910. + || ((core_if->dma_desc_enable != 0)
  84911. + && (desc_sts.b.bytes == 0))) {
  84912. + req->actual = ep->dwc_ep.xfer_count;
  84913. + /* Is a Zero Len Packet needed? */
  84914. + if (req->sent_zlp) {
  84915. +#ifdef DEBUG_EP0
  84916. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  84917. +#endif
  84918. + req->sent_zlp = 0;
  84919. + }
  84920. + do_setup_out_status_phase(pcd);
  84921. + }
  84922. + } else {
  84923. + /* ep0-OUT */
  84924. +#ifdef DEBUG_EP0
  84925. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  84926. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  84927. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  84928. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84929. +#endif
  84930. + req->actual = ep->dwc_ep.xfer_count;
  84931. +
  84932. + /* Is a Zero Len Packet needed? */
  84933. + if (req->sent_zlp) {
  84934. +#ifdef DEBUG_EP0
  84935. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  84936. +#endif
  84937. + req->sent_zlp = 0;
  84938. + }
  84939. + /* For older cores do setup in status phase in Slave/BDMA modes,
  84940. + * starting from 3.00 do that only in slave, and for DMA modes
  84941. + * just re-enable ep 0 OUT here*/
  84942. + if (core_if->dma_enable == 0
  84943. + || (core_if->dma_desc_enable == 0
  84944. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  84945. + do_setup_in_status_phase(pcd);
  84946. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  84947. + DWC_DEBUGPL(DBG_PCDV,
  84948. + "Enable out ep before in status phase\n");
  84949. + ep0_out_start(core_if, pcd);
  84950. + }
  84951. + }
  84952. +
  84953. + /* Complete the request */
  84954. + if (is_last) {
  84955. + dwc_otg_request_done(ep, req, 0);
  84956. + ep->dwc_ep.start_xfer_buff = 0;
  84957. + ep->dwc_ep.xfer_buff = 0;
  84958. + ep->dwc_ep.xfer_len = 0;
  84959. + return 1;
  84960. + }
  84961. + return 0;
  84962. +}
  84963. +
  84964. +#ifdef DWC_UTE_CFI
  84965. +/**
  84966. + * This function calculates traverses all the CFI DMA descriptors and
  84967. + * and accumulates the bytes that are left to be transfered.
  84968. + *
  84969. + * @return The total bytes left to transfered, or a negative value as failure
  84970. + */
  84971. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  84972. +{
  84973. + int32_t ret = 0;
  84974. + int i;
  84975. + struct dwc_otg_dma_desc *ddesc = NULL;
  84976. + struct cfi_ep *cfiep;
  84977. +
  84978. + /* See if the pcd_ep has its respective cfi_ep mapped */
  84979. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  84980. + if (!cfiep) {
  84981. + CFI_INFO("%s: Failed to find ep\n", __func__);
  84982. + return -1;
  84983. + }
  84984. +
  84985. + ddesc = ep->dwc_ep.descs;
  84986. +
  84987. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  84988. +
  84989. +#if defined(PRINT_CFI_DMA_DESCS)
  84990. + print_desc(ddesc, ep->ep.name, i);
  84991. +#endif
  84992. + ret += ddesc->status.b.bytes;
  84993. + ddesc++;
  84994. + }
  84995. +
  84996. + if (ret)
  84997. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  84998. + ret);
  84999. +
  85000. + return ret;
  85001. +}
  85002. +#endif
  85003. +
  85004. +/**
  85005. + * This function completes the request for the EP. If there are
  85006. + * additional requests for the EP in the queue they will be started.
  85007. + */
  85008. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  85009. +{
  85010. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  85011. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  85012. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  85013. + dev_if->in_ep_regs[ep->dwc_ep.num];
  85014. + deptsiz_data_t deptsiz;
  85015. + dev_dma_desc_sts_t desc_sts;
  85016. + dwc_otg_pcd_request_t *req = 0;
  85017. + dwc_otg_dev_dma_desc_t *dma_desc;
  85018. + uint32_t byte_count = 0;
  85019. + int is_last = 0;
  85020. + int i;
  85021. +
  85022. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  85023. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  85024. +
  85025. + /* Get any pending requests */
  85026. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  85027. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  85028. + if (!req) {
  85029. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  85030. + return;
  85031. + }
  85032. + } else {
  85033. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  85034. + return;
  85035. + }
  85036. +
  85037. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  85038. +
  85039. + if (ep->dwc_ep.is_in) {
  85040. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  85041. +
  85042. + if (core_if->dma_enable) {
  85043. + if (core_if->dma_desc_enable == 0) {
  85044. + if (deptsiz.b.xfersize == 0
  85045. + && deptsiz.b.pktcnt == 0) {
  85046. + byte_count =
  85047. + ep->dwc_ep.xfer_len -
  85048. + ep->dwc_ep.xfer_count;
  85049. +
  85050. + ep->dwc_ep.xfer_buff += byte_count;
  85051. + ep->dwc_ep.dma_addr += byte_count;
  85052. + ep->dwc_ep.xfer_count += byte_count;
  85053. +
  85054. + DWC_DEBUGPL(DBG_PCDV,
  85055. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  85056. + ep->dwc_ep.num,
  85057. + (ep->dwc_ep.
  85058. + is_in ? "IN" : "OUT"),
  85059. + ep->dwc_ep.xfer_len,
  85060. + deptsiz.b.xfersize,
  85061. + deptsiz.b.pktcnt);
  85062. +
  85063. + if (ep->dwc_ep.xfer_len <
  85064. + ep->dwc_ep.total_len) {
  85065. + dwc_otg_ep_start_transfer
  85066. + (core_if, &ep->dwc_ep);
  85067. + } else if (ep->dwc_ep.sent_zlp) {
  85068. + /*
  85069. + * This fragment of code should initiate 0
  85070. + * length transfer in case if it is queued
  85071. + * a transfer with size divisible to EPs max
  85072. + * packet size and with usb_request zero field
  85073. + * is set, which means that after data is transfered,
  85074. + * it is also should be transfered
  85075. + * a 0 length packet at the end. For Slave and
  85076. + * Buffer DMA modes in this case SW has
  85077. + * to initiate 2 transfers one with transfer size,
  85078. + * and the second with 0 size. For Descriptor
  85079. + * DMA mode SW is able to initiate a transfer,
  85080. + * which will handle all the packets including
  85081. + * the last 0 length.
  85082. + */
  85083. + ep->dwc_ep.sent_zlp = 0;
  85084. + dwc_otg_ep_start_zl_transfer
  85085. + (core_if, &ep->dwc_ep);
  85086. + } else {
  85087. + is_last = 1;
  85088. + }
  85089. + } else {
  85090. + if (ep->dwc_ep.type ==
  85091. + DWC_OTG_EP_TYPE_ISOC) {
  85092. + req->actual = 0;
  85093. + dwc_otg_request_done(ep, req, 0);
  85094. +
  85095. + ep->dwc_ep.start_xfer_buff = 0;
  85096. + ep->dwc_ep.xfer_buff = 0;
  85097. + ep->dwc_ep.xfer_len = 0;
  85098. +
  85099. + /* If there is a request in the queue start it. */
  85100. + start_next_request(ep);
  85101. + } else
  85102. + DWC_WARN
  85103. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  85104. + ep->dwc_ep.num,
  85105. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  85106. + deptsiz.b.xfersize,
  85107. + deptsiz.b.pktcnt);
  85108. + }
  85109. + } else {
  85110. + dma_desc = ep->dwc_ep.desc_addr;
  85111. + byte_count = 0;
  85112. + ep->dwc_ep.sent_zlp = 0;
  85113. +
  85114. +#ifdef DWC_UTE_CFI
  85115. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  85116. + ep->dwc_ep.buff_mode);
  85117. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  85118. + int residue;
  85119. +
  85120. + residue = cfi_calc_desc_residue(ep);
  85121. + if (residue < 0)
  85122. + return;
  85123. +
  85124. + byte_count = residue;
  85125. + } else {
  85126. +#endif
  85127. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  85128. + ++i) {
  85129. + desc_sts = dma_desc->status;
  85130. + byte_count += desc_sts.b.bytes;
  85131. + dma_desc++;
  85132. + }
  85133. +#ifdef DWC_UTE_CFI
  85134. + }
  85135. +#endif
  85136. + if (byte_count == 0) {
  85137. + ep->dwc_ep.xfer_count =
  85138. + ep->dwc_ep.total_len;
  85139. + is_last = 1;
  85140. + } else {
  85141. + DWC_WARN("Incomplete transfer\n");
  85142. + }
  85143. + }
  85144. + } else {
  85145. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  85146. + DWC_DEBUGPL(DBG_PCDV,
  85147. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  85148. + ep->dwc_ep.num,
  85149. + ep->dwc_ep.is_in ? "IN" : "OUT",
  85150. + ep->dwc_ep.xfer_len,
  85151. + deptsiz.b.xfersize,
  85152. + deptsiz.b.pktcnt);
  85153. +
  85154. + /* Check if the whole transfer was completed,
  85155. + * if no, setup transfer for next portion of data
  85156. + */
  85157. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  85158. + dwc_otg_ep_start_transfer(core_if,
  85159. + &ep->dwc_ep);
  85160. + } else if (ep->dwc_ep.sent_zlp) {
  85161. + /*
  85162. + * This fragment of code should initiate 0
  85163. + * length trasfer in case if it is queued
  85164. + * a trasfer with size divisible to EPs max
  85165. + * packet size and with usb_request zero field
  85166. + * is set, which means that after data is transfered,
  85167. + * it is also should be transfered
  85168. + * a 0 length packet at the end. For Slave and
  85169. + * Buffer DMA modes in this case SW has
  85170. + * to initiate 2 transfers one with transfer size,
  85171. + * and the second with 0 size. For Desriptor
  85172. + * DMA mode SW is able to initiate a transfer,
  85173. + * which will handle all the packets including
  85174. + * the last 0 legth.
  85175. + */
  85176. + ep->dwc_ep.sent_zlp = 0;
  85177. + dwc_otg_ep_start_zl_transfer(core_if,
  85178. + &ep->dwc_ep);
  85179. + } else {
  85180. + is_last = 1;
  85181. + }
  85182. + } else {
  85183. + DWC_WARN
  85184. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  85185. + ep->dwc_ep.num,
  85186. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  85187. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  85188. + }
  85189. + }
  85190. + } else {
  85191. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  85192. + dev_if->out_ep_regs[ep->dwc_ep.num];
  85193. + desc_sts.d32 = 0;
  85194. + if (core_if->dma_enable) {
  85195. + if (core_if->dma_desc_enable) {
  85196. + dma_desc = ep->dwc_ep.desc_addr;
  85197. + byte_count = 0;
  85198. + ep->dwc_ep.sent_zlp = 0;
  85199. +
  85200. +#ifdef DWC_UTE_CFI
  85201. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  85202. + ep->dwc_ep.buff_mode);
  85203. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  85204. + int residue;
  85205. + residue = cfi_calc_desc_residue(ep);
  85206. + if (residue < 0)
  85207. + return;
  85208. + byte_count = residue;
  85209. + } else {
  85210. +#endif
  85211. +
  85212. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  85213. + ++i) {
  85214. + desc_sts = dma_desc->status;
  85215. + byte_count += desc_sts.b.bytes;
  85216. + dma_desc++;
  85217. + }
  85218. +
  85219. +#ifdef DWC_UTE_CFI
  85220. + }
  85221. +#endif
  85222. + /* Checking for interrupt Out transfers with not
  85223. + * dword aligned mps sizes
  85224. + */
  85225. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  85226. + (ep->dwc_ep.maxpacket%4)) {
  85227. + ep->dwc_ep.xfer_count =
  85228. + ep->dwc_ep.total_len - byte_count;
  85229. + if ((ep->dwc_ep.xfer_len %
  85230. + ep->dwc_ep.maxpacket)
  85231. + && (ep->dwc_ep.xfer_len /
  85232. + ep->dwc_ep.maxpacket <
  85233. + MAX_DMA_DESC_CNT))
  85234. + ep->dwc_ep.xfer_len -=
  85235. + (ep->dwc_ep.desc_cnt -
  85236. + 1) * ep->dwc_ep.maxpacket +
  85237. + ep->dwc_ep.xfer_len %
  85238. + ep->dwc_ep.maxpacket;
  85239. + else
  85240. + ep->dwc_ep.xfer_len -=
  85241. + ep->dwc_ep.desc_cnt *
  85242. + ep->dwc_ep.maxpacket;
  85243. + if (ep->dwc_ep.xfer_len > 0) {
  85244. + dwc_otg_ep_start_transfer
  85245. + (core_if, &ep->dwc_ep);
  85246. + } else {
  85247. + is_last = 1;
  85248. + }
  85249. + } else {
  85250. + ep->dwc_ep.xfer_count =
  85251. + ep->dwc_ep.total_len - byte_count +
  85252. + ((4 -
  85253. + (ep->dwc_ep.
  85254. + total_len & 0x3)) & 0x3);
  85255. + is_last = 1;
  85256. + }
  85257. + } else {
  85258. + deptsiz.d32 = 0;
  85259. + deptsiz.d32 =
  85260. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  85261. +
  85262. + byte_count = (ep->dwc_ep.xfer_len -
  85263. + ep->dwc_ep.xfer_count -
  85264. + deptsiz.b.xfersize);
  85265. + ep->dwc_ep.xfer_buff += byte_count;
  85266. + ep->dwc_ep.dma_addr += byte_count;
  85267. + ep->dwc_ep.xfer_count += byte_count;
  85268. +
  85269. + /* Check if the whole transfer was completed,
  85270. + * if no, setup transfer for next portion of data
  85271. + */
  85272. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  85273. + dwc_otg_ep_start_transfer(core_if,
  85274. + &ep->dwc_ep);
  85275. + } else if (ep->dwc_ep.sent_zlp) {
  85276. + /*
  85277. + * This fragment of code should initiate 0
  85278. + * length trasfer in case if it is queued
  85279. + * a trasfer with size divisible to EPs max
  85280. + * packet size and with usb_request zero field
  85281. + * is set, which means that after data is transfered,
  85282. + * it is also should be transfered
  85283. + * a 0 length packet at the end. For Slave and
  85284. + * Buffer DMA modes in this case SW has
  85285. + * to initiate 2 transfers one with transfer size,
  85286. + * and the second with 0 size. For Desriptor
  85287. + * DMA mode SW is able to initiate a transfer,
  85288. + * which will handle all the packets including
  85289. + * the last 0 legth.
  85290. + */
  85291. + ep->dwc_ep.sent_zlp = 0;
  85292. + dwc_otg_ep_start_zl_transfer(core_if,
  85293. + &ep->dwc_ep);
  85294. + } else {
  85295. + is_last = 1;
  85296. + }
  85297. + }
  85298. + } else {
  85299. + /* Check if the whole transfer was completed,
  85300. + * if no, setup transfer for next portion of data
  85301. + */
  85302. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  85303. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  85304. + } else if (ep->dwc_ep.sent_zlp) {
  85305. + /*
  85306. + * This fragment of code should initiate 0
  85307. + * length transfer in case if it is queued
  85308. + * a transfer with size divisible to EPs max
  85309. + * packet size and with usb_request zero field
  85310. + * is set, which means that after data is transfered,
  85311. + * it is also should be transfered
  85312. + * a 0 length packet at the end. For Slave and
  85313. + * Buffer DMA modes in this case SW has
  85314. + * to initiate 2 transfers one with transfer size,
  85315. + * and the second with 0 size. For Descriptor
  85316. + * DMA mode SW is able to initiate a transfer,
  85317. + * which will handle all the packets including
  85318. + * the last 0 length.
  85319. + */
  85320. + ep->dwc_ep.sent_zlp = 0;
  85321. + dwc_otg_ep_start_zl_transfer(core_if,
  85322. + &ep->dwc_ep);
  85323. + } else {
  85324. + is_last = 1;
  85325. + }
  85326. + }
  85327. +
  85328. + DWC_DEBUGPL(DBG_PCDV,
  85329. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  85330. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  85331. + ep->dwc_ep.is_in ? "IN" : "OUT",
  85332. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  85333. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  85334. + }
  85335. +
  85336. + /* Complete the request */
  85337. + if (is_last) {
  85338. +#ifdef DWC_UTE_CFI
  85339. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  85340. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  85341. + } else {
  85342. +#endif
  85343. + req->actual = ep->dwc_ep.xfer_count;
  85344. +#ifdef DWC_UTE_CFI
  85345. + }
  85346. +#endif
  85347. + if (req->dw_align_buf) {
  85348. + if (!ep->dwc_ep.is_in) {
  85349. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  85350. + }
  85351. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  85352. + req->dw_align_buf_dma);
  85353. + }
  85354. +
  85355. + dwc_otg_request_done(ep, req, 0);
  85356. +
  85357. + ep->dwc_ep.start_xfer_buff = 0;
  85358. + ep->dwc_ep.xfer_buff = 0;
  85359. + ep->dwc_ep.xfer_len = 0;
  85360. +
  85361. + /* If there is a request in the queue start it. */
  85362. + start_next_request(ep);
  85363. + }
  85364. +}
  85365. +
  85366. +#ifdef DWC_EN_ISOC
  85367. +
  85368. +/**
  85369. + * This function BNA interrupt for Isochronous EPs
  85370. + *
  85371. + */
  85372. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  85373. +{
  85374. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85375. + volatile uint32_t *addr;
  85376. + depctl_data_t depctl = {.d32 = 0 };
  85377. + dwc_otg_pcd_t *pcd = ep->pcd;
  85378. + dwc_otg_dev_dma_desc_t *dma_desc;
  85379. + int i;
  85380. +
  85381. + dma_desc =
  85382. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  85383. +
  85384. + if (dwc_ep->is_in) {
  85385. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85386. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  85387. + sts.d32 = dma_desc->status.d32;
  85388. + sts.b_iso_in.bs = BS_HOST_READY;
  85389. + dma_desc->status.d32 = sts.d32;
  85390. + }
  85391. + } else {
  85392. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85393. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  85394. + sts.d32 = dma_desc->status.d32;
  85395. + sts.b_iso_out.bs = BS_HOST_READY;
  85396. + dma_desc->status.d32 = sts.d32;
  85397. + }
  85398. + }
  85399. +
  85400. + if (dwc_ep->is_in == 0) {
  85401. + addr =
  85402. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  85403. + num]->doepctl;
  85404. + } else {
  85405. + addr =
  85406. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  85407. + }
  85408. + depctl.b.epena = 1;
  85409. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  85410. +}
  85411. +
  85412. +/**
  85413. + * This function sets latest iso packet information(non-PTI mode)
  85414. + *
  85415. + * @param core_if Programming view of DWC_otg controller.
  85416. + * @param ep The EP to start the transfer on.
  85417. + *
  85418. + */
  85419. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  85420. +{
  85421. + deptsiz_data_t deptsiz = {.d32 = 0 };
  85422. + dma_addr_t dma_addr;
  85423. + uint32_t offset;
  85424. +
  85425. + if (ep->proc_buf_num)
  85426. + dma_addr = ep->dma_addr1;
  85427. + else
  85428. + dma_addr = ep->dma_addr0;
  85429. +
  85430. + if (ep->is_in) {
  85431. + deptsiz.d32 =
  85432. + DWC_READ_REG32(&core_if->dev_if->
  85433. + in_ep_regs[ep->num]->dieptsiz);
  85434. + offset = ep->data_per_frame;
  85435. + } else {
  85436. + deptsiz.d32 =
  85437. + DWC_READ_REG32(&core_if->dev_if->
  85438. + out_ep_regs[ep->num]->doeptsiz);
  85439. + offset =
  85440. + ep->data_per_frame +
  85441. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  85442. + }
  85443. +
  85444. + if (!deptsiz.b.xfersize) {
  85445. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  85446. + ep->pkt_info[ep->cur_pkt].offset =
  85447. + ep->cur_pkt_dma_addr - dma_addr;
  85448. + ep->pkt_info[ep->cur_pkt].status = 0;
  85449. + } else {
  85450. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  85451. + ep->pkt_info[ep->cur_pkt].offset =
  85452. + ep->cur_pkt_dma_addr - dma_addr;
  85453. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  85454. + }
  85455. + ep->cur_pkt_addr += offset;
  85456. + ep->cur_pkt_dma_addr += offset;
  85457. + ep->cur_pkt++;
  85458. +}
  85459. +
  85460. +/**
  85461. + * This function sets latest iso packet information(DDMA mode)
  85462. + *
  85463. + * @param core_if Programming view of DWC_otg controller.
  85464. + * @param dwc_ep The EP to start the transfer on.
  85465. + *
  85466. + */
  85467. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  85468. + dwc_ep_t * dwc_ep)
  85469. +{
  85470. + dwc_otg_dev_dma_desc_t *dma_desc;
  85471. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85472. + iso_pkt_info_t *iso_packet;
  85473. + uint32_t data_per_desc;
  85474. + uint32_t offset;
  85475. + int i, j;
  85476. +
  85477. + iso_packet = dwc_ep->pkt_info;
  85478. +
  85479. + /** Reinit closed DMA Descriptors*/
  85480. + /** ISO OUT EP */
  85481. + if (dwc_ep->is_in == 0) {
  85482. + dma_desc =
  85483. + dwc_ep->iso_desc_addr +
  85484. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85485. + offset = 0;
  85486. +
  85487. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  85488. + i += dwc_ep->pkt_per_frm) {
  85489. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  85490. + data_per_desc =
  85491. + ((j + 1) * dwc_ep->maxpacket >
  85492. + dwc_ep->
  85493. + data_per_frame) ? dwc_ep->data_per_frame -
  85494. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85495. + data_per_desc +=
  85496. + (data_per_desc % 4) ? (4 -
  85497. + data_per_desc %
  85498. + 4) : 0;
  85499. +
  85500. + sts.d32 = dma_desc->status.d32;
  85501. +
  85502. + /* Write status in iso_packet_decsriptor */
  85503. + iso_packet->status =
  85504. + sts.b_iso_out.rxsts +
  85505. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  85506. + if (iso_packet->status) {
  85507. + iso_packet->status = -DWC_E_NO_DATA;
  85508. + }
  85509. +
  85510. + /* Received data length */
  85511. + if (!sts.b_iso_out.rxbytes) {
  85512. + iso_packet->length =
  85513. + data_per_desc -
  85514. + sts.b_iso_out.rxbytes;
  85515. + } else {
  85516. + iso_packet->length =
  85517. + data_per_desc -
  85518. + sts.b_iso_out.rxbytes + (4 -
  85519. + dwc_ep->data_per_frame
  85520. + % 4);
  85521. + }
  85522. +
  85523. + iso_packet->offset = offset;
  85524. +
  85525. + offset += data_per_desc;
  85526. + dma_desc++;
  85527. + iso_packet++;
  85528. + }
  85529. + }
  85530. +
  85531. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  85532. + data_per_desc =
  85533. + ((j + 1) * dwc_ep->maxpacket >
  85534. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85535. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85536. + data_per_desc +=
  85537. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85538. +
  85539. + sts.d32 = dma_desc->status.d32;
  85540. +
  85541. + /* Write status in iso_packet_decsriptor */
  85542. + iso_packet->status =
  85543. + sts.b_iso_out.rxsts +
  85544. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  85545. + if (iso_packet->status) {
  85546. + iso_packet->status = -DWC_E_NO_DATA;
  85547. + }
  85548. +
  85549. + /* Received data length */
  85550. + iso_packet->length =
  85551. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  85552. +
  85553. + iso_packet->offset = offset;
  85554. +
  85555. + offset += data_per_desc;
  85556. + iso_packet++;
  85557. + dma_desc++;
  85558. + }
  85559. +
  85560. + sts.d32 = dma_desc->status.d32;
  85561. +
  85562. + /* Write status in iso_packet_decsriptor */
  85563. + iso_packet->status =
  85564. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  85565. + if (iso_packet->status) {
  85566. + iso_packet->status = -DWC_E_NO_DATA;
  85567. + }
  85568. + /* Received data length */
  85569. + if (!sts.b_iso_out.rxbytes) {
  85570. + iso_packet->length =
  85571. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  85572. + } else {
  85573. + iso_packet->length =
  85574. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  85575. + (4 - dwc_ep->data_per_frame % 4);
  85576. + }
  85577. +
  85578. + iso_packet->offset = offset;
  85579. + } else {
  85580. +/** ISO IN EP */
  85581. +
  85582. + dma_desc =
  85583. + dwc_ep->iso_desc_addr +
  85584. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85585. +
  85586. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  85587. + sts.d32 = dma_desc->status.d32;
  85588. +
  85589. + /* Write status in iso packet descriptor */
  85590. + iso_packet->status =
  85591. + sts.b_iso_in.txsts +
  85592. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  85593. + if (iso_packet->status != 0) {
  85594. + iso_packet->status = -DWC_E_NO_DATA;
  85595. +
  85596. + }
  85597. + /* Bytes has been transfered */
  85598. + iso_packet->length =
  85599. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  85600. +
  85601. + dma_desc++;
  85602. + iso_packet++;
  85603. + }
  85604. +
  85605. + sts.d32 = dma_desc->status.d32;
  85606. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  85607. + sts.d32 = dma_desc->status.d32;
  85608. + }
  85609. +
  85610. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  85611. + iso_packet->status =
  85612. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  85613. + if (iso_packet->status != 0) {
  85614. + iso_packet->status = -DWC_E_NO_DATA;
  85615. + }
  85616. +
  85617. + /* Bytes has been transfered */
  85618. + iso_packet->length =
  85619. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  85620. + }
  85621. +}
  85622. +
  85623. +/**
  85624. + * This function reinitialize DMA Descriptors for Isochronous transfer
  85625. + *
  85626. + * @param core_if Programming view of DWC_otg controller.
  85627. + * @param dwc_ep The EP to start the transfer on.
  85628. + *
  85629. + */
  85630. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  85631. +{
  85632. + int i, j;
  85633. + dwc_otg_dev_dma_desc_t *dma_desc;
  85634. + dma_addr_t dma_ad;
  85635. + volatile uint32_t *addr;
  85636. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85637. + uint32_t data_per_desc;
  85638. +
  85639. + if (dwc_ep->is_in == 0) {
  85640. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  85641. + } else {
  85642. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  85643. + }
  85644. +
  85645. + if (dwc_ep->proc_buf_num == 0) {
  85646. + /** Buffer 0 descriptors setup */
  85647. + dma_ad = dwc_ep->dma_addr0;
  85648. + } else {
  85649. + /** Buffer 1 descriptors setup */
  85650. + dma_ad = dwc_ep->dma_addr1;
  85651. + }
  85652. +
  85653. + /** Reinit closed DMA Descriptors*/
  85654. + /** ISO OUT EP */
  85655. + if (dwc_ep->is_in == 0) {
  85656. + dma_desc =
  85657. + dwc_ep->iso_desc_addr +
  85658. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85659. +
  85660. + sts.b_iso_out.bs = BS_HOST_READY;
  85661. + sts.b_iso_out.rxsts = 0;
  85662. + sts.b_iso_out.l = 0;
  85663. + sts.b_iso_out.sp = 0;
  85664. + sts.b_iso_out.ioc = 0;
  85665. + sts.b_iso_out.pid = 0;
  85666. + sts.b_iso_out.framenum = 0;
  85667. +
  85668. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  85669. + i += dwc_ep->pkt_per_frm) {
  85670. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  85671. + data_per_desc =
  85672. + ((j + 1) * dwc_ep->maxpacket >
  85673. + dwc_ep->
  85674. + data_per_frame) ? dwc_ep->data_per_frame -
  85675. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85676. + data_per_desc +=
  85677. + (data_per_desc % 4) ? (4 -
  85678. + data_per_desc %
  85679. + 4) : 0;
  85680. + sts.b_iso_out.rxbytes = data_per_desc;
  85681. + dma_desc->buf = dma_ad;
  85682. + dma_desc->status.d32 = sts.d32;
  85683. +
  85684. + dma_ad += data_per_desc;
  85685. + dma_desc++;
  85686. + }
  85687. + }
  85688. +
  85689. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  85690. +
  85691. + data_per_desc =
  85692. + ((j + 1) * dwc_ep->maxpacket >
  85693. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85694. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85695. + data_per_desc +=
  85696. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85697. + sts.b_iso_out.rxbytes = data_per_desc;
  85698. +
  85699. + dma_desc->buf = dma_ad;
  85700. + dma_desc->status.d32 = sts.d32;
  85701. +
  85702. + dma_desc++;
  85703. + dma_ad += data_per_desc;
  85704. + }
  85705. +
  85706. + sts.b_iso_out.ioc = 1;
  85707. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  85708. +
  85709. + data_per_desc =
  85710. + ((j + 1) * dwc_ep->maxpacket >
  85711. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85712. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85713. + data_per_desc +=
  85714. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85715. + sts.b_iso_out.rxbytes = data_per_desc;
  85716. +
  85717. + dma_desc->buf = dma_ad;
  85718. + dma_desc->status.d32 = sts.d32;
  85719. + } else {
  85720. +/** ISO IN EP */
  85721. +
  85722. + dma_desc =
  85723. + dwc_ep->iso_desc_addr +
  85724. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85725. +
  85726. + sts.b_iso_in.bs = BS_HOST_READY;
  85727. + sts.b_iso_in.txsts = 0;
  85728. + sts.b_iso_in.sp = 0;
  85729. + sts.b_iso_in.ioc = 0;
  85730. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  85731. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  85732. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  85733. + sts.b_iso_in.l = 0;
  85734. +
  85735. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  85736. + dma_desc->buf = dma_ad;
  85737. + dma_desc->status.d32 = sts.d32;
  85738. +
  85739. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  85740. + dma_ad += dwc_ep->data_per_frame;
  85741. + dma_desc++;
  85742. + }
  85743. +
  85744. + sts.b_iso_in.ioc = 1;
  85745. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  85746. +
  85747. + dma_desc->buf = dma_ad;
  85748. + dma_desc->status.d32 = sts.d32;
  85749. +
  85750. + dwc_ep->next_frame =
  85751. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  85752. + }
  85753. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85754. +}
  85755. +
  85756. +/**
  85757. + * This function is to handle Iso EP transfer complete interrupt
  85758. + * in case Iso out packet was dropped
  85759. + *
  85760. + * @param core_if Programming view of DWC_otg controller.
  85761. + * @param dwc_ep The EP for wihich transfer complete was asserted
  85762. + *
  85763. + */
  85764. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  85765. + dwc_ep_t * dwc_ep)
  85766. +{
  85767. + uint32_t dma_addr;
  85768. + uint32_t drp_pkt;
  85769. + uint32_t drp_pkt_cnt;
  85770. + deptsiz_data_t deptsiz = {.d32 = 0 };
  85771. + depctl_data_t depctl = {.d32 = 0 };
  85772. + int i;
  85773. +
  85774. + deptsiz.d32 =
  85775. + DWC_READ_REG32(&core_if->dev_if->
  85776. + out_ep_regs[dwc_ep->num]->doeptsiz);
  85777. +
  85778. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  85779. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  85780. +
  85781. + /* Setting dropped packets status */
  85782. + for (i = 0; i < drp_pkt_cnt; ++i) {
  85783. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  85784. + drp_pkt++;
  85785. + deptsiz.b.pktcnt--;
  85786. + }
  85787. +
  85788. + if (deptsiz.b.pktcnt > 0) {
  85789. + deptsiz.b.xfersize =
  85790. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  85791. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  85792. + } else {
  85793. + deptsiz.b.xfersize = 0;
  85794. + deptsiz.b.pktcnt = 0;
  85795. + }
  85796. +
  85797. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  85798. + deptsiz.d32);
  85799. +
  85800. + if (deptsiz.b.pktcnt > 0) {
  85801. + if (dwc_ep->proc_buf_num) {
  85802. + dma_addr =
  85803. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  85804. + deptsiz.b.xfersize;
  85805. + } else {
  85806. + dma_addr =
  85807. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  85808. + deptsiz.b.xfersize;;
  85809. + }
  85810. +
  85811. + DWC_WRITE_REG32(&core_if->dev_if->
  85812. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  85813. +
  85814. + /** Re-enable endpoint, clear nak */
  85815. + depctl.d32 = 0;
  85816. + depctl.b.epena = 1;
  85817. + depctl.b.cnak = 1;
  85818. +
  85819. + DWC_MODIFY_REG32(&core_if->dev_if->
  85820. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  85821. + depctl.d32);
  85822. + return 0;
  85823. + } else {
  85824. + return 1;
  85825. + }
  85826. +}
  85827. +
  85828. +/**
  85829. + * This function sets iso packets information(PTI mode)
  85830. + *
  85831. + * @param core_if Programming view of DWC_otg controller.
  85832. + * @param ep The EP to start the transfer on.
  85833. + *
  85834. + */
  85835. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  85836. +{
  85837. + int i, j;
  85838. + dma_addr_t dma_ad;
  85839. + iso_pkt_info_t *packet_info = ep->pkt_info;
  85840. + uint32_t offset;
  85841. + uint32_t frame_data;
  85842. + deptsiz_data_t deptsiz;
  85843. +
  85844. + if (ep->proc_buf_num == 0) {
  85845. + /** Buffer 0 descriptors setup */
  85846. + dma_ad = ep->dma_addr0;
  85847. + } else {
  85848. + /** Buffer 1 descriptors setup */
  85849. + dma_ad = ep->dma_addr1;
  85850. + }
  85851. +
  85852. + if (ep->is_in) {
  85853. + deptsiz.d32 =
  85854. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  85855. + dieptsiz);
  85856. + } else {
  85857. + deptsiz.d32 =
  85858. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  85859. + doeptsiz);
  85860. + }
  85861. +
  85862. + if (!deptsiz.b.xfersize) {
  85863. + offset = 0;
  85864. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  85865. + frame_data = ep->data_per_frame;
  85866. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  85867. +
  85868. + /* Packet status - is not set as initially
  85869. + * it is set to 0 and if packet was sent
  85870. + successfully, status field will remain 0*/
  85871. +
  85872. + /* Bytes has been transfered */
  85873. + packet_info->length =
  85874. + (ep->maxpacket <
  85875. + frame_data) ? ep->maxpacket : frame_data;
  85876. +
  85877. + /* Received packet offset */
  85878. + packet_info->offset = offset;
  85879. + offset += packet_info->length;
  85880. + frame_data -= packet_info->length;
  85881. +
  85882. + packet_info++;
  85883. + }
  85884. + }
  85885. + return 1;
  85886. + } else {
  85887. + /* This is a workaround for in case of Transfer Complete with
  85888. + * PktDrpSts interrupts merging - in this case Transfer complete
  85889. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  85890. + * set and with DOEPTSIZ register non zero. Investigations showed,
  85891. + * that this happens when Out packet is dropped, but because of
  85892. + * interrupts merging during first interrupt handling PktDrpSts
  85893. + * bit is cleared and for next merged interrupts it is not reset.
  85894. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  85895. + */
  85896. + if (ep->is_in) {
  85897. + return 1;
  85898. + } else {
  85899. + return handle_iso_out_pkt_dropped(core_if, ep);
  85900. + }
  85901. + }
  85902. +}
  85903. +
  85904. +/**
  85905. + * This function is to handle Iso EP transfer complete interrupt
  85906. + *
  85907. + * @param pcd The PCD
  85908. + * @param ep The EP for which transfer complete was asserted
  85909. + *
  85910. + */
  85911. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  85912. +{
  85913. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  85914. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85915. + uint8_t is_last = 0;
  85916. +
  85917. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  85918. + DWC_WARN("Next frame is not set!\n");
  85919. + return;
  85920. + }
  85921. +
  85922. + if (core_if->dma_enable) {
  85923. + if (core_if->dma_desc_enable) {
  85924. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  85925. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  85926. + is_last = 1;
  85927. + } else {
  85928. + if (core_if->pti_enh_enable) {
  85929. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  85930. + dwc_ep->proc_buf_num =
  85931. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85932. + dwc_otg_iso_ep_start_buf_transfer
  85933. + (core_if, dwc_ep);
  85934. + is_last = 1;
  85935. + }
  85936. + } else {
  85937. + set_current_pkt_info(core_if, dwc_ep);
  85938. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  85939. + is_last = 1;
  85940. + dwc_ep->cur_pkt = 0;
  85941. + dwc_ep->proc_buf_num =
  85942. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85943. + if (dwc_ep->proc_buf_num) {
  85944. + dwc_ep->cur_pkt_addr =
  85945. + dwc_ep->xfer_buff1;
  85946. + dwc_ep->cur_pkt_dma_addr =
  85947. + dwc_ep->dma_addr1;
  85948. + } else {
  85949. + dwc_ep->cur_pkt_addr =
  85950. + dwc_ep->xfer_buff0;
  85951. + dwc_ep->cur_pkt_dma_addr =
  85952. + dwc_ep->dma_addr0;
  85953. + }
  85954. +
  85955. + }
  85956. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  85957. + dwc_ep);
  85958. + }
  85959. + }
  85960. + } else {
  85961. + set_current_pkt_info(core_if, dwc_ep);
  85962. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  85963. + is_last = 1;
  85964. + dwc_ep->cur_pkt = 0;
  85965. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85966. + if (dwc_ep->proc_buf_num) {
  85967. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  85968. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  85969. + } else {
  85970. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  85971. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  85972. + }
  85973. +
  85974. + }
  85975. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  85976. + }
  85977. + if (is_last)
  85978. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  85979. +}
  85980. +#endif /* DWC_EN_ISOC */
  85981. +
  85982. +/**
  85983. + * This function handle BNA interrupt for Non Isochronous EPs
  85984. + *
  85985. + */
  85986. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  85987. +{
  85988. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85989. + volatile uint32_t *addr;
  85990. + depctl_data_t depctl = {.d32 = 0 };
  85991. + dwc_otg_pcd_t *pcd = ep->pcd;
  85992. + dwc_otg_dev_dma_desc_t *dma_desc;
  85993. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85994. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  85995. + int i, start;
  85996. +
  85997. + if (!dwc_ep->desc_cnt)
  85998. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  85999. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  86000. +
  86001. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  86002. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  86003. + uint32_t doepdma;
  86004. + dwc_otg_dev_out_ep_regs_t *out_regs =
  86005. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  86006. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  86007. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  86008. + dma_desc = &(dwc_ep->desc_addr[start]);
  86009. + } else {
  86010. + start = 0;
  86011. + dma_desc = dwc_ep->desc_addr;
  86012. + }
  86013. +
  86014. +
  86015. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  86016. + sts.d32 = dma_desc->status.d32;
  86017. + sts.b.bs = BS_HOST_READY;
  86018. + dma_desc->status.d32 = sts.d32;
  86019. + }
  86020. +
  86021. + if (dwc_ep->is_in == 0) {
  86022. + addr =
  86023. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  86024. + doepctl;
  86025. + } else {
  86026. + addr =
  86027. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  86028. + }
  86029. + depctl.b.epena = 1;
  86030. + depctl.b.cnak = 1;
  86031. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  86032. +}
  86033. +
  86034. +/**
  86035. + * This function handles EP0 Control transfers.
  86036. + *
  86037. + * The state of the control transfers are tracked in
  86038. + * <code>ep0state</code>.
  86039. + */
  86040. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  86041. +{
  86042. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86043. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  86044. + dev_dma_desc_sts_t desc_sts;
  86045. + deptsiz0_data_t deptsiz;
  86046. + uint32_t byte_count;
  86047. +
  86048. +#ifdef DEBUG_EP0
  86049. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  86050. + print_ep0_state(pcd);
  86051. +#endif
  86052. +
  86053. +// DWC_PRINTF("HANDLE EP0\n");
  86054. +
  86055. + switch (pcd->ep0state) {
  86056. + case EP0_DISCONNECT:
  86057. + break;
  86058. +
  86059. + case EP0_IDLE:
  86060. + pcd->request_config = 0;
  86061. +
  86062. + pcd_setup(pcd);
  86063. + break;
  86064. +
  86065. + case EP0_IN_DATA_PHASE:
  86066. +#ifdef DEBUG_EP0
  86067. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  86068. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  86069. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  86070. +#endif
  86071. +
  86072. + if (core_if->dma_enable != 0) {
  86073. + /*
  86074. + * For EP0 we can only program 1 packet at a time so we
  86075. + * need to do the make calculations after each complete.
  86076. + * Call write_packet to make the calculations, as in
  86077. + * slave mode, and use those values to determine if we
  86078. + * can complete.
  86079. + */
  86080. + if (core_if->dma_desc_enable == 0) {
  86081. + deptsiz.d32 =
  86082. + DWC_READ_REG32(&core_if->
  86083. + dev_if->in_ep_regs[0]->
  86084. + dieptsiz);
  86085. + byte_count =
  86086. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  86087. + } else {
  86088. + desc_sts =
  86089. + core_if->dev_if->in_desc_addr->status;
  86090. + byte_count =
  86091. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  86092. + }
  86093. + ep0->dwc_ep.xfer_count += byte_count;
  86094. + ep0->dwc_ep.xfer_buff += byte_count;
  86095. + ep0->dwc_ep.dma_addr += byte_count;
  86096. + }
  86097. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  86098. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86099. + &ep0->dwc_ep);
  86100. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  86101. + } else if (ep0->dwc_ep.sent_zlp) {
  86102. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86103. + &ep0->dwc_ep);
  86104. + ep0->dwc_ep.sent_zlp = 0;
  86105. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  86106. + } else {
  86107. + ep0_complete_request(ep0);
  86108. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  86109. + }
  86110. + break;
  86111. + case EP0_OUT_DATA_PHASE:
  86112. +#ifdef DEBUG_EP0
  86113. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  86114. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  86115. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  86116. +#endif
  86117. + if (core_if->dma_enable != 0) {
  86118. + if (core_if->dma_desc_enable == 0) {
  86119. + deptsiz.d32 =
  86120. + DWC_READ_REG32(&core_if->
  86121. + dev_if->out_ep_regs[0]->
  86122. + doeptsiz);
  86123. + byte_count =
  86124. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  86125. + } else {
  86126. + desc_sts =
  86127. + core_if->dev_if->out_desc_addr->status;
  86128. + byte_count =
  86129. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  86130. + }
  86131. + ep0->dwc_ep.xfer_count += byte_count;
  86132. + ep0->dwc_ep.xfer_buff += byte_count;
  86133. + ep0->dwc_ep.dma_addr += byte_count;
  86134. + }
  86135. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  86136. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86137. + &ep0->dwc_ep);
  86138. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  86139. + } else if (ep0->dwc_ep.sent_zlp) {
  86140. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  86141. + &ep0->dwc_ep);
  86142. + ep0->dwc_ep.sent_zlp = 0;
  86143. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  86144. + } else {
  86145. + ep0_complete_request(ep0);
  86146. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  86147. + }
  86148. + break;
  86149. +
  86150. + case EP0_IN_STATUS_PHASE:
  86151. + case EP0_OUT_STATUS_PHASE:
  86152. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  86153. + ep0_complete_request(ep0);
  86154. + pcd->ep0state = EP0_IDLE;
  86155. + ep0->stopped = 1;
  86156. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  86157. +
  86158. + /* Prepare for more SETUP Packets */
  86159. + if (core_if->dma_enable) {
  86160. + ep0_out_start(core_if, pcd);
  86161. + }
  86162. + break;
  86163. +
  86164. + case EP0_STALL:
  86165. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  86166. + break;
  86167. + }
  86168. +#ifdef DEBUG_EP0
  86169. + print_ep0_state(pcd);
  86170. +#endif
  86171. +}
  86172. +
  86173. +/**
  86174. + * Restart transfer
  86175. + */
  86176. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  86177. +{
  86178. + dwc_otg_core_if_t *core_if;
  86179. + dwc_otg_dev_if_t *dev_if;
  86180. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  86181. + dwc_otg_pcd_ep_t *ep;
  86182. +
  86183. + ep = get_in_ep(pcd, epnum);
  86184. +
  86185. +#ifdef DWC_EN_ISOC
  86186. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  86187. + return;
  86188. + }
  86189. +#endif /* DWC_EN_ISOC */
  86190. +
  86191. + core_if = GET_CORE_IF(pcd);
  86192. + dev_if = core_if->dev_if;
  86193. +
  86194. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  86195. +
  86196. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  86197. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  86198. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  86199. + /*
  86200. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  86201. + */
  86202. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  86203. + ep->dwc_ep.start_xfer_buff != 0) {
  86204. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  86205. + ep->dwc_ep.xfer_count = 0;
  86206. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  86207. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  86208. + } else {
  86209. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  86210. + /* convert packet size to dwords. */
  86211. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  86212. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  86213. + }
  86214. + ep->stopped = 0;
  86215. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  86216. + "xfer_len=%0x stopped=%d\n",
  86217. + ep->dwc_ep.xfer_buff,
  86218. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  86219. + ep->stopped);
  86220. + if (epnum == 0) {
  86221. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  86222. + } else {
  86223. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  86224. + }
  86225. + }
  86226. +}
  86227. +
  86228. +/*
  86229. + * This function create new nextep sequnce based on Learn Queue.
  86230. + *
  86231. + * @param core_if Programming view of DWC_otg controller
  86232. + */
  86233. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  86234. +{
  86235. + dwc_otg_device_global_regs_t *dev_global_regs =
  86236. + core_if->dev_if->dev_global_regs;
  86237. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  86238. + /* Number of Token Queue Registers */
  86239. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  86240. + dtknq1_data_t dtknqr1;
  86241. + uint32_t in_tkn_epnums[4];
  86242. + uint8_t seqnum[MAX_EPS_CHANNELS];
  86243. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  86244. + grstctl_t resetctl = {.d32 = 0 };
  86245. + uint8_t temp;
  86246. + int ndx = 0;
  86247. + int start = 0;
  86248. + int end = 0;
  86249. + int sort_done = 0;
  86250. + int i = 0;
  86251. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  86252. +
  86253. +
  86254. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  86255. +
  86256. + /* Read the DTKNQ Registers */
  86257. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  86258. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  86259. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  86260. + in_tkn_epnums[i]);
  86261. + if (addr == &dev_global_regs->dvbusdis) {
  86262. + addr = &dev_global_regs->dtknqr3_dthrctl;
  86263. + } else {
  86264. + ++addr;
  86265. + }
  86266. +
  86267. + }
  86268. +
  86269. + /* Copy the DTKNQR1 data to the bit field. */
  86270. + dtknqr1.d32 = in_tkn_epnums[0];
  86271. + if (dtknqr1.b.wrap_bit) {
  86272. + ndx = dtknqr1.b.intknwptr;
  86273. + end = ndx -1;
  86274. + if (end < 0)
  86275. + end = TOKEN_Q_DEPTH -1;
  86276. + } else {
  86277. + ndx = 0;
  86278. + end = dtknqr1.b.intknwptr -1;
  86279. + if (end < 0)
  86280. + end = 0;
  86281. + }
  86282. + start = ndx;
  86283. +
  86284. + /* Fill seqnum[] by initial values: EP number + 31 */
  86285. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  86286. + seqnum[i] = i +31;
  86287. + }
  86288. +
  86289. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  86290. + for (i=0; i < 6; i++)
  86291. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  86292. +
  86293. + if (TOKEN_Q_DEPTH > 6) {
  86294. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  86295. + for (i=6; i < 14; i++)
  86296. + intkn_seq[i] =
  86297. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  86298. + }
  86299. +
  86300. + if (TOKEN_Q_DEPTH > 14) {
  86301. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  86302. + for (i=14; i < 22; i++)
  86303. + intkn_seq[i] =
  86304. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  86305. + }
  86306. +
  86307. + if (TOKEN_Q_DEPTH > 22) {
  86308. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  86309. + for (i=22; i < 30; i++)
  86310. + intkn_seq[i] =
  86311. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  86312. + }
  86313. +
  86314. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  86315. + start, end);
  86316. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  86317. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  86318. +
  86319. + /* Update seqnum based on intkn_seq[] */
  86320. + i = 0;
  86321. + do {
  86322. + seqnum[intkn_seq[ndx]] = i;
  86323. + ndx++;
  86324. + i++;
  86325. + if (ndx == TOKEN_Q_DEPTH)
  86326. + ndx = 0;
  86327. + } while ( i < TOKEN_Q_DEPTH );
  86328. +
  86329. + /* Mark non active EP's in seqnum[] by 0xff */
  86330. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  86331. + if (core_if->nextep_seq[i] == 0xff )
  86332. + seqnum[i] = 0xff;
  86333. + }
  86334. +
  86335. + /* Sort seqnum[] */
  86336. + sort_done = 0;
  86337. + while (!sort_done) {
  86338. + sort_done = 1;
  86339. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  86340. + if (seqnum[i] > seqnum[i+1]) {
  86341. + temp = seqnum[i];
  86342. + seqnum[i] = seqnum[i+1];
  86343. + seqnum[i+1] = temp;
  86344. + sort_done = 0;
  86345. + }
  86346. + }
  86347. + }
  86348. +
  86349. + ndx = start + seqnum[0];
  86350. + if (ndx >= TOKEN_Q_DEPTH)
  86351. + ndx = ndx % TOKEN_Q_DEPTH;
  86352. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  86353. +
  86354. + /* Update seqnum[] by EP numbers */
  86355. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  86356. + ndx = start + i;
  86357. + if (seqnum[i] < 31) {
  86358. + ndx = start + seqnum[i];
  86359. + if (ndx >= TOKEN_Q_DEPTH)
  86360. + ndx = ndx % TOKEN_Q_DEPTH;
  86361. + seqnum[i] = intkn_seq[ndx];
  86362. + } else {
  86363. + if (seqnum[i] < 0xff) {
  86364. + seqnum[i] = seqnum[i] - 31;
  86365. + } else {
  86366. + break;
  86367. + }
  86368. + }
  86369. + }
  86370. +
  86371. + /* Update nextep_seq[] based on seqnum[] */
  86372. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  86373. + if (seqnum[i] != 0xff) {
  86374. + if (seqnum[i+1] != 0xff) {
  86375. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  86376. + } else {
  86377. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  86378. + break;
  86379. + }
  86380. + } else {
  86381. + break;
  86382. + }
  86383. + }
  86384. +
  86385. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  86386. + __func__, core_if->first_in_nextep_seq);
  86387. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  86388. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  86389. + }
  86390. +
  86391. + /* Flush the Learning Queue */
  86392. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  86393. + resetctl.b.intknqflsh = 1;
  86394. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  86395. +
  86396. +
  86397. +}
  86398. +
  86399. +/**
  86400. + * handle the IN EP disable interrupt.
  86401. + */
  86402. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  86403. + const uint32_t epnum)
  86404. +{
  86405. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86406. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86407. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  86408. + dctl_data_t dctl = {.d32 = 0 };
  86409. + dwc_otg_pcd_ep_t *ep;
  86410. + dwc_ep_t *dwc_ep;
  86411. + gintmsk_data_t gintmsk_data;
  86412. + depctl_data_t depctl;
  86413. + uint32_t diepdma;
  86414. + uint32_t remain_to_transfer = 0;
  86415. + uint8_t i;
  86416. + uint32_t xfer_size;
  86417. +
  86418. + ep = get_in_ep(pcd, epnum);
  86419. + dwc_ep = &ep->dwc_ep;
  86420. +
  86421. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86422. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  86423. + complete_ep(ep);
  86424. + return;
  86425. + }
  86426. +
  86427. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  86428. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  86429. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  86430. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86431. +
  86432. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  86433. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  86434. +
  86435. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  86436. + if (ep->stopped) {
  86437. + if (core_if->en_multiple_tx_fifo)
  86438. + /* Flush the Tx FIFO */
  86439. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  86440. + /* Clear the Global IN NP NAK */
  86441. + dctl.d32 = 0;
  86442. + dctl.b.cgnpinnak = 1;
  86443. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  86444. + /* Restart the transaction */
  86445. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  86446. + restart_transfer(pcd, epnum);
  86447. + }
  86448. + } else {
  86449. + /* Restart the transaction */
  86450. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  86451. + restart_transfer(pcd, epnum);
  86452. + }
  86453. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  86454. + }
  86455. + return;
  86456. + }
  86457. +
  86458. + if (core_if->start_predict > 2) { // NP IN EP
  86459. + core_if->start_predict--;
  86460. + return;
  86461. + }
  86462. +
  86463. + core_if->start_predict--;
  86464. +
  86465. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  86466. +
  86467. + predict_nextep_seq(core_if);
  86468. +
  86469. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  86470. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  86471. + depctl.d32 =
  86472. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86473. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  86474. + depctl.b.nextep = core_if->nextep_seq[i];
  86475. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  86476. + }
  86477. + }
  86478. + /* Flush Shared NP TxFIFO */
  86479. + dwc_otg_flush_tx_fifo(core_if, 0);
  86480. + /* Rewind buffers */
  86481. + if (!core_if->dma_desc_enable) {
  86482. + i = core_if->first_in_nextep_seq;
  86483. + do {
  86484. + ep = get_in_ep(pcd, i);
  86485. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  86486. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  86487. + if (xfer_size > ep->dwc_ep.maxxfer)
  86488. + xfer_size = ep->dwc_ep.maxxfer;
  86489. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86490. + if (dieptsiz.b.pktcnt != 0) {
  86491. + if (xfer_size == 0) {
  86492. + remain_to_transfer = 0;
  86493. + } else {
  86494. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  86495. + remain_to_transfer =
  86496. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  86497. + } else {
  86498. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  86499. + + (xfer_size % ep->dwc_ep.maxpacket);
  86500. + }
  86501. + }
  86502. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  86503. + dieptsiz.b.xfersize = remain_to_transfer;
  86504. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  86505. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  86506. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  86507. + }
  86508. + i = core_if->nextep_seq[i];
  86509. + } while (i != core_if->first_in_nextep_seq);
  86510. + } else { // dma_desc_enable
  86511. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  86512. + }
  86513. +
  86514. + /* Restart transfers in predicted sequences */
  86515. + i = core_if->first_in_nextep_seq;
  86516. + do {
  86517. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  86518. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86519. + if (dieptsiz.b.pktcnt != 0) {
  86520. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86521. + depctl.b.epena = 1;
  86522. + depctl.b.cnak = 1;
  86523. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  86524. + }
  86525. + i = core_if->nextep_seq[i];
  86526. + } while (i != core_if->first_in_nextep_seq);
  86527. +
  86528. + /* Clear the global non-periodic IN NAK handshake */
  86529. + dctl.d32 = 0;
  86530. + dctl.b.cgnpinnak = 1;
  86531. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  86532. +
  86533. + /* Unmask EP Mismatch interrupt */
  86534. + gintmsk_data.d32 = 0;
  86535. + gintmsk_data.b.epmismatch = 1;
  86536. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  86537. +
  86538. + core_if->start_predict = 0;
  86539. +
  86540. + }
  86541. +}
  86542. +
  86543. +/**
  86544. + * Handler for the IN EP timeout handshake interrupt.
  86545. + */
  86546. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  86547. + const uint32_t epnum)
  86548. +{
  86549. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86550. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86551. +
  86552. +#ifdef DEBUG
  86553. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  86554. + uint32_t num = 0;
  86555. +#endif
  86556. + dctl_data_t dctl = {.d32 = 0 };
  86557. + dwc_otg_pcd_ep_t *ep;
  86558. +
  86559. + gintmsk_data_t intr_mask = {.d32 = 0 };
  86560. +
  86561. + ep = get_in_ep(pcd, epnum);
  86562. +
  86563. + /* Disable the NP Tx Fifo Empty Interrrupt */
  86564. + if (!core_if->dma_enable) {
  86565. + intr_mask.b.nptxfempty = 1;
  86566. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  86567. + intr_mask.d32, 0);
  86568. + }
  86569. + /** @todo NGS Check EP type.
  86570. + * Implement for Periodic EPs */
  86571. + /*
  86572. + * Non-periodic EP
  86573. + */
  86574. + /* Enable the Global IN NAK Effective Interrupt */
  86575. + intr_mask.b.ginnakeff = 1;
  86576. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  86577. +
  86578. + /* Set Global IN NAK */
  86579. + dctl.b.sgnpinnak = 1;
  86580. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  86581. +
  86582. + ep->stopped = 1;
  86583. +
  86584. +#ifdef DEBUG
  86585. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  86586. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  86587. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  86588. +#endif
  86589. +
  86590. +#ifdef DISABLE_PERIODIC_EP
  86591. + /*
  86592. + * Set the NAK bit for this EP to
  86593. + * start the disable process.
  86594. + */
  86595. + diepctl.d32 = 0;
  86596. + diepctl.b.snak = 1;
  86597. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  86598. + diepctl.d32);
  86599. + ep->disabling = 1;
  86600. + ep->stopped = 1;
  86601. +#endif
  86602. +}
  86603. +
  86604. +/**
  86605. + * Handler for the IN EP NAK interrupt.
  86606. + */
  86607. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  86608. + const uint32_t epnum)
  86609. +{
  86610. + /** @todo implement ISR */
  86611. + dwc_otg_core_if_t *core_if;
  86612. + diepmsk_data_t intr_mask = {.d32 = 0 };
  86613. +
  86614. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  86615. + core_if = GET_CORE_IF(pcd);
  86616. + intr_mask.b.nak = 1;
  86617. +
  86618. + if (core_if->multiproc_int_enable) {
  86619. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86620. + diepeachintmsk[epnum], intr_mask.d32, 0);
  86621. + } else {
  86622. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  86623. + intr_mask.d32, 0);
  86624. + }
  86625. +
  86626. + return 1;
  86627. +}
  86628. +
  86629. +/**
  86630. + * Handler for the OUT EP Babble interrupt.
  86631. + */
  86632. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  86633. + const uint32_t epnum)
  86634. +{
  86635. + /** @todo implement ISR */
  86636. + dwc_otg_core_if_t *core_if;
  86637. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86638. +
  86639. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  86640. + "OUT EP Babble");
  86641. + core_if = GET_CORE_IF(pcd);
  86642. + intr_mask.b.babble = 1;
  86643. +
  86644. + if (core_if->multiproc_int_enable) {
  86645. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86646. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86647. + } else {
  86648. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86649. + intr_mask.d32, 0);
  86650. + }
  86651. +
  86652. + return 1;
  86653. +}
  86654. +
  86655. +/**
  86656. + * Handler for the OUT EP NAK interrupt.
  86657. + */
  86658. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  86659. + const uint32_t epnum)
  86660. +{
  86661. + /** @todo implement ISR */
  86662. + dwc_otg_core_if_t *core_if;
  86663. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86664. +
  86665. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  86666. + core_if = GET_CORE_IF(pcd);
  86667. + intr_mask.b.nak = 1;
  86668. +
  86669. + if (core_if->multiproc_int_enable) {
  86670. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86671. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86672. + } else {
  86673. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86674. + intr_mask.d32, 0);
  86675. + }
  86676. +
  86677. + return 1;
  86678. +}
  86679. +
  86680. +/**
  86681. + * Handler for the OUT EP NYET interrupt.
  86682. + */
  86683. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  86684. + const uint32_t epnum)
  86685. +{
  86686. + /** @todo implement ISR */
  86687. + dwc_otg_core_if_t *core_if;
  86688. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86689. +
  86690. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  86691. + core_if = GET_CORE_IF(pcd);
  86692. + intr_mask.b.nyet = 1;
  86693. +
  86694. + if (core_if->multiproc_int_enable) {
  86695. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86696. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86697. + } else {
  86698. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86699. + intr_mask.d32, 0);
  86700. + }
  86701. +
  86702. + return 1;
  86703. +}
  86704. +
  86705. +/**
  86706. + * This interrupt indicates that an IN EP has a pending Interrupt.
  86707. + * The sequence for handling the IN EP interrupt is shown below:
  86708. + * -# Read the Device All Endpoint Interrupt register
  86709. + * -# Repeat the following for each IN EP interrupt bit set (from
  86710. + * LSB to MSB).
  86711. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  86712. + * -# If "Transfer Complete" call the request complete function
  86713. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  86714. + * -# If "AHB Error Interrupt" log error
  86715. + * -# If "Time-out Handshake" log error
  86716. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  86717. + * FIFO.
  86718. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  86719. + * Mismatch Interrupt)
  86720. + */
  86721. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  86722. +{
  86723. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  86724. +do { \
  86725. + diepint_data_t diepint = {.d32=0}; \
  86726. + diepint.b.__intr = 1; \
  86727. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  86728. + diepint.d32); \
  86729. +} while (0)
  86730. +
  86731. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86732. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86733. + diepint_data_t diepint = {.d32 = 0 };
  86734. + depctl_data_t depctl = {.d32 = 0 };
  86735. + uint32_t ep_intr;
  86736. + uint32_t epnum = 0;
  86737. + dwc_otg_pcd_ep_t *ep;
  86738. + dwc_ep_t *dwc_ep;
  86739. + gintmsk_data_t intr_mask = {.d32 = 0 };
  86740. +
  86741. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  86742. +
  86743. + /* Read in the device interrupt bits */
  86744. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  86745. +
  86746. + /* Service the Device IN interrupts for each endpoint */
  86747. + while (ep_intr) {
  86748. + if (ep_intr & 0x1) {
  86749. + uint32_t empty_msk;
  86750. + /* Get EP pointer */
  86751. + ep = get_in_ep(pcd, epnum);
  86752. + dwc_ep = &ep->dwc_ep;
  86753. +
  86754. + depctl.d32 =
  86755. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86756. + empty_msk =
  86757. + DWC_READ_REG32(&dev_if->
  86758. + dev_global_regs->dtknqr4_fifoemptymsk);
  86759. +
  86760. + DWC_DEBUGPL(DBG_PCDV,
  86761. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  86762. + epnum, empty_msk, depctl.d32);
  86763. +
  86764. + DWC_DEBUGPL(DBG_PCD,
  86765. + "EP%d-%s: type=%d, mps=%d\n",
  86766. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  86767. + dwc_ep->type, dwc_ep->maxpacket);
  86768. +
  86769. + diepint.d32 =
  86770. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  86771. +
  86772. + DWC_DEBUGPL(DBG_PCDV,
  86773. + "EP %d Interrupt Register - 0x%x\n", epnum,
  86774. + diepint.d32);
  86775. + /* Transfer complete */
  86776. + if (diepint.b.xfercompl) {
  86777. + /* Disable the NP Tx FIFO Empty
  86778. + * Interrupt */
  86779. + if (core_if->en_multiple_tx_fifo == 0) {
  86780. + intr_mask.b.nptxfempty = 1;
  86781. + DWC_MODIFY_REG32
  86782. + (&core_if->core_global_regs->gintmsk,
  86783. + intr_mask.d32, 0);
  86784. + } else {
  86785. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  86786. + uint32_t fifoemptymsk =
  86787. + 0x1 << dwc_ep->num;
  86788. + DWC_MODIFY_REG32(&core_if->
  86789. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  86790. + fifoemptymsk, 0);
  86791. + }
  86792. + /* Clear the bit in DIEPINTn for this interrupt */
  86793. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  86794. +
  86795. + /* Complete the transfer */
  86796. + if (epnum == 0) {
  86797. + handle_ep0(pcd);
  86798. + }
  86799. +#ifdef DWC_EN_ISOC
  86800. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86801. + if (!ep->stopped)
  86802. + complete_iso_ep(pcd, ep);
  86803. + }
  86804. +#endif /* DWC_EN_ISOC */
  86805. +#ifdef DWC_UTE_PER_IO
  86806. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86807. + if (!ep->stopped)
  86808. + complete_xiso_ep(ep);
  86809. + }
  86810. +#endif /* DWC_UTE_PER_IO */
  86811. + else {
  86812. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  86813. + dwc_ep->bInterval > 1) {
  86814. + dwc_ep->frame_num += dwc_ep->bInterval;
  86815. + if (dwc_ep->frame_num > 0x3FFF)
  86816. + {
  86817. + dwc_ep->frm_overrun = 1;
  86818. + dwc_ep->frame_num &= 0x3FFF;
  86819. + } else
  86820. + dwc_ep->frm_overrun = 0;
  86821. + }
  86822. + complete_ep(ep);
  86823. + if(diepint.b.nak)
  86824. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  86825. + }
  86826. + }
  86827. + /* Endpoint disable */
  86828. + if (diepint.b.epdisabled) {
  86829. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  86830. + epnum);
  86831. + handle_in_ep_disable_intr(pcd, epnum);
  86832. +
  86833. + /* Clear the bit in DIEPINTn for this interrupt */
  86834. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  86835. + }
  86836. + /* AHB Error */
  86837. + if (diepint.b.ahberr) {
  86838. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  86839. + /* Clear the bit in DIEPINTn for this interrupt */
  86840. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  86841. + }
  86842. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  86843. + if (diepint.b.timeout) {
  86844. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  86845. + handle_in_ep_timeout_intr(pcd, epnum);
  86846. +
  86847. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  86848. + }
  86849. + /** IN Token received with TxF Empty */
  86850. + if (diepint.b.intktxfemp) {
  86851. + DWC_DEBUGPL(DBG_ANY,
  86852. + "EP%d IN TKN TxFifo Empty\n",
  86853. + epnum);
  86854. + if (!ep->stopped && epnum != 0) {
  86855. +
  86856. + diepmsk_data_t diepmsk = {.d32 = 0 };
  86857. + diepmsk.b.intktxfemp = 1;
  86858. +
  86859. + if (core_if->multiproc_int_enable) {
  86860. + DWC_MODIFY_REG32
  86861. + (&dev_if->dev_global_regs->diepeachintmsk
  86862. + [epnum], diepmsk.d32, 0);
  86863. + } else {
  86864. + DWC_MODIFY_REG32
  86865. + (&dev_if->dev_global_regs->diepmsk,
  86866. + diepmsk.d32, 0);
  86867. + }
  86868. + } else if (core_if->dma_desc_enable
  86869. + && epnum == 0
  86870. + && pcd->ep0state ==
  86871. + EP0_OUT_STATUS_PHASE) {
  86872. + // EP0 IN set STALL
  86873. + depctl.d32 =
  86874. + DWC_READ_REG32(&dev_if->in_ep_regs
  86875. + [epnum]->diepctl);
  86876. +
  86877. + /* set the disable and stall bits */
  86878. + if (depctl.b.epena) {
  86879. + depctl.b.epdis = 1;
  86880. + }
  86881. + depctl.b.stall = 1;
  86882. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  86883. + [epnum]->diepctl,
  86884. + depctl.d32);
  86885. + }
  86886. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  86887. + }
  86888. + /** IN Token Received with EP mismatch */
  86889. + if (diepint.b.intknepmis) {
  86890. + DWC_DEBUGPL(DBG_ANY,
  86891. + "EP%d IN TKN EP Mismatch\n", epnum);
  86892. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  86893. + }
  86894. + /** IN Endpoint NAK Effective */
  86895. + if (diepint.b.inepnakeff) {
  86896. + DWC_DEBUGPL(DBG_ANY,
  86897. + "EP%d IN EP NAK Effective\n",
  86898. + epnum);
  86899. + /* Periodic EP */
  86900. + if (ep->disabling) {
  86901. + depctl.d32 = 0;
  86902. + depctl.b.snak = 1;
  86903. + depctl.b.epdis = 1;
  86904. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  86905. + [epnum]->diepctl,
  86906. + depctl.d32,
  86907. + depctl.d32);
  86908. + }
  86909. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  86910. +
  86911. + }
  86912. +
  86913. + /** IN EP Tx FIFO Empty Intr */
  86914. + if (diepint.b.emptyintr) {
  86915. + DWC_DEBUGPL(DBG_ANY,
  86916. + "EP%d Tx FIFO Empty Intr \n",
  86917. + epnum);
  86918. + write_empty_tx_fifo(pcd, epnum);
  86919. +
  86920. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  86921. +
  86922. + }
  86923. +
  86924. + /** IN EP BNA Intr */
  86925. + if (diepint.b.bna) {
  86926. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  86927. + if (core_if->dma_desc_enable) {
  86928. +#ifdef DWC_EN_ISOC
  86929. + if (dwc_ep->type ==
  86930. + DWC_OTG_EP_TYPE_ISOC) {
  86931. + /*
  86932. + * This checking is performed to prevent first "false" BNA
  86933. + * handling occuring right after reconnect
  86934. + */
  86935. + if (dwc_ep->next_frame !=
  86936. + 0xffffffff)
  86937. + dwc_otg_pcd_handle_iso_bna(ep);
  86938. + } else
  86939. +#endif /* DWC_EN_ISOC */
  86940. + {
  86941. + dwc_otg_pcd_handle_noniso_bna(ep);
  86942. + }
  86943. + }
  86944. + }
  86945. + /* NAK Interrutp */
  86946. + if (diepint.b.nak) {
  86947. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  86948. + epnum);
  86949. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  86950. + depctl_data_t depctl;
  86951. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  86952. + ep->dwc_ep.frame_num = core_if->frame_num;
  86953. + if (ep->dwc_ep.bInterval > 1) {
  86954. + depctl.d32 = 0;
  86955. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86956. + if (ep->dwc_ep.frame_num & 0x1) {
  86957. + depctl.b.setd1pid = 1;
  86958. + depctl.b.setd0pid = 0;
  86959. + } else {
  86960. + depctl.b.setd0pid = 1;
  86961. + depctl.b.setd1pid = 0;
  86962. + }
  86963. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  86964. + }
  86965. + start_next_request(ep);
  86966. + }
  86967. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  86968. + if (dwc_ep->frame_num > 0x3FFF) {
  86969. + dwc_ep->frm_overrun = 1;
  86970. + dwc_ep->frame_num &= 0x3FFF;
  86971. + } else
  86972. + dwc_ep->frm_overrun = 0;
  86973. + }
  86974. +
  86975. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  86976. + }
  86977. + }
  86978. + epnum++;
  86979. + ep_intr >>= 1;
  86980. + }
  86981. +
  86982. + return 1;
  86983. +#undef CLEAR_IN_EP_INTR
  86984. +}
  86985. +
  86986. +/**
  86987. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  86988. + * The sequence for handling the OUT EP interrupt is shown below:
  86989. + * -# Read the Device All Endpoint Interrupt register
  86990. + * -# Repeat the following for each OUT EP interrupt bit set (from
  86991. + * LSB to MSB).
  86992. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  86993. + * -# If "Transfer Complete" call the request complete function
  86994. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  86995. + * -# If "AHB Error Interrupt" log error
  86996. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  86997. + * Command Processing)
  86998. + */
  86999. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  87000. +{
  87001. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  87002. +do { \
  87003. + doepint_data_t doepint = {.d32=0}; \
  87004. + doepint.b.__intr = 1; \
  87005. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  87006. + doepint.d32); \
  87007. +} while (0)
  87008. +
  87009. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87010. + uint32_t ep_intr;
  87011. + doepint_data_t doepint = {.d32 = 0 };
  87012. + uint32_t epnum = 0;
  87013. + dwc_otg_pcd_ep_t *ep;
  87014. + dwc_ep_t *dwc_ep;
  87015. + dctl_data_t dctl = {.d32 = 0 };
  87016. + gintmsk_data_t gintmsk = {.d32 = 0 };
  87017. +
  87018. +
  87019. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  87020. +
  87021. + /* Read in the device interrupt bits */
  87022. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  87023. +
  87024. + while (ep_intr) {
  87025. + if (ep_intr & 0x1) {
  87026. + /* Get EP pointer */
  87027. + ep = get_out_ep(pcd, epnum);
  87028. + dwc_ep = &ep->dwc_ep;
  87029. +
  87030. +#ifdef VERBOSE
  87031. + DWC_DEBUGPL(DBG_PCDV,
  87032. + "EP%d-%s: type=%d, mps=%d\n",
  87033. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  87034. + dwc_ep->type, dwc_ep->maxpacket);
  87035. +#endif
  87036. + doepint.d32 =
  87037. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  87038. + /* Moved this interrupt upper due to core deffect of asserting
  87039. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  87040. + if (doepint.b.stsphsercvd) {
  87041. + deptsiz0_data_t deptsiz;
  87042. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  87043. + deptsiz.d32 =
  87044. + DWC_READ_REG32(&core_if->dev_if->
  87045. + out_ep_regs[0]->doeptsiz);
  87046. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  87047. + && core_if->dma_enable
  87048. + && core_if->dma_desc_enable == 0
  87049. + && doepint.b.xfercompl
  87050. + && deptsiz.b.xfersize == 24) {
  87051. + CLEAR_OUT_EP_INTR(core_if, epnum,
  87052. + xfercompl);
  87053. + doepint.b.xfercompl = 0;
  87054. + ep0_out_start(core_if, pcd);
  87055. + }
  87056. + if ((core_if->dma_desc_enable) ||
  87057. + (core_if->dma_enable
  87058. + && core_if->snpsid >=
  87059. + OTG_CORE_REV_3_00a)) {
  87060. + do_setup_in_status_phase(pcd);
  87061. + }
  87062. + }
  87063. + /* Transfer complete */
  87064. + if (doepint.b.xfercompl) {
  87065. +
  87066. + if (epnum == 0) {
  87067. + /* Clear the bit in DOEPINTn for this interrupt */
  87068. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  87069. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  87070. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  87071. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  87072. + doepint.d32);
  87073. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  87074. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  87075. +
  87076. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  87077. + && core_if->dma_enable == 0) {
  87078. + doepint_data_t doepint;
  87079. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87080. + out_ep_regs[0]->doepint);
  87081. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  87082. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  87083. + goto exit_xfercompl;
  87084. + }
  87085. + }
  87086. + /* In case of DDMA look at SR bit to go to the Data Stage */
  87087. + if (core_if->dma_desc_enable) {
  87088. + dev_dma_desc_sts_t status = {.d32 = 0};
  87089. + if (pcd->ep0state == EP0_IDLE) {
  87090. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  87091. + dev_if->setup_desc_index]->status.d32;
  87092. + if(pcd->data_terminated) {
  87093. + pcd->data_terminated = 0;
  87094. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  87095. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  87096. + }
  87097. + if (status.b.sr) {
  87098. + if (doepint.b.setup) {
  87099. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  87100. + /* Already started data stage, clear setup */
  87101. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87102. + doepint.b.setup = 0;
  87103. + handle_ep0(pcd);
  87104. + /* Prepare for more setup packets */
  87105. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  87106. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  87107. + ep0_out_start(core_if, pcd);
  87108. + }
  87109. +
  87110. + goto exit_xfercompl;
  87111. + } else {
  87112. + /* Prepare for more setup packets */
  87113. + DWC_DEBUGPL(DBG_PCDV,
  87114. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  87115. + ep0_out_start(core_if, pcd);
  87116. + }
  87117. + }
  87118. + } else {
  87119. + dwc_otg_pcd_request_t *req;
  87120. + dev_dma_desc_sts_t status = {.d32 = 0};
  87121. + diepint_data_t diepint0;
  87122. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87123. + in_ep_regs[0]->diepint);
  87124. +
  87125. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  87126. + DWC_ERROR("EP0 is stalled/disconnected\n");
  87127. + }
  87128. +
  87129. + /* Clear IN xfercompl if set */
  87130. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  87131. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  87132. + DWC_WRITE_REG32(&core_if->dev_if->
  87133. + in_ep_regs[0]->diepint, diepint0.d32);
  87134. + }
  87135. +
  87136. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  87137. + dev_if->setup_desc_index]->status.d32;
  87138. +
  87139. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  87140. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  87141. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  87142. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  87143. + status.d32 = core_if->dev_if->
  87144. + out_desc_addr->status.d32;
  87145. +
  87146. + if (status.b.sr) {
  87147. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87148. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  87149. + } else {
  87150. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  87151. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87152. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  87153. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  87154. + /* Read arrived setup packet from req->buf */
  87155. + dwc_memcpy(&pcd->setup_pkt->req,
  87156. + req->buf + ep->dwc_ep.xfer_count, 8);
  87157. + }
  87158. + req->actual = ep->dwc_ep.xfer_count;
  87159. + dwc_otg_request_done(ep, req, -ECONNRESET);
  87160. + ep->dwc_ep.start_xfer_buff = 0;
  87161. + ep->dwc_ep.xfer_buff = 0;
  87162. + ep->dwc_ep.xfer_len = 0;
  87163. + }
  87164. + pcd->ep0state = EP0_IDLE;
  87165. + if (doepint.b.setup) {
  87166. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  87167. + /* Data stage started, clear setup */
  87168. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87169. + doepint.b.setup = 0;
  87170. + handle_ep0(pcd);
  87171. + /* Prepare for setup packets if ep0in was enabled*/
  87172. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  87173. + ep0_out_start(core_if, pcd);
  87174. + }
  87175. +
  87176. + goto exit_xfercompl;
  87177. + } else {
  87178. + /* Prepare for more setup packets */
  87179. + DWC_DEBUGPL(DBG_PCDV,
  87180. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  87181. + ep0_out_start(core_if, pcd);
  87182. + }
  87183. + }
  87184. + }
  87185. + }
  87186. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  87187. + && core_if->dma_desc_enable == 0) {
  87188. + doepint_data_t doepint_temp = {.d32 = 0};
  87189. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  87190. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  87191. + out_ep_regs[ep->dwc_ep.num]->doepint);
  87192. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87193. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  87194. + if (pcd->ep0state == EP0_IDLE) {
  87195. + if (doepint_temp.b.sr) {
  87196. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  87197. + }
  87198. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87199. + out_ep_regs[0]->doepint);
  87200. + if (doeptsize0.b.supcnt == 3) {
  87201. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  87202. + ep->dwc_ep.stp_rollover = 1;
  87203. + }
  87204. + if (doepint.b.setup) {
  87205. +retry:
  87206. + /* Already started data stage, clear setup */
  87207. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87208. + doepint.b.setup = 0;
  87209. + handle_ep0(pcd);
  87210. + ep->dwc_ep.stp_rollover = 0;
  87211. + /* Prepare for more setup packets */
  87212. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  87213. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  87214. + ep0_out_start(core_if, pcd);
  87215. + }
  87216. + goto exit_xfercompl;
  87217. + } else {
  87218. + /* Prepare for more setup packets */
  87219. + DWC_DEBUGPL(DBG_ANY,
  87220. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  87221. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87222. + out_ep_regs[0]->doepint);
  87223. + if(doepint.b.setup)
  87224. + goto retry;
  87225. + ep0_out_start(core_if, pcd);
  87226. + }
  87227. + } else {
  87228. + dwc_otg_pcd_request_t *req;
  87229. + diepint_data_t diepint0 = {.d32 = 0};
  87230. + doepint_data_t doepint_temp = {.d32 = 0};
  87231. + depctl_data_t diepctl0;
  87232. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87233. + in_ep_regs[0]->diepint);
  87234. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  87235. + in_ep_regs[0]->diepctl);
  87236. +
  87237. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  87238. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  87239. + if (diepint0.b.xfercompl) {
  87240. + DWC_WRITE_REG32(&core_if->dev_if->
  87241. + in_ep_regs[0]->diepint, diepint0.d32);
  87242. + }
  87243. + if (diepctl0.b.epena) {
  87244. + diepint_data_t diepint = {.d32 = 0};
  87245. + diepctl0.b.snak = 1;
  87246. + DWC_WRITE_REG32(&core_if->dev_if->
  87247. + in_ep_regs[0]->diepctl, diepctl0.d32);
  87248. + do {
  87249. + dwc_udelay(10);
  87250. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87251. + in_ep_regs[0]->diepint);
  87252. + } while (!diepint.b.inepnakeff);
  87253. + diepint.b.inepnakeff = 1;
  87254. + DWC_WRITE_REG32(&core_if->dev_if->
  87255. + in_ep_regs[0]->diepint, diepint.d32);
  87256. + diepctl0.d32 = 0;
  87257. + diepctl0.b.epdis = 1;
  87258. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  87259. + diepctl0.d32);
  87260. + do {
  87261. + dwc_udelay(10);
  87262. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  87263. + in_ep_regs[0]->diepint);
  87264. + } while (!diepint.b.epdisabled);
  87265. + diepint.b.epdisabled = 1;
  87266. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  87267. + diepint.d32);
  87268. + }
  87269. + }
  87270. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  87271. + out_ep_regs[ep->dwc_ep.num]->doepint);
  87272. + if (doepint_temp.b.sr) {
  87273. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  87274. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87275. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  87276. + } else {
  87277. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  87278. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87279. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  87280. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  87281. + /* Read arrived setup packet from req->buf */
  87282. + dwc_memcpy(&pcd->setup_pkt->req,
  87283. + req->buf + ep->dwc_ep.xfer_count, 8);
  87284. + }
  87285. + req->actual = ep->dwc_ep.xfer_count;
  87286. + dwc_otg_request_done(ep, req, -ECONNRESET);
  87287. + ep->dwc_ep.start_xfer_buff = 0;
  87288. + ep->dwc_ep.xfer_buff = 0;
  87289. + ep->dwc_ep.xfer_len = 0;
  87290. + }
  87291. + pcd->ep0state = EP0_IDLE;
  87292. + if (doepint.b.setup) {
  87293. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  87294. + /* Data stage started, clear setup */
  87295. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87296. + doepint.b.setup = 0;
  87297. + handle_ep0(pcd);
  87298. + /* Prepare for setup packets if ep0in was enabled*/
  87299. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  87300. + ep0_out_start(core_if, pcd);
  87301. + }
  87302. + goto exit_xfercompl;
  87303. + } else {
  87304. + /* Prepare for more setup packets */
  87305. + DWC_DEBUGPL(DBG_PCDV,
  87306. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  87307. + ep0_out_start(core_if, pcd);
  87308. + }
  87309. + }
  87310. + }
  87311. + }
  87312. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  87313. + handle_ep0(pcd);
  87314. +exit_xfercompl:
  87315. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  87316. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  87317. + } else {
  87318. + if (core_if->dma_desc_enable == 0
  87319. + || pcd->ep0state != EP0_IDLE)
  87320. + handle_ep0(pcd);
  87321. + }
  87322. +#ifdef DWC_EN_ISOC
  87323. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87324. + if (doepint.b.pktdrpsts == 0) {
  87325. + /* Clear the bit in DOEPINTn for this interrupt */
  87326. + CLEAR_OUT_EP_INTR(core_if,
  87327. + epnum,
  87328. + xfercompl);
  87329. + complete_iso_ep(pcd, ep);
  87330. + } else {
  87331. +
  87332. + doepint_data_t doepint = {.d32 = 0 };
  87333. + doepint.b.xfercompl = 1;
  87334. + doepint.b.pktdrpsts = 1;
  87335. + DWC_WRITE_REG32
  87336. + (&core_if->dev_if->out_ep_regs
  87337. + [epnum]->doepint,
  87338. + doepint.d32);
  87339. + if (handle_iso_out_pkt_dropped
  87340. + (core_if, dwc_ep)) {
  87341. + complete_iso_ep(pcd,
  87342. + ep);
  87343. + }
  87344. + }
  87345. +#endif /* DWC_EN_ISOC */
  87346. +#ifdef DWC_UTE_PER_IO
  87347. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87348. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  87349. + if (!ep->stopped)
  87350. + complete_xiso_ep(ep);
  87351. +#endif /* DWC_UTE_PER_IO */
  87352. + } else {
  87353. + /* Clear the bit in DOEPINTn for this interrupt */
  87354. + CLEAR_OUT_EP_INTR(core_if, epnum,
  87355. + xfercompl);
  87356. +
  87357. + if (core_if->core_params->dev_out_nak) {
  87358. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  87359. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  87360. +#ifdef DEBUG
  87361. + print_memory_payload(pcd, dwc_ep);
  87362. +#endif
  87363. + }
  87364. + complete_ep(ep);
  87365. + }
  87366. +
  87367. + }
  87368. +
  87369. + /* Endpoint disable */
  87370. + if (doepint.b.epdisabled) {
  87371. +
  87372. + /* Clear the bit in DOEPINTn for this interrupt */
  87373. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  87374. + if (core_if->core_params->dev_out_nak) {
  87375. +#ifdef DEBUG
  87376. + print_memory_payload(pcd, dwc_ep);
  87377. +#endif
  87378. + /* In case of timeout condition */
  87379. + if (core_if->ep_xfer_info[epnum].state == 2) {
  87380. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  87381. + dev_global_regs->dctl);
  87382. + dctl.b.cgoutnak = 1;
  87383. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  87384. + dctl.d32);
  87385. + /* Unmask goutnakeff interrupt which was masked
  87386. + * during handle nak out interrupt */
  87387. + gintmsk.b.goutnakeff = 1;
  87388. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  87389. + 0, gintmsk.d32);
  87390. +
  87391. + complete_ep(ep);
  87392. + }
  87393. + }
  87394. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  87395. + {
  87396. + dctl_data_t dctl;
  87397. + gintmsk_data_t intr_mask = {.d32 = 0};
  87398. + dwc_otg_pcd_request_t *req = 0;
  87399. +
  87400. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  87401. + dev_global_regs->dctl);
  87402. + dctl.b.cgoutnak = 1;
  87403. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  87404. + dctl.d32);
  87405. +
  87406. + intr_mask.d32 = 0;
  87407. + intr_mask.b.incomplisoout = 1;
  87408. +
  87409. + /* Get any pending requests */
  87410. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87411. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87412. + if (!req) {
  87413. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  87414. + } else {
  87415. + dwc_otg_request_done(ep, req, 0);
  87416. + start_next_request(ep);
  87417. + }
  87418. + } else {
  87419. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  87420. + }
  87421. + }
  87422. + }
  87423. + /* AHB Error */
  87424. + if (doepint.b.ahberr) {
  87425. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  87426. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  87427. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  87428. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  87429. + }
  87430. + /* Setup Phase Done (contorl EPs) */
  87431. + if (doepint.b.setup) {
  87432. +#ifdef DEBUG_EP0
  87433. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  87434. +#endif
  87435. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  87436. +
  87437. + handle_ep0(pcd);
  87438. + }
  87439. +
  87440. + /** OUT EP BNA Intr */
  87441. + if (doepint.b.bna) {
  87442. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  87443. + if (core_if->dma_desc_enable) {
  87444. +#ifdef DWC_EN_ISOC
  87445. + if (dwc_ep->type ==
  87446. + DWC_OTG_EP_TYPE_ISOC) {
  87447. + /*
  87448. + * This checking is performed to prevent first "false" BNA
  87449. + * handling occuring right after reconnect
  87450. + */
  87451. + if (dwc_ep->next_frame !=
  87452. + 0xffffffff)
  87453. + dwc_otg_pcd_handle_iso_bna(ep);
  87454. + } else
  87455. +#endif /* DWC_EN_ISOC */
  87456. + {
  87457. + dwc_otg_pcd_handle_noniso_bna(ep);
  87458. + }
  87459. + }
  87460. + }
  87461. + /* Babble Interrupt */
  87462. + if (doepint.b.babble) {
  87463. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  87464. + epnum);
  87465. + handle_out_ep_babble_intr(pcd, epnum);
  87466. +
  87467. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  87468. + }
  87469. + if (doepint.b.outtknepdis) {
  87470. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  87471. + disabled\n",epnum);
  87472. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  87473. + doepmsk_data_t doepmsk = {.d32 = 0};
  87474. + ep->dwc_ep.frame_num = core_if->frame_num;
  87475. + if (ep->dwc_ep.bInterval > 1) {
  87476. + depctl_data_t depctl;
  87477. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  87478. + out_ep_regs[epnum]->doepctl);
  87479. + if (ep->dwc_ep.frame_num & 0x1) {
  87480. + depctl.b.setd1pid = 1;
  87481. + depctl.b.setd0pid = 0;
  87482. + } else {
  87483. + depctl.b.setd0pid = 1;
  87484. + depctl.b.setd1pid = 0;
  87485. + }
  87486. + DWC_WRITE_REG32(&core_if->dev_if->
  87487. + out_ep_regs[epnum]->doepctl, depctl.d32);
  87488. + }
  87489. + start_next_request(ep);
  87490. + doepmsk.b.outtknepdis = 1;
  87491. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  87492. + doepmsk.d32, 0);
  87493. + }
  87494. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  87495. + }
  87496. +
  87497. + /* NAK Interrutp */
  87498. + if (doepint.b.nak) {
  87499. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  87500. + handle_out_ep_nak_intr(pcd, epnum);
  87501. +
  87502. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  87503. + }
  87504. + /* NYET Interrutp */
  87505. + if (doepint.b.nyet) {
  87506. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  87507. + handle_out_ep_nyet_intr(pcd, epnum);
  87508. +
  87509. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  87510. + }
  87511. + }
  87512. +
  87513. + epnum++;
  87514. + ep_intr >>= 1;
  87515. + }
  87516. +
  87517. + return 1;
  87518. +
  87519. +#undef CLEAR_OUT_EP_INTR
  87520. +}
  87521. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  87522. +{
  87523. + int retval = 0;
  87524. + if(!frm_overrun && curr_fr >= trgt_fr)
  87525. + retval = 1;
  87526. + else if (frm_overrun
  87527. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  87528. + retval = 1;
  87529. + return retval;
  87530. +}
  87531. +/**
  87532. + * Incomplete ISO IN Transfer Interrupt.
  87533. + * This interrupt indicates one of the following conditions occurred
  87534. + * while transmitting an ISOC transaction.
  87535. + * - Corrupted IN Token for ISOC EP.
  87536. + * - Packet not complete in FIFO.
  87537. + * The follow actions will be taken:
  87538. + * -# Determine the EP
  87539. + * -# Set incomplete flag in dwc_ep structure
  87540. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  87541. + * Flush FIFO
  87542. + */
  87543. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  87544. +{
  87545. + gintsts_data_t gintsts;
  87546. +
  87547. +#ifdef DWC_EN_ISOC
  87548. + dwc_otg_dev_if_t *dev_if;
  87549. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87550. + depctl_data_t depctl = {.d32 = 0 };
  87551. + dsts_data_t dsts = {.d32 = 0 };
  87552. + dwc_ep_t *dwc_ep;
  87553. + int i;
  87554. +
  87555. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87556. +
  87557. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  87558. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  87559. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87560. + deptsiz.d32 =
  87561. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  87562. + depctl.d32 =
  87563. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87564. +
  87565. + if (depctl.b.epdis && deptsiz.d32) {
  87566. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  87567. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  87568. + dwc_ep->cur_pkt = 0;
  87569. + dwc_ep->proc_buf_num =
  87570. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  87571. +
  87572. + if (dwc_ep->proc_buf_num) {
  87573. + dwc_ep->cur_pkt_addr =
  87574. + dwc_ep->xfer_buff1;
  87575. + dwc_ep->cur_pkt_dma_addr =
  87576. + dwc_ep->dma_addr1;
  87577. + } else {
  87578. + dwc_ep->cur_pkt_addr =
  87579. + dwc_ep->xfer_buff0;
  87580. + dwc_ep->cur_pkt_dma_addr =
  87581. + dwc_ep->dma_addr0;
  87582. + }
  87583. +
  87584. + }
  87585. +
  87586. + dsts.d32 =
  87587. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  87588. + dev_global_regs->dsts);
  87589. + dwc_ep->next_frame = dsts.b.soffn;
  87590. +
  87591. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  87592. + (pcd),
  87593. + dwc_ep);
  87594. + }
  87595. + }
  87596. + }
  87597. +
  87598. +#else
  87599. + depctl_data_t depctl = {.d32 = 0 };
  87600. + dwc_ep_t *dwc_ep;
  87601. + dwc_otg_dev_if_t *dev_if;
  87602. + int i;
  87603. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87604. +
  87605. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  87606. +
  87607. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  87608. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  87609. + depctl.d32 =
  87610. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87611. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  87612. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  87613. + dwc_ep->frm_overrun))
  87614. + {
  87615. + depctl.d32 =
  87616. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87617. + depctl.b.snak = 1;
  87618. + depctl.b.epdis = 1;
  87619. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  87620. + }
  87621. + }
  87622. + }
  87623. +
  87624. + /*intr_mask.b.incomplisoin = 1;
  87625. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87626. + intr_mask.d32, 0); */
  87627. +#endif //DWC_EN_ISOC
  87628. +
  87629. + /* Clear interrupt */
  87630. + gintsts.d32 = 0;
  87631. + gintsts.b.incomplisoin = 1;
  87632. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87633. + gintsts.d32);
  87634. +
  87635. + return 1;
  87636. +}
  87637. +
  87638. +/**
  87639. + * Incomplete ISO OUT Transfer Interrupt.
  87640. + *
  87641. + * This interrupt indicates that the core has dropped an ISO OUT
  87642. + * packet. The following conditions can be the cause:
  87643. + * - FIFO Full, the entire packet would not fit in the FIFO.
  87644. + * - CRC Error
  87645. + * - Corrupted Token
  87646. + * The follow actions will be taken:
  87647. + * -# Determine the EP
  87648. + * -# Set incomplete flag in dwc_ep structure
  87649. + * -# Read any data from the FIFO
  87650. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  87651. + * re-enable EP.
  87652. + */
  87653. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  87654. +{
  87655. +
  87656. + gintsts_data_t gintsts;
  87657. +
  87658. +#ifdef DWC_EN_ISOC
  87659. + dwc_otg_dev_if_t *dev_if;
  87660. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87661. + depctl_data_t depctl = {.d32 = 0 };
  87662. + dsts_data_t dsts = {.d32 = 0 };
  87663. + dwc_ep_t *dwc_ep;
  87664. + int i;
  87665. +
  87666. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87667. +
  87668. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  87669. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  87670. + if (pcd->out_ep[i].dwc_ep.active &&
  87671. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  87672. + deptsiz.d32 =
  87673. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  87674. + depctl.d32 =
  87675. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  87676. +
  87677. + if (depctl.b.epdis && deptsiz.d32) {
  87678. + set_current_pkt_info(GET_CORE_IF(pcd),
  87679. + &pcd->out_ep[i].dwc_ep);
  87680. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  87681. + dwc_ep->cur_pkt = 0;
  87682. + dwc_ep->proc_buf_num =
  87683. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  87684. +
  87685. + if (dwc_ep->proc_buf_num) {
  87686. + dwc_ep->cur_pkt_addr =
  87687. + dwc_ep->xfer_buff1;
  87688. + dwc_ep->cur_pkt_dma_addr =
  87689. + dwc_ep->dma_addr1;
  87690. + } else {
  87691. + dwc_ep->cur_pkt_addr =
  87692. + dwc_ep->xfer_buff0;
  87693. + dwc_ep->cur_pkt_dma_addr =
  87694. + dwc_ep->dma_addr0;
  87695. + }
  87696. +
  87697. + }
  87698. +
  87699. + dsts.d32 =
  87700. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  87701. + dev_global_regs->dsts);
  87702. + dwc_ep->next_frame = dsts.b.soffn;
  87703. +
  87704. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  87705. + (pcd),
  87706. + dwc_ep);
  87707. + }
  87708. + }
  87709. + }
  87710. +#else
  87711. + /** @todo implement ISR */
  87712. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87713. + dwc_otg_core_if_t *core_if;
  87714. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87715. + depctl_data_t depctl = {.d32 = 0 };
  87716. + dctl_data_t dctl = {.d32 = 0 };
  87717. + dwc_ep_t *dwc_ep = NULL;
  87718. + int i;
  87719. + core_if = GET_CORE_IF(pcd);
  87720. +
  87721. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  87722. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  87723. + depctl.d32 =
  87724. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  87725. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  87726. + core_if->dev_if->isoc_ep = dwc_ep;
  87727. + deptsiz.d32 =
  87728. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  87729. + break;
  87730. + }
  87731. + }
  87732. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  87733. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  87734. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  87735. +
  87736. + if (!intr_mask.b.goutnakeff) {
  87737. + /* Unmask it */
  87738. + intr_mask.b.goutnakeff = 1;
  87739. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  87740. + }
  87741. + if (!gintsts.b.goutnakeff) {
  87742. + dctl.b.sgoutnak = 1;
  87743. + }
  87744. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  87745. +
  87746. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  87747. + if (depctl.b.epena) {
  87748. + depctl.b.epdis = 1;
  87749. + depctl.b.snak = 1;
  87750. + }
  87751. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  87752. +
  87753. + intr_mask.d32 = 0;
  87754. + intr_mask.b.incomplisoout = 1;
  87755. +
  87756. +#endif /* DWC_EN_ISOC */
  87757. +
  87758. + /* Clear interrupt */
  87759. + gintsts.d32 = 0;
  87760. + gintsts.b.incomplisoout = 1;
  87761. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87762. + gintsts.d32);
  87763. +
  87764. + return 1;
  87765. +}
  87766. +
  87767. +/**
  87768. + * This function handles the Global IN NAK Effective interrupt.
  87769. + *
  87770. + */
  87771. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  87772. +{
  87773. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  87774. + depctl_data_t diepctl = {.d32 = 0 };
  87775. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87776. + gintsts_data_t gintsts;
  87777. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87778. + int i;
  87779. +
  87780. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  87781. +
  87782. + /* Disable all active IN EPs */
  87783. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  87784. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87785. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  87786. + if (core_if->start_predict > 0)
  87787. + core_if->start_predict++;
  87788. + diepctl.b.epdis = 1;
  87789. + diepctl.b.snak = 1;
  87790. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  87791. + }
  87792. + }
  87793. +
  87794. +
  87795. + /* Disable the Global IN NAK Effective Interrupt */
  87796. + intr_mask.b.ginnakeff = 1;
  87797. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87798. + intr_mask.d32, 0);
  87799. +
  87800. + /* Clear interrupt */
  87801. + gintsts.d32 = 0;
  87802. + gintsts.b.ginnakeff = 1;
  87803. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87804. + gintsts.d32);
  87805. +
  87806. + return 1;
  87807. +}
  87808. +
  87809. +/**
  87810. + * OUT NAK Effective.
  87811. + *
  87812. + */
  87813. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  87814. +{
  87815. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  87816. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87817. + gintsts_data_t gintsts;
  87818. + depctl_data_t doepctl;
  87819. + int i;
  87820. +
  87821. + /* Disable the Global OUT NAK Effective Interrupt */
  87822. + intr_mask.b.goutnakeff = 1;
  87823. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87824. + intr_mask.d32, 0);
  87825. +
  87826. + /* If DEV OUT NAK enabled*/
  87827. + if (pcd->core_if->core_params->dev_out_nak) {
  87828. + /* Run over all out endpoints to determine the ep number on
  87829. + * which the timeout has happened
  87830. + */
  87831. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  87832. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  87833. + break;
  87834. + }
  87835. + if (i > dev_if->num_out_eps) {
  87836. + dctl_data_t dctl;
  87837. + dctl.d32 =
  87838. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  87839. + dctl.b.cgoutnak = 1;
  87840. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  87841. + dctl.d32);
  87842. + goto out;
  87843. + }
  87844. +
  87845. + /* Disable the endpoint */
  87846. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  87847. + if (doepctl.b.epena) {
  87848. + doepctl.b.epdis = 1;
  87849. + doepctl.b.snak = 1;
  87850. + }
  87851. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  87852. + return 1;
  87853. + }
  87854. + /* We come here from Incomplete ISO OUT handler */
  87855. + if (dev_if->isoc_ep) {
  87856. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  87857. + uint32_t epnum = dwc_ep->num;
  87858. + doepint_data_t doepint;
  87859. + doepint.d32 =
  87860. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  87861. + dev_if->isoc_ep = NULL;
  87862. + doepctl.d32 =
  87863. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  87864. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  87865. + if (doepctl.b.epena) {
  87866. + doepctl.b.epdis = 1;
  87867. + doepctl.b.snak = 1;
  87868. + }
  87869. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  87870. + doepctl.d32);
  87871. + return 1;
  87872. + } else
  87873. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  87874. + "Global OUT NAK Effective\n");
  87875. +
  87876. +out:
  87877. + /* Clear interrupt */
  87878. + gintsts.d32 = 0;
  87879. + gintsts.b.goutnakeff = 1;
  87880. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87881. + gintsts.d32);
  87882. +
  87883. + return 1;
  87884. +}
  87885. +
  87886. +/**
  87887. + * PCD interrupt handler.
  87888. + *
  87889. + * The PCD handles the device interrupts. Many conditions can cause a
  87890. + * device interrupt. When an interrupt occurs, the device interrupt
  87891. + * service routine determines the cause of the interrupt and
  87892. + * dispatches handling to the appropriate function. These interrupt
  87893. + * handling functions are described below.
  87894. + *
  87895. + * All interrupt registers are processed from LSB to MSB.
  87896. + *
  87897. + */
  87898. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  87899. +{
  87900. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87901. +#ifdef VERBOSE
  87902. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  87903. +#endif
  87904. + gintsts_data_t gintr_status;
  87905. + int32_t retval = 0;
  87906. +
  87907. + /* Exit from ISR if core is hibernated */
  87908. + if (core_if->hibernation_suspend == 1) {
  87909. + return retval;
  87910. + }
  87911. +#ifdef VERBOSE
  87912. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  87913. + __func__,
  87914. + DWC_READ_REG32(&global_regs->gintsts),
  87915. + DWC_READ_REG32(&global_regs->gintmsk));
  87916. +#endif
  87917. +
  87918. + if (dwc_otg_is_device_mode(core_if)) {
  87919. + DWC_SPINLOCK(pcd->lock);
  87920. +#ifdef VERBOSE
  87921. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  87922. + __func__,
  87923. + DWC_READ_REG32(&global_regs->gintsts),
  87924. + DWC_READ_REG32(&global_regs->gintmsk));
  87925. +#endif
  87926. +
  87927. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  87928. +
  87929. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  87930. + __func__, gintr_status.d32);
  87931. +
  87932. + if (gintr_status.b.sofintr) {
  87933. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  87934. + }
  87935. + if (gintr_status.b.rxstsqlvl) {
  87936. + retval |=
  87937. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  87938. + }
  87939. + if (gintr_status.b.nptxfempty) {
  87940. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  87941. + }
  87942. + if (gintr_status.b.goutnakeff) {
  87943. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  87944. + }
  87945. + if (gintr_status.b.i2cintr) {
  87946. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  87947. + }
  87948. + if (gintr_status.b.erlysuspend) {
  87949. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  87950. + }
  87951. + if (gintr_status.b.usbreset) {
  87952. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  87953. + }
  87954. + if (gintr_status.b.enumdone) {
  87955. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  87956. + }
  87957. + if (gintr_status.b.isooutdrop) {
  87958. + retval |=
  87959. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  87960. + (pcd);
  87961. + }
  87962. + if (gintr_status.b.eopframe) {
  87963. + retval |=
  87964. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  87965. + }
  87966. + if (gintr_status.b.inepint) {
  87967. + if (!core_if->multiproc_int_enable) {
  87968. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  87969. + }
  87970. + }
  87971. + if (gintr_status.b.outepintr) {
  87972. + if (!core_if->multiproc_int_enable) {
  87973. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  87974. + }
  87975. + }
  87976. + if (gintr_status.b.epmismatch) {
  87977. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  87978. + }
  87979. + if (gintr_status.b.fetsusp) {
  87980. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  87981. + }
  87982. + if (gintr_status.b.ginnakeff) {
  87983. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  87984. + }
  87985. + if (gintr_status.b.incomplisoin) {
  87986. + retval |=
  87987. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  87988. + }
  87989. + if (gintr_status.b.incomplisoout) {
  87990. + retval |=
  87991. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  87992. + }
  87993. +
  87994. + /* In MPI mode Device Endpoints interrupts are asserted
  87995. + * without setting outepintr and inepint bits set, so these
  87996. + * Interrupt handlers are called without checking these bit-fields
  87997. + */
  87998. + if (core_if->multiproc_int_enable) {
  87999. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  88000. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  88001. + }
  88002. +#ifdef VERBOSE
  88003. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  88004. + DWC_READ_REG32(&global_regs->gintsts));
  88005. +#endif
  88006. + DWC_SPINUNLOCK(pcd->lock);
  88007. + }
  88008. + return retval;
  88009. +}
  88010. +
  88011. +#endif /* DWC_HOST_ONLY */
  88012. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  88013. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  88014. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 2014-04-24 15:35:04.177565820 +0200
  88015. @@ -0,0 +1,1358 @@
  88016. + /* ==========================================================================
  88017. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  88018. + * $Revision: #21 $
  88019. + * $Date: 2012/08/10 $
  88020. + * $Change: 2047372 $
  88021. + *
  88022. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  88023. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  88024. + * otherwise expressly agreed to in writing between Synopsys and you.
  88025. + *
  88026. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  88027. + * any End User Software License Agreement or Agreement for Licensed Product
  88028. + * with Synopsys or any supplement thereto. You are permitted to use and
  88029. + * redistribute this Software in source and binary forms, with or without
  88030. + * modification, provided that redistributions of source code must retain this
  88031. + * notice. You may not view, use, disclose, copy or distribute this file or
  88032. + * any information contained herein except pursuant to this license grant from
  88033. + * Synopsys. If you do not agree with this notice, including the disclaimer
  88034. + * below, then you are not authorized to use the Software.
  88035. + *
  88036. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  88037. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88038. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  88039. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  88040. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  88041. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88042. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88043. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  88044. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  88045. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  88046. + * DAMAGE.
  88047. + * ========================================================================== */
  88048. +#ifndef DWC_HOST_ONLY
  88049. +
  88050. +/** @file
  88051. + * This file implements the Peripheral Controller Driver.
  88052. + *
  88053. + * The Peripheral Controller Driver (PCD) is responsible for
  88054. + * translating requests from the Function Driver into the appropriate
  88055. + * actions on the DWC_otg controller. It isolates the Function Driver
  88056. + * from the specifics of the controller by providing an API to the
  88057. + * Function Driver.
  88058. + *
  88059. + * The Peripheral Controller Driver for Linux will implement the
  88060. + * Gadget API, so that the existing Gadget drivers can be used.
  88061. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  88062. + *
  88063. + * The Linux Gadget API is defined in the header file
  88064. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  88065. + * defined in the structure <code>usb_ep_ops</code> and the USB
  88066. + * Controller API is defined in the structure
  88067. + * <code>usb_gadget_ops</code>.
  88068. + *
  88069. + */
  88070. +
  88071. +#include "dwc_otg_os_dep.h"
  88072. +#include "dwc_otg_pcd_if.h"
  88073. +#include "dwc_otg_pcd.h"
  88074. +#include "dwc_otg_driver.h"
  88075. +#include "dwc_otg_dbg.h"
  88076. +
  88077. +static struct gadget_wrapper {
  88078. + dwc_otg_pcd_t *pcd;
  88079. +
  88080. + struct usb_gadget gadget;
  88081. + struct usb_gadget_driver *driver;
  88082. +
  88083. + struct usb_ep ep0;
  88084. + struct usb_ep in_ep[16];
  88085. + struct usb_ep out_ep[16];
  88086. +
  88087. +} *gadget_wrapper;
  88088. +
  88089. +/* Display the contents of the buffer */
  88090. +extern void dump_msg(const u8 * buf, unsigned int length);
  88091. +/**
  88092. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  88093. + * if the endpoint is not found
  88094. + */
  88095. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  88096. +{
  88097. + int i;
  88098. + if (pcd->ep0.priv == handle) {
  88099. + return &pcd->ep0;
  88100. + }
  88101. +
  88102. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  88103. + if (pcd->in_ep[i].priv == handle)
  88104. + return &pcd->in_ep[i];
  88105. + if (pcd->out_ep[i].priv == handle)
  88106. + return &pcd->out_ep[i];
  88107. + }
  88108. +
  88109. + return NULL;
  88110. +}
  88111. +
  88112. +/* USB Endpoint Operations */
  88113. +/*
  88114. + * The following sections briefly describe the behavior of the Gadget
  88115. + * API endpoint operations implemented in the DWC_otg driver
  88116. + * software. Detailed descriptions of the generic behavior of each of
  88117. + * these functions can be found in the Linux header file
  88118. + * include/linux/usb_gadget.h.
  88119. + *
  88120. + * The Gadget API provides wrapper functions for each of the function
  88121. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  88122. + * function, which then calls the underlying PCD function. The
  88123. + * following sections are named according to the wrapper
  88124. + * functions. Within each section, the corresponding DWC_otg PCD
  88125. + * function name is specified.
  88126. + *
  88127. + */
  88128. +
  88129. +/**
  88130. + * This function is called by the Gadget Driver for each EP to be
  88131. + * configured for the current configuration (SET_CONFIGURATION).
  88132. + *
  88133. + * This function initializes the dwc_otg_ep_t data structure, and then
  88134. + * calls dwc_otg_ep_activate.
  88135. + */
  88136. +static int ep_enable(struct usb_ep *usb_ep,
  88137. + const struct usb_endpoint_descriptor *ep_desc)
  88138. +{
  88139. + int retval;
  88140. +
  88141. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  88142. +
  88143. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  88144. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  88145. + return -EINVAL;
  88146. + }
  88147. + if (usb_ep == &gadget_wrapper->ep0) {
  88148. + DWC_WARN("%s, bad ep(0)\n", __func__);
  88149. + return -EINVAL;
  88150. + }
  88151. +
  88152. + /* Check FIFO size? */
  88153. + if (!ep_desc->wMaxPacketSize) {
  88154. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  88155. + return -ERANGE;
  88156. + }
  88157. +
  88158. + if (!gadget_wrapper->driver ||
  88159. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88160. + DWC_WARN("%s, bogus device state\n", __func__);
  88161. + return -ESHUTDOWN;
  88162. + }
  88163. +
  88164. + /* Delete after check - MAS */
  88165. +#if 0
  88166. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  88167. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  88168. + nat = (nat >> 11) & 0x03;
  88169. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  88170. +#endif
  88171. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  88172. + (const uint8_t *)ep_desc,
  88173. + (void *)usb_ep);
  88174. + if (retval) {
  88175. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  88176. + return -EINVAL;
  88177. + }
  88178. +
  88179. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  88180. +
  88181. + return 0;
  88182. +}
  88183. +
  88184. +/**
  88185. + * This function is called when an EP is disabled due to disconnect or
  88186. + * change in configuration. Any pending requests will terminate with a
  88187. + * status of -ESHUTDOWN.
  88188. + *
  88189. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  88190. + * and then calls dwc_otg_ep_deactivate.
  88191. + */
  88192. +static int ep_disable(struct usb_ep *usb_ep)
  88193. +{
  88194. + int retval;
  88195. +
  88196. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  88197. + if (!usb_ep) {
  88198. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  88199. + usb_ep ? usb_ep->name : NULL);
  88200. + return -EINVAL;
  88201. + }
  88202. +
  88203. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  88204. + if (retval) {
  88205. + retval = -EINVAL;
  88206. + }
  88207. +
  88208. + return retval;
  88209. +}
  88210. +
  88211. +/**
  88212. + * This function allocates a request object to use with the specified
  88213. + * endpoint.
  88214. + *
  88215. + * @param ep The endpoint to be used with with the request
  88216. + * @param gfp_flags the GFP_* flags to use.
  88217. + */
  88218. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  88219. + gfp_t gfp_flags)
  88220. +{
  88221. + struct usb_request *usb_req;
  88222. +
  88223. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  88224. + if (0 == ep) {
  88225. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  88226. + return 0;
  88227. + }
  88228. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  88229. + if (0 == usb_req) {
  88230. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  88231. + return 0;
  88232. + }
  88233. + memset(usb_req, 0, sizeof(*usb_req));
  88234. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  88235. +
  88236. + return usb_req;
  88237. +}
  88238. +
  88239. +/**
  88240. + * This function frees a request object.
  88241. + *
  88242. + * @param ep The endpoint associated with the request
  88243. + * @param req The request being freed
  88244. + */
  88245. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  88246. +{
  88247. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  88248. +
  88249. + if (0 == ep || 0 == req) {
  88250. + DWC_WARN("%s() %s\n", __func__,
  88251. + "Invalid ep or req argument!\n");
  88252. + return;
  88253. + }
  88254. +
  88255. + kfree(req);
  88256. +}
  88257. +
  88258. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88259. +/**
  88260. + * This function allocates an I/O buffer to be used for a transfer
  88261. + * to/from the specified endpoint.
  88262. + *
  88263. + * @param usb_ep The endpoint to be used with with the request
  88264. + * @param bytes The desired number of bytes for the buffer
  88265. + * @param dma Pointer to the buffer's DMA address; must be valid
  88266. + * @param gfp_flags the GFP_* flags to use.
  88267. + * @return address of a new buffer or null is buffer could not be allocated.
  88268. + */
  88269. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  88270. + dma_addr_t * dma, gfp_t gfp_flags)
  88271. +{
  88272. + void *buf;
  88273. + dwc_otg_pcd_t *pcd = 0;
  88274. +
  88275. + pcd = gadget_wrapper->pcd;
  88276. +
  88277. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  88278. + dma, gfp_flags);
  88279. +
  88280. + /* Check dword alignment */
  88281. + if ((bytes & 0x3UL) != 0) {
  88282. + DWC_WARN("%s() Buffer size is not a multiple of"
  88283. + "DWORD size (%d)", __func__, bytes);
  88284. + }
  88285. +
  88286. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  88287. +
  88288. + /* Check dword alignment */
  88289. + if (((int)buf & 0x3UL) != 0) {
  88290. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  88291. + __func__, buf);
  88292. + }
  88293. +
  88294. + return buf;
  88295. +}
  88296. +
  88297. +/**
  88298. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  88299. + *
  88300. + * @param usb_ep the endpoint associated with the buffer
  88301. + * @param buf address of the buffer
  88302. + * @param dma The buffer's DMA address
  88303. + * @param bytes The number of bytes of the buffer
  88304. + */
  88305. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  88306. + dma_addr_t dma, unsigned bytes)
  88307. +{
  88308. + dwc_otg_pcd_t *pcd = 0;
  88309. +
  88310. + pcd = gadget_wrapper->pcd;
  88311. +
  88312. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  88313. +
  88314. + dma_free_coherent(NULL, bytes, buf, dma);
  88315. +}
  88316. +#endif
  88317. +
  88318. +/**
  88319. + * This function is used to submit an I/O Request to an EP.
  88320. + *
  88321. + * - When the request completes the request's completion callback
  88322. + * is called to return the request to the driver.
  88323. + * - An EP, except control EPs, may have multiple requests
  88324. + * pending.
  88325. + * - Once submitted the request cannot be examined or modified.
  88326. + * - Each request is turned into one or more packets.
  88327. + * - A BULK EP can queue any amount of data; the transfer is
  88328. + * packetized.
  88329. + * - Zero length Packets are specified with the request 'zero'
  88330. + * flag.
  88331. + */
  88332. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  88333. + gfp_t gfp_flags)
  88334. +{
  88335. + dwc_otg_pcd_t *pcd;
  88336. + struct dwc_otg_pcd_ep *ep = NULL;
  88337. + int retval = 0, is_isoc_ep = 0;
  88338. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  88339. +
  88340. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  88341. + __func__, usb_ep, usb_req, gfp_flags);
  88342. +
  88343. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  88344. + DWC_WARN("bad params\n");
  88345. + return -EINVAL;
  88346. + }
  88347. +
  88348. + if (!usb_ep) {
  88349. + DWC_WARN("bad ep\n");
  88350. + return -EINVAL;
  88351. + }
  88352. +
  88353. + pcd = gadget_wrapper->pcd;
  88354. + if (!gadget_wrapper->driver ||
  88355. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88356. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  88357. + gadget_wrapper->gadget.speed);
  88358. + DWC_WARN("bogus device state\n");
  88359. + return -ESHUTDOWN;
  88360. + }
  88361. +
  88362. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  88363. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  88364. +
  88365. + usb_req->status = -EINPROGRESS;
  88366. + usb_req->actual = 0;
  88367. +
  88368. + ep = ep_from_handle(pcd, usb_ep);
  88369. + if (ep == NULL)
  88370. + is_isoc_ep = 0;
  88371. + else
  88372. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  88373. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88374. + dma_addr = usb_req->dma;
  88375. +#else
  88376. + if (GET_CORE_IF(pcd)->dma_enable) {
  88377. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  88378. + struct device *dev = NULL;
  88379. +
  88380. + if (otg_dev != NULL)
  88381. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  88382. +
  88383. + if (usb_req->length != 0 &&
  88384. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  88385. + dma_addr = dma_map_single(dev, usb_req->buf,
  88386. + usb_req->length,
  88387. + ep->dwc_ep.is_in ?
  88388. + DMA_TO_DEVICE:
  88389. + DMA_FROM_DEVICE);
  88390. + }
  88391. + }
  88392. +#endif
  88393. +
  88394. +#ifdef DWC_UTE_PER_IO
  88395. + if (is_isoc_ep == 1) {
  88396. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  88397. + usb_req->length, usb_req->zero, usb_req,
  88398. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  88399. + if (retval)
  88400. + return -EINVAL;
  88401. +
  88402. + return 0;
  88403. + }
  88404. +#endif
  88405. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  88406. + usb_req->length, usb_req->zero, usb_req,
  88407. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  88408. + if (retval) {
  88409. + return -EINVAL;
  88410. + }
  88411. +
  88412. + return 0;
  88413. +}
  88414. +
  88415. +/**
  88416. + * This function cancels an I/O request from an EP.
  88417. + */
  88418. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  88419. +{
  88420. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  88421. +
  88422. + if (!usb_ep || !usb_req) {
  88423. + DWC_WARN("bad argument\n");
  88424. + return -EINVAL;
  88425. + }
  88426. + if (!gadget_wrapper->driver ||
  88427. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88428. + DWC_WARN("bogus device state\n");
  88429. + return -ESHUTDOWN;
  88430. + }
  88431. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  88432. + return -EINVAL;
  88433. + }
  88434. +
  88435. + return 0;
  88436. +}
  88437. +
  88438. +/**
  88439. + * usb_ep_set_halt stalls an endpoint.
  88440. + *
  88441. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  88442. + * toggle.
  88443. + *
  88444. + * Both of these functions are implemented with the same underlying
  88445. + * function. The behavior depends on the value argument.
  88446. + *
  88447. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  88448. + * @param[in] value
  88449. + * - 0 means clear_halt.
  88450. + * - 1 means set_halt,
  88451. + * - 2 means clear stall lock flag.
  88452. + * - 3 means set stall lock flag.
  88453. + */
  88454. +static int ep_halt(struct usb_ep *usb_ep, int value)
  88455. +{
  88456. + int retval = 0;
  88457. +
  88458. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  88459. +
  88460. + if (!usb_ep) {
  88461. + DWC_WARN("bad ep\n");
  88462. + return -EINVAL;
  88463. + }
  88464. +
  88465. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  88466. + if (retval == -DWC_E_AGAIN) {
  88467. + return -EAGAIN;
  88468. + } else if (retval) {
  88469. + retval = -EINVAL;
  88470. + }
  88471. +
  88472. + return retval;
  88473. +}
  88474. +
  88475. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  88476. +#if 0
  88477. +/**
  88478. + * ep_wedge: sets the halt feature and ignores clear requests
  88479. + *
  88480. + * @usb_ep: the endpoint being wedged
  88481. + *
  88482. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  88483. + * requests. If the gadget driver clears the halt status, it will
  88484. + * automatically unwedge the endpoint.
  88485. + *
  88486. + * Returns zero on success, else negative errno. *
  88487. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  88488. + */
  88489. +static int ep_wedge(struct usb_ep *usb_ep)
  88490. +{
  88491. + int retval = 0;
  88492. +
  88493. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  88494. +
  88495. + if (!usb_ep) {
  88496. + DWC_WARN("bad ep\n");
  88497. + return -EINVAL;
  88498. + }
  88499. +
  88500. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  88501. + if (retval == -DWC_E_AGAIN) {
  88502. + retval = -EAGAIN;
  88503. + } else if (retval) {
  88504. + retval = -EINVAL;
  88505. + }
  88506. +
  88507. + return retval;
  88508. +}
  88509. +#endif
  88510. +
  88511. +#ifdef DWC_EN_ISOC
  88512. +/**
  88513. + * This function is used to submit an ISOC Transfer Request to an EP.
  88514. + *
  88515. + * - Every time a sync period completes the request's completion callback
  88516. + * is called to provide data to the gadget driver.
  88517. + * - Once submitted the request cannot be modified.
  88518. + * - Each request is turned into periodic data packets untill ISO
  88519. + * Transfer is stopped..
  88520. + */
  88521. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  88522. + gfp_t gfp_flags)
  88523. +{
  88524. + int retval = 0;
  88525. +
  88526. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  88527. + DWC_WARN("bad params\n");
  88528. + return -EINVAL;
  88529. + }
  88530. +
  88531. + if (!usb_ep) {
  88532. + DWC_PRINTF("bad params\n");
  88533. + return -EINVAL;
  88534. + }
  88535. +
  88536. + req->status = -EINPROGRESS;
  88537. +
  88538. + retval =
  88539. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  88540. + req->buf1, req->dma0, req->dma1,
  88541. + req->sync_frame, req->data_pattern_frame,
  88542. + req->data_per_frame,
  88543. + req->
  88544. + flags & USB_REQ_ISO_ASAP ? -1 :
  88545. + req->start_frame, req->buf_proc_intrvl,
  88546. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  88547. +
  88548. + if (retval) {
  88549. + return -EINVAL;
  88550. + }
  88551. +
  88552. + return retval;
  88553. +}
  88554. +
  88555. +/**
  88556. + * This function stops ISO EP Periodic Data Transfer.
  88557. + */
  88558. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  88559. +{
  88560. + int retval = 0;
  88561. + if (!usb_ep) {
  88562. + DWC_WARN("bad ep\n");
  88563. + }
  88564. +
  88565. + if (!gadget_wrapper->driver ||
  88566. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  88567. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  88568. + gadget_wrapper->gadget.speed);
  88569. + DWC_WARN("bogus device state\n");
  88570. + }
  88571. +
  88572. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  88573. + if (retval) {
  88574. + retval = -EINVAL;
  88575. + }
  88576. +
  88577. + return retval;
  88578. +}
  88579. +
  88580. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  88581. + int packets, gfp_t gfp_flags)
  88582. +{
  88583. + struct usb_iso_request *pReq = NULL;
  88584. + uint32_t req_size;
  88585. +
  88586. + req_size = sizeof(struct usb_iso_request);
  88587. + req_size +=
  88588. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  88589. +
  88590. + pReq = kmalloc(req_size, gfp_flags);
  88591. + if (!pReq) {
  88592. + DWC_WARN("Can't allocate Iso Request\n");
  88593. + return 0;
  88594. + }
  88595. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  88596. +
  88597. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  88598. +
  88599. + return pReq;
  88600. +}
  88601. +
  88602. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  88603. +{
  88604. + kfree(req);
  88605. +}
  88606. +
  88607. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  88608. + .ep_ops = {
  88609. + .enable = ep_enable,
  88610. + .disable = ep_disable,
  88611. +
  88612. + .alloc_request = dwc_otg_pcd_alloc_request,
  88613. + .free_request = dwc_otg_pcd_free_request,
  88614. +
  88615. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88616. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  88617. + .free_buffer = dwc_otg_pcd_free_buffer,
  88618. +#endif
  88619. +
  88620. + .queue = ep_queue,
  88621. + .dequeue = ep_dequeue,
  88622. +
  88623. + .set_halt = ep_halt,
  88624. + .fifo_status = 0,
  88625. + .fifo_flush = 0,
  88626. + },
  88627. + .iso_ep_start = iso_ep_start,
  88628. + .iso_ep_stop = iso_ep_stop,
  88629. + .alloc_iso_request = alloc_iso_request,
  88630. + .free_iso_request = free_iso_request,
  88631. +};
  88632. +
  88633. +#else
  88634. +
  88635. + int (*enable) (struct usb_ep *ep,
  88636. + const struct usb_endpoint_descriptor *desc);
  88637. + int (*disable) (struct usb_ep *ep);
  88638. +
  88639. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  88640. + gfp_t gfp_flags);
  88641. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  88642. +
  88643. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  88644. + gfp_t gfp_flags);
  88645. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  88646. +
  88647. + int (*set_halt) (struct usb_ep *ep, int value);
  88648. + int (*set_wedge) (struct usb_ep *ep);
  88649. +
  88650. + int (*fifo_status) (struct usb_ep *ep);
  88651. + void (*fifo_flush) (struct usb_ep *ep);
  88652. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  88653. + .enable = ep_enable,
  88654. + .disable = ep_disable,
  88655. +
  88656. + .alloc_request = dwc_otg_pcd_alloc_request,
  88657. + .free_request = dwc_otg_pcd_free_request,
  88658. +
  88659. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88660. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  88661. + .free_buffer = dwc_otg_pcd_free_buffer,
  88662. +#else
  88663. + /* .set_wedge = ep_wedge, */
  88664. + .set_wedge = NULL, /* uses set_halt instead */
  88665. +#endif
  88666. +
  88667. + .queue = ep_queue,
  88668. + .dequeue = ep_dequeue,
  88669. +
  88670. + .set_halt = ep_halt,
  88671. + .fifo_status = 0,
  88672. + .fifo_flush = 0,
  88673. +
  88674. +};
  88675. +
  88676. +#endif /* _EN_ISOC_ */
  88677. +/* Gadget Operations */
  88678. +/**
  88679. + * The following gadget operations will be implemented in the DWC_otg
  88680. + * PCD. Functions in the API that are not described below are not
  88681. + * implemented.
  88682. + *
  88683. + * The Gadget API provides wrapper functions for each of the function
  88684. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  88685. + * wrapper function, which then calls the underlying PCD function. The
  88686. + * following sections are named according to the wrapper functions
  88687. + * (except for ioctl, which doesn't have a wrapper function). Within
  88688. + * each section, the corresponding DWC_otg PCD function name is
  88689. + * specified.
  88690. + *
  88691. + */
  88692. +
  88693. +/**
  88694. + *Gets the USB Frame number of the last SOF.
  88695. + */
  88696. +static int get_frame_number(struct usb_gadget *gadget)
  88697. +{
  88698. + struct gadget_wrapper *d;
  88699. +
  88700. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  88701. +
  88702. + if (gadget == 0) {
  88703. + return -ENODEV;
  88704. + }
  88705. +
  88706. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88707. + return dwc_otg_pcd_get_frame_number(d->pcd);
  88708. +}
  88709. +
  88710. +#ifdef CONFIG_USB_DWC_OTG_LPM
  88711. +static int test_lpm_enabled(struct usb_gadget *gadget)
  88712. +{
  88713. + struct gadget_wrapper *d;
  88714. +
  88715. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88716. +
  88717. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  88718. +}
  88719. +#endif
  88720. +
  88721. +/**
  88722. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  88723. + * session is in progress. If a session is already in progress, but
  88724. + * the device is suspended, remote wakeup signaling is started.
  88725. + *
  88726. + */
  88727. +static int wakeup(struct usb_gadget *gadget)
  88728. +{
  88729. + struct gadget_wrapper *d;
  88730. +
  88731. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  88732. +
  88733. + if (gadget == 0) {
  88734. + return -ENODEV;
  88735. + } else {
  88736. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88737. + }
  88738. + dwc_otg_pcd_wakeup(d->pcd);
  88739. + return 0;
  88740. +}
  88741. +
  88742. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  88743. + .get_frame = get_frame_number,
  88744. + .wakeup = wakeup,
  88745. +#ifdef CONFIG_USB_DWC_OTG_LPM
  88746. + .lpm_support = test_lpm_enabled,
  88747. +#endif
  88748. + // current versions must always be self-powered
  88749. +};
  88750. +
  88751. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  88752. +{
  88753. + int retval = -DWC_E_NOT_SUPPORTED;
  88754. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  88755. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  88756. + (struct usb_ctrlrequest
  88757. + *)bytes);
  88758. + }
  88759. +
  88760. + if (retval == -ENOTSUPP) {
  88761. + retval = -DWC_E_NOT_SUPPORTED;
  88762. + } else if (retval < 0) {
  88763. + retval = -DWC_E_INVALID;
  88764. + }
  88765. +
  88766. + return retval;
  88767. +}
  88768. +
  88769. +#ifdef DWC_EN_ISOC
  88770. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88771. + void *req_handle, int proc_buf_num)
  88772. +{
  88773. + int i, packet_count;
  88774. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  88775. + struct usb_iso_request *iso_req = req_handle;
  88776. +
  88777. + if (proc_buf_num) {
  88778. + iso_packet = iso_req->iso_packet_desc1;
  88779. + } else {
  88780. + iso_packet = iso_req->iso_packet_desc0;
  88781. + }
  88782. + packet_count =
  88783. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  88784. + for (i = 0; i < packet_count; ++i) {
  88785. + int status;
  88786. + int actual;
  88787. + int offset;
  88788. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  88789. + i, &status, &actual, &offset);
  88790. + switch (status) {
  88791. + case -DWC_E_NO_DATA:
  88792. + status = -ENODATA;
  88793. + break;
  88794. + default:
  88795. + if (status) {
  88796. + DWC_PRINTF("unknown status in isoc packet\n");
  88797. + }
  88798. +
  88799. + }
  88800. + iso_packet[i].status = status;
  88801. + iso_packet[i].offset = offset;
  88802. + iso_packet[i].actual_length = actual;
  88803. + }
  88804. +
  88805. + iso_req->status = 0;
  88806. + iso_req->process_buffer(ep_handle, iso_req);
  88807. +
  88808. + return 0;
  88809. +}
  88810. +#endif /* DWC_EN_ISOC */
  88811. +
  88812. +#ifdef DWC_UTE_PER_IO
  88813. +/**
  88814. + * Copy the contents of the extended request to the Linux usb_request's
  88815. + * extended part and call the gadget's completion.
  88816. + *
  88817. + * @param pcd Pointer to the pcd structure
  88818. + * @param ep_handle Void pointer to the usb_ep structure
  88819. + * @param req_handle Void pointer to the usb_request structure
  88820. + * @param status Request status returned from the portable logic
  88821. + * @param ereq_port Void pointer to the extended request structure
  88822. + * created in the the portable part that contains the
  88823. + * results of the processed iso packets.
  88824. + */
  88825. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88826. + void *req_handle, int32_t status, void *ereq_port)
  88827. +{
  88828. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  88829. + struct dwc_iso_xreq_port *ereqport = NULL;
  88830. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  88831. + int i;
  88832. + struct usb_request *req;
  88833. + //struct dwc_ute_iso_packet_descriptor *
  88834. + //int status = 0;
  88835. +
  88836. + req = (struct usb_request *)req_handle;
  88837. + ereqorg = &req->ext_req;
  88838. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  88839. + desc_org = ereqorg->per_io_frame_descs;
  88840. +
  88841. + if (req && req->complete) {
  88842. + /* Copy the request data from the portable logic to our request */
  88843. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  88844. + desc_org[i].actual_length =
  88845. + ereqport->per_io_frame_descs[i].actual_length;
  88846. + desc_org[i].status =
  88847. + ereqport->per_io_frame_descs[i].status;
  88848. + }
  88849. +
  88850. + switch (status) {
  88851. + case -DWC_E_SHUTDOWN:
  88852. + req->status = -ESHUTDOWN;
  88853. + break;
  88854. + case -DWC_E_RESTART:
  88855. + req->status = -ECONNRESET;
  88856. + break;
  88857. + case -DWC_E_INVALID:
  88858. + req->status = -EINVAL;
  88859. + break;
  88860. + case -DWC_E_TIMEOUT:
  88861. + req->status = -ETIMEDOUT;
  88862. + break;
  88863. + default:
  88864. + req->status = status;
  88865. + }
  88866. +
  88867. + /* And call the gadget's completion */
  88868. + req->complete(ep_handle, req);
  88869. + }
  88870. +
  88871. + return 0;
  88872. +}
  88873. +#endif /* DWC_UTE_PER_IO */
  88874. +
  88875. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88876. + void *req_handle, int32_t status, uint32_t actual)
  88877. +{
  88878. + struct usb_request *req = (struct usb_request *)req_handle;
  88879. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  88880. + struct dwc_otg_pcd_ep *ep = NULL;
  88881. +#endif
  88882. +
  88883. + if (req && req->complete) {
  88884. + switch (status) {
  88885. + case -DWC_E_SHUTDOWN:
  88886. + req->status = -ESHUTDOWN;
  88887. + break;
  88888. + case -DWC_E_RESTART:
  88889. + req->status = -ECONNRESET;
  88890. + break;
  88891. + case -DWC_E_INVALID:
  88892. + req->status = -EINVAL;
  88893. + break;
  88894. + case -DWC_E_TIMEOUT:
  88895. + req->status = -ETIMEDOUT;
  88896. + break;
  88897. + default:
  88898. + req->status = status;
  88899. +
  88900. + }
  88901. +
  88902. + req->actual = actual;
  88903. + DWC_SPINUNLOCK(pcd->lock);
  88904. + req->complete(ep_handle, req);
  88905. + DWC_SPINLOCK(pcd->lock);
  88906. + }
  88907. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  88908. + ep = ep_from_handle(pcd, ep_handle);
  88909. + if (GET_CORE_IF(pcd)->dma_enable) {
  88910. + if (req->length != 0) {
  88911. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  88912. + struct device *dev = NULL;
  88913. +
  88914. + if (otg_dev != NULL)
  88915. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  88916. +
  88917. + dma_unmap_single(dev, req->dma, req->length,
  88918. + ep->dwc_ep.is_in ?
  88919. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  88920. + }
  88921. + }
  88922. +#endif
  88923. +
  88924. + return 0;
  88925. +}
  88926. +
  88927. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  88928. +{
  88929. + gadget_wrapper->gadget.speed = speed;
  88930. + return 0;
  88931. +}
  88932. +
  88933. +static int _disconnect(dwc_otg_pcd_t * pcd)
  88934. +{
  88935. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  88936. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  88937. + }
  88938. + return 0;
  88939. +}
  88940. +
  88941. +static int _resume(dwc_otg_pcd_t * pcd)
  88942. +{
  88943. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  88944. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  88945. + }
  88946. +
  88947. + return 0;
  88948. +}
  88949. +
  88950. +static int _suspend(dwc_otg_pcd_t * pcd)
  88951. +{
  88952. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  88953. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  88954. + }
  88955. + return 0;
  88956. +}
  88957. +
  88958. +/**
  88959. + * This function updates the otg values in the gadget structure.
  88960. + */
  88961. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  88962. +{
  88963. +
  88964. + if (!gadget_wrapper->gadget.is_otg)
  88965. + return 0;
  88966. +
  88967. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  88968. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  88969. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  88970. + return 0;
  88971. +}
  88972. +
  88973. +static int _reset(dwc_otg_pcd_t * pcd)
  88974. +{
  88975. + return 0;
  88976. +}
  88977. +
  88978. +#ifdef DWC_UTE_CFI
  88979. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  88980. +{
  88981. + int retval = -DWC_E_INVALID;
  88982. + if (gadget_wrapper->driver->cfi_feature_setup) {
  88983. + retval =
  88984. + gadget_wrapper->driver->
  88985. + cfi_feature_setup(&gadget_wrapper->gadget,
  88986. + (struct cfi_usb_ctrlrequest *)cfi_req);
  88987. + }
  88988. +
  88989. + return retval;
  88990. +}
  88991. +#endif
  88992. +
  88993. +static const struct dwc_otg_pcd_function_ops fops = {
  88994. + .complete = _complete,
  88995. +#ifdef DWC_EN_ISOC
  88996. + .isoc_complete = _isoc_complete,
  88997. +#endif
  88998. + .setup = _setup,
  88999. + .disconnect = _disconnect,
  89000. + .connect = _connect,
  89001. + .resume = _resume,
  89002. + .suspend = _suspend,
  89003. + .hnp_changed = _hnp_changed,
  89004. + .reset = _reset,
  89005. +#ifdef DWC_UTE_CFI
  89006. + .cfi_setup = _cfi_setup,
  89007. +#endif
  89008. +#ifdef DWC_UTE_PER_IO
  89009. + .xisoc_complete = _xisoc_complete,
  89010. +#endif
  89011. +};
  89012. +
  89013. +/**
  89014. + * This function is the top level PCD interrupt handler.
  89015. + */
  89016. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  89017. +{
  89018. + dwc_otg_pcd_t *pcd = dev;
  89019. + int32_t retval = IRQ_NONE;
  89020. +
  89021. + retval = dwc_otg_pcd_handle_intr(pcd);
  89022. + if (retval != 0) {
  89023. + S3C2410X_CLEAR_EINTPEND();
  89024. + }
  89025. + return IRQ_RETVAL(retval);
  89026. +}
  89027. +
  89028. +/**
  89029. + * This function initialized the usb_ep structures to there default
  89030. + * state.
  89031. + *
  89032. + * @param d Pointer on gadget_wrapper.
  89033. + */
  89034. +void gadget_add_eps(struct gadget_wrapper *d)
  89035. +{
  89036. + static const char *names[] = {
  89037. +
  89038. + "ep0",
  89039. + "ep1in",
  89040. + "ep2in",
  89041. + "ep3in",
  89042. + "ep4in",
  89043. + "ep5in",
  89044. + "ep6in",
  89045. + "ep7in",
  89046. + "ep8in",
  89047. + "ep9in",
  89048. + "ep10in",
  89049. + "ep11in",
  89050. + "ep12in",
  89051. + "ep13in",
  89052. + "ep14in",
  89053. + "ep15in",
  89054. + "ep1out",
  89055. + "ep2out",
  89056. + "ep3out",
  89057. + "ep4out",
  89058. + "ep5out",
  89059. + "ep6out",
  89060. + "ep7out",
  89061. + "ep8out",
  89062. + "ep9out",
  89063. + "ep10out",
  89064. + "ep11out",
  89065. + "ep12out",
  89066. + "ep13out",
  89067. + "ep14out",
  89068. + "ep15out"
  89069. + };
  89070. +
  89071. + int i;
  89072. + struct usb_ep *ep;
  89073. + int8_t dev_endpoints;
  89074. +
  89075. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  89076. +
  89077. + INIT_LIST_HEAD(&d->gadget.ep_list);
  89078. + d->gadget.ep0 = &d->ep0;
  89079. + d->gadget.speed = USB_SPEED_UNKNOWN;
  89080. +
  89081. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  89082. +
  89083. + /**
  89084. + * Initialize the EP0 structure.
  89085. + */
  89086. + ep = &d->ep0;
  89087. +
  89088. + /* Init the usb_ep structure. */
  89089. + ep->name = names[0];
  89090. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  89091. +
  89092. + /**
  89093. + * @todo NGS: What should the max packet size be set to
  89094. + * here? Before EP type is set?
  89095. + */
  89096. + ep->maxpacket = MAX_PACKET_SIZE;
  89097. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  89098. +
  89099. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  89100. +
  89101. + /**
  89102. + * Initialize the EP structures.
  89103. + */
  89104. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  89105. +
  89106. + for (i = 0; i < dev_endpoints; i++) {
  89107. + ep = &d->in_ep[i];
  89108. +
  89109. + /* Init the usb_ep structure. */
  89110. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  89111. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  89112. +
  89113. + /**
  89114. + * @todo NGS: What should the max packet size be set to
  89115. + * here? Before EP type is set?
  89116. + */
  89117. + ep->maxpacket = MAX_PACKET_SIZE;
  89118. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  89119. + }
  89120. +
  89121. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  89122. +
  89123. + for (i = 0; i < dev_endpoints; i++) {
  89124. + ep = &d->out_ep[i];
  89125. +
  89126. + /* Init the usb_ep structure. */
  89127. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  89128. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  89129. +
  89130. + /**
  89131. + * @todo NGS: What should the max packet size be set to
  89132. + * here? Before EP type is set?
  89133. + */
  89134. + ep->maxpacket = MAX_PACKET_SIZE;
  89135. +
  89136. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  89137. + }
  89138. +
  89139. + /* remove ep0 from the list. There is a ep0 pointer. */
  89140. + list_del_init(&d->ep0.ep_list);
  89141. +
  89142. + d->ep0.maxpacket = MAX_EP0_SIZE;
  89143. +}
  89144. +
  89145. +/**
  89146. + * This function releases the Gadget device.
  89147. + * required by device_unregister().
  89148. + *
  89149. + * @todo Should this do something? Should it free the PCD?
  89150. + */
  89151. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  89152. +{
  89153. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  89154. +}
  89155. +
  89156. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  89157. +{
  89158. + static char pcd_name[] = "dwc_otg_pcd";
  89159. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  89160. + struct gadget_wrapper *d;
  89161. + int retval;
  89162. +
  89163. + d = DWC_ALLOC(sizeof(*d));
  89164. + if (d == NULL) {
  89165. + return NULL;
  89166. + }
  89167. +
  89168. + memset(d, 0, sizeof(*d));
  89169. +
  89170. + d->gadget.name = pcd_name;
  89171. + d->pcd = otg_dev->pcd;
  89172. +
  89173. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  89174. + strcpy(d->gadget.dev.bus_id, "gadget");
  89175. +#else
  89176. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  89177. +#endif
  89178. +
  89179. + d->gadget.dev.parent = &_dev->dev;
  89180. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  89181. + d->gadget.ops = &dwc_otg_pcd_ops;
  89182. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  89183. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  89184. +
  89185. + d->driver = 0;
  89186. + /* Register the gadget device */
  89187. + retval = device_register(&d->gadget.dev);
  89188. + if (retval != 0) {
  89189. + DWC_ERROR("device_register failed\n");
  89190. + DWC_FREE(d);
  89191. + return NULL;
  89192. + }
  89193. +
  89194. + return d;
  89195. +}
  89196. +
  89197. +static void free_wrapper(struct gadget_wrapper *d)
  89198. +{
  89199. + if (d->driver) {
  89200. + /* should have been done already by driver model core */
  89201. + DWC_WARN("driver '%s' is still registered\n",
  89202. + d->driver->driver.name);
  89203. + usb_gadget_unregister_driver(d->driver);
  89204. + }
  89205. +
  89206. + device_unregister(&d->gadget.dev);
  89207. + DWC_FREE(d);
  89208. +}
  89209. +
  89210. +/**
  89211. + * This function initialized the PCD portion of the driver.
  89212. + *
  89213. + */
  89214. +int pcd_init(dwc_bus_dev_t *_dev)
  89215. +{
  89216. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  89217. + int retval = 0;
  89218. +
  89219. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  89220. +
  89221. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  89222. +
  89223. + if (!otg_dev->pcd) {
  89224. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  89225. + return -ENOMEM;
  89226. + }
  89227. +
  89228. + otg_dev->pcd->otg_dev = otg_dev;
  89229. + gadget_wrapper = alloc_wrapper(_dev);
  89230. +
  89231. + /*
  89232. + * Initialize EP structures
  89233. + */
  89234. + gadget_add_eps(gadget_wrapper);
  89235. + /*
  89236. + * Setup interupt handler
  89237. + */
  89238. +#ifdef PLATFORM_INTERFACE
  89239. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  89240. + platform_get_irq(_dev, 0));
  89241. + retval = request_irq(platform_get_irq(_dev, 0), dwc_otg_pcd_irq,
  89242. + IRQF_SHARED, gadget_wrapper->gadget.name,
  89243. + otg_dev->pcd);
  89244. + if (retval != 0) {
  89245. + DWC_ERROR("request of irq%d failed\n",
  89246. + platform_get_irq(_dev, 0));
  89247. + free_wrapper(gadget_wrapper);
  89248. + return -EBUSY;
  89249. + }
  89250. +#else
  89251. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  89252. + _dev->irq);
  89253. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  89254. + IRQF_SHARED | IRQF_DISABLED,
  89255. + gadget_wrapper->gadget.name, otg_dev->pcd);
  89256. + if (retval != 0) {
  89257. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  89258. + free_wrapper(gadget_wrapper);
  89259. + return -EBUSY;
  89260. + }
  89261. +#endif
  89262. +
  89263. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  89264. +
  89265. + return retval;
  89266. +}
  89267. +
  89268. +/**
  89269. + * Cleanup the PCD.
  89270. + */
  89271. +void pcd_remove(dwc_bus_dev_t *_dev)
  89272. +{
  89273. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  89274. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  89275. +
  89276. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  89277. +
  89278. + /*
  89279. + * Free the IRQ
  89280. + */
  89281. +#ifdef PLATFORM_INTERFACE
  89282. + free_irq(platform_get_irq(_dev, 0), pcd);
  89283. +#else
  89284. + free_irq(_dev->irq, pcd);
  89285. +#endif
  89286. + dwc_otg_pcd_remove(otg_dev->pcd);
  89287. + free_wrapper(gadget_wrapper);
  89288. + otg_dev->pcd = 0;
  89289. +}
  89290. +
  89291. +/**
  89292. + * This function registers a gadget driver with the PCD.
  89293. + *
  89294. + * When a driver is successfully registered, it will receive control
  89295. + * requests including set_configuration(), which enables non-control
  89296. + * requests. then usb traffic follows until a disconnect is reported.
  89297. + * then a host may connect again, or the driver might get unbound.
  89298. + *
  89299. + * @param driver The driver being registered
  89300. + * @param bind The bind function of gadget driver
  89301. + */
  89302. +
  89303. +int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  89304. +{
  89305. + int retval;
  89306. +
  89307. + DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  89308. + driver->driver.name);
  89309. +
  89310. + if (!driver || driver->max_speed == USB_SPEED_UNKNOWN ||
  89311. + !driver->bind ||
  89312. + !driver->unbind || !driver->disconnect || !driver->setup) {
  89313. + DWC_DEBUGPL(DBG_PCDV, "EINVAL\n");
  89314. + return -EINVAL;
  89315. + }
  89316. + if (gadget_wrapper == 0) {
  89317. + DWC_DEBUGPL(DBG_PCDV, "ENODEV\n");
  89318. + return -ENODEV;
  89319. + }
  89320. + if (gadget_wrapper->driver != 0) {
  89321. + DWC_DEBUGPL(DBG_PCDV, "EBUSY (%p)\n", gadget_wrapper->driver);
  89322. + return -EBUSY;
  89323. + }
  89324. +
  89325. + /* hook up the driver */
  89326. + gadget_wrapper->driver = driver;
  89327. + gadget_wrapper->gadget.dev.driver = &driver->driver;
  89328. +
  89329. + DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  89330. + retval = driver->bind(&gadget_wrapper->gadget, gadget_wrapper->driver);
  89331. + if (retval) {
  89332. + DWC_ERROR("bind to driver %s --> error %d\n",
  89333. + driver->driver.name, retval);
  89334. + gadget_wrapper->driver = 0;
  89335. + gadget_wrapper->gadget.dev.driver = 0;
  89336. + return retval;
  89337. + }
  89338. + DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  89339. + driver->driver.name);
  89340. + return 0;
  89341. +}
  89342. +EXPORT_SYMBOL(usb_gadget_probe_driver);
  89343. +
  89344. +/**
  89345. + * This function unregisters a gadget driver
  89346. + *
  89347. + * @param driver The driver being unregistered
  89348. + */
  89349. +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  89350. +{
  89351. + //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  89352. +
  89353. + if (gadget_wrapper == 0) {
  89354. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  89355. + -ENODEV);
  89356. + return -ENODEV;
  89357. + }
  89358. + if (driver == 0 || driver != gadget_wrapper->driver) {
  89359. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  89360. + -EINVAL);
  89361. + return -EINVAL;
  89362. + }
  89363. +
  89364. + driver->unbind(&gadget_wrapper->gadget);
  89365. + gadget_wrapper->driver = 0;
  89366. +
  89367. + DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  89368. + return 0;
  89369. +}
  89370. +
  89371. +EXPORT_SYMBOL(usb_gadget_unregister_driver);
  89372. +
  89373. +#endif /* DWC_HOST_ONLY */
  89374. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_regs.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  89375. --- linux-3.10.37/drivers/usb/host/dwc_otg/dwc_otg_regs.h 1970-01-01 01:00:00.000000000 +0100
  89376. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h 2014-04-24 15:35:04.177565820 +0200
  89377. @@ -0,0 +1,2550 @@
  89378. +/* ==========================================================================
  89379. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  89380. + * $Revision: #98 $
  89381. + * $Date: 2012/08/10 $
  89382. + * $Change: 2047372 $
  89383. + *
  89384. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  89385. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  89386. + * otherwise expressly agreed to in writing between Synopsys and you.
  89387. + *
  89388. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  89389. + * any End User Software License Agreement or Agreement for Licensed Product
  89390. + * with Synopsys or any supplement thereto. You are permitted to use and
  89391. + * redistribute this Software in source and binary forms, with or without
  89392. + * modification, provided that redistributions of source code must retain this
  89393. + * notice. You may not view, use, disclose, copy or distribute this file or
  89394. + * any information contained herein except pursuant to this license grant from
  89395. + * Synopsys. If you do not agree with this notice, including the disclaimer
  89396. + * below, then you are not authorized to use the Software.
  89397. + *
  89398. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  89399. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  89400. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  89401. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  89402. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  89403. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89404. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  89405. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  89406. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  89407. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  89408. + * DAMAGE.
  89409. + * ========================================================================== */
  89410. +
  89411. +#ifndef __DWC_OTG_REGS_H__
  89412. +#define __DWC_OTG_REGS_H__
  89413. +
  89414. +#include "dwc_otg_core_if.h"
  89415. +
  89416. +/**
  89417. + * @file
  89418. + *
  89419. + * This file contains the data structures for accessing the DWC_otg core registers.
  89420. + *
  89421. + * The application interfaces with the HS OTG core by reading from and
  89422. + * writing to the Control and Status Register (CSR) space through the
  89423. + * AHB Slave interface. These registers are 32 bits wide, and the
  89424. + * addresses are 32-bit-block aligned.
  89425. + * CSRs are classified as follows:
  89426. + * - Core Global Registers
  89427. + * - Device Mode Registers
  89428. + * - Device Global Registers
  89429. + * - Device Endpoint Specific Registers
  89430. + * - Host Mode Registers
  89431. + * - Host Global Registers
  89432. + * - Host Port CSRs
  89433. + * - Host Channel Specific Registers
  89434. + *
  89435. + * Only the Core Global registers can be accessed in both Device and
  89436. + * Host modes. When the HS OTG core is operating in one mode, either
  89437. + * Device or Host, the application must not access registers from the
  89438. + * other mode. When the core switches from one mode to another, the
  89439. + * registers in the new mode of operation must be reprogrammed as they
  89440. + * would be after a power-on reset.
  89441. + */
  89442. +
  89443. +/****************************************************************************/
  89444. +/** DWC_otg Core registers .
  89445. + * The dwc_otg_core_global_regs structure defines the size
  89446. + * and relative field offsets for the Core Global registers.
  89447. + */
  89448. +typedef struct dwc_otg_core_global_regs {
  89449. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  89450. + volatile uint32_t gotgctl;
  89451. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  89452. + volatile uint32_t gotgint;
  89453. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  89454. + volatile uint32_t gahbcfg;
  89455. +
  89456. +#define DWC_GLBINTRMASK 0x0001
  89457. +#define DWC_DMAENABLE 0x0020
  89458. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  89459. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  89460. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  89461. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  89462. +
  89463. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  89464. + volatile uint32_t gusbcfg;
  89465. + /**Core Reset Register. <i>Offset: 010h</i> */
  89466. + volatile uint32_t grstctl;
  89467. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  89468. + volatile uint32_t gintsts;
  89469. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  89470. + volatile uint32_t gintmsk;
  89471. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  89472. + volatile uint32_t grxstsr;
  89473. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  89474. + volatile uint32_t grxstsp;
  89475. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  89476. + volatile uint32_t grxfsiz;
  89477. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  89478. + volatile uint32_t gnptxfsiz;
  89479. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  89480. + * Only). <i>Offset: 02Ch</i> */
  89481. + volatile uint32_t gnptxsts;
  89482. + /**I2C Access Register. <i>Offset: 030h</i> */
  89483. + volatile uint32_t gi2cctl;
  89484. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  89485. + volatile uint32_t gpvndctl;
  89486. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  89487. + volatile uint32_t ggpio;
  89488. + /**User ID Register. <i>Offset: 03Ch</i> */
  89489. + volatile uint32_t guid;
  89490. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  89491. + volatile uint32_t gsnpsid;
  89492. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  89493. + volatile uint32_t ghwcfg1;
  89494. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  89495. + volatile uint32_t ghwcfg2;
  89496. +#define DWC_SLAVE_ONLY_ARCH 0
  89497. +#define DWC_EXT_DMA_ARCH 1
  89498. +#define DWC_INT_DMA_ARCH 2
  89499. +
  89500. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  89501. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  89502. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  89503. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  89504. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  89505. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  89506. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  89507. +
  89508. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  89509. + volatile uint32_t ghwcfg3;
  89510. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  89511. + volatile uint32_t ghwcfg4;
  89512. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  89513. + volatile uint32_t glpmcfg;
  89514. + /** Global PowerDn Register <i>Offset: 058h</i> */
  89515. + volatile uint32_t gpwrdn;
  89516. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  89517. + volatile uint32_t gdfifocfg;
  89518. + /** ADP Control Register <i>Offset: 060h</i> */
  89519. + volatile uint32_t adpctl;
  89520. + /** Reserved <i>Offset: 064h-0FFh</i> */
  89521. + volatile uint32_t reserved39[39];
  89522. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  89523. + volatile uint32_t hptxfsiz;
  89524. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  89525. + otherwise Device Transmit FIFO#n Register.
  89526. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  89527. + volatile uint32_t dtxfsiz[15];
  89528. +} dwc_otg_core_global_regs_t;
  89529. +
  89530. +/**
  89531. + * This union represents the bit fields of the Core OTG Control
  89532. + * and Status Register (GOTGCTL). Set the bits using the bit
  89533. + * fields then write the <i>d32</i> value to the register.
  89534. + */
  89535. +typedef union gotgctl_data {
  89536. + /** raw register data */
  89537. + uint32_t d32;
  89538. + /** register bits */
  89539. + struct {
  89540. + unsigned sesreqscs:1;
  89541. + unsigned sesreq:1;
  89542. + unsigned vbvalidoven:1;
  89543. + unsigned vbvalidovval:1;
  89544. + unsigned avalidoven:1;
  89545. + unsigned avalidovval:1;
  89546. + unsigned bvalidoven:1;
  89547. + unsigned bvalidovval:1;
  89548. + unsigned hstnegscs:1;
  89549. + unsigned hnpreq:1;
  89550. + unsigned hstsethnpen:1;
  89551. + unsigned devhnpen:1;
  89552. + unsigned reserved12_15:4;
  89553. + unsigned conidsts:1;
  89554. + unsigned dbnctime:1;
  89555. + unsigned asesvld:1;
  89556. + unsigned bsesvld:1;
  89557. + unsigned otgver:1;
  89558. + unsigned reserved1:1;
  89559. + unsigned multvalidbc:5;
  89560. + unsigned chirpen:1;
  89561. + unsigned reserved28_31:4;
  89562. + } b;
  89563. +} gotgctl_data_t;
  89564. +
  89565. +/**
  89566. + * This union represents the bit fields of the Core OTG Interrupt Register
  89567. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  89568. + * value to the register.
  89569. + */
  89570. +typedef union gotgint_data {
  89571. + /** raw register data */
  89572. + uint32_t d32;
  89573. + /** register bits */
  89574. + struct {
  89575. + /** Current Mode */
  89576. + unsigned reserved0_1:2;
  89577. +
  89578. + /** Session End Detected */
  89579. + unsigned sesenddet:1;
  89580. +
  89581. + unsigned reserved3_7:5;
  89582. +
  89583. + /** Session Request Success Status Change */
  89584. + unsigned sesreqsucstschng:1;
  89585. + /** Host Negotiation Success Status Change */
  89586. + unsigned hstnegsucstschng:1;
  89587. +
  89588. + unsigned reserved10_16:7;
  89589. +
  89590. + /** Host Negotiation Detected */
  89591. + unsigned hstnegdet:1;
  89592. + /** A-Device Timeout Change */
  89593. + unsigned adevtoutchng:1;
  89594. + /** Debounce Done */
  89595. + unsigned debdone:1;
  89596. + /** Multi-Valued input changed */
  89597. + unsigned mvic:1;
  89598. +
  89599. + unsigned reserved31_21:11;
  89600. +
  89601. + } b;
  89602. +} gotgint_data_t;
  89603. +
  89604. +/**
  89605. + * This union represents the bit fields of the Core AHB Configuration
  89606. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  89607. + * write the <i>d32</i> value to the register.
  89608. + */
  89609. +typedef union gahbcfg_data {
  89610. + /** raw register data */
  89611. + uint32_t d32;
  89612. + /** register bits */
  89613. + struct {
  89614. + unsigned glblintrmsk:1;
  89615. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  89616. +
  89617. + unsigned hburstlen:4;
  89618. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  89619. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  89620. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  89621. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  89622. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  89623. +
  89624. + unsigned dmaenable:1;
  89625. +#define DWC_GAHBCFG_DMAENABLE 1
  89626. + unsigned reserved:1;
  89627. + unsigned nptxfemplvl_txfemplvl:1;
  89628. + unsigned ptxfemplvl:1;
  89629. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  89630. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  89631. + unsigned reserved9_20:12;
  89632. + unsigned remmemsupp:1;
  89633. + unsigned notialldmawrit:1;
  89634. + unsigned ahbsingle:1;
  89635. + unsigned reserved24_31:8;
  89636. + } b;
  89637. +} gahbcfg_data_t;
  89638. +
  89639. +/**
  89640. + * This union represents the bit fields of the Core USB Configuration
  89641. + * Register (GUSBCFG). Set the bits using the bit fields then write
  89642. + * the <i>d32</i> value to the register.
  89643. + */
  89644. +typedef union gusbcfg_data {
  89645. + /** raw register data */
  89646. + uint32_t d32;
  89647. + /** register bits */
  89648. + struct {
  89649. + unsigned toutcal:3;
  89650. + unsigned phyif:1;
  89651. + unsigned ulpi_utmi_sel:1;
  89652. + unsigned fsintf:1;
  89653. + unsigned physel:1;
  89654. + unsigned ddrsel:1;
  89655. + unsigned srpcap:1;
  89656. + unsigned hnpcap:1;
  89657. + unsigned usbtrdtim:4;
  89658. + unsigned reserved1:1;
  89659. + unsigned phylpwrclksel:1;
  89660. + unsigned otgutmifssel:1;
  89661. + unsigned ulpi_fsls:1;
  89662. + unsigned ulpi_auto_res:1;
  89663. + unsigned ulpi_clk_sus_m:1;
  89664. + unsigned ulpi_ext_vbus_drv:1;
  89665. + unsigned ulpi_int_vbus_indicator:1;
  89666. + unsigned term_sel_dl_pulse:1;
  89667. + unsigned indicator_complement:1;
  89668. + unsigned indicator_pass_through:1;
  89669. + unsigned ulpi_int_prot_dis:1;
  89670. + unsigned ic_usb_cap:1;
  89671. + unsigned ic_traffic_pull_remove:1;
  89672. + unsigned tx_end_delay:1;
  89673. + unsigned force_host_mode:1;
  89674. + unsigned force_dev_mode:1;
  89675. + unsigned reserved31:1;
  89676. + } b;
  89677. +} gusbcfg_data_t;
  89678. +
  89679. +/**
  89680. + * This union represents the bit fields of the Core Reset Register
  89681. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  89682. + * <i>d32</i> value to the register.
  89683. + */
  89684. +typedef union grstctl_data {
  89685. + /** raw register data */
  89686. + uint32_t d32;
  89687. + /** register bits */
  89688. + struct {
  89689. + /** Core Soft Reset (CSftRst) (Device and Host)
  89690. + *
  89691. + * The application can flush the control logic in the
  89692. + * entire core using this bit. This bit resets the
  89693. + * pipelines in the AHB Clock domain as well as the
  89694. + * PHY Clock domain.
  89695. + *
  89696. + * The state machines are reset to an IDLE state, the
  89697. + * control bits in the CSRs are cleared, all the
  89698. + * transmit FIFOs and the receive FIFO are flushed.
  89699. + *
  89700. + * The status mask bits that control the generation of
  89701. + * the interrupt, are cleared, to clear the
  89702. + * interrupt. The interrupt status bits are not
  89703. + * cleared, so the application can get the status of
  89704. + * any events that occurred in the core after it has
  89705. + * set this bit.
  89706. + *
  89707. + * Any transactions on the AHB are terminated as soon
  89708. + * as possible following the protocol. Any
  89709. + * transactions on the USB are terminated immediately.
  89710. + *
  89711. + * The configuration settings in the CSRs are
  89712. + * unchanged, so the software doesn't have to
  89713. + * reprogram these registers (Device
  89714. + * Configuration/Host Configuration/Core System
  89715. + * Configuration/Core PHY Configuration).
  89716. + *
  89717. + * The application can write to this bit, any time it
  89718. + * wants to reset the core. This is a self clearing
  89719. + * bit and the core clears this bit after all the
  89720. + * necessary logic is reset in the core, which may
  89721. + * take several clocks, depending on the current state
  89722. + * of the core.
  89723. + */
  89724. + unsigned csftrst:1;
  89725. + /** Hclk Soft Reset
  89726. + *
  89727. + * The application uses this bit to reset the control logic in
  89728. + * the AHB clock domain. Only AHB clock domain pipelines are
  89729. + * reset.
  89730. + */
  89731. + unsigned hsftrst:1;
  89732. + /** Host Frame Counter Reset (Host Only)<br>
  89733. + *
  89734. + * The application can reset the (micro)frame number
  89735. + * counter inside the core, using this bit. When the
  89736. + * (micro)frame counter is reset, the subsequent SOF
  89737. + * sent out by the core, will have a (micro)frame
  89738. + * number of 0.
  89739. + */
  89740. + unsigned hstfrm:1;
  89741. + /** In Token Sequence Learning Queue Flush
  89742. + * (INTknQFlsh) (Device Only)
  89743. + */
  89744. + unsigned intknqflsh:1;
  89745. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  89746. + *
  89747. + * The application can flush the entire Receive FIFO
  89748. + * using this bit. The application must first
  89749. + * ensure that the core is not in the middle of a
  89750. + * transaction. The application should write into
  89751. + * this bit, only after making sure that neither the
  89752. + * DMA engine is reading from the RxFIFO nor the MAC
  89753. + * is writing the data in to the FIFO. The
  89754. + * application should wait until the bit is cleared
  89755. + * before performing any other operations. This bit
  89756. + * will takes 8 clocks (slowest of PHY or AHB clock)
  89757. + * to clear.
  89758. + */
  89759. + unsigned rxfflsh:1;
  89760. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  89761. + *
  89762. + * This bit is used to selectively flush a single or
  89763. + * all transmit FIFOs. The application must first
  89764. + * ensure that the core is not in the middle of a
  89765. + * transaction. The application should write into
  89766. + * this bit, only after making sure that neither the
  89767. + * DMA engine is writing into the TxFIFO nor the MAC
  89768. + * is reading the data out of the FIFO. The
  89769. + * application should wait until the core clears this
  89770. + * bit, before performing any operations. This bit
  89771. + * will takes 8 clocks (slowest of PHY or AHB clock)
  89772. + * to clear.
  89773. + */
  89774. + unsigned txfflsh:1;
  89775. +
  89776. + /** TxFIFO Number (TxFNum) (Device and Host).
  89777. + *
  89778. + * This is the FIFO number which needs to be flushed,
  89779. + * using the TxFIFO Flush bit. This field should not
  89780. + * be changed until the TxFIFO Flush bit is cleared by
  89781. + * the core.
  89782. + * - 0x0 : Non Periodic TxFIFO Flush
  89783. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  89784. + * or Periodic TxFIFO in host mode
  89785. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  89786. + * - ...
  89787. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  89788. + * - 0x10: Flush all the Transmit NonPeriodic and
  89789. + * Transmit Periodic FIFOs in the core
  89790. + */
  89791. + unsigned txfnum:5;
  89792. + /** Reserved */
  89793. + unsigned reserved11_29:19;
  89794. + /** DMA Request Signal. Indicated DMA request is in
  89795. + * probress. Used for debug purpose. */
  89796. + unsigned dmareq:1;
  89797. + /** AHB Master Idle. Indicates the AHB Master State
  89798. + * Machine is in IDLE condition. */
  89799. + unsigned ahbidle:1;
  89800. + } b;
  89801. +} grstctl_t;
  89802. +
  89803. +/**
  89804. + * This union represents the bit fields of the Core Interrupt Mask
  89805. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  89806. + * write the <i>d32</i> value to the register.
  89807. + */
  89808. +typedef union gintmsk_data {
  89809. + /** raw register data */
  89810. + uint32_t d32;
  89811. + /** register bits */
  89812. + struct {
  89813. + unsigned reserved0:1;
  89814. + unsigned modemismatch:1;
  89815. + unsigned otgintr:1;
  89816. + unsigned sofintr:1;
  89817. + unsigned rxstsqlvl:1;
  89818. + unsigned nptxfempty:1;
  89819. + unsigned ginnakeff:1;
  89820. + unsigned goutnakeff:1;
  89821. + unsigned ulpickint:1;
  89822. + unsigned i2cintr:1;
  89823. + unsigned erlysuspend:1;
  89824. + unsigned usbsuspend:1;
  89825. + unsigned usbreset:1;
  89826. + unsigned enumdone:1;
  89827. + unsigned isooutdrop:1;
  89828. + unsigned eopframe:1;
  89829. + unsigned restoredone:1;
  89830. + unsigned epmismatch:1;
  89831. + unsigned inepintr:1;
  89832. + unsigned outepintr:1;
  89833. + unsigned incomplisoin:1;
  89834. + unsigned incomplisoout:1;
  89835. + unsigned fetsusp:1;
  89836. + unsigned resetdet:1;
  89837. + unsigned portintr:1;
  89838. + unsigned hcintr:1;
  89839. + unsigned ptxfempty:1;
  89840. + unsigned lpmtranrcvd:1;
  89841. + unsigned conidstschng:1;
  89842. + unsigned disconnect:1;
  89843. + unsigned sessreqintr:1;
  89844. + unsigned wkupintr:1;
  89845. + } b;
  89846. +} gintmsk_data_t;
  89847. +/**
  89848. + * This union represents the bit fields of the Core Interrupt Register
  89849. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  89850. + * <i>d32</i> value to the register.
  89851. + */
  89852. +typedef union gintsts_data {
  89853. + /** raw register data */
  89854. + uint32_t d32;
  89855. +#define DWC_SOF_INTR_MASK 0x0008
  89856. + /** register bits */
  89857. + struct {
  89858. +#define DWC_HOST_MODE 1
  89859. + unsigned curmode:1;
  89860. + unsigned modemismatch:1;
  89861. + unsigned otgintr:1;
  89862. + unsigned sofintr:1;
  89863. + unsigned rxstsqlvl:1;
  89864. + unsigned nptxfempty:1;
  89865. + unsigned ginnakeff:1;
  89866. + unsigned goutnakeff:1;
  89867. + unsigned ulpickint:1;
  89868. + unsigned i2cintr:1;
  89869. + unsigned erlysuspend:1;
  89870. + unsigned usbsuspend:1;
  89871. + unsigned usbreset:1;
  89872. + unsigned enumdone:1;
  89873. + unsigned isooutdrop:1;
  89874. + unsigned eopframe:1;
  89875. + unsigned restoredone:1;
  89876. + unsigned epmismatch:1;
  89877. + unsigned inepint:1;
  89878. + unsigned outepintr:1;
  89879. + unsigned incomplisoin:1;
  89880. + unsigned incomplisoout:1;
  89881. + unsigned fetsusp:1;
  89882. + unsigned resetdet:1;
  89883. + unsigned portintr:1;
  89884. + unsigned hcintr:1;
  89885. + unsigned ptxfempty:1;
  89886. + unsigned lpmtranrcvd:1;
  89887. + unsigned conidstschng:1;
  89888. + unsigned disconnect:1;
  89889. + unsigned sessreqintr:1;
  89890. + unsigned wkupintr:1;
  89891. + } b;
  89892. +} gintsts_data_t;
  89893. +
  89894. +/**
  89895. + * This union represents the bit fields in the Device Receive Status Read and
  89896. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  89897. + * element then read out the bits using the <i>b</i>it elements.
  89898. + */
  89899. +typedef union device_grxsts_data {
  89900. + /** raw register data */
  89901. + uint32_t d32;
  89902. + /** register bits */
  89903. + struct {
  89904. + unsigned epnum:4;
  89905. + unsigned bcnt:11;
  89906. + unsigned dpid:2;
  89907. +
  89908. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  89909. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  89910. +
  89911. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  89912. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  89913. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  89914. + unsigned pktsts:4;
  89915. + unsigned fn:4;
  89916. + unsigned reserved25_31:7;
  89917. + } b;
  89918. +} device_grxsts_data_t;
  89919. +
  89920. +/**
  89921. + * This union represents the bit fields in the Host Receive Status Read and
  89922. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  89923. + * element then read out the bits using the <i>b</i>it elements.
  89924. + */
  89925. +typedef union host_grxsts_data {
  89926. + /** raw register data */
  89927. + uint32_t d32;
  89928. + /** register bits */
  89929. + struct {
  89930. + unsigned chnum:4;
  89931. + unsigned bcnt:11;
  89932. + unsigned dpid:2;
  89933. +
  89934. + unsigned pktsts:4;
  89935. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  89936. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  89937. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  89938. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  89939. +
  89940. + unsigned reserved21_31:11;
  89941. + } b;
  89942. +} host_grxsts_data_t;
  89943. +
  89944. +/**
  89945. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  89946. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  89947. + * then read out the bits using the <i>b</i>it elements.
  89948. + */
  89949. +typedef union fifosize_data {
  89950. + /** raw register data */
  89951. + uint32_t d32;
  89952. + /** register bits */
  89953. + struct {
  89954. + unsigned startaddr:16;
  89955. + unsigned depth:16;
  89956. + } b;
  89957. +} fifosize_data_t;
  89958. +
  89959. +/**
  89960. + * This union represents the bit fields in the Non-Periodic Transmit
  89961. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  89962. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  89963. + * elements.
  89964. + */
  89965. +typedef union gnptxsts_data {
  89966. + /** raw register data */
  89967. + uint32_t d32;
  89968. + /** register bits */
  89969. + struct {
  89970. + unsigned nptxfspcavail:16;
  89971. + unsigned nptxqspcavail:8;
  89972. + /** Top of the Non-Periodic Transmit Request Queue
  89973. + * - bit 24 - Terminate (Last entry for the selected
  89974. + * channel/EP)
  89975. + * - bits 26:25 - Token Type
  89976. + * - 2'b00 - IN/OUT
  89977. + * - 2'b01 - Zero Length OUT
  89978. + * - 2'b10 - PING/Complete Split
  89979. + * - 2'b11 - Channel Halt
  89980. + * - bits 30:27 - Channel/EP Number
  89981. + */
  89982. + unsigned nptxqtop_terminate:1;
  89983. + unsigned nptxqtop_token:2;
  89984. + unsigned nptxqtop_chnep:4;
  89985. + unsigned reserved:1;
  89986. + } b;
  89987. +} gnptxsts_data_t;
  89988. +
  89989. +/**
  89990. + * This union represents the bit fields in the Transmit
  89991. + * FIFO Status Register (DTXFSTS). Read the register into the
  89992. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  89993. + * elements.
  89994. + */
  89995. +typedef union dtxfsts_data {
  89996. + /** raw register data */
  89997. + uint32_t d32;
  89998. + /** register bits */
  89999. + struct {
  90000. + unsigned txfspcavail:16;
  90001. + unsigned reserved:16;
  90002. + } b;
  90003. +} dtxfsts_data_t;
  90004. +
  90005. +/**
  90006. + * This union represents the bit fields in the I2C Control Register
  90007. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  90008. + * bits using the <i>b</i>it elements.
  90009. + */
  90010. +typedef union gi2cctl_data {
  90011. + /** raw register data */
  90012. + uint32_t d32;
  90013. + /** register bits */
  90014. + struct {
  90015. + unsigned rwdata:8;
  90016. + unsigned regaddr:8;
  90017. + unsigned addr:7;
  90018. + unsigned i2cen:1;
  90019. + unsigned ack:1;
  90020. + unsigned i2csuspctl:1;
  90021. + unsigned i2cdevaddr:2;
  90022. + unsigned i2cdatse0:1;
  90023. + unsigned reserved:1;
  90024. + unsigned rw:1;
  90025. + unsigned bsydne:1;
  90026. + } b;
  90027. +} gi2cctl_data_t;
  90028. +
  90029. +/**
  90030. + * This union represents the bit fields in the PHY Vendor Control Register
  90031. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  90032. + * bits using the <i>b</i>it elements.
  90033. + */
  90034. +typedef union gpvndctl_data {
  90035. + /** raw register data */
  90036. + uint32_t d32;
  90037. + /** register bits */
  90038. + struct {
  90039. + unsigned regdata:8;
  90040. + unsigned vctrl:8;
  90041. + unsigned regaddr16_21:6;
  90042. + unsigned regwr:1;
  90043. + unsigned reserved23_24:2;
  90044. + unsigned newregreq:1;
  90045. + unsigned vstsbsy:1;
  90046. + unsigned vstsdone:1;
  90047. + unsigned reserved28_30:3;
  90048. + unsigned disulpidrvr:1;
  90049. + } b;
  90050. +} gpvndctl_data_t;
  90051. +
  90052. +/**
  90053. + * This union represents the bit fields in the General Purpose
  90054. + * Input/Output Register (GGPIO).
  90055. + * Read the register into the <i>d32</i> element then read out the
  90056. + * bits using the <i>b</i>it elements.
  90057. + */
  90058. +typedef union ggpio_data {
  90059. + /** raw register data */
  90060. + uint32_t d32;
  90061. + /** register bits */
  90062. + struct {
  90063. + unsigned gpi:16;
  90064. + unsigned gpo:16;
  90065. + } b;
  90066. +} ggpio_data_t;
  90067. +
  90068. +/**
  90069. + * This union represents the bit fields in the User ID Register
  90070. + * (GUID). Read the register into the <i>d32</i> element then read out the
  90071. + * bits using the <i>b</i>it elements.
  90072. + */
  90073. +typedef union guid_data {
  90074. + /** raw register data */
  90075. + uint32_t d32;
  90076. + /** register bits */
  90077. + struct {
  90078. + unsigned rwdata:32;
  90079. + } b;
  90080. +} guid_data_t;
  90081. +
  90082. +/**
  90083. + * This union represents the bit fields in the Synopsys ID Register
  90084. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  90085. + * bits using the <i>b</i>it elements.
  90086. + */
  90087. +typedef union gsnpsid_data {
  90088. + /** raw register data */
  90089. + uint32_t d32;
  90090. + /** register bits */
  90091. + struct {
  90092. + unsigned rwdata:32;
  90093. + } b;
  90094. +} gsnpsid_data_t;
  90095. +
  90096. +/**
  90097. + * This union represents the bit fields in the User HW Config1
  90098. + * Register. Read the register into the <i>d32</i> element then read
  90099. + * out the bits using the <i>b</i>it elements.
  90100. + */
  90101. +typedef union hwcfg1_data {
  90102. + /** raw register data */
  90103. + uint32_t d32;
  90104. + /** register bits */
  90105. + struct {
  90106. + unsigned ep_dir0:2;
  90107. + unsigned ep_dir1:2;
  90108. + unsigned ep_dir2:2;
  90109. + unsigned ep_dir3:2;
  90110. + unsigned ep_dir4:2;
  90111. + unsigned ep_dir5:2;
  90112. + unsigned ep_dir6:2;
  90113. + unsigned ep_dir7:2;
  90114. + unsigned ep_dir8:2;
  90115. + unsigned ep_dir9:2;
  90116. + unsigned ep_dir10:2;
  90117. + unsigned ep_dir11:2;
  90118. + unsigned ep_dir12:2;
  90119. + unsigned ep_dir13:2;
  90120. + unsigned ep_dir14:2;
  90121. + unsigned ep_dir15:2;
  90122. + } b;
  90123. +} hwcfg1_data_t;
  90124. +
  90125. +/**
  90126. + * This union represents the bit fields in the User HW Config2
  90127. + * Register. Read the register into the <i>d32</i> element then read
  90128. + * out the bits using the <i>b</i>it elements.
  90129. + */
  90130. +typedef union hwcfg2_data {
  90131. + /** raw register data */
  90132. + uint32_t d32;
  90133. + /** register bits */
  90134. + struct {
  90135. + /* GHWCFG2 */
  90136. + unsigned op_mode:3;
  90137. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  90138. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  90139. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  90140. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  90141. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  90142. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  90143. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  90144. +
  90145. + unsigned architecture:2;
  90146. + unsigned point2point:1;
  90147. + unsigned hs_phy_type:2;
  90148. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  90149. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  90150. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  90151. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  90152. +
  90153. + unsigned fs_phy_type:2;
  90154. + unsigned num_dev_ep:4;
  90155. + unsigned num_host_chan:4;
  90156. + unsigned perio_ep_supported:1;
  90157. + unsigned dynamic_fifo:1;
  90158. + unsigned multi_proc_int:1;
  90159. + unsigned reserved21:1;
  90160. + unsigned nonperio_tx_q_depth:2;
  90161. + unsigned host_perio_tx_q_depth:2;
  90162. + unsigned dev_token_q_depth:5;
  90163. + unsigned otg_enable_ic_usb:1;
  90164. + } b;
  90165. +} hwcfg2_data_t;
  90166. +
  90167. +/**
  90168. + * This union represents the bit fields in the User HW Config3
  90169. + * Register. Read the register into the <i>d32</i> element then read
  90170. + * out the bits using the <i>b</i>it elements.
  90171. + */
  90172. +typedef union hwcfg3_data {
  90173. + /** raw register data */
  90174. + uint32_t d32;
  90175. + /** register bits */
  90176. + struct {
  90177. + /* GHWCFG3 */
  90178. + unsigned xfer_size_cntr_width:4;
  90179. + unsigned packet_size_cntr_width:3;
  90180. + unsigned otg_func:1;
  90181. + unsigned i2c:1;
  90182. + unsigned vendor_ctrl_if:1;
  90183. + unsigned optional_features:1;
  90184. + unsigned synch_reset_type:1;
  90185. + unsigned adp_supp:1;
  90186. + unsigned otg_enable_hsic:1;
  90187. + unsigned bc_support:1;
  90188. + unsigned otg_lpm_en:1;
  90189. + unsigned dfifo_depth:16;
  90190. + } b;
  90191. +} hwcfg3_data_t;
  90192. +
  90193. +/**
  90194. + * This union represents the bit fields in the User HW Config4
  90195. + * Register. Read the register into the <i>d32</i> element then read
  90196. + * out the bits using the <i>b</i>it elements.
  90197. + */
  90198. +typedef union hwcfg4_data {
  90199. + /** raw register data */
  90200. + uint32_t d32;
  90201. + /** register bits */
  90202. + struct {
  90203. + unsigned num_dev_perio_in_ep:4;
  90204. + unsigned power_optimiz:1;
  90205. + unsigned min_ahb_freq:1;
  90206. + unsigned hiber:1;
  90207. + unsigned xhiber:1;
  90208. + unsigned reserved:6;
  90209. + unsigned utmi_phy_data_width:2;
  90210. + unsigned num_dev_mode_ctrl_ep:4;
  90211. + unsigned iddig_filt_en:1;
  90212. + unsigned vbus_valid_filt_en:1;
  90213. + unsigned a_valid_filt_en:1;
  90214. + unsigned b_valid_filt_en:1;
  90215. + unsigned session_end_filt_en:1;
  90216. + unsigned ded_fifo_en:1;
  90217. + unsigned num_in_eps:4;
  90218. + unsigned desc_dma:1;
  90219. + unsigned desc_dma_dyn:1;
  90220. + } b;
  90221. +} hwcfg4_data_t;
  90222. +
  90223. +/**
  90224. + * This union represents the bit fields of the Core LPM Configuration
  90225. + * Register (GLPMCFG). Set the bits using bit fields then write
  90226. + * the <i>d32</i> value to the register.
  90227. + */
  90228. +typedef union glpmctl_data {
  90229. + /** raw register data */
  90230. + uint32_t d32;
  90231. + /** register bits */
  90232. + struct {
  90233. + /** LPM-Capable (LPMCap) (Device and Host)
  90234. + * The application uses this bit to control
  90235. + * the DWC_otg core LPM capabilities.
  90236. + */
  90237. + unsigned lpm_cap_en:1;
  90238. + /** LPM response programmed by application (AppL1Res) (Device)
  90239. + * Handshake response to LPM token pre-programmed
  90240. + * by device application software.
  90241. + */
  90242. + unsigned appl_resp:1;
  90243. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  90244. + * In Host mode this field indicates the value of HIRD
  90245. + * to be sent in an LPM transaction.
  90246. + * In Device mode this field is updated with the
  90247. + * Received LPM Token HIRD bmAttribute
  90248. + * when an ACK/NYET/STALL response is sent
  90249. + * to an LPM transaction.
  90250. + */
  90251. + unsigned hird:4;
  90252. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  90253. + * In Host mode this bit indicates the value of remote
  90254. + * wake up to be sent in wIndex field of LPM transaction.
  90255. + * In Device mode this field is updated with the
  90256. + * Received LPM Token bRemoteWake bmAttribute
  90257. + * when an ACK/NYET/STALL response is sent
  90258. + * to an LPM transaction.
  90259. + */
  90260. + unsigned rem_wkup_en:1;
  90261. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  90262. + * The application uses this bit to control
  90263. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  90264. + */
  90265. + unsigned en_utmi_sleep:1;
  90266. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  90267. + */
  90268. + unsigned hird_thres:5;
  90269. + /** LPM Response (CoreL1Res) (Device and Host)
  90270. + * In Host mode this bit contains handsake response to
  90271. + * LPM transaction.
  90272. + * In Device mode the response of the core to
  90273. + * LPM transaction received is reflected in these two bits.
  90274. + - 0x0 : ERROR (No handshake response)
  90275. + - 0x1 : STALL
  90276. + - 0x2 : NYET
  90277. + - 0x3 : ACK
  90278. + */
  90279. + unsigned lpm_resp:2;
  90280. + /** Port Sleep Status (SlpSts) (Device and Host)
  90281. + * This bit is set as long as a Sleep condition
  90282. + * is present on the USB bus.
  90283. + */
  90284. + unsigned prt_sleep_sts:1;
  90285. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  90286. + * Indicates that the application or host
  90287. + * can start resume from Sleep state.
  90288. + */
  90289. + unsigned sleep_state_resumeok:1;
  90290. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  90291. + * The channel number on which the LPM transaction
  90292. + * has to be applied while sending
  90293. + * an LPM transaction to the local device.
  90294. + */
  90295. + unsigned lpm_chan_index:4;
  90296. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  90297. + * Number host retries that would be performed
  90298. + * if the device response was not valid response.
  90299. + */
  90300. + unsigned retry_count:3;
  90301. + /** Send LPM Transaction (SndLPM) (Host)
  90302. + * When set by application software,
  90303. + * an LPM transaction containing two tokens
  90304. + * is sent.
  90305. + */
  90306. + unsigned send_lpm:1;
  90307. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  90308. + * Number of LPM Host Retries still remaining
  90309. + * to be transmitted for the current LPM sequence
  90310. + */
  90311. + unsigned retry_count_sts:3;
  90312. + unsigned reserved28_29:2;
  90313. + /** In host mode once this bit is set, the host
  90314. + * configures to drive the HSIC Idle state on the bus.
  90315. + * It then waits for the device to initiate the Connect sequence.
  90316. + * In device mode once this bit is set, the device waits for
  90317. + * the HSIC Idle line state on the bus. Upon receving the Idle
  90318. + * line state, it initiates the HSIC Connect sequence.
  90319. + */
  90320. + unsigned hsic_connect:1;
  90321. + /** This bit overrides and functionally inverts
  90322. + * the if_select_hsic input port signal.
  90323. + */
  90324. + unsigned inv_sel_hsic:1;
  90325. + } b;
  90326. +} glpmcfg_data_t;
  90327. +
  90328. +/**
  90329. + * This union represents the bit fields of the Core ADP Timer, Control and
  90330. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  90331. + * the <i>d32</i> value to the register.
  90332. + */
  90333. +typedef union adpctl_data {
  90334. + /** raw register data */
  90335. + uint32_t d32;
  90336. + /** register bits */
  90337. + struct {
  90338. + /** Probe Discharge (PRB_DSCHG)
  90339. + * These bits set the times for TADP_DSCHG.
  90340. + * These bits are defined as follows:
  90341. + * 2'b00 - 4 msec
  90342. + * 2'b01 - 8 msec
  90343. + * 2'b10 - 16 msec
  90344. + * 2'b11 - 32 msec
  90345. + */
  90346. + unsigned prb_dschg:2;
  90347. + /** Probe Delta (PRB_DELTA)
  90348. + * These bits set the resolution for RTIM value.
  90349. + * The bits are defined in units of 32 kHz clock cycles as follows:
  90350. + * 2'b00 - 1 cycles
  90351. + * 2'b01 - 2 cycles
  90352. + * 2'b10 - 3 cycles
  90353. + * 2'b11 - 4 cycles
  90354. + * For example if this value is chosen to 2'b01, it means that RTIM
  90355. + * increments for every 3(three) 32Khz clock cycles.
  90356. + */
  90357. + unsigned prb_delta:2;
  90358. + /** Probe Period (PRB_PER)
  90359. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  90360. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  90361. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  90362. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  90363. + * 2'b11 - Reserved
  90364. + */
  90365. + unsigned prb_per:2;
  90366. + /** These bits capture the latest time it took for VBUS to ramp from
  90367. + * VADP_SINK to VADP_PRB.
  90368. + * 0x000 - 1 cycles
  90369. + * 0x001 - 2 cycles
  90370. + * 0x002 - 3 cycles
  90371. + * etc
  90372. + * 0x7FF - 2048 cycles
  90373. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  90374. + */
  90375. + unsigned rtim:11;
  90376. + /** Enable Probe (EnaPrb)
  90377. + * When programmed to 1'b1, the core performs a probe operation.
  90378. + * This bit is valid only if OTG_Ver = 1'b1.
  90379. + */
  90380. + unsigned enaprb:1;
  90381. + /** Enable Sense (EnaSns)
  90382. + * When programmed to 1'b1, the core performs a Sense operation.
  90383. + * This bit is valid only if OTG_Ver = 1'b1.
  90384. + */
  90385. + unsigned enasns:1;
  90386. + /** ADP Reset (ADPRes)
  90387. + * When set, ADP controller is reset.
  90388. + * This bit is valid only if OTG_Ver = 1'b1.
  90389. + */
  90390. + unsigned adpres:1;
  90391. + /** ADP Enable (ADPEn)
  90392. + * When set, the core performs either ADP probing or sensing
  90393. + * based on EnaPrb or EnaSns.
  90394. + * This bit is valid only if OTG_Ver = 1'b1.
  90395. + */
  90396. + unsigned adpen:1;
  90397. + /** ADP Probe Interrupt (ADP_PRB_INT)
  90398. + * When this bit is set, it means that the VBUS
  90399. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  90400. + * This bit is valid only if OTG_Ver = 1'b1.
  90401. + */
  90402. + unsigned adp_prb_int:1;
  90403. + /**
  90404. + * ADP Sense Interrupt (ADP_SNS_INT)
  90405. + * When this bit is set, it means that the VBUS voltage is greater than
  90406. + * VADP_SNS value or VADP_SNS is reached.
  90407. + * This bit is valid only if OTG_Ver = 1'b1.
  90408. + */
  90409. + unsigned adp_sns_int:1;
  90410. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  90411. + * This bit is relevant only for an ADP probe.
  90412. + * When this bit is set, it means that the ramp time has
  90413. + * completed ie ADPCTL.RTIM has reached its terminal value
  90414. + * of 0x7FF. This is a debug feature that allows software
  90415. + * to read the ramp time after each cycle.
  90416. + * This bit is valid only if OTG_Ver = 1'b1.
  90417. + */
  90418. + unsigned adp_tmout_int:1;
  90419. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  90420. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  90421. + * This bit is valid only if OTG_Ver = 1'b1.
  90422. + */
  90423. + unsigned adp_prb_int_msk:1;
  90424. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  90425. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  90426. + * This bit is valid only if OTG_Ver = 1'b1.
  90427. + */
  90428. + unsigned adp_sns_int_msk:1;
  90429. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  90430. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  90431. + * This bit is valid only if OTG_Ver = 1'b1.
  90432. + */
  90433. + unsigned adp_tmout_int_msk:1;
  90434. + /** Access Request
  90435. + * 2'b00 - Read/Write Valid (updated by the core)
  90436. + * 2'b01 - Read
  90437. + * 2'b00 - Write
  90438. + * 2'b00 - Reserved
  90439. + */
  90440. + unsigned ar:2;
  90441. + /** Reserved */
  90442. + unsigned reserved29_31:3;
  90443. + } b;
  90444. +} adpctl_data_t;
  90445. +
  90446. +////////////////////////////////////////////
  90447. +// Device Registers
  90448. +/**
  90449. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  90450. + *
  90451. + * The following structures define the size and relative field offsets
  90452. + * for the Device Mode Registers.
  90453. + *
  90454. + * <i>These registers are visible only in Device mode and must not be
  90455. + * accessed in Host mode, as the results are unknown.</i>
  90456. + */
  90457. +typedef struct dwc_otg_dev_global_regs {
  90458. + /** Device Configuration Register. <i>Offset 800h</i> */
  90459. + volatile uint32_t dcfg;
  90460. + /** Device Control Register. <i>Offset: 804h</i> */
  90461. + volatile uint32_t dctl;
  90462. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  90463. + volatile uint32_t dsts;
  90464. + /** Reserved. <i>Offset: 80Ch</i> */
  90465. + uint32_t unused;
  90466. + /** Device IN Endpoint Common Interrupt Mask
  90467. + * Register. <i>Offset: 810h</i> */
  90468. + volatile uint32_t diepmsk;
  90469. + /** Device OUT Endpoint Common Interrupt Mask
  90470. + * Register. <i>Offset: 814h</i> */
  90471. + volatile uint32_t doepmsk;
  90472. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  90473. + volatile uint32_t daint;
  90474. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  90475. + * 81Ch</i> */
  90476. + volatile uint32_t daintmsk;
  90477. + /** Device IN Token Queue Read Register-1 (Read Only).
  90478. + * <i>Offset: 820h</i> */
  90479. + volatile uint32_t dtknqr1;
  90480. + /** Device IN Token Queue Read Register-2 (Read Only).
  90481. + * <i>Offset: 824h</i> */
  90482. + volatile uint32_t dtknqr2;
  90483. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  90484. + volatile uint32_t dvbusdis;
  90485. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  90486. + volatile uint32_t dvbuspulse;
  90487. + /** Device IN Token Queue Read Register-3 (Read Only). /
  90488. + * Device Thresholding control register (Read/Write)
  90489. + * <i>Offset: 830h</i> */
  90490. + volatile uint32_t dtknqr3_dthrctl;
  90491. + /** Device IN Token Queue Read Register-4 (Read Only). /
  90492. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  90493. + * <i>Offset: 834h</i> */
  90494. + volatile uint32_t dtknqr4_fifoemptymsk;
  90495. + /** Device Each Endpoint Interrupt Register (Read Only). /
  90496. + * <i>Offset: 838h</i> */
  90497. + volatile uint32_t deachint;
  90498. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  90499. + * <i>Offset: 83Ch</i> */
  90500. + volatile uint32_t deachintmsk;
  90501. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  90502. + * <i>Offset: 840h</i> */
  90503. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  90504. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  90505. + * <i>Offset: 880h</i> */
  90506. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  90507. +} dwc_otg_device_global_regs_t;
  90508. +
  90509. +/**
  90510. + * This union represents the bit fields in the Device Configuration
  90511. + * Register. Read the register into the <i>d32</i> member then
  90512. + * set/clear the bits using the <i>b</i>it elements. Write the
  90513. + * <i>d32</i> member to the dcfg register.
  90514. + */
  90515. +typedef union dcfg_data {
  90516. + /** raw register data */
  90517. + uint32_t d32;
  90518. + /** register bits */
  90519. + struct {
  90520. + /** Device Speed */
  90521. + unsigned devspd:2;
  90522. + /** Non Zero Length Status OUT Handshake */
  90523. + unsigned nzstsouthshk:1;
  90524. +#define DWC_DCFG_SEND_STALL 1
  90525. +
  90526. + unsigned ena32khzs:1;
  90527. + /** Device Addresses */
  90528. + unsigned devaddr:7;
  90529. + /** Periodic Frame Interval */
  90530. + unsigned perfrint:2;
  90531. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  90532. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  90533. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  90534. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  90535. +
  90536. + /** Enable Device OUT NAK for bulk in DDMA mode */
  90537. + unsigned endevoutnak:1;
  90538. +
  90539. + unsigned reserved14_17:4;
  90540. + /** In Endpoint Mis-match count */
  90541. + unsigned epmscnt:5;
  90542. + /** Enable Descriptor DMA in Device mode */
  90543. + unsigned descdma:1;
  90544. + unsigned perschintvl:2;
  90545. + unsigned resvalid:6;
  90546. + } b;
  90547. +} dcfg_data_t;
  90548. +
  90549. +/**
  90550. + * This union represents the bit fields in the Device Control
  90551. + * Register. Read the register into the <i>d32</i> member then
  90552. + * set/clear the bits using the <i>b</i>it elements.
  90553. + */
  90554. +typedef union dctl_data {
  90555. + /** raw register data */
  90556. + uint32_t d32;
  90557. + /** register bits */
  90558. + struct {
  90559. + /** Remote Wakeup */
  90560. + unsigned rmtwkupsig:1;
  90561. + /** Soft Disconnect */
  90562. + unsigned sftdiscon:1;
  90563. + /** Global Non-Periodic IN NAK Status */
  90564. + unsigned gnpinnaksts:1;
  90565. + /** Global OUT NAK Status */
  90566. + unsigned goutnaksts:1;
  90567. + /** Test Control */
  90568. + unsigned tstctl:3;
  90569. + /** Set Global Non-Periodic IN NAK */
  90570. + unsigned sgnpinnak:1;
  90571. + /** Clear Global Non-Periodic IN NAK */
  90572. + unsigned cgnpinnak:1;
  90573. + /** Set Global OUT NAK */
  90574. + unsigned sgoutnak:1;
  90575. + /** Clear Global OUT NAK */
  90576. + unsigned cgoutnak:1;
  90577. + /** Power-On Programming Done */
  90578. + unsigned pwronprgdone:1;
  90579. + /** Reserved */
  90580. + unsigned reserved:1;
  90581. + /** Global Multi Count */
  90582. + unsigned gmc:2;
  90583. + /** Ignore Frame Number for ISOC EPs */
  90584. + unsigned ifrmnum:1;
  90585. + /** NAK on Babble */
  90586. + unsigned nakonbble:1;
  90587. + /** Enable Continue on BNA */
  90588. + unsigned encontonbna:1;
  90589. +
  90590. + unsigned reserved18_31:14;
  90591. + } b;
  90592. +} dctl_data_t;
  90593. +
  90594. +/**
  90595. + * This union represents the bit fields in the Device Status
  90596. + * Register. Read the register into the <i>d32</i> member then
  90597. + * set/clear the bits using the <i>b</i>it elements.
  90598. + */
  90599. +typedef union dsts_data {
  90600. + /** raw register data */
  90601. + uint32_t d32;
  90602. + /** register bits */
  90603. + struct {
  90604. + /** Suspend Status */
  90605. + unsigned suspsts:1;
  90606. + /** Enumerated Speed */
  90607. + unsigned enumspd:2;
  90608. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  90609. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  90610. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  90611. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  90612. + /** Erratic Error */
  90613. + unsigned errticerr:1;
  90614. + unsigned reserved4_7:4;
  90615. + /** Frame or Microframe Number of the received SOF */
  90616. + unsigned soffn:14;
  90617. + unsigned reserved22_31:10;
  90618. + } b;
  90619. +} dsts_data_t;
  90620. +
  90621. +/**
  90622. + * This union represents the bit fields in the Device IN EP Interrupt
  90623. + * Register and the Device IN EP Common Mask Register.
  90624. + *
  90625. + * - Read the register into the <i>d32</i> member then set/clear the
  90626. + * bits using the <i>b</i>it elements.
  90627. + */
  90628. +typedef union diepint_data {
  90629. + /** raw register data */
  90630. + uint32_t d32;
  90631. + /** register bits */
  90632. + struct {
  90633. + /** Transfer complete mask */
  90634. + unsigned xfercompl:1;
  90635. + /** Endpoint disable mask */
  90636. + unsigned epdisabled:1;
  90637. + /** AHB Error mask */
  90638. + unsigned ahberr:1;
  90639. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  90640. + unsigned timeout:1;
  90641. + /** IN Token received with TxF Empty mask */
  90642. + unsigned intktxfemp:1;
  90643. + /** IN Token Received with EP mismatch mask */
  90644. + unsigned intknepmis:1;
  90645. + /** IN Endpoint NAK Effective mask */
  90646. + unsigned inepnakeff:1;
  90647. + /** Reserved */
  90648. + unsigned emptyintr:1;
  90649. +
  90650. + unsigned txfifoundrn:1;
  90651. +
  90652. + /** BNA Interrupt mask */
  90653. + unsigned bna:1;
  90654. +
  90655. + unsigned reserved10_12:3;
  90656. + /** BNA Interrupt mask */
  90657. + unsigned nak:1;
  90658. +
  90659. + unsigned reserved14_31:18;
  90660. + } b;
  90661. +} diepint_data_t;
  90662. +
  90663. +/**
  90664. + * This union represents the bit fields in the Device IN EP
  90665. + * Common/Dedicated Interrupt Mask Register.
  90666. + */
  90667. +typedef union diepint_data diepmsk_data_t;
  90668. +
  90669. +/**
  90670. + * This union represents the bit fields in the Device OUT EP Interrupt
  90671. + * Registerand Device OUT EP Common Interrupt Mask Register.
  90672. + *
  90673. + * - Read the register into the <i>d32</i> member then set/clear the
  90674. + * bits using the <i>b</i>it elements.
  90675. + */
  90676. +typedef union doepint_data {
  90677. + /** raw register data */
  90678. + uint32_t d32;
  90679. + /** register bits */
  90680. + struct {
  90681. + /** Transfer complete */
  90682. + unsigned xfercompl:1;
  90683. + /** Endpoint disable */
  90684. + unsigned epdisabled:1;
  90685. + /** AHB Error */
  90686. + unsigned ahberr:1;
  90687. + /** Setup Phase Done (contorl EPs) */
  90688. + unsigned setup:1;
  90689. + /** OUT Token Received when Endpoint Disabled */
  90690. + unsigned outtknepdis:1;
  90691. +
  90692. + unsigned stsphsercvd:1;
  90693. + /** Back-to-Back SETUP Packets Received */
  90694. + unsigned back2backsetup:1;
  90695. +
  90696. + unsigned reserved7:1;
  90697. + /** OUT packet Error */
  90698. + unsigned outpkterr:1;
  90699. + /** BNA Interrupt */
  90700. + unsigned bna:1;
  90701. +
  90702. + unsigned reserved10:1;
  90703. + /** Packet Drop Status */
  90704. + unsigned pktdrpsts:1;
  90705. + /** Babble Interrupt */
  90706. + unsigned babble:1;
  90707. + /** NAK Interrupt */
  90708. + unsigned nak:1;
  90709. + /** NYET Interrupt */
  90710. + unsigned nyet:1;
  90711. + /** Bit indicating setup packet received */
  90712. + unsigned sr:1;
  90713. +
  90714. + unsigned reserved16_31:16;
  90715. + } b;
  90716. +} doepint_data_t;
  90717. +
  90718. +/**
  90719. + * This union represents the bit fields in the Device OUT EP
  90720. + * Common/Dedicated Interrupt Mask Register.
  90721. + */
  90722. +typedef union doepint_data doepmsk_data_t;
  90723. +
  90724. +/**
  90725. + * This union represents the bit fields in the Device All EP Interrupt
  90726. + * and Mask Registers.
  90727. + * - Read the register into the <i>d32</i> member then set/clear the
  90728. + * bits using the <i>b</i>it elements.
  90729. + */
  90730. +typedef union daint_data {
  90731. + /** raw register data */
  90732. + uint32_t d32;
  90733. + /** register bits */
  90734. + struct {
  90735. + /** IN Endpoint bits */
  90736. + unsigned in:16;
  90737. + /** OUT Endpoint bits */
  90738. + unsigned out:16;
  90739. + } ep;
  90740. + struct {
  90741. + /** IN Endpoint bits */
  90742. + unsigned inep0:1;
  90743. + unsigned inep1:1;
  90744. + unsigned inep2:1;
  90745. + unsigned inep3:1;
  90746. + unsigned inep4:1;
  90747. + unsigned inep5:1;
  90748. + unsigned inep6:1;
  90749. + unsigned inep7:1;
  90750. + unsigned inep8:1;
  90751. + unsigned inep9:1;
  90752. + unsigned inep10:1;
  90753. + unsigned inep11:1;
  90754. + unsigned inep12:1;
  90755. + unsigned inep13:1;
  90756. + unsigned inep14:1;
  90757. + unsigned inep15:1;
  90758. + /** OUT Endpoint bits */
  90759. + unsigned outep0:1;
  90760. + unsigned outep1:1;
  90761. + unsigned outep2:1;
  90762. + unsigned outep3:1;
  90763. + unsigned outep4:1;
  90764. + unsigned outep5:1;
  90765. + unsigned outep6:1;
  90766. + unsigned outep7:1;
  90767. + unsigned outep8:1;
  90768. + unsigned outep9:1;
  90769. + unsigned outep10:1;
  90770. + unsigned outep11:1;
  90771. + unsigned outep12:1;
  90772. + unsigned outep13:1;
  90773. + unsigned outep14:1;
  90774. + unsigned outep15:1;
  90775. + } b;
  90776. +} daint_data_t;
  90777. +
  90778. +/**
  90779. + * This union represents the bit fields in the Device IN Token Queue
  90780. + * Read Registers.
  90781. + * - Read the register into the <i>d32</i> member.
  90782. + * - READ-ONLY Register
  90783. + */
  90784. +typedef union dtknq1_data {
  90785. + /** raw register data */
  90786. + uint32_t d32;
  90787. + /** register bits */
  90788. + struct {
  90789. + /** In Token Queue Write Pointer */
  90790. + unsigned intknwptr:5;
  90791. + /** Reserved */
  90792. + unsigned reserved05_06:2;
  90793. + /** write pointer has wrapped. */
  90794. + unsigned wrap_bit:1;
  90795. + /** EP Numbers of IN Tokens 0 ... 4 */
  90796. + unsigned epnums0_5:24;
  90797. + } b;
  90798. +} dtknq1_data_t;
  90799. +
  90800. +/**
  90801. + * This union represents Threshold control Register
  90802. + * - Read and write the register into the <i>d32</i> member.
  90803. + * - READ-WRITABLE Register
  90804. + */
  90805. +typedef union dthrctl_data {
  90806. + /** raw register data */
  90807. + uint32_t d32;
  90808. + /** register bits */
  90809. + struct {
  90810. + /** non ISO Tx Thr. Enable */
  90811. + unsigned non_iso_thr_en:1;
  90812. + /** ISO Tx Thr. Enable */
  90813. + unsigned iso_thr_en:1;
  90814. + /** Tx Thr. Length */
  90815. + unsigned tx_thr_len:9;
  90816. + /** AHB Threshold ratio */
  90817. + unsigned ahb_thr_ratio:2;
  90818. + /** Reserved */
  90819. + unsigned reserved13_15:3;
  90820. + /** Rx Thr. Enable */
  90821. + unsigned rx_thr_en:1;
  90822. + /** Rx Thr. Length */
  90823. + unsigned rx_thr_len:9;
  90824. + unsigned reserved26:1;
  90825. + /** Arbiter Parking Enable*/
  90826. + unsigned arbprken:1;
  90827. + /** Reserved */
  90828. + unsigned reserved28_31:4;
  90829. + } b;
  90830. +} dthrctl_data_t;
  90831. +
  90832. +/**
  90833. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  90834. + * 900h-AFCh</i>
  90835. + *
  90836. + * There will be one set of endpoint registers per logical endpoint
  90837. + * implemented.
  90838. + *
  90839. + * <i>These registers are visible only in Device mode and must not be
  90840. + * accessed in Host mode, as the results are unknown.</i>
  90841. + */
  90842. +typedef struct dwc_otg_dev_in_ep_regs {
  90843. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  90844. + * (ep_num * 20h) + 00h</i> */
  90845. + volatile uint32_t diepctl;
  90846. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  90847. + uint32_t reserved04;
  90848. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  90849. + * (ep_num * 20h) + 08h</i> */
  90850. + volatile uint32_t diepint;
  90851. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  90852. + uint32_t reserved0C;
  90853. + /** Device IN Endpoint Transfer Size
  90854. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  90855. + volatile uint32_t dieptsiz;
  90856. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  90857. + * (ep_num * 20h) + 14h</i> */
  90858. + volatile uint32_t diepdma;
  90859. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  90860. + * (ep_num * 20h) + 18h</i> */
  90861. + volatile uint32_t dtxfsts;
  90862. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  90863. + * (ep_num * 20h) + 1Ch</i> */
  90864. + volatile uint32_t diepdmab;
  90865. +} dwc_otg_dev_in_ep_regs_t;
  90866. +
  90867. +/**
  90868. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  90869. + * B00h-CFCh</i>
  90870. + *
  90871. + * There will be one set of endpoint registers per logical endpoint
  90872. + * implemented.
  90873. + *
  90874. + * <i>These registers are visible only in Device mode and must not be
  90875. + * accessed in Host mode, as the results are unknown.</i>
  90876. + */
  90877. +typedef struct dwc_otg_dev_out_ep_regs {
  90878. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  90879. + * (ep_num * 20h) + 00h</i> */
  90880. + volatile uint32_t doepctl;
  90881. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  90882. + uint32_t reserved04;
  90883. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  90884. + * (ep_num * 20h) + 08h</i> */
  90885. + volatile uint32_t doepint;
  90886. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  90887. + uint32_t reserved0C;
  90888. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  90889. + * B00h + (ep_num * 20h) + 10h</i> */
  90890. + volatile uint32_t doeptsiz;
  90891. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  90892. + * + (ep_num * 20h) + 14h</i> */
  90893. + volatile uint32_t doepdma;
  90894. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  90895. + uint32_t unused;
  90896. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  90897. + * + (ep_num * 20h) + 1Ch</i> */
  90898. + uint32_t doepdmab;
  90899. +} dwc_otg_dev_out_ep_regs_t;
  90900. +
  90901. +/**
  90902. + * This union represents the bit fields in the Device EP Control
  90903. + * Register. Read the register into the <i>d32</i> member then
  90904. + * set/clear the bits using the <i>b</i>it elements.
  90905. + */
  90906. +typedef union depctl_data {
  90907. + /** raw register data */
  90908. + uint32_t d32;
  90909. + /** register bits */
  90910. + struct {
  90911. + /** Maximum Packet Size
  90912. + * IN/OUT EPn
  90913. + * IN/OUT EP0 - 2 bits
  90914. + * 2'b00: 64 Bytes
  90915. + * 2'b01: 32
  90916. + * 2'b10: 16
  90917. + * 2'b11: 8 */
  90918. + unsigned mps:11;
  90919. +#define DWC_DEP0CTL_MPS_64 0
  90920. +#define DWC_DEP0CTL_MPS_32 1
  90921. +#define DWC_DEP0CTL_MPS_16 2
  90922. +#define DWC_DEP0CTL_MPS_8 3
  90923. +
  90924. + /** Next Endpoint
  90925. + * IN EPn/IN EP0
  90926. + * OUT EPn/OUT EP0 - reserved */
  90927. + unsigned nextep:4;
  90928. +
  90929. + /** USB Active Endpoint */
  90930. + unsigned usbactep:1;
  90931. +
  90932. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  90933. + * This field contains the PID of the packet going to
  90934. + * be received or transmitted on this endpoint. The
  90935. + * application should program the PID of the first
  90936. + * packet going to be received or transmitted on this
  90937. + * endpoint , after the endpoint is
  90938. + * activated. Application use the SetD1PID and
  90939. + * SetD0PID fields of this register to program either
  90940. + * D0 or D1 PID.
  90941. + *
  90942. + * The encoding for this field is
  90943. + * - 0: D0
  90944. + * - 1: D1
  90945. + */
  90946. + unsigned dpid:1;
  90947. +
  90948. + /** NAK Status */
  90949. + unsigned naksts:1;
  90950. +
  90951. + /** Endpoint Type
  90952. + * 2'b00: Control
  90953. + * 2'b01: Isochronous
  90954. + * 2'b10: Bulk
  90955. + * 2'b11: Interrupt */
  90956. + unsigned eptype:2;
  90957. +
  90958. + /** Snoop Mode
  90959. + * OUT EPn/OUT EP0
  90960. + * IN EPn/IN EP0 - reserved */
  90961. + unsigned snp:1;
  90962. +
  90963. + /** Stall Handshake */
  90964. + unsigned stall:1;
  90965. +
  90966. + /** Tx Fifo Number
  90967. + * IN EPn/IN EP0
  90968. + * OUT EPn/OUT EP0 - reserved */
  90969. + unsigned txfnum:4;
  90970. +
  90971. + /** Clear NAK */
  90972. + unsigned cnak:1;
  90973. + /** Set NAK */
  90974. + unsigned snak:1;
  90975. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  90976. + * Writing to this field sets the Endpoint DPID (DPID)
  90977. + * field in this register to DATA0. Set Even
  90978. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  90979. + * Writing to this field sets the Even/Odd
  90980. + * (micro)frame (EO_FrNum) field to even (micro)
  90981. + * frame.
  90982. + */
  90983. + unsigned setd0pid:1;
  90984. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  90985. + * Writing to this field sets the Endpoint DPID (DPID)
  90986. + * field in this register to DATA1 Set Odd
  90987. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  90988. + * Writing to this field sets the Even/Odd
  90989. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  90990. + */
  90991. + unsigned setd1pid:1;
  90992. +
  90993. + /** Endpoint Disable */
  90994. + unsigned epdis:1;
  90995. + /** Endpoint Enable */
  90996. + unsigned epena:1;
  90997. + } b;
  90998. +} depctl_data_t;
  90999. +
  91000. +/**
  91001. + * This union represents the bit fields in the Device EP Transfer
  91002. + * Size Register. Read the register into the <i>d32</i> member then
  91003. + * set/clear the bits using the <i>b</i>it elements.
  91004. + */
  91005. +typedef union deptsiz_data {
  91006. + /** raw register data */
  91007. + uint32_t d32;
  91008. + /** register bits */
  91009. + struct {
  91010. + /** Transfer size */
  91011. + unsigned xfersize:19;
  91012. +/** Max packet count for EP (pow(2,10)-1) */
  91013. +#define MAX_PKT_CNT 1023
  91014. + /** Packet Count */
  91015. + unsigned pktcnt:10;
  91016. + /** Multi Count - Periodic IN endpoints */
  91017. + unsigned mc:2;
  91018. + unsigned reserved:1;
  91019. + } b;
  91020. +} deptsiz_data_t;
  91021. +
  91022. +/**
  91023. + * This union represents the bit fields in the Device EP 0 Transfer
  91024. + * Size Register. Read the register into the <i>d32</i> member then
  91025. + * set/clear the bits using the <i>b</i>it elements.
  91026. + */
  91027. +typedef union deptsiz0_data {
  91028. + /** raw register data */
  91029. + uint32_t d32;
  91030. + /** register bits */
  91031. + struct {
  91032. + /** Transfer size */
  91033. + unsigned xfersize:7;
  91034. + /** Reserved */
  91035. + unsigned reserved7_18:12;
  91036. + /** Packet Count */
  91037. + unsigned pktcnt:2;
  91038. + /** Reserved */
  91039. + unsigned reserved21_28:8;
  91040. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  91041. + unsigned supcnt:2;
  91042. + unsigned reserved31;
  91043. + } b;
  91044. +} deptsiz0_data_t;
  91045. +
  91046. +/////////////////////////////////////////////////
  91047. +// DMA Descriptor Specific Structures
  91048. +//
  91049. +
  91050. +/** Buffer status definitions */
  91051. +
  91052. +#define BS_HOST_READY 0x0
  91053. +#define BS_DMA_BUSY 0x1
  91054. +#define BS_DMA_DONE 0x2
  91055. +#define BS_HOST_BUSY 0x3
  91056. +
  91057. +/** Receive/Transmit status definitions */
  91058. +
  91059. +#define RTS_SUCCESS 0x0
  91060. +#define RTS_BUFFLUSH 0x1
  91061. +#define RTS_RESERVED 0x2
  91062. +#define RTS_BUFERR 0x3
  91063. +
  91064. +/**
  91065. + * This union represents the bit fields in the DMA Descriptor
  91066. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  91067. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  91068. + * <i>b_iso_in</i> elements.
  91069. + */
  91070. +typedef union dev_dma_desc_sts {
  91071. + /** raw register data */
  91072. + uint32_t d32;
  91073. + /** quadlet bits */
  91074. + struct {
  91075. + /** Received number of bytes */
  91076. + unsigned bytes:16;
  91077. + /** NAK bit - only for OUT EPs */
  91078. + unsigned nak:1;
  91079. + unsigned reserved17_22:6;
  91080. + /** Multiple Transfer - only for OUT EPs */
  91081. + unsigned mtrf:1;
  91082. + /** Setup Packet received - only for OUT EPs */
  91083. + unsigned sr:1;
  91084. + /** Interrupt On Complete */
  91085. + unsigned ioc:1;
  91086. + /** Short Packet */
  91087. + unsigned sp:1;
  91088. + /** Last */
  91089. + unsigned l:1;
  91090. + /** Receive Status */
  91091. + unsigned sts:2;
  91092. + /** Buffer Status */
  91093. + unsigned bs:2;
  91094. + } b;
  91095. +
  91096. +//#ifdef DWC_EN_ISOC
  91097. + /** iso out quadlet bits */
  91098. + struct {
  91099. + /** Received number of bytes */
  91100. + unsigned rxbytes:11;
  91101. +
  91102. + unsigned reserved11:1;
  91103. + /** Frame Number */
  91104. + unsigned framenum:11;
  91105. + /** Received ISO Data PID */
  91106. + unsigned pid:2;
  91107. + /** Interrupt On Complete */
  91108. + unsigned ioc:1;
  91109. + /** Short Packet */
  91110. + unsigned sp:1;
  91111. + /** Last */
  91112. + unsigned l:1;
  91113. + /** Receive Status */
  91114. + unsigned rxsts:2;
  91115. + /** Buffer Status */
  91116. + unsigned bs:2;
  91117. + } b_iso_out;
  91118. +
  91119. + /** iso in quadlet bits */
  91120. + struct {
  91121. + /** Transmited number of bytes */
  91122. + unsigned txbytes:12;
  91123. + /** Frame Number */
  91124. + unsigned framenum:11;
  91125. + /** Transmited ISO Data PID */
  91126. + unsigned pid:2;
  91127. + /** Interrupt On Complete */
  91128. + unsigned ioc:1;
  91129. + /** Short Packet */
  91130. + unsigned sp:1;
  91131. + /** Last */
  91132. + unsigned l:1;
  91133. + /** Transmit Status */
  91134. + unsigned txsts:2;
  91135. + /** Buffer Status */
  91136. + unsigned bs:2;
  91137. + } b_iso_in;
  91138. +//#endif /* DWC_EN_ISOC */
  91139. +} dev_dma_desc_sts_t;
  91140. +
  91141. +/**
  91142. + * DMA Descriptor structure
  91143. + *
  91144. + * DMA Descriptor structure contains two quadlets:
  91145. + * Status quadlet and Data buffer pointer.
  91146. + */
  91147. +typedef struct dwc_otg_dev_dma_desc {
  91148. + /** DMA Descriptor status quadlet */
  91149. + dev_dma_desc_sts_t status;
  91150. + /** DMA Descriptor data buffer pointer */
  91151. + uint32_t buf;
  91152. +} dwc_otg_dev_dma_desc_t;
  91153. +
  91154. +/**
  91155. + * The dwc_otg_dev_if structure contains information needed to manage
  91156. + * the DWC_otg controller acting in device mode. It represents the
  91157. + * programming view of the device-specific aspects of the controller.
  91158. + */
  91159. +typedef struct dwc_otg_dev_if {
  91160. + /** Pointer to device Global registers.
  91161. + * Device Global Registers starting at offset 800h
  91162. + */
  91163. + dwc_otg_device_global_regs_t *dev_global_regs;
  91164. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  91165. +
  91166. + /**
  91167. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  91168. + */
  91169. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  91170. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  91171. +#define DWC_EP_REG_OFFSET 0x20
  91172. +
  91173. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  91174. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  91175. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  91176. +
  91177. + /* Device configuration information */
  91178. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  91179. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  91180. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  91181. +
  91182. + /** Size of periodic FIFOs (Bytes) */
  91183. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  91184. +
  91185. + /** Size of Tx FIFOs (Bytes) */
  91186. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  91187. +
  91188. + /** Thresholding enable flags and length varaiables **/
  91189. + uint16_t rx_thr_en;
  91190. + uint16_t iso_tx_thr_en;
  91191. + uint16_t non_iso_tx_thr_en;
  91192. +
  91193. + uint16_t rx_thr_length;
  91194. + uint16_t tx_thr_length;
  91195. +
  91196. + /**
  91197. + * Pointers to the DMA Descriptors for EP0 Control
  91198. + * transfers (virtual and physical)
  91199. + */
  91200. +
  91201. + /** 2 descriptors for SETUP packets */
  91202. + dwc_dma_t dma_setup_desc_addr[2];
  91203. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  91204. +
  91205. + /** Pointer to Descriptor with latest SETUP packet */
  91206. + dwc_otg_dev_dma_desc_t *psetup;
  91207. +
  91208. + /** Index of current SETUP handler descriptor */
  91209. + uint32_t setup_desc_index;
  91210. +
  91211. + /** Descriptor for Data In or Status In phases */
  91212. + dwc_dma_t dma_in_desc_addr;
  91213. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  91214. +
  91215. + /** Descriptor for Data Out or Status Out phases */
  91216. + dwc_dma_t dma_out_desc_addr;
  91217. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  91218. +
  91219. + /** Setup Packet Detected - if set clear NAK when queueing */
  91220. + uint32_t spd;
  91221. + /** Isoc ep pointer on which incomplete happens */
  91222. + void *isoc_ep;
  91223. +
  91224. +} dwc_otg_dev_if_t;
  91225. +
  91226. +/////////////////////////////////////////////////
  91227. +// Host Mode Register Structures
  91228. +//
  91229. +/**
  91230. + * The Host Global Registers structure defines the size and relative
  91231. + * field offsets for the Host Mode Global Registers. Host Global
  91232. + * Registers offsets 400h-7FFh.
  91233. +*/
  91234. +typedef struct dwc_otg_host_global_regs {
  91235. + /** Host Configuration Register. <i>Offset: 400h</i> */
  91236. + volatile uint32_t hcfg;
  91237. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  91238. + volatile uint32_t hfir;
  91239. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  91240. + volatile uint32_t hfnum;
  91241. + /** Reserved. <i>Offset: 40Ch</i> */
  91242. + uint32_t reserved40C;
  91243. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  91244. + volatile uint32_t hptxsts;
  91245. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  91246. + volatile uint32_t haint;
  91247. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  91248. + volatile uint32_t haintmsk;
  91249. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  91250. + volatile uint32_t hflbaddr;
  91251. +} dwc_otg_host_global_regs_t;
  91252. +
  91253. +/**
  91254. + * This union represents the bit fields in the Host Configuration Register.
  91255. + * Read the register into the <i>d32</i> member then set/clear the bits using
  91256. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  91257. + */
  91258. +typedef union hcfg_data {
  91259. + /** raw register data */
  91260. + uint32_t d32;
  91261. +
  91262. + /** register bits */
  91263. + struct {
  91264. + /** FS/LS Phy Clock Select */
  91265. + unsigned fslspclksel:2;
  91266. +#define DWC_HCFG_30_60_MHZ 0
  91267. +#define DWC_HCFG_48_MHZ 1
  91268. +#define DWC_HCFG_6_MHZ 2
  91269. +
  91270. + /** FS/LS Only Support */
  91271. + unsigned fslssupp:1;
  91272. + unsigned reserved3_6:4;
  91273. + /** Enable 32-KHz Suspend Mode */
  91274. + unsigned ena32khzs:1;
  91275. + /** Resume Validation Periiod */
  91276. + unsigned resvalid:8;
  91277. + unsigned reserved16_22:7;
  91278. + /** Enable Scatter/gather DMA in Host mode */
  91279. + unsigned descdma:1;
  91280. + /** Frame List Entries */
  91281. + unsigned frlisten:2;
  91282. + /** Enable Periodic Scheduling */
  91283. + unsigned perschedena:1;
  91284. + unsigned reserved27_30:4;
  91285. + unsigned modechtimen:1;
  91286. + } b;
  91287. +} hcfg_data_t;
  91288. +
  91289. +/**
  91290. + * This union represents the bit fields in the Host Frame Remaing/Number
  91291. + * Register.
  91292. + */
  91293. +typedef union hfir_data {
  91294. + /** raw register data */
  91295. + uint32_t d32;
  91296. +
  91297. + /** register bits */
  91298. + struct {
  91299. + unsigned frint:16;
  91300. + unsigned hfirrldctrl:1;
  91301. + unsigned reserved:15;
  91302. + } b;
  91303. +} hfir_data_t;
  91304. +
  91305. +/**
  91306. + * This union represents the bit fields in the Host Frame Remaing/Number
  91307. + * Register.
  91308. + */
  91309. +typedef union hfnum_data {
  91310. + /** raw register data */
  91311. + uint32_t d32;
  91312. +
  91313. + /** register bits */
  91314. + struct {
  91315. + unsigned frnum:16;
  91316. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  91317. + unsigned frrem:16;
  91318. + } b;
  91319. +} hfnum_data_t;
  91320. +
  91321. +typedef union hptxsts_data {
  91322. + /** raw register data */
  91323. + uint32_t d32;
  91324. +
  91325. + /** register bits */
  91326. + struct {
  91327. + unsigned ptxfspcavail:16;
  91328. + unsigned ptxqspcavail:8;
  91329. + /** Top of the Periodic Transmit Request Queue
  91330. + * - bit 24 - Terminate (last entry for the selected channel)
  91331. + * - bits 26:25 - Token Type
  91332. + * - 2'b00 - Zero length
  91333. + * - 2'b01 - Ping
  91334. + * - 2'b10 - Disable
  91335. + * - bits 30:27 - Channel Number
  91336. + * - bit 31 - Odd/even microframe
  91337. + */
  91338. + unsigned ptxqtop_terminate:1;
  91339. + unsigned ptxqtop_token:2;
  91340. + unsigned ptxqtop_chnum:4;
  91341. + unsigned ptxqtop_odd:1;
  91342. + } b;
  91343. +} hptxsts_data_t;
  91344. +
  91345. +/**
  91346. + * This union represents the bit fields in the Host Port Control and Status
  91347. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91348. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91349. + * hprt0 register.
  91350. + */
  91351. +typedef union hprt0_data {
  91352. + /** raw register data */
  91353. + uint32_t d32;
  91354. + /** register bits */
  91355. + struct {
  91356. + unsigned prtconnsts:1;
  91357. + unsigned prtconndet:1;
  91358. + unsigned prtena:1;
  91359. + unsigned prtenchng:1;
  91360. + unsigned prtovrcurract:1;
  91361. + unsigned prtovrcurrchng:1;
  91362. + unsigned prtres:1;
  91363. + unsigned prtsusp:1;
  91364. + unsigned prtrst:1;
  91365. + unsigned reserved9:1;
  91366. + unsigned prtlnsts:2;
  91367. + unsigned prtpwr:1;
  91368. + unsigned prttstctl:4;
  91369. + unsigned prtspd:2;
  91370. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  91371. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  91372. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  91373. + unsigned reserved19_31:13;
  91374. + } b;
  91375. +} hprt0_data_t;
  91376. +
  91377. +/**
  91378. + * This union represents the bit fields in the Host All Interrupt
  91379. + * Register.
  91380. + */
  91381. +typedef union haint_data {
  91382. + /** raw register data */
  91383. + uint32_t d32;
  91384. + /** register bits */
  91385. + struct {
  91386. + unsigned ch0:1;
  91387. + unsigned ch1:1;
  91388. + unsigned ch2:1;
  91389. + unsigned ch3:1;
  91390. + unsigned ch4:1;
  91391. + unsigned ch5:1;
  91392. + unsigned ch6:1;
  91393. + unsigned ch7:1;
  91394. + unsigned ch8:1;
  91395. + unsigned ch9:1;
  91396. + unsigned ch10:1;
  91397. + unsigned ch11:1;
  91398. + unsigned ch12:1;
  91399. + unsigned ch13:1;
  91400. + unsigned ch14:1;
  91401. + unsigned ch15:1;
  91402. + unsigned reserved:16;
  91403. + } b;
  91404. +
  91405. + struct {
  91406. + unsigned chint:16;
  91407. + unsigned reserved:16;
  91408. + } b2;
  91409. +} haint_data_t;
  91410. +
  91411. +/**
  91412. + * This union represents the bit fields in the Host All Interrupt
  91413. + * Register.
  91414. + */
  91415. +typedef union haintmsk_data {
  91416. + /** raw register data */
  91417. + uint32_t d32;
  91418. + /** register bits */
  91419. + struct {
  91420. + unsigned ch0:1;
  91421. + unsigned ch1:1;
  91422. + unsigned ch2:1;
  91423. + unsigned ch3:1;
  91424. + unsigned ch4:1;
  91425. + unsigned ch5:1;
  91426. + unsigned ch6:1;
  91427. + unsigned ch7:1;
  91428. + unsigned ch8:1;
  91429. + unsigned ch9:1;
  91430. + unsigned ch10:1;
  91431. + unsigned ch11:1;
  91432. + unsigned ch12:1;
  91433. + unsigned ch13:1;
  91434. + unsigned ch14:1;
  91435. + unsigned ch15:1;
  91436. + unsigned reserved:16;
  91437. + } b;
  91438. +
  91439. + struct {
  91440. + unsigned chint:16;
  91441. + unsigned reserved:16;
  91442. + } b2;
  91443. +} haintmsk_data_t;
  91444. +
  91445. +/**
  91446. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  91447. + */
  91448. +typedef struct dwc_otg_hc_regs {
  91449. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  91450. + volatile uint32_t hcchar;
  91451. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  91452. + volatile uint32_t hcsplt;
  91453. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  91454. + volatile uint32_t hcint;
  91455. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  91456. + volatile uint32_t hcintmsk;
  91457. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  91458. + volatile uint32_t hctsiz;
  91459. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  91460. + volatile uint32_t hcdma;
  91461. + volatile uint32_t reserved;
  91462. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  91463. + volatile uint32_t hcdmab;
  91464. +} dwc_otg_hc_regs_t;
  91465. +
  91466. +/**
  91467. + * This union represents the bit fields in the Host Channel Characteristics
  91468. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91469. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91470. + * hcchar register.
  91471. + */
  91472. +typedef union hcchar_data {
  91473. + /** raw register data */
  91474. + uint32_t d32;
  91475. +
  91476. + /** register bits */
  91477. + struct {
  91478. + /** Maximum packet size in bytes */
  91479. + unsigned mps:11;
  91480. +
  91481. + /** Endpoint number */
  91482. + unsigned epnum:4;
  91483. +
  91484. + /** 0: OUT, 1: IN */
  91485. + unsigned epdir:1;
  91486. +
  91487. + unsigned reserved:1;
  91488. +
  91489. + /** 0: Full/high speed device, 1: Low speed device */
  91490. + unsigned lspddev:1;
  91491. +
  91492. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  91493. + unsigned eptype:2;
  91494. +
  91495. + /** Packets per frame for periodic transfers. 0 is reserved. */
  91496. + unsigned multicnt:2;
  91497. +
  91498. + /** Device address */
  91499. + unsigned devaddr:7;
  91500. +
  91501. + /**
  91502. + * Frame to transmit periodic transaction.
  91503. + * 0: even, 1: odd
  91504. + */
  91505. + unsigned oddfrm:1;
  91506. +
  91507. + /** Channel disable */
  91508. + unsigned chdis:1;
  91509. +
  91510. + /** Channel enable */
  91511. + unsigned chen:1;
  91512. + } b;
  91513. +} hcchar_data_t;
  91514. +
  91515. +typedef union hcsplt_data {
  91516. + /** raw register data */
  91517. + uint32_t d32;
  91518. +
  91519. + /** register bits */
  91520. + struct {
  91521. + /** Port Address */
  91522. + unsigned prtaddr:7;
  91523. +
  91524. + /** Hub Address */
  91525. + unsigned hubaddr:7;
  91526. +
  91527. + /** Transaction Position */
  91528. + unsigned xactpos:2;
  91529. +#define DWC_HCSPLIT_XACTPOS_MID 0
  91530. +#define DWC_HCSPLIT_XACTPOS_END 1
  91531. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  91532. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  91533. +
  91534. + /** Do Complete Split */
  91535. + unsigned compsplt:1;
  91536. +
  91537. + /** Reserved */
  91538. + unsigned reserved:14;
  91539. +
  91540. + /** Split Enble */
  91541. + unsigned spltena:1;
  91542. + } b;
  91543. +} hcsplt_data_t;
  91544. +
  91545. +/**
  91546. + * This union represents the bit fields in the Host All Interrupt
  91547. + * Register.
  91548. + */
  91549. +typedef union hcint_data {
  91550. + /** raw register data */
  91551. + uint32_t d32;
  91552. + /** register bits */
  91553. + struct {
  91554. + /** Transfer Complete */
  91555. + unsigned xfercomp:1;
  91556. + /** Channel Halted */
  91557. + unsigned chhltd:1;
  91558. + /** AHB Error */
  91559. + unsigned ahberr:1;
  91560. + /** STALL Response Received */
  91561. + unsigned stall:1;
  91562. + /** NAK Response Received */
  91563. + unsigned nak:1;
  91564. + /** ACK Response Received */
  91565. + unsigned ack:1;
  91566. + /** NYET Response Received */
  91567. + unsigned nyet:1;
  91568. + /** Transaction Err */
  91569. + unsigned xacterr:1;
  91570. + /** Babble Error */
  91571. + unsigned bblerr:1;
  91572. + /** Frame Overrun */
  91573. + unsigned frmovrun:1;
  91574. + /** Data Toggle Error */
  91575. + unsigned datatglerr:1;
  91576. + /** Buffer Not Available (only for DDMA mode) */
  91577. + unsigned bna:1;
  91578. + /** Exessive transaction error (only for DDMA mode) */
  91579. + unsigned xcs_xact:1;
  91580. + /** Frame List Rollover interrupt */
  91581. + unsigned frm_list_roll:1;
  91582. + /** Reserved */
  91583. + unsigned reserved14_31:18;
  91584. + } b;
  91585. +} hcint_data_t;
  91586. +
  91587. +/**
  91588. + * This union represents the bit fields in the Host Channel Interrupt Mask
  91589. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91590. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91591. + * hcintmsk register.
  91592. + */
  91593. +typedef union hcintmsk_data {
  91594. + /** raw register data */
  91595. + uint32_t d32;
  91596. +
  91597. + /** register bits */
  91598. + struct {
  91599. + unsigned xfercompl:1;
  91600. + unsigned chhltd:1;
  91601. + unsigned ahberr:1;
  91602. + unsigned stall:1;
  91603. + unsigned nak:1;
  91604. + unsigned ack:1;
  91605. + unsigned nyet:1;
  91606. + unsigned xacterr:1;
  91607. + unsigned bblerr:1;
  91608. + unsigned frmovrun:1;
  91609. + unsigned datatglerr:1;
  91610. + unsigned bna:1;
  91611. + unsigned xcs_xact:1;
  91612. + unsigned frm_list_roll:1;
  91613. + unsigned reserved14_31:18;
  91614. + } b;
  91615. +} hcintmsk_data_t;
  91616. +
  91617. +/**
  91618. + * This union represents the bit fields in the Host Channel Transfer Size
  91619. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91620. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  91621. + * hcchar register.
  91622. + */
  91623. +
  91624. +typedef union hctsiz_data {
  91625. + /** raw register data */
  91626. + uint32_t d32;
  91627. +
  91628. + /** register bits */
  91629. + struct {
  91630. + /** Total transfer size in bytes */
  91631. + unsigned xfersize:19;
  91632. +
  91633. + /** Data packets to transfer */
  91634. + unsigned pktcnt:10;
  91635. +
  91636. + /**
  91637. + * Packet ID for next data packet
  91638. + * 0: DATA0
  91639. + * 1: DATA2
  91640. + * 2: DATA1
  91641. + * 3: MDATA (non-Control), SETUP (Control)
  91642. + */
  91643. + unsigned pid:2;
  91644. +#define DWC_HCTSIZ_DATA0 0
  91645. +#define DWC_HCTSIZ_DATA1 2
  91646. +#define DWC_HCTSIZ_DATA2 1
  91647. +#define DWC_HCTSIZ_MDATA 3
  91648. +#define DWC_HCTSIZ_SETUP 3
  91649. +
  91650. + /** Do PING protocol when 1 */
  91651. + unsigned dopng:1;
  91652. + } b;
  91653. +
  91654. + /** register bits */
  91655. + struct {
  91656. + /** Scheduling information */
  91657. + unsigned schinfo:8;
  91658. +
  91659. + /** Number of transfer descriptors.
  91660. + * Max value:
  91661. + * 64 in general,
  91662. + * 256 only for HS isochronous endpoint.
  91663. + */
  91664. + unsigned ntd:8;
  91665. +
  91666. + /** Data packets to transfer */
  91667. + unsigned reserved16_28:13;
  91668. +
  91669. + /**
  91670. + * Packet ID for next data packet
  91671. + * 0: DATA0
  91672. + * 1: DATA2
  91673. + * 2: DATA1
  91674. + * 3: MDATA (non-Control)
  91675. + */
  91676. + unsigned pid:2;
  91677. +
  91678. + /** Do PING protocol when 1 */
  91679. + unsigned dopng:1;
  91680. + } b_ddma;
  91681. +} hctsiz_data_t;
  91682. +
  91683. +/**
  91684. + * This union represents the bit fields in the Host DMA Address
  91685. + * Register used in Descriptor DMA mode.
  91686. + */
  91687. +typedef union hcdma_data {
  91688. + /** raw register data */
  91689. + uint32_t d32;
  91690. + /** register bits */
  91691. + struct {
  91692. + unsigned reserved0_2:3;
  91693. + /** Current Transfer Descriptor. Not used for ISOC */
  91694. + unsigned ctd:8;
  91695. + /** Start Address of Descriptor List */
  91696. + unsigned dma_addr:21;
  91697. + } b;
  91698. +} hcdma_data_t;
  91699. +
  91700. +/**
  91701. + * This union represents the bit fields in the DMA Descriptor
  91702. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  91703. + * set/clear the bits using the <i>b</i>it elements.
  91704. + */
  91705. +typedef union host_dma_desc_sts {
  91706. + /** raw register data */
  91707. + uint32_t d32;
  91708. + /** quadlet bits */
  91709. +
  91710. + /* for non-isochronous */
  91711. + struct {
  91712. + /** Number of bytes */
  91713. + unsigned n_bytes:17;
  91714. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  91715. + unsigned qtd_offset:6;
  91716. + /**
  91717. + * Set to request the core to jump to alternate QTD if
  91718. + * Short Packet received - only for IN EPs
  91719. + */
  91720. + unsigned a_qtd:1;
  91721. + /**
  91722. + * Setup Packet bit. When set indicates that buffer contains
  91723. + * setup packet.
  91724. + */
  91725. + unsigned sup:1;
  91726. + /** Interrupt On Complete */
  91727. + unsigned ioc:1;
  91728. + /** End of List */
  91729. + unsigned eol:1;
  91730. + unsigned reserved27:1;
  91731. + /** Rx/Tx Status */
  91732. + unsigned sts:2;
  91733. +#define DMA_DESC_STS_PKTERR 1
  91734. + unsigned reserved30:1;
  91735. + /** Active Bit */
  91736. + unsigned a:1;
  91737. + } b;
  91738. + /* for isochronous */
  91739. + struct {
  91740. + /** Number of bytes */
  91741. + unsigned n_bytes:12;
  91742. + unsigned reserved12_24:13;
  91743. + /** Interrupt On Complete */
  91744. + unsigned ioc:1;
  91745. + unsigned reserved26_27:2;
  91746. + /** Rx/Tx Status */
  91747. + unsigned sts:2;
  91748. + unsigned reserved30:1;
  91749. + /** Active Bit */
  91750. + unsigned a:1;
  91751. + } b_isoc;
  91752. +} host_dma_desc_sts_t;
  91753. +
  91754. +#define MAX_DMA_DESC_SIZE 131071
  91755. +#define MAX_DMA_DESC_NUM_GENERIC 64
  91756. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  91757. +#define MAX_FRLIST_EN_NUM 64
  91758. +/**
  91759. + * Host-mode DMA Descriptor structure
  91760. + *
  91761. + * DMA Descriptor structure contains two quadlets:
  91762. + * Status quadlet and Data buffer pointer.
  91763. + */
  91764. +typedef struct dwc_otg_host_dma_desc {
  91765. + /** DMA Descriptor status quadlet */
  91766. + host_dma_desc_sts_t status;
  91767. + /** DMA Descriptor data buffer pointer */
  91768. + uint32_t buf;
  91769. +} dwc_otg_host_dma_desc_t;
  91770. +
  91771. +/** OTG Host Interface Structure.
  91772. + *
  91773. + * The OTG Host Interface Structure structure contains information
  91774. + * needed to manage the DWC_otg controller acting in host mode. It
  91775. + * represents the programming view of the host-specific aspects of the
  91776. + * controller.
  91777. + */
  91778. +typedef struct dwc_otg_host_if {
  91779. + /** Host Global Registers starting at offset 400h.*/
  91780. + dwc_otg_host_global_regs_t *host_global_regs;
  91781. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  91782. +
  91783. + /** Host Port 0 Control and Status Register */
  91784. + volatile uint32_t *hprt0;
  91785. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  91786. +
  91787. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  91788. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  91789. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  91790. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  91791. +
  91792. + /* Host configuration information */
  91793. + /** Number of Host Channels (range: 1-16) */
  91794. + uint8_t num_host_channels;
  91795. + /** Periodic EPs supported (0: no, 1: yes) */
  91796. + uint8_t perio_eps_supported;
  91797. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  91798. + uint16_t perio_tx_fifo_size;
  91799. +
  91800. +} dwc_otg_host_if_t;
  91801. +
  91802. +/**
  91803. + * This union represents the bit fields in the Power and Clock Gating Control
  91804. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91805. + * bits using the <i>b</i>it elements.
  91806. + */
  91807. +typedef union pcgcctl_data {
  91808. + /** raw register data */
  91809. + uint32_t d32;
  91810. +
  91811. + /** register bits */
  91812. + struct {
  91813. + /** Stop Pclk */
  91814. + unsigned stoppclk:1;
  91815. + /** Gate Hclk */
  91816. + unsigned gatehclk:1;
  91817. + /** Power Clamp */
  91818. + unsigned pwrclmp:1;
  91819. + /** Reset Power Down Modules */
  91820. + unsigned rstpdwnmodule:1;
  91821. + /** Reserved */
  91822. + unsigned reserved:1;
  91823. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  91824. + unsigned enbl_sleep_gating:1;
  91825. + /** PHY In Sleep (PhySleep) */
  91826. + unsigned phy_in_sleep:1;
  91827. + /** Deep Sleep*/
  91828. + unsigned deep_sleep:1;
  91829. + unsigned resetaftsusp:1;
  91830. + unsigned restoremode:1;
  91831. + unsigned enbl_extnd_hiber:1;
  91832. + unsigned extnd_hiber_pwrclmp:1;
  91833. + unsigned extnd_hiber_switch:1;
  91834. + unsigned ess_reg_restored:1;
  91835. + unsigned prt_clk_sel:2;
  91836. + unsigned port_power:1;
  91837. + unsigned max_xcvrselect:2;
  91838. + unsigned max_termsel:1;
  91839. + unsigned mac_dev_addr:7;
  91840. + unsigned p2hd_dev_enum_spd:2;
  91841. + unsigned p2hd_prt_spd:2;
  91842. + unsigned if_dev_mode:1;
  91843. + } b;
  91844. +} pcgcctl_data_t;
  91845. +
  91846. +/**
  91847. + * This union represents the bit fields in the Global Data FIFO Software
  91848. + * Configuration Register. Read the register into the <i>d32</i> member then
  91849. + * set/clear the bits using the <i>b</i>it elements.
  91850. + */
  91851. +typedef union gdfifocfg_data {
  91852. + /* raw register data */
  91853. + uint32_t d32;
  91854. + /** register bits */
  91855. + struct {
  91856. + /** OTG Data FIFO depth */
  91857. + unsigned gdfifocfg:16;
  91858. + /** Start address of EP info controller */
  91859. + unsigned epinfobase:16;
  91860. + } b;
  91861. +} gdfifocfg_data_t;
  91862. +
  91863. +/**
  91864. + * This union represents the bit fields in the Global Power Down Register
  91865. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91866. + * bits using the <i>b</i>it elements.
  91867. + */
  91868. +typedef union gpwrdn_data {
  91869. + /* raw register data */
  91870. + uint32_t d32;
  91871. +
  91872. + /** register bits */
  91873. + struct {
  91874. + /** PMU Interrupt Select */
  91875. + unsigned pmuintsel:1;
  91876. + /** PMU Active */
  91877. + unsigned pmuactv:1;
  91878. + /** Restore */
  91879. + unsigned restore:1;
  91880. + /** Power Down Clamp */
  91881. + unsigned pwrdnclmp:1;
  91882. + /** Power Down Reset */
  91883. + unsigned pwrdnrstn:1;
  91884. + /** Power Down Switch */
  91885. + unsigned pwrdnswtch:1;
  91886. + /** Disable VBUS */
  91887. + unsigned dis_vbus:1;
  91888. + /** Line State Change */
  91889. + unsigned lnstschng:1;
  91890. + /** Line state change mask */
  91891. + unsigned lnstchng_msk:1;
  91892. + /** Reset Detected */
  91893. + unsigned rst_det:1;
  91894. + /** Reset Detect mask */
  91895. + unsigned rst_det_msk:1;
  91896. + /** Disconnect Detected */
  91897. + unsigned disconn_det:1;
  91898. + /** Disconnect Detect mask */
  91899. + unsigned disconn_det_msk:1;
  91900. + /** Connect Detected*/
  91901. + unsigned connect_det:1;
  91902. + /** Connect Detected Mask*/
  91903. + unsigned connect_det_msk:1;
  91904. + /** SRP Detected */
  91905. + unsigned srp_det:1;
  91906. + /** SRP Detect mask */
  91907. + unsigned srp_det_msk:1;
  91908. + /** Status Change Interrupt */
  91909. + unsigned sts_chngint:1;
  91910. + /** Status Change Interrupt Mask */
  91911. + unsigned sts_chngint_msk:1;
  91912. + /** Line State */
  91913. + unsigned linestate:2;
  91914. + /** Indicates current mode(status of IDDIG signal) */
  91915. + unsigned idsts:1;
  91916. + /** B Session Valid signal status*/
  91917. + unsigned bsessvld:1;
  91918. + /** ADP Event Detected */
  91919. + unsigned adp_int:1;
  91920. + /** Multi Valued ID pin */
  91921. + unsigned mult_val_id_bc:5;
  91922. + /** Reserved 24_31 */
  91923. + unsigned reserved29_31:3;
  91924. + } b;
  91925. +} gpwrdn_data_t;
  91926. +
  91927. +#endif
  91928. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/Makefile linux-rpi/drivers/usb/host/dwc_otg/Makefile
  91929. --- linux-3.10.37/drivers/usb/host/dwc_otg/Makefile 1970-01-01 01:00:00.000000000 +0100
  91930. +++ linux-rpi/drivers/usb/host/dwc_otg/Makefile 2014-04-24 15:35:04.169565731 +0200
  91931. @@ -0,0 +1,81 @@
  91932. +#
  91933. +# Makefile for DWC_otg Highspeed USB controller driver
  91934. +#
  91935. +
  91936. +ifneq ($(KERNELRELEASE),)
  91937. +
  91938. +# Use the BUS_INTERFACE variable to compile the software for either
  91939. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  91940. +ifeq ($(BUS_INTERFACE),)
  91941. +# BUS_INTERFACE = -DPCI_INTERFACE
  91942. +# BUS_INTERFACE = -DLM_INTERFACE
  91943. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  91944. +endif
  91945. +
  91946. +#ccflags-y += -DDEBUG
  91947. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  91948. +
  91949. +# Use one of the following flags to compile the software in host-only or
  91950. +# device-only mode.
  91951. +#ccflags-y += -DDWC_HOST_ONLY
  91952. +#ccflags-y += -DDWC_DEVICE_ONLY
  91953. +
  91954. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  91955. +#ccflags-y += -DDWC_EN_ISOC
  91956. +ccflags-y += -I$(obj)/../dwc_common_port
  91957. +#ccflags-y += -I$(PORTLIB)
  91958. +ccflags-y += -DDWC_LINUX
  91959. +ccflags-y += $(CFI)
  91960. +ccflags-y += $(BUS_INTERFACE)
  91961. +#ccflags-y += -DDWC_DEV_SRPCAP
  91962. +
  91963. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  91964. +
  91965. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  91966. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  91967. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  91968. +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
  91969. +dwc_otg-objs += dwc_otg_adp.o
  91970. +dwc_otg-objs += dwc_otg_mphi_fix.o
  91971. +ifneq ($(CFI),)
  91972. +dwc_otg-objs += dwc_otg_cfi.o
  91973. +endif
  91974. +
  91975. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  91976. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  91977. +
  91978. +ifneq ($(kernrel3),2.6.20)
  91979. +ccflags-y += $(CPPFLAGS)
  91980. +endif
  91981. +
  91982. +else
  91983. +
  91984. +PWD := $(shell pwd)
  91985. +PORTLIB := $(PWD)/../dwc_common_port
  91986. +
  91987. +# Command paths
  91988. +CTAGS := $(CTAGS)
  91989. +DOXYGEN := $(DOXYGEN)
  91990. +
  91991. +default: portlib
  91992. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  91993. +
  91994. +install: default
  91995. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  91996. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  91997. +
  91998. +portlib:
  91999. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  92000. + cp $(PORTLIB)/Module.symvers $(PWD)/
  92001. +
  92002. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  92003. + $(DOXYGEN) doc/doxygen.cfg
  92004. +
  92005. +tags: $(wildcard *.[hc])
  92006. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  92007. +
  92008. +
  92009. +clean:
  92010. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  92011. +
  92012. +endif
  92013. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm linux-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  92014. --- linux-3.10.37/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 1970-01-01 01:00:00.000000000 +0100
  92015. +++ linux-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 2014-04-24 15:35:04.177565820 +0200
  92016. @@ -0,0 +1,337 @@
  92017. +package dwc_otg_test;
  92018. +
  92019. +use strict;
  92020. +use Exporter ();
  92021. +
  92022. +use vars qw(@ISA @EXPORT
  92023. +$sysfsdir $paramdir $errors $params
  92024. +);
  92025. +
  92026. +@ISA = qw(Exporter);
  92027. +
  92028. +#
  92029. +# Globals
  92030. +#
  92031. +$sysfsdir = "/sys/devices/lm0";
  92032. +$paramdir = "/sys/module/dwc_otg";
  92033. +$errors = 0;
  92034. +
  92035. +$params = [
  92036. + {
  92037. + NAME => "otg_cap",
  92038. + DEFAULT => 0,
  92039. + ENUM => [],
  92040. + LOW => 0,
  92041. + HIGH => 2
  92042. + },
  92043. + {
  92044. + NAME => "dma_enable",
  92045. + DEFAULT => 0,
  92046. + ENUM => [],
  92047. + LOW => 0,
  92048. + HIGH => 1
  92049. + },
  92050. + {
  92051. + NAME => "dma_burst_size",
  92052. + DEFAULT => 32,
  92053. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  92054. + LOW => 1,
  92055. + HIGH => 256
  92056. + },
  92057. + {
  92058. + NAME => "host_speed",
  92059. + DEFAULT => 0,
  92060. + ENUM => [],
  92061. + LOW => 0,
  92062. + HIGH => 1
  92063. + },
  92064. + {
  92065. + NAME => "host_support_fs_ls_low_power",
  92066. + DEFAULT => 0,
  92067. + ENUM => [],
  92068. + LOW => 0,
  92069. + HIGH => 1
  92070. + },
  92071. + {
  92072. + NAME => "host_ls_low_power_phy_clk",
  92073. + DEFAULT => 0,
  92074. + ENUM => [],
  92075. + LOW => 0,
  92076. + HIGH => 1
  92077. + },
  92078. + {
  92079. + NAME => "dev_speed",
  92080. + DEFAULT => 0,
  92081. + ENUM => [],
  92082. + LOW => 0,
  92083. + HIGH => 1
  92084. + },
  92085. + {
  92086. + NAME => "enable_dynamic_fifo",
  92087. + DEFAULT => 1,
  92088. + ENUM => [],
  92089. + LOW => 0,
  92090. + HIGH => 1
  92091. + },
  92092. + {
  92093. + NAME => "data_fifo_size",
  92094. + DEFAULT => 8192,
  92095. + ENUM => [],
  92096. + LOW => 32,
  92097. + HIGH => 32768
  92098. + },
  92099. + {
  92100. + NAME => "dev_rx_fifo_size",
  92101. + DEFAULT => 1064,
  92102. + ENUM => [],
  92103. + LOW => 16,
  92104. + HIGH => 32768
  92105. + },
  92106. + {
  92107. + NAME => "dev_nperio_tx_fifo_size",
  92108. + DEFAULT => 1024,
  92109. + ENUM => [],
  92110. + LOW => 16,
  92111. + HIGH => 32768
  92112. + },
  92113. + {
  92114. + NAME => "dev_perio_tx_fifo_size_1",
  92115. + DEFAULT => 256,
  92116. + ENUM => [],
  92117. + LOW => 4,
  92118. + HIGH => 768
  92119. + },
  92120. + {
  92121. + NAME => "dev_perio_tx_fifo_size_2",
  92122. + DEFAULT => 256,
  92123. + ENUM => [],
  92124. + LOW => 4,
  92125. + HIGH => 768
  92126. + },
  92127. + {
  92128. + NAME => "dev_perio_tx_fifo_size_3",
  92129. + DEFAULT => 256,
  92130. + ENUM => [],
  92131. + LOW => 4,
  92132. + HIGH => 768
  92133. + },
  92134. + {
  92135. + NAME => "dev_perio_tx_fifo_size_4",
  92136. + DEFAULT => 256,
  92137. + ENUM => [],
  92138. + LOW => 4,
  92139. + HIGH => 768
  92140. + },
  92141. + {
  92142. + NAME => "dev_perio_tx_fifo_size_5",
  92143. + DEFAULT => 256,
  92144. + ENUM => [],
  92145. + LOW => 4,
  92146. + HIGH => 768
  92147. + },
  92148. + {
  92149. + NAME => "dev_perio_tx_fifo_size_6",
  92150. + DEFAULT => 256,
  92151. + ENUM => [],
  92152. + LOW => 4,
  92153. + HIGH => 768
  92154. + },
  92155. + {
  92156. + NAME => "dev_perio_tx_fifo_size_7",
  92157. + DEFAULT => 256,
  92158. + ENUM => [],
  92159. + LOW => 4,
  92160. + HIGH => 768
  92161. + },
  92162. + {
  92163. + NAME => "dev_perio_tx_fifo_size_8",
  92164. + DEFAULT => 256,
  92165. + ENUM => [],
  92166. + LOW => 4,
  92167. + HIGH => 768
  92168. + },
  92169. + {
  92170. + NAME => "dev_perio_tx_fifo_size_9",
  92171. + DEFAULT => 256,
  92172. + ENUM => [],
  92173. + LOW => 4,
  92174. + HIGH => 768
  92175. + },
  92176. + {
  92177. + NAME => "dev_perio_tx_fifo_size_10",
  92178. + DEFAULT => 256,
  92179. + ENUM => [],
  92180. + LOW => 4,
  92181. + HIGH => 768
  92182. + },
  92183. + {
  92184. + NAME => "dev_perio_tx_fifo_size_11",
  92185. + DEFAULT => 256,
  92186. + ENUM => [],
  92187. + LOW => 4,
  92188. + HIGH => 768
  92189. + },
  92190. + {
  92191. + NAME => "dev_perio_tx_fifo_size_12",
  92192. + DEFAULT => 256,
  92193. + ENUM => [],
  92194. + LOW => 4,
  92195. + HIGH => 768
  92196. + },
  92197. + {
  92198. + NAME => "dev_perio_tx_fifo_size_13",
  92199. + DEFAULT => 256,
  92200. + ENUM => [],
  92201. + LOW => 4,
  92202. + HIGH => 768
  92203. + },
  92204. + {
  92205. + NAME => "dev_perio_tx_fifo_size_14",
  92206. + DEFAULT => 256,
  92207. + ENUM => [],
  92208. + LOW => 4,
  92209. + HIGH => 768
  92210. + },
  92211. + {
  92212. + NAME => "dev_perio_tx_fifo_size_15",
  92213. + DEFAULT => 256,
  92214. + ENUM => [],
  92215. + LOW => 4,
  92216. + HIGH => 768
  92217. + },
  92218. + {
  92219. + NAME => "host_rx_fifo_size",
  92220. + DEFAULT => 1024,
  92221. + ENUM => [],
  92222. + LOW => 16,
  92223. + HIGH => 32768
  92224. + },
  92225. + {
  92226. + NAME => "host_nperio_tx_fifo_size",
  92227. + DEFAULT => 1024,
  92228. + ENUM => [],
  92229. + LOW => 16,
  92230. + HIGH => 32768
  92231. + },
  92232. + {
  92233. + NAME => "host_perio_tx_fifo_size",
  92234. + DEFAULT => 1024,
  92235. + ENUM => [],
  92236. + LOW => 16,
  92237. + HIGH => 32768
  92238. + },
  92239. + {
  92240. + NAME => "max_transfer_size",
  92241. + DEFAULT => 65535,
  92242. + ENUM => [],
  92243. + LOW => 2047,
  92244. + HIGH => 65535
  92245. + },
  92246. + {
  92247. + NAME => "max_packet_count",
  92248. + DEFAULT => 511,
  92249. + ENUM => [],
  92250. + LOW => 15,
  92251. + HIGH => 511
  92252. + },
  92253. + {
  92254. + NAME => "host_channels",
  92255. + DEFAULT => 12,
  92256. + ENUM => [],
  92257. + LOW => 1,
  92258. + HIGH => 16
  92259. + },
  92260. + {
  92261. + NAME => "dev_endpoints",
  92262. + DEFAULT => 6,
  92263. + ENUM => [],
  92264. + LOW => 1,
  92265. + HIGH => 15
  92266. + },
  92267. + {
  92268. + NAME => "phy_type",
  92269. + DEFAULT => 1,
  92270. + ENUM => [],
  92271. + LOW => 0,
  92272. + HIGH => 2
  92273. + },
  92274. + {
  92275. + NAME => "phy_utmi_width",
  92276. + DEFAULT => 16,
  92277. + ENUM => [8, 16],
  92278. + LOW => 8,
  92279. + HIGH => 16
  92280. + },
  92281. + {
  92282. + NAME => "phy_ulpi_ddr",
  92283. + DEFAULT => 0,
  92284. + ENUM => [],
  92285. + LOW => 0,
  92286. + HIGH => 1
  92287. + },
  92288. + ];
  92289. +
  92290. +
  92291. +#
  92292. +#
  92293. +sub check_arch {
  92294. + $_ = `uname -m`;
  92295. + chomp;
  92296. + unless (m/armv4tl/) {
  92297. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  92298. + return 0;
  92299. + }
  92300. + return 1;
  92301. +}
  92302. +
  92303. +#
  92304. +#
  92305. +sub load_module {
  92306. + my $params = shift;
  92307. + print "\nRemoving Module\n";
  92308. + system "rmmod dwc_otg";
  92309. + print "Loading Module\n";
  92310. + if ($params ne "") {
  92311. + print "Module Parameters: $params\n";
  92312. + }
  92313. + if (system("modprobe dwc_otg $params")) {
  92314. + warn "Unable to load module\n";
  92315. + return 0;
  92316. + }
  92317. + return 1;
  92318. +}
  92319. +
  92320. +#
  92321. +#
  92322. +sub test_status {
  92323. + my $arg = shift;
  92324. +
  92325. + print "\n";
  92326. +
  92327. + if (defined $arg) {
  92328. + warn "WARNING: $arg\n";
  92329. + }
  92330. +
  92331. + if ($errors > 0) {
  92332. + warn "TEST FAILED with $errors errors\n";
  92333. + return 0;
  92334. + } else {
  92335. + print "TEST PASSED\n";
  92336. + return 0 if (defined $arg);
  92337. + }
  92338. + return 1;
  92339. +}
  92340. +
  92341. +#
  92342. +#
  92343. +@EXPORT = qw(
  92344. +$sysfsdir
  92345. +$paramdir
  92346. +$params
  92347. +$errors
  92348. +check_arch
  92349. +load_module
  92350. +test_status
  92351. +);
  92352. +
  92353. +1;
  92354. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/test/Makefile linux-rpi/drivers/usb/host/dwc_otg/test/Makefile
  92355. --- linux-3.10.37/drivers/usb/host/dwc_otg/test/Makefile 1970-01-01 01:00:00.000000000 +0100
  92356. +++ linux-rpi/drivers/usb/host/dwc_otg/test/Makefile 2014-04-24 15:35:04.177565820 +0200
  92357. @@ -0,0 +1,16 @@
  92358. +
  92359. +PERL=/usr/bin/perl
  92360. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  92361. +
  92362. +.PHONY : test
  92363. +test : perl_tests
  92364. +
  92365. +perl_tests :
  92366. + @echo
  92367. + @echo Running perl tests
  92368. + @for test in $(PL_TESTS); do \
  92369. + if $(PERL) ./$$test ; then \
  92370. + echo "=======> $$test, PASSED" ; \
  92371. + else echo "=======> $$test, FAILED" ; \
  92372. + fi \
  92373. + done
  92374. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/test/test_mod_param.pl linux-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  92375. --- linux-3.10.37/drivers/usb/host/dwc_otg/test/test_mod_param.pl 1970-01-01 01:00:00.000000000 +0100
  92376. +++ linux-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl 2014-04-24 15:35:04.177565820 +0200
  92377. @@ -0,0 +1,133 @@
  92378. +#!/usr/bin/perl -w
  92379. +#
  92380. +# Run this program on the integrator.
  92381. +#
  92382. +# - Tests module parameter default values.
  92383. +# - Tests setting of valid module parameter values via modprobe.
  92384. +# - Tests invalid module parameter values.
  92385. +# -----------------------------------------------------------------------------
  92386. +use strict;
  92387. +use dwc_otg_test;
  92388. +
  92389. +check_arch() or die;
  92390. +
  92391. +#
  92392. +#
  92393. +sub test {
  92394. + my ($param,$expected) = @_;
  92395. + my $value = get($param);
  92396. +
  92397. + if ($value == $expected) {
  92398. + print "$param = $value, okay\n";
  92399. + }
  92400. +
  92401. + else {
  92402. + warn "ERROR: value of $param != $expected, $value\n";
  92403. + $errors ++;
  92404. + }
  92405. +}
  92406. +
  92407. +#
  92408. +#
  92409. +sub get {
  92410. + my $param = shift;
  92411. + my $tmp = `cat $paramdir/$param`;
  92412. + chomp $tmp;
  92413. + return $tmp;
  92414. +}
  92415. +
  92416. +#
  92417. +#
  92418. +sub test_main {
  92419. +
  92420. + print "\nTesting Module Parameters\n";
  92421. +
  92422. + load_module("") or die;
  92423. +
  92424. + # Test initial values
  92425. + print "\nTesting Default Values\n";
  92426. + foreach (@{$params}) {
  92427. + test ($_->{NAME}, $_->{DEFAULT});
  92428. + }
  92429. +
  92430. + # Test low value
  92431. + print "\nTesting Low Value\n";
  92432. + my $cmd_params = "";
  92433. + foreach (@{$params}) {
  92434. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  92435. + }
  92436. + load_module($cmd_params) or die;
  92437. +
  92438. + foreach (@{$params}) {
  92439. + test ($_->{NAME}, $_->{LOW});
  92440. + }
  92441. +
  92442. + # Test high value
  92443. + print "\nTesting High Value\n";
  92444. + $cmd_params = "";
  92445. + foreach (@{$params}) {
  92446. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  92447. + }
  92448. + load_module($cmd_params) or die;
  92449. +
  92450. + foreach (@{$params}) {
  92451. + test ($_->{NAME}, $_->{HIGH});
  92452. + }
  92453. +
  92454. + # Test Enum
  92455. + print "\nTesting Enumerated\n";
  92456. + foreach (@{$params}) {
  92457. + if (defined $_->{ENUM}) {
  92458. + my $value;
  92459. + foreach $value (@{$_->{ENUM}}) {
  92460. + $cmd_params = "$_->{NAME}=$value";
  92461. + load_module($cmd_params) or die;
  92462. + test ($_->{NAME}, $value);
  92463. + }
  92464. + }
  92465. + }
  92466. +
  92467. + # Test Invalid Values
  92468. + print "\nTesting Invalid Values\n";
  92469. + $cmd_params = "";
  92470. + foreach (@{$params}) {
  92471. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  92472. + }
  92473. + load_module($cmd_params) or die;
  92474. +
  92475. + foreach (@{$params}) {
  92476. + test ($_->{NAME}, $_->{DEFAULT});
  92477. + }
  92478. +
  92479. + $cmd_params = "";
  92480. + foreach (@{$params}) {
  92481. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  92482. + }
  92483. + load_module($cmd_params) or die;
  92484. +
  92485. + foreach (@{$params}) {
  92486. + test ($_->{NAME}, $_->{DEFAULT});
  92487. + }
  92488. +
  92489. + print "\nTesting Enumerated\n";
  92490. + foreach (@{$params}) {
  92491. + if (defined $_->{ENUM}) {
  92492. + my $value;
  92493. + foreach $value (@{$_->{ENUM}}) {
  92494. + $value = $value + 1;
  92495. + $cmd_params = "$_->{NAME}=$value";
  92496. + load_module($cmd_params) or die;
  92497. + test ($_->{NAME}, $_->{DEFAULT});
  92498. + $value = $value - 2;
  92499. + $cmd_params = "$_->{NAME}=$value";
  92500. + load_module($cmd_params) or die;
  92501. + test ($_->{NAME}, $_->{DEFAULT});
  92502. + }
  92503. + }
  92504. + }
  92505. +
  92506. + test_status() or die;
  92507. +}
  92508. +
  92509. +test_main();
  92510. +0;
  92511. diff -Nur linux-3.10.37/drivers/usb/host/dwc_otg/test/test_sysfs.pl linux-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  92512. --- linux-3.10.37/drivers/usb/host/dwc_otg/test/test_sysfs.pl 1970-01-01 01:00:00.000000000 +0100
  92513. +++ linux-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl 2014-04-24 15:35:04.177565820 +0200
  92514. @@ -0,0 +1,193 @@
  92515. +#!/usr/bin/perl -w
  92516. +#
  92517. +# Run this program on the integrator
  92518. +# - Tests select sysfs attributes.
  92519. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  92520. +# -----------------------------------------------------------------------------
  92521. +use strict;
  92522. +use dwc_otg_test;
  92523. +
  92524. +check_arch() or die;
  92525. +
  92526. +#
  92527. +#
  92528. +sub test {
  92529. + my ($attr,$expected) = @_;
  92530. + my $string = get($attr);
  92531. +
  92532. + if ($string eq $expected) {
  92533. + printf("$attr = $string, okay\n");
  92534. + }
  92535. + else {
  92536. + warn "ERROR: value of $attr != $expected, $string\n";
  92537. + $errors ++;
  92538. + }
  92539. +}
  92540. +
  92541. +#
  92542. +#
  92543. +sub set {
  92544. + my ($reg, $value) = @_;
  92545. + system "echo $value > $sysfsdir/$reg";
  92546. +}
  92547. +
  92548. +#
  92549. +#
  92550. +sub get {
  92551. + my $attr = shift;
  92552. + my $string = `cat $sysfsdir/$attr`;
  92553. + chomp $string;
  92554. + if ($string =~ m/\s\=\s/) {
  92555. + my $tmp;
  92556. + ($tmp, $string) = split /\s=\s/, $string;
  92557. + }
  92558. + return $string;
  92559. +}
  92560. +
  92561. +#
  92562. +#
  92563. +sub test_main {
  92564. + print("\nTesting Sysfs Attributes\n");
  92565. +
  92566. + load_module("") or die;
  92567. +
  92568. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  92569. + print("\nTesting Default Values\n");
  92570. +
  92571. + test("regoffset", "0xffffffff");
  92572. + test("regvalue", "invalid offset");
  92573. + test("guid", "0x12345678"); # this will fail if it has been changed
  92574. + test("gsnpsid", "0x4f54200a");
  92575. +
  92576. + # Test operation of regoffset/regvalue
  92577. + print("\nTesting regoffset\n");
  92578. + set('regoffset', '5a5a5a5a');
  92579. + test("regoffset", "0xffffffff");
  92580. +
  92581. + set('regoffset', '0');
  92582. + test("regoffset", "0x00000000");
  92583. +
  92584. + set('regoffset', '40000');
  92585. + test("regoffset", "0x00000000");
  92586. +
  92587. + set('regoffset', '3ffff');
  92588. + test("regoffset", "0x0003ffff");
  92589. +
  92590. + set('regoffset', '1');
  92591. + test("regoffset", "0x00000001");
  92592. +
  92593. + print("\nTesting regvalue\n");
  92594. + set('regoffset', '3c');
  92595. + test("regvalue", "0x12345678");
  92596. + set('regvalue', '5a5a5a5a');
  92597. + test("regvalue", "0x5a5a5a5a");
  92598. + set('regvalue','a5a5a5a5');
  92599. + test("regvalue", "0xa5a5a5a5");
  92600. + set('guid','12345678');
  92601. +
  92602. + # Test HNP Capable
  92603. + print("\nTesting HNP Capable bit\n");
  92604. + set('hnpcapable', '1');
  92605. + test("hnpcapable", "0x1");
  92606. + set('hnpcapable','0');
  92607. + test("hnpcapable", "0x0");
  92608. +
  92609. + set('regoffset','0c');
  92610. +
  92611. + my $old = get('gusbcfg');
  92612. + print("setting hnpcapable\n");
  92613. + set('hnpcapable', '1');
  92614. + test("hnpcapable", "0x1");
  92615. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  92616. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  92617. +
  92618. + $old = get('gusbcfg');
  92619. + print("clearing hnpcapable\n");
  92620. + set('hnpcapable', '0');
  92621. + test("hnpcapable", "0x0");
  92622. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  92623. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  92624. +
  92625. + # Test SRP Capable
  92626. + print("\nTesting SRP Capable bit\n");
  92627. + set('srpcapable', '1');
  92628. + test("srpcapable", "0x1");
  92629. + set('srpcapable','0');
  92630. + test("srpcapable", "0x0");
  92631. +
  92632. + set('regoffset','0c');
  92633. +
  92634. + $old = get('gusbcfg');
  92635. + print("setting srpcapable\n");
  92636. + set('srpcapable', '1');
  92637. + test("srpcapable", "0x1");
  92638. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  92639. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  92640. +
  92641. + $old = get('gusbcfg');
  92642. + print("clearing srpcapable\n");
  92643. + set('srpcapable', '0');
  92644. + test("srpcapable", "0x0");
  92645. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  92646. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  92647. +
  92648. + # Test GGPIO
  92649. + print("\nTesting GGPIO\n");
  92650. + set('ggpio','5a5a5a5a');
  92651. + test('ggpio','0x5a5a0000');
  92652. + set('ggpio','a5a5a5a5');
  92653. + test('ggpio','0xa5a50000');
  92654. + set('ggpio','11110000');
  92655. + test('ggpio','0x11110000');
  92656. + set('ggpio','00001111');
  92657. + test('ggpio','0x00000000');
  92658. +
  92659. + # Test DEVSPEED
  92660. + print("\nTesting DEVSPEED\n");
  92661. + set('regoffset','800');
  92662. + $old = get('regvalue');
  92663. + set('devspeed','0');
  92664. + test('devspeed','0x0');
  92665. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  92666. + set('devspeed','1');
  92667. + test('devspeed','0x1');
  92668. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  92669. + set('devspeed','2');
  92670. + test('devspeed','0x2');
  92671. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  92672. + set('devspeed','3');
  92673. + test('devspeed','0x3');
  92674. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  92675. + set('devspeed','4');
  92676. + test('devspeed','0x0');
  92677. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  92678. + set('devspeed','5');
  92679. + test('devspeed','0x1');
  92680. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  92681. +
  92682. +
  92683. + # mode Returns the current mode:0 for device mode1 for host mode Read
  92684. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  92685. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  92686. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  92687. + # bussuspend Suspend the USB bus. Read/Write
  92688. + # busconnected Get the connection status of the bus Read
  92689. +
  92690. + # gotgctl Get or set the Core Control Status Register. Read/Write
  92691. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  92692. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  92693. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  92694. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  92695. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  92696. + ## guid Get or set the value of the User ID Register Read/Write
  92697. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  92698. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  92699. + # enumspeed Gets the device enumeration Speed. Read
  92700. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  92701. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  92702. +
  92703. + test_status("TEST NYI") or die;
  92704. +}
  92705. +
  92706. +test_main();
  92707. +0;
  92708. diff -Nur linux-3.10.37/drivers/usb/host/Kconfig linux-rpi/drivers/usb/host/Kconfig
  92709. --- linux-3.10.37/drivers/usb/host/Kconfig 2014-04-14 15:42:31.000000000 +0200
  92710. +++ linux-rpi/drivers/usb/host/Kconfig 2014-04-24 15:35:04.169565731 +0200
  92711. @@ -663,6 +663,19 @@
  92712. To compile this driver a module, choose M here: the module
  92713. will be called "hwa-hc".
  92714. +config USB_DWCOTG
  92715. + tristate "Synopsis DWC host support"
  92716. + depends on USB
  92717. + help
  92718. + The Synopsis DWC controller is a dual-role
  92719. + host/peripheral/OTG ("On The Go") USB controllers.
  92720. +
  92721. + Enable this option to support this IP in host controller mode.
  92722. + If unsure, say N.
  92723. +
  92724. + To compile this driver as a module, choose M here: the
  92725. + modules built will be called dwc_otg and dwc_common_port.
  92726. +
  92727. config USB_IMX21_HCD
  92728. tristate "i.MX21 HCD support"
  92729. depends on ARM && ARCH_MXC
  92730. diff -Nur linux-3.10.37/drivers/usb/host/Makefile linux-rpi/drivers/usb/host/Makefile
  92731. --- linux-3.10.37/drivers/usb/host/Makefile 2014-04-14 15:42:31.000000000 +0200
  92732. +++ linux-rpi/drivers/usb/host/Makefile 2014-04-24 15:35:04.169565731 +0200
  92733. @@ -47,6 +47,8 @@
  92734. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  92735. obj-$(CONFIG_USB_ISP1760_HCD) += isp1760.o
  92736. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  92737. +
  92738. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  92739. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  92740. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  92741. obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
  92742. diff -Nur linux-3.10.37/drivers/usb/Makefile linux-rpi/drivers/usb/Makefile
  92743. --- linux-3.10.37/drivers/usb/Makefile 2014-04-14 15:42:31.000000000 +0200
  92744. +++ linux-rpi/drivers/usb/Makefile 2014-04-24 15:35:04.129565286 +0200
  92745. @@ -23,6 +23,7 @@
  92746. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  92747. obj-$(CONFIG_USB_HWA_HCD) += host/
  92748. obj-$(CONFIG_USB_ISP1760_HCD) += host/
  92749. +obj-$(CONFIG_USB_DWCOTG) += host/
  92750. obj-$(CONFIG_USB_IMX21_HCD) += host/
  92751. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += host/
  92752. diff -Nur linux-3.10.37/drivers/video/bcm2708_fb.c linux-rpi/drivers/video/bcm2708_fb.c
  92753. --- linux-3.10.37/drivers/video/bcm2708_fb.c 1970-01-01 01:00:00.000000000 +0100
  92754. +++ linux-rpi/drivers/video/bcm2708_fb.c 2014-04-24 15:35:04.265566801 +0200
  92755. @@ -0,0 +1,763 @@
  92756. +/*
  92757. + * linux/drivers/video/bcm2708_fb.c
  92758. + *
  92759. + * Copyright (C) 2010 Broadcom
  92760. + *
  92761. + * This file is subject to the terms and conditions of the GNU General Public
  92762. + * License. See the file COPYING in the main directory of this archive
  92763. + * for more details.
  92764. + *
  92765. + * Broadcom simple framebuffer driver
  92766. + *
  92767. + * This file is derived from cirrusfb.c
  92768. + * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  92769. + *
  92770. + */
  92771. +#include <linux/module.h>
  92772. +#include <linux/kernel.h>
  92773. +#include <linux/errno.h>
  92774. +#include <linux/string.h>
  92775. +#include <linux/slab.h>
  92776. +#include <linux/mm.h>
  92777. +#include <linux/fb.h>
  92778. +#include <linux/init.h>
  92779. +#include <linux/interrupt.h>
  92780. +#include <linux/ioport.h>
  92781. +#include <linux/list.h>
  92782. +#include <linux/platform_device.h>
  92783. +#include <linux/clk.h>
  92784. +#include <linux/printk.h>
  92785. +#include <linux/console.h>
  92786. +#include <linux/debugfs.h>
  92787. +
  92788. +#include <mach/dma.h>
  92789. +#include <mach/platform.h>
  92790. +#include <mach/vcio.h>
  92791. +
  92792. +#include <asm/sizes.h>
  92793. +#include <linux/io.h>
  92794. +#include <linux/dma-mapping.h>
  92795. +
  92796. +#ifdef BCM2708_FB_DEBUG
  92797. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  92798. +#else
  92799. +#define print_debug(fmt,...)
  92800. +#endif
  92801. +
  92802. +/* This is limited to 16 characters when displayed by X startup */
  92803. +static const char *bcm2708_name = "BCM2708 FB";
  92804. +
  92805. +#define DRIVER_NAME "bcm2708_fb"
  92806. +
  92807. +static u32 dma_busy_wait_threshold = 1<<15;
  92808. +module_param(dma_busy_wait_threshold, int, 0644);
  92809. +MODULE_PARM_DESC(dma_busy_wait_threshold, "Busy-wait for DMA completion below this area");
  92810. +
  92811. +static int fbswap = 0; /* module parameter */
  92812. +
  92813. +/* this data structure describes each frame buffer device we find */
  92814. +
  92815. +struct fbinfo_s {
  92816. + u32 xres, yres, xres_virtual, yres_virtual;
  92817. + u32 pitch, bpp;
  92818. + u32 xoffset, yoffset;
  92819. + u32 base;
  92820. + u32 screen_size;
  92821. + u16 cmap[256];
  92822. +};
  92823. +
  92824. +struct bcm2708_fb_stats {
  92825. + struct debugfs_regset32 regset;
  92826. + u32 dma_copies;
  92827. + u32 dma_irqs;
  92828. +};
  92829. +
  92830. +struct bcm2708_fb {
  92831. + struct fb_info fb;
  92832. + struct platform_device *dev;
  92833. + struct fbinfo_s *info;
  92834. + dma_addr_t dma;
  92835. + u32 cmap[16];
  92836. + int dma_chan;
  92837. + int dma_irq;
  92838. + void __iomem *dma_chan_base;
  92839. + void *cb_base; /* DMA control blocks */
  92840. + dma_addr_t cb_handle;
  92841. + struct dentry *debugfs_dir;
  92842. + wait_queue_head_t dma_waitq;
  92843. + struct bcm2708_fb_stats stats;
  92844. +};
  92845. +
  92846. +#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  92847. +
  92848. +static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
  92849. +{
  92850. + debugfs_remove_recursive(fb->debugfs_dir);
  92851. + fb->debugfs_dir = NULL;
  92852. +}
  92853. +
  92854. +static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
  92855. +{
  92856. + static struct debugfs_reg32 stats_registers[] = {
  92857. + {
  92858. + "dma_copies",
  92859. + offsetof(struct bcm2708_fb_stats, dma_copies)
  92860. + },
  92861. + {
  92862. + "dma_irqs",
  92863. + offsetof(struct bcm2708_fb_stats, dma_irqs)
  92864. + },
  92865. + };
  92866. +
  92867. + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
  92868. + if (!fb->debugfs_dir) {
  92869. + pr_warn("%s: could not create debugfs entry\n",
  92870. + __func__);
  92871. + return -EFAULT;
  92872. + }
  92873. +
  92874. + fb->stats.regset.regs = stats_registers;
  92875. + fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
  92876. + fb->stats.regset.base = &fb->stats;
  92877. +
  92878. + if (!debugfs_create_regset32(
  92879. + "stats", 0444, fb->debugfs_dir, &fb->stats.regset)) {
  92880. + pr_warn("%s: could not create statistics registers\n",
  92881. + __func__);
  92882. + goto fail;
  92883. + }
  92884. + return 0;
  92885. +
  92886. +fail:
  92887. + bcm2708_fb_debugfs_deinit(fb);
  92888. + return -EFAULT;
  92889. +}
  92890. +
  92891. +static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
  92892. +{
  92893. + int ret = 0;
  92894. +
  92895. + memset(&var->transp, 0, sizeof(var->transp));
  92896. +
  92897. + var->red.msb_right = 0;
  92898. + var->green.msb_right = 0;
  92899. + var->blue.msb_right = 0;
  92900. +
  92901. + switch (var->bits_per_pixel) {
  92902. + case 1:
  92903. + case 2:
  92904. + case 4:
  92905. + case 8:
  92906. + var->red.length = var->bits_per_pixel;
  92907. + var->red.offset = 0;
  92908. + var->green.length = var->bits_per_pixel;
  92909. + var->green.offset = 0;
  92910. + var->blue.length = var->bits_per_pixel;
  92911. + var->blue.offset = 0;
  92912. + break;
  92913. + case 16:
  92914. + var->red.length = 5;
  92915. + var->blue.length = 5;
  92916. + /*
  92917. + * Green length can be 5 or 6 depending whether
  92918. + * we're operating in RGB555 or RGB565 mode.
  92919. + */
  92920. + if (var->green.length != 5 && var->green.length != 6)
  92921. + var->green.length = 6;
  92922. + break;
  92923. + case 24:
  92924. + var->red.length = 8;
  92925. + var->blue.length = 8;
  92926. + var->green.length = 8;
  92927. + break;
  92928. + case 32:
  92929. + var->red.length = 8;
  92930. + var->green.length = 8;
  92931. + var->blue.length = 8;
  92932. + var->transp.length = 8;
  92933. + break;
  92934. + default:
  92935. + ret = -EINVAL;
  92936. + break;
  92937. + }
  92938. +
  92939. + /*
  92940. + * >= 16bpp displays have separate colour component bitfields
  92941. + * encoded in the pixel data. Calculate their position from
  92942. + * the bitfield length defined above.
  92943. + */
  92944. + if (ret == 0 && var->bits_per_pixel >= 24 && fbswap) {
  92945. + var->blue.offset = 0;
  92946. + var->green.offset = var->blue.offset + var->blue.length;
  92947. + var->red.offset = var->green.offset + var->green.length;
  92948. + var->transp.offset = var->red.offset + var->red.length;
  92949. + } else if (ret == 0 && var->bits_per_pixel >= 24) {
  92950. + var->red.offset = 0;
  92951. + var->green.offset = var->red.offset + var->red.length;
  92952. + var->blue.offset = var->green.offset + var->green.length;
  92953. + var->transp.offset = var->blue.offset + var->blue.length;
  92954. + } else if (ret == 0 && var->bits_per_pixel >= 16) {
  92955. + var->blue.offset = 0;
  92956. + var->green.offset = var->blue.offset + var->blue.length;
  92957. + var->red.offset = var->green.offset + var->green.length;
  92958. + var->transp.offset = var->red.offset + var->red.length;
  92959. + }
  92960. +
  92961. + return ret;
  92962. +}
  92963. +
  92964. +static int bcm2708_fb_check_var(struct fb_var_screeninfo *var,
  92965. + struct fb_info *info)
  92966. +{
  92967. + /* info input, var output */
  92968. + int yres;
  92969. +
  92970. + /* info input, var output */
  92971. + print_debug("bcm2708_fb_check_var info(%p) %dx%d (%dx%d), %d, %d\n", info,
  92972. + info->var.xres, info->var.yres, info->var.xres_virtual,
  92973. + info->var.yres_virtual, (int)info->screen_size,
  92974. + info->var.bits_per_pixel);
  92975. + print_debug("bcm2708_fb_check_var var(%p) %dx%d (%dx%d), %d\n", var,
  92976. + var->xres, var->yres, var->xres_virtual, var->yres_virtual,
  92977. + var->bits_per_pixel);
  92978. +
  92979. + if (!var->bits_per_pixel)
  92980. + var->bits_per_pixel = 16;
  92981. +
  92982. + if (bcm2708_fb_set_bitfields(var) != 0) {
  92983. + pr_err("bcm2708_fb_check_var: invalid bits_per_pixel %d\n",
  92984. + var->bits_per_pixel);
  92985. + return -EINVAL;
  92986. + }
  92987. +
  92988. +
  92989. + if (var->xres_virtual < var->xres)
  92990. + var->xres_virtual = var->xres;
  92991. + /* use highest possible virtual resolution */
  92992. + if (var->yres_virtual == -1) {
  92993. + var->yres_virtual = 480;
  92994. +
  92995. + pr_err
  92996. + ("bcm2708_fb_check_var: virtual resolution set to maximum of %dx%d\n",
  92997. + var->xres_virtual, var->yres_virtual);
  92998. + }
  92999. + if (var->yres_virtual < var->yres)
  93000. + var->yres_virtual = var->yres;
  93001. +
  93002. + if (var->xoffset < 0)
  93003. + var->xoffset = 0;
  93004. + if (var->yoffset < 0)
  93005. + var->yoffset = 0;
  93006. +
  93007. + /* truncate xoffset and yoffset to maximum if too high */
  93008. + if (var->xoffset > var->xres_virtual - var->xres)
  93009. + var->xoffset = var->xres_virtual - var->xres - 1;
  93010. + if (var->yoffset > var->yres_virtual - var->yres)
  93011. + var->yoffset = var->yres_virtual - var->yres - 1;
  93012. +
  93013. + yres = var->yres;
  93014. + if (var->vmode & FB_VMODE_DOUBLE)
  93015. + yres *= 2;
  93016. + else if (var->vmode & FB_VMODE_INTERLACED)
  93017. + yres = (yres + 1) / 2;
  93018. +
  93019. + if (var->xres * yres > 1920 * 1200) {
  93020. + pr_err("bcm2708_fb_check_var: ERROR: Pixel size >= 1920x1200; "
  93021. + "special treatment required! (TODO)\n");
  93022. + return -EINVAL;
  93023. + }
  93024. +
  93025. + return 0;
  93026. +}
  93027. +
  93028. +static int bcm2708_fb_set_par(struct fb_info *info)
  93029. +{
  93030. + uint32_t val = 0;
  93031. + struct bcm2708_fb *fb = to_bcm2708(info);
  93032. + volatile struct fbinfo_s *fbinfo = fb->info;
  93033. + fbinfo->xres = info->var.xres;
  93034. + fbinfo->yres = info->var.yres;
  93035. + fbinfo->xres_virtual = info->var.xres_virtual;
  93036. + fbinfo->yres_virtual = info->var.yres_virtual;
  93037. + fbinfo->bpp = info->var.bits_per_pixel;
  93038. + fbinfo->xoffset = info->var.xoffset;
  93039. + fbinfo->yoffset = info->var.yoffset;
  93040. + fbinfo->base = 0; /* filled in by VC */
  93041. + fbinfo->pitch = 0; /* filled in by VC */
  93042. +
  93043. + print_debug("bcm2708_fb_set_par info(%p) %dx%d (%dx%d), %d, %d\n", info,
  93044. + info->var.xres, info->var.yres, info->var.xres_virtual,
  93045. + info->var.yres_virtual, (int)info->screen_size,
  93046. + info->var.bits_per_pixel);
  93047. +
  93048. + /* ensure last write to fbinfo is visible to GPU */
  93049. + wmb();
  93050. +
  93051. + /* inform vc about new framebuffer */
  93052. + bcm_mailbox_write(MBOX_CHAN_FB, fb->dma);
  93053. +
  93054. + /* TODO: replace fb driver with vchiq version */
  93055. + /* wait for response */
  93056. + bcm_mailbox_read(MBOX_CHAN_FB, &val);
  93057. +
  93058. + /* ensure GPU writes are visible to us */
  93059. + rmb();
  93060. +
  93061. + if (val == 0) {
  93062. + fb->fb.fix.line_length = fbinfo->pitch;
  93063. +
  93064. + if (info->var.bits_per_pixel <= 8)
  93065. + fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  93066. + else
  93067. + fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  93068. +
  93069. + fb->fb.fix.smem_start = fbinfo->base;
  93070. + fb->fb.fix.smem_len = fbinfo->pitch * fbinfo->yres_virtual;
  93071. + fb->fb.screen_size = fbinfo->screen_size;
  93072. + if (fb->fb.screen_base)
  93073. + iounmap(fb->fb.screen_base);
  93074. + fb->fb.screen_base =
  93075. + (void *)ioremap_wc(fb->fb.fix.smem_start, fb->fb.screen_size);
  93076. + if (!fb->fb.screen_base) {
  93077. + /* the console may currently be locked */
  93078. + console_trylock();
  93079. + console_unlock();
  93080. +
  93081. + BUG(); /* what can we do here */
  93082. + }
  93083. + }
  93084. + print_debug
  93085. + ("BCM2708FB: start = %p,%p width=%d, height=%d, bpp=%d, pitch=%d size=%d success=%d\n",
  93086. + (void *)fb->fb.screen_base, (void *)fb->fb.fix.smem_start,
  93087. + fbinfo->xres, fbinfo->yres, fbinfo->bpp,
  93088. + fbinfo->pitch, (int)fb->fb.screen_size, val);
  93089. +
  93090. + return val;
  93091. +}
  93092. +
  93093. +static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  93094. +{
  93095. + unsigned int mask = (1 << bf->length) - 1;
  93096. +
  93097. + return (val >> (16 - bf->length) & mask) << bf->offset;
  93098. +}
  93099. +
  93100. +
  93101. +static int bcm2708_fb_setcolreg(unsigned int regno, unsigned int red,
  93102. + unsigned int green, unsigned int blue,
  93103. + unsigned int transp, struct fb_info *info)
  93104. +{
  93105. + struct bcm2708_fb *fb = to_bcm2708(info);
  93106. +
  93107. + /*print_debug("BCM2708FB: setcolreg %d:(%02x,%02x,%02x,%02x) %x\n", regno, red, green, blue, transp, fb->fb.fix.visual);*/
  93108. + if (fb->fb.var.bits_per_pixel <= 8) {
  93109. + if (regno < 256) {
  93110. + /* blue [0:4], green [5:10], red [11:15] */
  93111. + fb->info->cmap[regno] = ((red >> (16-5)) & 0x1f) << 11 |
  93112. + ((green >> (16-6)) & 0x3f) << 5 |
  93113. + ((blue >> (16-5)) & 0x1f) << 0;
  93114. + }
  93115. + /* Hack: we need to tell GPU the palette has changed, but currently bcm2708_fb_set_par takes noticable time when called for every (256) colour */
  93116. + /* So just call it for what looks like the last colour in a list for now. */
  93117. + if (regno == 15 || regno == 255)
  93118. + bcm2708_fb_set_par(info);
  93119. + } else if (regno < 16) {
  93120. + fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  93121. + convert_bitfield(blue, &fb->fb.var.blue) |
  93122. + convert_bitfield(green, &fb->fb.var.green) |
  93123. + convert_bitfield(red, &fb->fb.var.red);
  93124. + }
  93125. + return regno > 255;
  93126. +}
  93127. +
  93128. +static int bcm2708_fb_blank(int blank_mode, struct fb_info *info)
  93129. +{
  93130. + /*print_debug("bcm2708_fb_blank\n"); */
  93131. + return -1;
  93132. +}
  93133. +
  93134. +static void bcm2708_fb_fillrect(struct fb_info *info,
  93135. + const struct fb_fillrect *rect)
  93136. +{
  93137. + /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
  93138. + cfb_fillrect(info, rect);
  93139. +}
  93140. +
  93141. +/* A helper function for configuring dma control block */
  93142. +static void set_dma_cb(struct bcm2708_dma_cb *cb,
  93143. + int burst_size,
  93144. + dma_addr_t dst,
  93145. + int dst_stride,
  93146. + dma_addr_t src,
  93147. + int src_stride,
  93148. + int w,
  93149. + int h)
  93150. +{
  93151. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  93152. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  93153. + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
  93154. + cb->dst = dst;
  93155. + cb->src = src;
  93156. + /*
  93157. + * This is not really obvious from the DMA documentation,
  93158. + * but the top 16 bits must be programmmed to "height -1"
  93159. + * and not "height" in 2D mode.
  93160. + */
  93161. + cb->length = ((h - 1) << 16) | w;
  93162. + cb->stride = ((dst_stride - w) << 16) | (u16)(src_stride - w);
  93163. + cb->pad[0] = 0;
  93164. + cb->pad[1] = 0;
  93165. +}
  93166. +
  93167. +static void bcm2708_fb_copyarea(struct fb_info *info,
  93168. + const struct fb_copyarea *region)
  93169. +{
  93170. + struct bcm2708_fb *fb = to_bcm2708(info);
  93171. + struct bcm2708_dma_cb *cb = fb->cb_base;
  93172. + int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
  93173. + /* Channel 0 supports larger bursts and is a bit faster */
  93174. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  93175. + int pixels = region->width * region->height;
  93176. +
  93177. + /* Fallback to cfb_copyarea() if we don't like something */
  93178. + if (bytes_per_pixel > 4 ||
  93179. + info->var.xres * info->var.yres > 1920 * 1200 ||
  93180. + region->width <= 0 || region->width > info->var.xres ||
  93181. + region->height <= 0 || region->height > info->var.yres ||
  93182. + region->sx < 0 || region->sx >= info->var.xres ||
  93183. + region->sy < 0 || region->sy >= info->var.yres ||
  93184. + region->dx < 0 || region->dx >= info->var.xres ||
  93185. + region->dy < 0 || region->dy >= info->var.yres ||
  93186. + region->sx + region->width > info->var.xres ||
  93187. + region->dx + region->width > info->var.xres ||
  93188. + region->sy + region->height > info->var.yres ||
  93189. + region->dy + region->height > info->var.yres) {
  93190. + cfb_copyarea(info, region);
  93191. + return;
  93192. + }
  93193. +
  93194. + if (region->dy == region->sy && region->dx > region->sx) {
  93195. + /*
  93196. + * A difficult case of overlapped copy. Because DMA can't
  93197. + * copy individual scanlines in backwards direction, we need
  93198. + * two-pass processing. We do it by programming a chain of dma
  93199. + * control blocks in the first 16K part of the buffer and use
  93200. + * the remaining 48K as the intermediate temporary scratch
  93201. + * buffer. The buffer size is sufficient to handle up to
  93202. + * 1920x1200 resolution at 32bpp pixel depth.
  93203. + */
  93204. + int y;
  93205. + dma_addr_t control_block_pa = fb->cb_handle;
  93206. + dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
  93207. + int scanline_size = bytes_per_pixel * region->width;
  93208. + int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
  93209. +
  93210. + for (y = 0; y < region->height; y += scanlines_per_cb) {
  93211. + dma_addr_t src =
  93212. + fb->fb.fix.smem_start +
  93213. + bytes_per_pixel * region->sx +
  93214. + (region->sy + y) * fb->fb.fix.line_length;
  93215. + dma_addr_t dst =
  93216. + fb->fb.fix.smem_start +
  93217. + bytes_per_pixel * region->dx +
  93218. + (region->dy + y) * fb->fb.fix.line_length;
  93219. +
  93220. + if (region->height - y < scanlines_per_cb)
  93221. + scanlines_per_cb = region->height - y;
  93222. +
  93223. + set_dma_cb(cb, burst_size, scratchbuf, scanline_size,
  93224. + src, fb->fb.fix.line_length,
  93225. + scanline_size, scanlines_per_cb);
  93226. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  93227. + cb->next = control_block_pa;
  93228. + cb++;
  93229. +
  93230. + set_dma_cb(cb, burst_size, dst, fb->fb.fix.line_length,
  93231. + scratchbuf, scanline_size,
  93232. + scanline_size, scanlines_per_cb);
  93233. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  93234. + cb->next = control_block_pa;
  93235. + cb++;
  93236. + }
  93237. + /* move the pointer back to the last dma control block */
  93238. + cb--;
  93239. + } else {
  93240. + /* A single dma control block is enough. */
  93241. + int sy, dy, stride;
  93242. + if (region->dy <= region->sy) {
  93243. + /* processing from top to bottom */
  93244. + dy = region->dy;
  93245. + sy = region->sy;
  93246. + stride = fb->fb.fix.line_length;
  93247. + } else {
  93248. + /* processing from bottom to top */
  93249. + dy = region->dy + region->height - 1;
  93250. + sy = region->sy + region->height - 1;
  93251. + stride = -fb->fb.fix.line_length;
  93252. + }
  93253. + set_dma_cb(cb, burst_size,
  93254. + fb->fb.fix.smem_start + dy * fb->fb.fix.line_length +
  93255. + bytes_per_pixel * region->dx,
  93256. + stride,
  93257. + fb->fb.fix.smem_start + sy * fb->fb.fix.line_length +
  93258. + bytes_per_pixel * region->sx,
  93259. + stride,
  93260. + region->width * bytes_per_pixel,
  93261. + region->height);
  93262. + }
  93263. +
  93264. + /* end of dma control blocks chain */
  93265. + cb->next = 0;
  93266. +
  93267. +
  93268. + if (pixels < dma_busy_wait_threshold) {
  93269. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  93270. + bcm_dma_wait_idle(fb->dma_chan_base);
  93271. + } else {
  93272. + void __iomem *dma_chan = fb->dma_chan_base;
  93273. + cb->info |= BCM2708_DMA_INT_EN;
  93274. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  93275. + while (bcm_dma_is_busy(dma_chan)) {
  93276. + wait_event_interruptible(
  93277. + fb->dma_waitq,
  93278. + !bcm_dma_is_busy(dma_chan));
  93279. + }
  93280. + fb->stats.dma_irqs++;
  93281. + }
  93282. + fb->stats.dma_copies++;
  93283. +}
  93284. +
  93285. +static void bcm2708_fb_imageblit(struct fb_info *info,
  93286. + const struct fb_image *image)
  93287. +{
  93288. + /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
  93289. + cfb_imageblit(info, image);
  93290. +}
  93291. +
  93292. +static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
  93293. +{
  93294. + struct bcm2708_fb *fb = cxt;
  93295. +
  93296. + /* FIXME: should read status register to check if this is
  93297. + * actually interrupting us or not, in case this interrupt
  93298. + * ever becomes shared amongst several DMA channels
  93299. + *
  93300. + * readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_IRQ;
  93301. + */
  93302. +
  93303. + /* acknowledge the interrupt */
  93304. + writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
  93305. +
  93306. + wake_up(&fb->dma_waitq);
  93307. + return IRQ_HANDLED;
  93308. +}
  93309. +
  93310. +static struct fb_ops bcm2708_fb_ops = {
  93311. + .owner = THIS_MODULE,
  93312. + .fb_check_var = bcm2708_fb_check_var,
  93313. + .fb_set_par = bcm2708_fb_set_par,
  93314. + .fb_setcolreg = bcm2708_fb_setcolreg,
  93315. + .fb_blank = bcm2708_fb_blank,
  93316. + .fb_fillrect = bcm2708_fb_fillrect,
  93317. + .fb_copyarea = bcm2708_fb_copyarea,
  93318. + .fb_imageblit = bcm2708_fb_imageblit,
  93319. +};
  93320. +
  93321. +static int fbwidth = 800; /* module parameter */
  93322. +static int fbheight = 480; /* module parameter */
  93323. +static int fbdepth = 16; /* module parameter */
  93324. +
  93325. +static int bcm2708_fb_register(struct bcm2708_fb *fb)
  93326. +{
  93327. + int ret;
  93328. + dma_addr_t dma;
  93329. + void *mem;
  93330. +
  93331. + mem =
  93332. + dma_alloc_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), &dma,
  93333. + GFP_KERNEL);
  93334. +
  93335. + if (NULL == mem) {
  93336. + pr_err(": unable to allocate fbinfo buffer\n");
  93337. + ret = -ENOMEM;
  93338. + } else {
  93339. + fb->info = (struct fbinfo_s *)mem;
  93340. + fb->dma = dma;
  93341. + }
  93342. + fb->fb.fbops = &bcm2708_fb_ops;
  93343. + fb->fb.flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_COPYAREA;
  93344. + fb->fb.pseudo_palette = fb->cmap;
  93345. +
  93346. + strncpy(fb->fb.fix.id, bcm2708_name, sizeof(fb->fb.fix.id));
  93347. + fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  93348. + fb->fb.fix.type_aux = 0;
  93349. + fb->fb.fix.xpanstep = 0;
  93350. + fb->fb.fix.ypanstep = 0;
  93351. + fb->fb.fix.ywrapstep = 0;
  93352. + fb->fb.fix.accel = FB_ACCEL_NONE;
  93353. +
  93354. + fb->fb.var.xres = fbwidth;
  93355. + fb->fb.var.yres = fbheight;
  93356. + fb->fb.var.xres_virtual = fbwidth;
  93357. + fb->fb.var.yres_virtual = fbheight;
  93358. + fb->fb.var.bits_per_pixel = fbdepth;
  93359. + fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  93360. + fb->fb.var.activate = FB_ACTIVATE_NOW;
  93361. + fb->fb.var.nonstd = 0;
  93362. + fb->fb.var.height = -1; /* height of picture in mm */
  93363. + fb->fb.var.width = -1; /* width of picture in mm */
  93364. + fb->fb.var.accel_flags = 0;
  93365. +
  93366. + fb->fb.monspecs.hfmin = 0;
  93367. + fb->fb.monspecs.hfmax = 100000;
  93368. + fb->fb.monspecs.vfmin = 0;
  93369. + fb->fb.monspecs.vfmax = 400;
  93370. + fb->fb.monspecs.dclkmin = 1000000;
  93371. + fb->fb.monspecs.dclkmax = 100000000;
  93372. +
  93373. + bcm2708_fb_set_bitfields(&fb->fb.var);
  93374. + init_waitqueue_head(&fb->dma_waitq);
  93375. +
  93376. + /*
  93377. + * Allocate colourmap.
  93378. + */
  93379. +
  93380. + fb_set_var(&fb->fb, &fb->fb.var);
  93381. +
  93382. + print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n", fbwidth
  93383. + fbheight, fbdepth, fbswap);
  93384. +
  93385. + ret = register_framebuffer(&fb->fb);
  93386. + print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
  93387. + if (ret == 0)
  93388. + goto out;
  93389. +
  93390. + print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
  93391. +out:
  93392. + return ret;
  93393. +}
  93394. +
  93395. +static int bcm2708_fb_probe(struct platform_device *dev)
  93396. +{
  93397. + struct bcm2708_fb *fb;
  93398. + int ret;
  93399. +
  93400. + fb = kzalloc(sizeof(struct bcm2708_fb), GFP_KERNEL);
  93401. + if (!fb) {
  93402. + dev_err(&dev->dev,
  93403. + "could not allocate new bcm2708_fb struct\n");
  93404. + ret = -ENOMEM;
  93405. + goto free_region;
  93406. + }
  93407. +
  93408. + bcm2708_fb_debugfs_init(fb);
  93409. +
  93410. + fb->cb_base = dma_alloc_writecombine(&dev->dev, SZ_64K,
  93411. + &fb->cb_handle, GFP_KERNEL);
  93412. + if (!fb->cb_base) {
  93413. + dev_err(&dev->dev, "cannot allocate DMA CBs\n");
  93414. + ret = -ENOMEM;
  93415. + goto free_fb;
  93416. + }
  93417. +
  93418. + pr_info("BCM2708FB: allocated DMA memory %08x\n",
  93419. + fb->cb_handle);
  93420. +
  93421. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
  93422. + &fb->dma_chan_base, &fb->dma_irq);
  93423. + if (ret < 0) {
  93424. + dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
  93425. + goto free_cb;
  93426. + }
  93427. + fb->dma_chan = ret;
  93428. +
  93429. + ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
  93430. + 0, "bcm2708_fb dma", fb);
  93431. + if (ret) {
  93432. + pr_err("%s: failed to request DMA irq\n", __func__);
  93433. + goto free_dma_chan;
  93434. + }
  93435. +
  93436. +
  93437. + pr_info("BCM2708FB: allocated DMA channel %d @ %p\n",
  93438. + fb->dma_chan, fb->dma_chan_base);
  93439. +
  93440. + fb->dev = dev;
  93441. +
  93442. + ret = bcm2708_fb_register(fb);
  93443. + if (ret == 0) {
  93444. + platform_set_drvdata(dev, fb);
  93445. + goto out;
  93446. + }
  93447. +
  93448. +free_dma_chan:
  93449. + bcm_dma_chan_free(fb->dma_chan);
  93450. +free_cb:
  93451. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  93452. +free_fb:
  93453. + kfree(fb);
  93454. +free_region:
  93455. + dev_err(&dev->dev, "probe failed, err %d\n", ret);
  93456. +out:
  93457. + return ret;
  93458. +}
  93459. +
  93460. +static int bcm2708_fb_remove(struct platform_device *dev)
  93461. +{
  93462. + struct bcm2708_fb *fb = platform_get_drvdata(dev);
  93463. +
  93464. + platform_set_drvdata(dev, NULL);
  93465. +
  93466. + if (fb->fb.screen_base)
  93467. + iounmap(fb->fb.screen_base);
  93468. + unregister_framebuffer(&fb->fb);
  93469. +
  93470. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  93471. + bcm_dma_chan_free(fb->dma_chan);
  93472. +
  93473. + dma_free_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), (void *)fb->info,
  93474. + fb->dma);
  93475. + bcm2708_fb_debugfs_deinit(fb);
  93476. +
  93477. + free_irq(fb->dma_irq, fb);
  93478. +
  93479. + kfree(fb);
  93480. +
  93481. + return 0;
  93482. +}
  93483. +
  93484. +static struct platform_driver bcm2708_fb_driver = {
  93485. + .probe = bcm2708_fb_probe,
  93486. + .remove = bcm2708_fb_remove,
  93487. + .driver = {
  93488. + .name = DRIVER_NAME,
  93489. + .owner = THIS_MODULE,
  93490. + },
  93491. +};
  93492. +
  93493. +static int __init bcm2708_fb_init(void)
  93494. +{
  93495. + return platform_driver_register(&bcm2708_fb_driver);
  93496. +}
  93497. +
  93498. +module_init(bcm2708_fb_init);
  93499. +
  93500. +static void __exit bcm2708_fb_exit(void)
  93501. +{
  93502. + platform_driver_unregister(&bcm2708_fb_driver);
  93503. +}
  93504. +
  93505. +module_exit(bcm2708_fb_exit);
  93506. +
  93507. +module_param(fbwidth, int, 0644);
  93508. +module_param(fbheight, int, 0644);
  93509. +module_param(fbdepth, int, 0644);
  93510. +module_param(fbswap, int, 0644);
  93511. +
  93512. +MODULE_DESCRIPTION("BCM2708 framebuffer driver");
  93513. +MODULE_LICENSE("GPL");
  93514. +
  93515. +MODULE_PARM_DESC(fbwidth, "Width of ARM Framebuffer");
  93516. +MODULE_PARM_DESC(fbheight, "Height of ARM Framebuffer");
  93517. +MODULE_PARM_DESC(fbdepth, "Bit depth of ARM Framebuffer");
  93518. +MODULE_PARM_DESC(fbswap, "Swap order of red and blue in 24 and 32 bit modes");
  93519. diff -Nur linux-3.10.37/drivers/video/cfbimgblt.c linux-rpi/drivers/video/cfbimgblt.c
  93520. --- linux-3.10.37/drivers/video/cfbimgblt.c 2014-04-14 15:42:31.000000000 +0200
  93521. +++ linux-rpi/drivers/video/cfbimgblt.c 2014-04-24 15:35:04.265566801 +0200
  93522. @@ -28,6 +28,11 @@
  93523. *
  93524. * Also need to add code to deal with cards endians that are different than
  93525. * the native cpu endians. I also need to deal with MSB position in the word.
  93526. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  93527. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  93528. + * significantly faster than the previous implementation.
  93529. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  93530. + * divides.
  93531. */
  93532. #include <linux/module.h>
  93533. #include <linux/string.h>
  93534. @@ -262,6 +267,133 @@
  93535. }
  93536. }
  93537. +/*
  93538. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  93539. + * into the code, main loop unrolled.
  93540. + */
  93541. +
  93542. +static inline void fast_imageblit16(const struct fb_image *image,
  93543. + struct fb_info *p, u8 __iomem * dst1,
  93544. + u32 fgcolor, u32 bgcolor)
  93545. +{
  93546. + u32 fgx = fgcolor, bgx = bgcolor;
  93547. + u32 spitch = (image->width + 7) / 8;
  93548. + u32 end_mask, eorx;
  93549. + const char *s = image->data, *src;
  93550. + u32 __iomem *dst;
  93551. + const u32 *tab = NULL;
  93552. + int i, j, k;
  93553. +
  93554. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  93555. +
  93556. + fgx <<= 16;
  93557. + bgx <<= 16;
  93558. + fgx |= fgcolor;
  93559. + bgx |= bgcolor;
  93560. +
  93561. + eorx = fgx ^ bgx;
  93562. + k = image->width / 2;
  93563. +
  93564. + for (i = image->height; i--;) {
  93565. + dst = (u32 __iomem *) dst1;
  93566. + src = s;
  93567. +
  93568. + j = k;
  93569. + while (j >= 4) {
  93570. + u8 bits = *src;
  93571. + end_mask = tab[(bits >> 6) & 3];
  93572. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93573. + end_mask = tab[(bits >> 4) & 3];
  93574. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93575. + end_mask = tab[(bits >> 2) & 3];
  93576. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93577. + end_mask = tab[bits & 3];
  93578. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93579. + src++;
  93580. + j -= 4;
  93581. + }
  93582. + if (j != 0) {
  93583. + u8 bits = *src;
  93584. + end_mask = tab[(bits >> 6) & 3];
  93585. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93586. + if (j >= 2) {
  93587. + end_mask = tab[(bits >> 4) & 3];
  93588. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93589. + if (j == 3) {
  93590. + end_mask = tab[(bits >> 2) & 3];
  93591. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  93592. + }
  93593. + }
  93594. + }
  93595. + dst1 += p->fix.line_length;
  93596. + s += spitch;
  93597. + }
  93598. +}
  93599. +
  93600. +/*
  93601. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  93602. + * into the code, main loop unrolled.
  93603. + */
  93604. +
  93605. +static inline void fast_imageblit32(const struct fb_image *image,
  93606. + struct fb_info *p, u8 __iomem * dst1,
  93607. + u32 fgcolor, u32 bgcolor)
  93608. +{
  93609. + u32 fgx = fgcolor, bgx = bgcolor;
  93610. + u32 spitch = (image->width + 7) / 8;
  93611. + u32 end_mask, eorx;
  93612. + const char *s = image->data, *src;
  93613. + u32 __iomem *dst;
  93614. + const u32 *tab = NULL;
  93615. + int i, j, k;
  93616. +
  93617. + tab = cfb_tab32;
  93618. +
  93619. + eorx = fgx ^ bgx;
  93620. + k = image->width;
  93621. +
  93622. + for (i = image->height; i--;) {
  93623. + dst = (u32 __iomem *) dst1;
  93624. + src = s;
  93625. +
  93626. + j = k;
  93627. + while (j >= 8) {
  93628. + u8 bits = *src;
  93629. + end_mask = tab[(bits >> 7) & 1];
  93630. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93631. + end_mask = tab[(bits >> 6) & 1];
  93632. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93633. + end_mask = tab[(bits >> 5) & 1];
  93634. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93635. + end_mask = tab[(bits >> 4) & 1];
  93636. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93637. + end_mask = tab[(bits >> 3) & 1];
  93638. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93639. + end_mask = tab[(bits >> 2) & 1];
  93640. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93641. + end_mask = tab[(bits >> 1) & 1];
  93642. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93643. + end_mask = tab[bits & 1];
  93644. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93645. + src++;
  93646. + j -= 8;
  93647. + }
  93648. + if (j != 0) {
  93649. + u32 bits = (u32) * src;
  93650. + while (j > 1) {
  93651. + end_mask = tab[(bits >> 7) & 1];
  93652. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  93653. + bits <<= 1;
  93654. + j--;
  93655. + }
  93656. + end_mask = tab[(bits >> 7) & 1];
  93657. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  93658. + }
  93659. + dst1 += p->fix.line_length;
  93660. + s += spitch;
  93661. + }
  93662. +}
  93663. +
  93664. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  93665. {
  93666. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  93667. @@ -294,11 +426,21 @@
  93668. bgcolor = image->bg_color;
  93669. }
  93670. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  93671. - ((width & (32/bpp-1)) == 0) &&
  93672. - bpp >= 8 && bpp <= 32)
  93673. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  93674. - else
  93675. + if (!start_index && !pitch_index) {
  93676. + if (bpp == 32)
  93677. + fast_imageblit32(image, p, dst1, fgcolor,
  93678. + bgcolor);
  93679. + else if (bpp == 16 && (width & 1) == 0)
  93680. + fast_imageblit16(image, p, dst1, fgcolor,
  93681. + bgcolor);
  93682. + else if (bpp == 8 && (width & 3) == 0)
  93683. + fast_imageblit(image, p, dst1, fgcolor,
  93684. + bgcolor);
  93685. + else
  93686. + slow_imageblit(image, p, dst1, fgcolor,
  93687. + bgcolor,
  93688. + start_index, pitch_index);
  93689. + } else
  93690. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  93691. start_index, pitch_index);
  93692. } else
  93693. diff -Nur linux-3.10.37/drivers/video/fbmem.c linux-rpi/drivers/video/fbmem.c
  93694. --- linux-3.10.37/drivers/video/fbmem.c 2014-04-14 15:42:31.000000000 +0200
  93695. +++ linux-rpi/drivers/video/fbmem.c 2014-04-24 15:35:04.277566935 +0200
  93696. @@ -1074,6 +1074,25 @@
  93697. return ret;
  93698. }
  93699. +static int fb_copyarea_user(struct fb_info *info,
  93700. + struct fb_copyarea *copy)
  93701. +{
  93702. + int ret = 0;
  93703. + if (!lock_fb_info(info))
  93704. + return -ENODEV;
  93705. + if (copy->dx + copy->width > info->var.xres ||
  93706. + copy->sx + copy->width > info->var.xres ||
  93707. + copy->dy + copy->height > info->var.yres ||
  93708. + copy->sy + copy->height > info->var.yres) {
  93709. + ret = -EINVAL;
  93710. + goto out;
  93711. + }
  93712. + info->fbops->fb_copyarea(info, copy);
  93713. +out:
  93714. + unlock_fb_info(info);
  93715. + return ret;
  93716. +}
  93717. +
  93718. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  93719. unsigned long arg)
  93720. {
  93721. @@ -1084,6 +1103,7 @@
  93722. struct fb_cmap cmap_from;
  93723. struct fb_cmap_user cmap;
  93724. struct fb_event event;
  93725. + struct fb_copyarea copy;
  93726. void __user *argp = (void __user *)arg;
  93727. long ret = 0;
  93728. @@ -1193,6 +1213,15 @@
  93729. console_unlock();
  93730. unlock_fb_info(info);
  93731. break;
  93732. + case FBIOCOPYAREA:
  93733. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  93734. + /* only provide this ioctl if it is accelerated */
  93735. + if (copy_from_user(&copy, argp, sizeof(copy)))
  93736. + return -EFAULT;
  93737. + ret = fb_copyarea_user(info, &copy);
  93738. + break;
  93739. + }
  93740. + /* fall through */
  93741. default:
  93742. if (!lock_fb_info(info))
  93743. return -ENODEV;
  93744. @@ -1345,6 +1374,7 @@
  93745. case FBIOPAN_DISPLAY:
  93746. case FBIOGET_CON2FBMAP:
  93747. case FBIOPUT_CON2FBMAP:
  93748. + case FBIOCOPYAREA:
  93749. arg = (unsigned long) compat_ptr(arg);
  93750. case FBIOBLANK:
  93751. ret = do_fb_ioctl(info, cmd, arg);
  93752. diff -Nur linux-3.10.37/drivers/video/Kconfig linux-rpi/drivers/video/Kconfig
  93753. --- linux-3.10.37/drivers/video/Kconfig 2014-04-14 15:42:31.000000000 +0200
  93754. +++ linux-rpi/drivers/video/Kconfig 2014-04-24 15:35:04.249566623 +0200
  93755. @@ -310,6 +310,20 @@
  93756. help
  93757. Support the Permedia2 FIFO disconnect feature.
  93758. +config FB_BCM2708
  93759. + tristate "BCM2708 framebuffer support"
  93760. + depends on FB && ARM
  93761. + select FB_CFB_FILLRECT
  93762. + select FB_CFB_COPYAREA
  93763. + select FB_CFB_IMAGEBLIT
  93764. + help
  93765. + This framebuffer device driver is for the BCM2708 framebuffer.
  93766. +
  93767. + If you want to compile this as a module (=code which can be
  93768. + inserted into and removed from the running kernel), say M
  93769. + here and read <file:Documentation/kbuild/modules.txt>. The module
  93770. + will be called bcm2708_fb.
  93771. +
  93772. config FB_ARMCLCD
  93773. tristate "ARM PrimeCell PL110 support"
  93774. depends on FB && ARM && ARM_AMBA
  93775. diff -Nur linux-3.10.37/drivers/video/logo/logo_linux_clut224.ppm linux-rpi/drivers/video/logo/logo_linux_clut224.ppm
  93776. --- linux-3.10.37/drivers/video/logo/logo_linux_clut224.ppm 2014-04-14 15:42:31.000000000 +0200
  93777. +++ linux-rpi/drivers/video/logo/logo_linux_clut224.ppm 2014-04-24 15:35:04.285567024 +0200
  93778. @@ -1,1604 +1,883 @@
  93779. P3
  93780. -# Standard 224-color Linux logo
  93781. -80 80
  93782. +63 80
  93783. 255
  93784. - 0 0 0 0 0 0 0 0 0 0 0 0
  93785. - 0 0 0 0 0 0 0 0 0 0 0 0
  93786. - 0 0 0 0 0 0 0 0 0 0 0 0
  93787. - 0 0 0 0 0 0 0 0 0 0 0 0
  93788. - 0 0 0 0 0 0 0 0 0 0 0 0
  93789. - 0 0 0 0 0 0 0 0 0 0 0 0
  93790. - 0 0 0 0 0 0 0 0 0 0 0 0
  93791. - 0 0 0 0 0 0 0 0 0 0 0 0
  93792. - 0 0 0 0 0 0 0 0 0 0 0 0
  93793. - 6 6 6 6 6 6 10 10 10 10 10 10
  93794. - 10 10 10 6 6 6 6 6 6 6 6 6
  93795. - 0 0 0 0 0 0 0 0 0 0 0 0
  93796. - 0 0 0 0 0 0 0 0 0 0 0 0
  93797. - 0 0 0 0 0 0 0 0 0 0 0 0
  93798. - 0 0 0 0 0 0 0 0 0 0 0 0
  93799. - 0 0 0 0 0 0 0 0 0 0 0 0
  93800. - 0 0 0 0 0 0 0 0 0 0 0 0
  93801. - 0 0 0 0 0 0 0 0 0 0 0 0
  93802. - 0 0 0 0 0 0 0 0 0 0 0 0
  93803. - 0 0 0 0 0 0 0 0 0 0 0 0
  93804. - 0 0 0 0 0 0 0 0 0 0 0 0
  93805. - 0 0 0 0 0 0 0 0 0 0 0 0
  93806. - 0 0 0 0 0 0 0 0 0 0 0 0
  93807. - 0 0 0 0 0 0 0 0 0 0 0 0
  93808. - 0 0 0 0 0 0 0 0 0 0 0 0
  93809. - 0 0 0 0 0 0 0 0 0 0 0 0
  93810. - 0 0 0 0 0 0 0 0 0 0 0 0
  93811. - 0 0 0 0 0 0 0 0 0 0 0 0
  93812. - 0 0 0 6 6 6 10 10 10 14 14 14
  93813. - 22 22 22 26 26 26 30 30 30 34 34 34
  93814. - 30 30 30 30 30 30 26 26 26 18 18 18
  93815. - 14 14 14 10 10 10 6 6 6 0 0 0
  93816. - 0 0 0 0 0 0 0 0 0 0 0 0
  93817. - 0 0 0 0 0 0 0 0 0 0 0 0
  93818. - 0 0 0 0 0 0 0 0 0 0 0 0
  93819. - 0 0 0 0 0 0 0 0 0 0 0 0
  93820. - 0 0 0 0 0 0 0 0 0 0 0 0
  93821. - 0 0 0 0 0 0 0 0 0 0 0 0
  93822. - 0 0 0 0 0 0 0 0 0 0 0 0
  93823. - 0 0 0 0 0 0 0 0 0 0 0 0
  93824. - 0 0 0 0 0 0 0 0 0 0 0 0
  93825. - 0 0 0 0 0 1 0 0 1 0 0 0
  93826. - 0 0 0 0 0 0 0 0 0 0 0 0
  93827. - 0 0 0 0 0 0 0 0 0 0 0 0
  93828. - 0 0 0 0 0 0 0 0 0 0 0 0
  93829. - 0 0 0 0 0 0 0 0 0 0 0 0
  93830. - 0 0 0 0 0 0 0 0 0 0 0 0
  93831. - 0 0 0 0 0 0 0 0 0 0 0 0
  93832. - 6 6 6 14 14 14 26 26 26 42 42 42
  93833. - 54 54 54 66 66 66 78 78 78 78 78 78
  93834. - 78 78 78 74 74 74 66 66 66 54 54 54
  93835. - 42 42 42 26 26 26 18 18 18 10 10 10
  93836. - 6 6 6 0 0 0 0 0 0 0 0 0
  93837. - 0 0 0 0 0 0 0 0 0 0 0 0
  93838. - 0 0 0 0 0 0 0 0 0 0 0 0
  93839. - 0 0 0 0 0 0 0 0 0 0 0 0
  93840. - 0 0 0 0 0 0 0 0 0 0 0 0
  93841. - 0 0 0 0 0 0 0 0 0 0 0 0
  93842. - 0 0 0 0 0 0 0 0 0 0 0 0
  93843. - 0 0 0 0 0 0 0 0 0 0 0 0
  93844. - 0 0 0 0 0 0 0 0 0 0 0 0
  93845. - 0 0 1 0 0 0 0 0 0 0 0 0
  93846. - 0 0 0 0 0 0 0 0 0 0 0 0
  93847. - 0 0 0 0 0 0 0 0 0 0 0 0
  93848. - 0 0 0 0 0 0 0 0 0 0 0 0
  93849. - 0 0 0 0 0 0 0 0 0 0 0 0
  93850. - 0 0 0 0 0 0 0 0 0 0 0 0
  93851. - 0 0 0 0 0 0 0 0 0 10 10 10
  93852. - 22 22 22 42 42 42 66 66 66 86 86 86
  93853. - 66 66 66 38 38 38 38 38 38 22 22 22
  93854. - 26 26 26 34 34 34 54 54 54 66 66 66
  93855. - 86 86 86 70 70 70 46 46 46 26 26 26
  93856. - 14 14 14 6 6 6 0 0 0 0 0 0
  93857. - 0 0 0 0 0 0 0 0 0 0 0 0
  93858. - 0 0 0 0 0 0 0 0 0 0 0 0
  93859. - 0 0 0 0 0 0 0 0 0 0 0 0
  93860. - 0 0 0 0 0 0 0 0 0 0 0 0
  93861. - 0 0 0 0 0 0 0 0 0 0 0 0
  93862. - 0 0 0 0 0 0 0 0 0 0 0 0
  93863. - 0 0 0 0 0 0 0 0 0 0 0 0
  93864. - 0 0 0 0 0 0 0 0 0 0 0 0
  93865. - 0 0 1 0 0 1 0 0 1 0 0 0
  93866. - 0 0 0 0 0 0 0 0 0 0 0 0
  93867. - 0 0 0 0 0 0 0 0 0 0 0 0
  93868. - 0 0 0 0 0 0 0 0 0 0 0 0
  93869. - 0 0 0 0 0 0 0 0 0 0 0 0
  93870. - 0 0 0 0 0 0 0 0 0 0 0 0
  93871. - 0 0 0 0 0 0 10 10 10 26 26 26
  93872. - 50 50 50 82 82 82 58 58 58 6 6 6
  93873. - 2 2 6 2 2 6 2 2 6 2 2 6
  93874. - 2 2 6 2 2 6 2 2 6 2 2 6
  93875. - 6 6 6 54 54 54 86 86 86 66 66 66
  93876. - 38 38 38 18 18 18 6 6 6 0 0 0
  93877. - 0 0 0 0 0 0 0 0 0 0 0 0
  93878. - 0 0 0 0 0 0 0 0 0 0 0 0
  93879. - 0 0 0 0 0 0 0 0 0 0 0 0
  93880. - 0 0 0 0 0 0 0 0 0 0 0 0
  93881. - 0 0 0 0 0 0 0 0 0 0 0 0
  93882. - 0 0 0 0 0 0 0 0 0 0 0 0
  93883. - 0 0 0 0 0 0 0 0 0 0 0 0
  93884. - 0 0 0 0 0 0 0 0 0 0 0 0
  93885. - 0 0 0 0 0 0 0 0 0 0 0 0
  93886. - 0 0 0 0 0 0 0 0 0 0 0 0
  93887. - 0 0 0 0 0 0 0 0 0 0 0 0
  93888. - 0 0 0 0 0 0 0 0 0 0 0 0
  93889. - 0 0 0 0 0 0 0 0 0 0 0 0
  93890. - 0 0 0 0 0 0 0 0 0 0 0 0
  93891. - 0 0 0 6 6 6 22 22 22 50 50 50
  93892. - 78 78 78 34 34 34 2 2 6 2 2 6
  93893. - 2 2 6 2 2 6 2 2 6 2 2 6
  93894. - 2 2 6 2 2 6 2 2 6 2 2 6
  93895. - 2 2 6 2 2 6 6 6 6 70 70 70
  93896. - 78 78 78 46 46 46 22 22 22 6 6 6
  93897. - 0 0 0 0 0 0 0 0 0 0 0 0
  93898. - 0 0 0 0 0 0 0 0 0 0 0 0
  93899. - 0 0 0 0 0 0 0 0 0 0 0 0
  93900. - 0 0 0 0 0 0 0 0 0 0 0 0
  93901. - 0 0 0 0 0 0 0 0 0 0 0 0
  93902. - 0 0 0 0 0 0 0 0 0 0 0 0
  93903. - 0 0 0 0 0 0 0 0 0 0 0 0
  93904. - 0 0 0 0 0 0 0 0 0 0 0 0
  93905. - 0 0 1 0 0 1 0 0 1 0 0 0
  93906. - 0 0 0 0 0 0 0 0 0 0 0 0
  93907. - 0 0 0 0 0 0 0 0 0 0 0 0
  93908. - 0 0 0 0 0 0 0 0 0 0 0 0
  93909. - 0 0 0 0 0 0 0 0 0 0 0 0
  93910. - 0 0 0 0 0 0 0 0 0 0 0 0
  93911. - 6 6 6 18 18 18 42 42 42 82 82 82
  93912. - 26 26 26 2 2 6 2 2 6 2 2 6
  93913. - 2 2 6 2 2 6 2 2 6 2 2 6
  93914. - 2 2 6 2 2 6 2 2 6 14 14 14
  93915. - 46 46 46 34 34 34 6 6 6 2 2 6
  93916. - 42 42 42 78 78 78 42 42 42 18 18 18
  93917. - 6 6 6 0 0 0 0 0 0 0 0 0
  93918. - 0 0 0 0 0 0 0 0 0 0 0 0
  93919. - 0 0 0 0 0 0 0 0 0 0 0 0
  93920. - 0 0 0 0 0 0 0 0 0 0 0 0
  93921. - 0 0 0 0 0 0 0 0 0 0 0 0
  93922. - 0 0 0 0 0 0 0 0 0 0 0 0
  93923. - 0 0 0 0 0 0 0 0 0 0 0 0
  93924. - 0 0 0 0 0 0 0 0 0 0 0 0
  93925. - 0 0 1 0 0 0 0 0 1 0 0 0
  93926. - 0 0 0 0 0 0 0 0 0 0 0 0
  93927. - 0 0 0 0 0 0 0 0 0 0 0 0
  93928. - 0 0 0 0 0 0 0 0 0 0 0 0
  93929. - 0 0 0 0 0 0 0 0 0 0 0 0
  93930. - 0 0 0 0 0 0 0 0 0 0 0 0
  93931. - 10 10 10 30 30 30 66 66 66 58 58 58
  93932. - 2 2 6 2 2 6 2 2 6 2 2 6
  93933. - 2 2 6 2 2 6 2 2 6 2 2 6
  93934. - 2 2 6 2 2 6 2 2 6 26 26 26
  93935. - 86 86 86 101 101 101 46 46 46 10 10 10
  93936. - 2 2 6 58 58 58 70 70 70 34 34 34
  93937. - 10 10 10 0 0 0 0 0 0 0 0 0
  93938. - 0 0 0 0 0 0 0 0 0 0 0 0
  93939. - 0 0 0 0 0 0 0 0 0 0 0 0
  93940. - 0 0 0 0 0 0 0 0 0 0 0 0
  93941. - 0 0 0 0 0 0 0 0 0 0 0 0
  93942. - 0 0 0 0 0 0 0 0 0 0 0 0
  93943. - 0 0 0 0 0 0 0 0 0 0 0 0
  93944. - 0 0 0 0 0 0 0 0 0 0 0 0
  93945. - 0 0 1 0 0 1 0 0 1 0 0 0
  93946. - 0 0 0 0 0 0 0 0 0 0 0 0
  93947. - 0 0 0 0 0 0 0 0 0 0 0 0
  93948. - 0 0 0 0 0 0 0 0 0 0 0 0
  93949. - 0 0 0 0 0 0 0 0 0 0 0 0
  93950. - 0 0 0 0 0 0 0 0 0 0 0 0
  93951. - 14 14 14 42 42 42 86 86 86 10 10 10
  93952. - 2 2 6 2 2 6 2 2 6 2 2 6
  93953. - 2 2 6 2 2 6 2 2 6 2 2 6
  93954. - 2 2 6 2 2 6 2 2 6 30 30 30
  93955. - 94 94 94 94 94 94 58 58 58 26 26 26
  93956. - 2 2 6 6 6 6 78 78 78 54 54 54
  93957. - 22 22 22 6 6 6 0 0 0 0 0 0
  93958. - 0 0 0 0 0 0 0 0 0 0 0 0
  93959. - 0 0 0 0 0 0 0 0 0 0 0 0
  93960. - 0 0 0 0 0 0 0 0 0 0 0 0
  93961. - 0 0 0 0 0 0 0 0 0 0 0 0
  93962. - 0 0 0 0 0 0 0 0 0 0 0 0
  93963. - 0 0 0 0 0 0 0 0 0 0 0 0
  93964. - 0 0 0 0 0 0 0 0 0 0 0 0
  93965. - 0 0 0 0 0 0 0 0 0 0 0 0
  93966. - 0 0 0 0 0 0 0 0 0 0 0 0
  93967. - 0 0 0 0 0 0 0 0 0 0 0 0
  93968. - 0 0 0 0 0 0 0 0 0 0 0 0
  93969. - 0 0 0 0 0 0 0 0 0 0 0 0
  93970. - 0 0 0 0 0 0 0 0 0 6 6 6
  93971. - 22 22 22 62 62 62 62 62 62 2 2 6
  93972. - 2 2 6 2 2 6 2 2 6 2 2 6
  93973. - 2 2 6 2 2 6 2 2 6 2 2 6
  93974. - 2 2 6 2 2 6 2 2 6 26 26 26
  93975. - 54 54 54 38 38 38 18 18 18 10 10 10
  93976. - 2 2 6 2 2 6 34 34 34 82 82 82
  93977. - 38 38 38 14 14 14 0 0 0 0 0 0
  93978. - 0 0 0 0 0 0 0 0 0 0 0 0
  93979. - 0 0 0 0 0 0 0 0 0 0 0 0
  93980. - 0 0 0 0 0 0 0 0 0 0 0 0
  93981. - 0 0 0 0 0 0 0 0 0 0 0 0
  93982. - 0 0 0 0 0 0 0 0 0 0 0 0
  93983. - 0 0 0 0 0 0 0 0 0 0 0 0
  93984. - 0 0 0 0 0 0 0 0 0 0 0 0
  93985. - 0 0 0 0 0 1 0 0 1 0 0 0
  93986. - 0 0 0 0 0 0 0 0 0 0 0 0
  93987. - 0 0 0 0 0 0 0 0 0 0 0 0
  93988. - 0 0 0 0 0 0 0 0 0 0 0 0
  93989. - 0 0 0 0 0 0 0 0 0 0 0 0
  93990. - 0 0 0 0 0 0 0 0 0 6 6 6
  93991. - 30 30 30 78 78 78 30 30 30 2 2 6
  93992. - 2 2 6 2 2 6 2 2 6 2 2 6
  93993. - 2 2 6 2 2 6 2 2 6 2 2 6
  93994. - 2 2 6 2 2 6 2 2 6 10 10 10
  93995. - 10 10 10 2 2 6 2 2 6 2 2 6
  93996. - 2 2 6 2 2 6 2 2 6 78 78 78
  93997. - 50 50 50 18 18 18 6 6 6 0 0 0
  93998. - 0 0 0 0 0 0 0 0 0 0 0 0
  93999. - 0 0 0 0 0 0 0 0 0 0 0 0
  94000. - 0 0 0 0 0 0 0 0 0 0 0 0
  94001. - 0 0 0 0 0 0 0 0 0 0 0 0
  94002. - 0 0 0 0 0 0 0 0 0 0 0 0
  94003. - 0 0 0 0 0 0 0 0 0 0 0 0
  94004. - 0 0 0 0 0 0 0 0 0 0 0 0
  94005. - 0 0 1 0 0 0 0 0 0 0 0 0
  94006. - 0 0 0 0 0 0 0 0 0 0 0 0
  94007. - 0 0 0 0 0 0 0 0 0 0 0 0
  94008. - 0 0 0 0 0 0 0 0 0 0 0 0
  94009. - 0 0 0 0 0 0 0 0 0 0 0 0
  94010. - 0 0 0 0 0 0 0 0 0 10 10 10
  94011. - 38 38 38 86 86 86 14 14 14 2 2 6
  94012. - 2 2 6 2 2 6 2 2 6 2 2 6
  94013. - 2 2 6 2 2 6 2 2 6 2 2 6
  94014. - 2 2 6 2 2 6 2 2 6 2 2 6
  94015. - 2 2 6 2 2 6 2 2 6 2 2 6
  94016. - 2 2 6 2 2 6 2 2 6 54 54 54
  94017. - 66 66 66 26 26 26 6 6 6 0 0 0
  94018. - 0 0 0 0 0 0 0 0 0 0 0 0
  94019. - 0 0 0 0 0 0 0 0 0 0 0 0
  94020. - 0 0 0 0 0 0 0 0 0 0 0 0
  94021. - 0 0 0 0 0 0 0 0 0 0 0 0
  94022. - 0 0 0 0 0 0 0 0 0 0 0 0
  94023. - 0 0 0 0 0 0 0 0 0 0 0 0
  94024. - 0 0 0 0 0 0 0 0 0 0 0 0
  94025. - 0 0 0 0 0 1 0 0 1 0 0 0
  94026. - 0 0 0 0 0 0 0 0 0 0 0 0
  94027. - 0 0 0 0 0 0 0 0 0 0 0 0
  94028. - 0 0 0 0 0 0 0 0 0 0 0 0
  94029. - 0 0 0 0 0 0 0 0 0 0 0 0
  94030. - 0 0 0 0 0 0 0 0 0 14 14 14
  94031. - 42 42 42 82 82 82 2 2 6 2 2 6
  94032. - 2 2 6 6 6 6 10 10 10 2 2 6
  94033. - 2 2 6 2 2 6 2 2 6 2 2 6
  94034. - 2 2 6 2 2 6 2 2 6 6 6 6
  94035. - 14 14 14 10 10 10 2 2 6 2 2 6
  94036. - 2 2 6 2 2 6 2 2 6 18 18 18
  94037. - 82 82 82 34 34 34 10 10 10 0 0 0
  94038. - 0 0 0 0 0 0 0 0 0 0 0 0
  94039. - 0 0 0 0 0 0 0 0 0 0 0 0
  94040. - 0 0 0 0 0 0 0 0 0 0 0 0
  94041. - 0 0 0 0 0 0 0 0 0 0 0 0
  94042. - 0 0 0 0 0 0 0 0 0 0 0 0
  94043. - 0 0 0 0 0 0 0 0 0 0 0 0
  94044. - 0 0 0 0 0 0 0 0 0 0 0 0
  94045. - 0 0 1 0 0 0 0 0 0 0 0 0
  94046. - 0 0 0 0 0 0 0 0 0 0 0 0
  94047. - 0 0 0 0 0 0 0 0 0 0 0 0
  94048. - 0 0 0 0 0 0 0 0 0 0 0 0
  94049. - 0 0 0 0 0 0 0 0 0 0 0 0
  94050. - 0 0 0 0 0 0 0 0 0 14 14 14
  94051. - 46 46 46 86 86 86 2 2 6 2 2 6
  94052. - 6 6 6 6 6 6 22 22 22 34 34 34
  94053. - 6 6 6 2 2 6 2 2 6 2 2 6
  94054. - 2 2 6 2 2 6 18 18 18 34 34 34
  94055. - 10 10 10 50 50 50 22 22 22 2 2 6
  94056. - 2 2 6 2 2 6 2 2 6 10 10 10
  94057. - 86 86 86 42 42 42 14 14 14 0 0 0
  94058. - 0 0 0 0 0 0 0 0 0 0 0 0
  94059. - 0 0 0 0 0 0 0 0 0 0 0 0
  94060. - 0 0 0 0 0 0 0 0 0 0 0 0
  94061. - 0 0 0 0 0 0 0 0 0 0 0 0
  94062. - 0 0 0 0 0 0 0 0 0 0 0 0
  94063. - 0 0 0 0 0 0 0 0 0 0 0 0
  94064. - 0 0 0 0 0 0 0 0 0 0 0 0
  94065. - 0 0 1 0 0 1 0 0 1 0 0 0
  94066. - 0 0 0 0 0 0 0 0 0 0 0 0
  94067. - 0 0 0 0 0 0 0 0 0 0 0 0
  94068. - 0 0 0 0 0 0 0 0 0 0 0 0
  94069. - 0 0 0 0 0 0 0 0 0 0 0 0
  94070. - 0 0 0 0 0 0 0 0 0 14 14 14
  94071. - 46 46 46 86 86 86 2 2 6 2 2 6
  94072. - 38 38 38 116 116 116 94 94 94 22 22 22
  94073. - 22 22 22 2 2 6 2 2 6 2 2 6
  94074. - 14 14 14 86 86 86 138 138 138 162 162 162
  94075. -154 154 154 38 38 38 26 26 26 6 6 6
  94076. - 2 2 6 2 2 6 2 2 6 2 2 6
  94077. - 86 86 86 46 46 46 14 14 14 0 0 0
  94078. - 0 0 0 0 0 0 0 0 0 0 0 0
  94079. - 0 0 0 0 0 0 0 0 0 0 0 0
  94080. - 0 0 0 0 0 0 0 0 0 0 0 0
  94081. - 0 0 0 0 0 0 0 0 0 0 0 0
  94082. - 0 0 0 0 0 0 0 0 0 0 0 0
  94083. - 0 0 0 0 0 0 0 0 0 0 0 0
  94084. - 0 0 0 0 0 0 0 0 0 0 0 0
  94085. - 0 0 0 0 0 0 0 0 0 0 0 0
  94086. - 0 0 0 0 0 0 0 0 0 0 0 0
  94087. - 0 0 0 0 0 0 0 0 0 0 0 0
  94088. - 0 0 0 0 0 0 0 0 0 0 0 0
  94089. - 0 0 0 0 0 0 0 0 0 0 0 0
  94090. - 0 0 0 0 0 0 0 0 0 14 14 14
  94091. - 46 46 46 86 86 86 2 2 6 14 14 14
  94092. -134 134 134 198 198 198 195 195 195 116 116 116
  94093. - 10 10 10 2 2 6 2 2 6 6 6 6
  94094. -101 98 89 187 187 187 210 210 210 218 218 218
  94095. -214 214 214 134 134 134 14 14 14 6 6 6
  94096. - 2 2 6 2 2 6 2 2 6 2 2 6
  94097. - 86 86 86 50 50 50 18 18 18 6 6 6
  94098. - 0 0 0 0 0 0 0 0 0 0 0 0
  94099. - 0 0 0 0 0 0 0 0 0 0 0 0
  94100. - 0 0 0 0 0 0 0 0 0 0 0 0
  94101. - 0 0 0 0 0 0 0 0 0 0 0 0
  94102. - 0 0 0 0 0 0 0 0 0 0 0 0
  94103. - 0 0 0 0 0 0 0 0 0 0 0 0
  94104. - 0 0 0 0 0 0 0 0 1 0 0 0
  94105. - 0 0 1 0 0 1 0 0 1 0 0 0
  94106. - 0 0 0 0 0 0 0 0 0 0 0 0
  94107. - 0 0 0 0 0 0 0 0 0 0 0 0
  94108. - 0 0 0 0 0 0 0 0 0 0 0 0
  94109. - 0 0 0 0 0 0 0 0 0 0 0 0
  94110. - 0 0 0 0 0 0 0 0 0 14 14 14
  94111. - 46 46 46 86 86 86 2 2 6 54 54 54
  94112. -218 218 218 195 195 195 226 226 226 246 246 246
  94113. - 58 58 58 2 2 6 2 2 6 30 30 30
  94114. -210 210 210 253 253 253 174 174 174 123 123 123
  94115. -221 221 221 234 234 234 74 74 74 2 2 6
  94116. - 2 2 6 2 2 6 2 2 6 2 2 6
  94117. - 70 70 70 58 58 58 22 22 22 6 6 6
  94118. - 0 0 0 0 0 0 0 0 0 0 0 0
  94119. - 0 0 0 0 0 0 0 0 0 0 0 0
  94120. - 0 0 0 0 0 0 0 0 0 0 0 0
  94121. - 0 0 0 0 0 0 0 0 0 0 0 0
  94122. - 0 0 0 0 0 0 0 0 0 0 0 0
  94123. - 0 0 0 0 0 0 0 0 0 0 0 0
  94124. - 0 0 0 0 0 0 0 0 0 0 0 0
  94125. - 0 0 0 0 0 0 0 0 0 0 0 0
  94126. - 0 0 0 0 0 0 0 0 0 0 0 0
  94127. - 0 0 0 0 0 0 0 0 0 0 0 0
  94128. - 0 0 0 0 0 0 0 0 0 0 0 0
  94129. - 0 0 0 0 0 0 0 0 0 0 0 0
  94130. - 0 0 0 0 0 0 0 0 0 14 14 14
  94131. - 46 46 46 82 82 82 2 2 6 106 106 106
  94132. -170 170 170 26 26 26 86 86 86 226 226 226
  94133. -123 123 123 10 10 10 14 14 14 46 46 46
  94134. -231 231 231 190 190 190 6 6 6 70 70 70
  94135. - 90 90 90 238 238 238 158 158 158 2 2 6
  94136. - 2 2 6 2 2 6 2 2 6 2 2 6
  94137. - 70 70 70 58 58 58 22 22 22 6 6 6
  94138. - 0 0 0 0 0 0 0 0 0 0 0 0
  94139. - 0 0 0 0 0 0 0 0 0 0 0 0
  94140. - 0 0 0 0 0 0 0 0 0 0 0 0
  94141. - 0 0 0 0 0 0 0 0 0 0 0 0
  94142. - 0 0 0 0 0 0 0 0 0 0 0 0
  94143. - 0 0 0 0 0 0 0 0 0 0 0 0
  94144. - 0 0 0 0 0 0 0 0 1 0 0 0
  94145. - 0 0 1 0 0 1 0 0 1 0 0 0
  94146. - 0 0 0 0 0 0 0 0 0 0 0 0
  94147. - 0 0 0 0 0 0 0 0 0 0 0 0
  94148. - 0 0 0 0 0 0 0 0 0 0 0 0
  94149. - 0 0 0 0 0 0 0 0 0 0 0 0
  94150. - 0 0 0 0 0 0 0 0 0 14 14 14
  94151. - 42 42 42 86 86 86 6 6 6 116 116 116
  94152. -106 106 106 6 6 6 70 70 70 149 149 149
  94153. -128 128 128 18 18 18 38 38 38 54 54 54
  94154. -221 221 221 106 106 106 2 2 6 14 14 14
  94155. - 46 46 46 190 190 190 198 198 198 2 2 6
  94156. - 2 2 6 2 2 6 2 2 6 2 2 6
  94157. - 74 74 74 62 62 62 22 22 22 6 6 6
  94158. - 0 0 0 0 0 0 0 0 0 0 0 0
  94159. - 0 0 0 0 0 0 0 0 0 0 0 0
  94160. - 0 0 0 0 0 0 0 0 0 0 0 0
  94161. - 0 0 0 0 0 0 0 0 0 0 0 0
  94162. - 0 0 0 0 0 0 0 0 0 0 0 0
  94163. - 0 0 0 0 0 0 0 0 0 0 0 0
  94164. - 0 0 0 0 0 0 0 0 1 0 0 0
  94165. - 0 0 1 0 0 0 0 0 1 0 0 0
  94166. - 0 0 0 0 0 0 0 0 0 0 0 0
  94167. - 0 0 0 0 0 0 0 0 0 0 0 0
  94168. - 0 0 0 0 0 0 0 0 0 0 0 0
  94169. - 0 0 0 0 0 0 0 0 0 0 0 0
  94170. - 0 0 0 0 0 0 0 0 0 14 14 14
  94171. - 42 42 42 94 94 94 14 14 14 101 101 101
  94172. -128 128 128 2 2 6 18 18 18 116 116 116
  94173. -118 98 46 121 92 8 121 92 8 98 78 10
  94174. -162 162 162 106 106 106 2 2 6 2 2 6
  94175. - 2 2 6 195 195 195 195 195 195 6 6 6
  94176. - 2 2 6 2 2 6 2 2 6 2 2 6
  94177. - 74 74 74 62 62 62 22 22 22 6 6 6
  94178. - 0 0 0 0 0 0 0 0 0 0 0 0
  94179. - 0 0 0 0 0 0 0 0 0 0 0 0
  94180. - 0 0 0 0 0 0 0 0 0 0 0 0
  94181. - 0 0 0 0 0 0 0 0 0 0 0 0
  94182. - 0 0 0 0 0 0 0 0 0 0 0 0
  94183. - 0 0 0 0 0 0 0 0 0 0 0 0
  94184. - 0 0 0 0 0 0 0 0 1 0 0 1
  94185. - 0 0 1 0 0 0 0 0 1 0 0 0
  94186. - 0 0 0 0 0 0 0 0 0 0 0 0
  94187. - 0 0 0 0 0 0 0 0 0 0 0 0
  94188. - 0 0 0 0 0 0 0 0 0 0 0 0
  94189. - 0 0 0 0 0 0 0 0 0 0 0 0
  94190. - 0 0 0 0 0 0 0 0 0 10 10 10
  94191. - 38 38 38 90 90 90 14 14 14 58 58 58
  94192. -210 210 210 26 26 26 54 38 6 154 114 10
  94193. -226 170 11 236 186 11 225 175 15 184 144 12
  94194. -215 174 15 175 146 61 37 26 9 2 2 6
  94195. - 70 70 70 246 246 246 138 138 138 2 2 6
  94196. - 2 2 6 2 2 6 2 2 6 2 2 6
  94197. - 70 70 70 66 66 66 26 26 26 6 6 6
  94198. - 0 0 0 0 0 0 0 0 0 0 0 0
  94199. - 0 0 0 0 0 0 0 0 0 0 0 0
  94200. - 0 0 0 0 0 0 0 0 0 0 0 0
  94201. - 0 0 0 0 0 0 0 0 0 0 0 0
  94202. - 0 0 0 0 0 0 0 0 0 0 0 0
  94203. - 0 0 0 0 0 0 0 0 0 0 0 0
  94204. - 0 0 0 0 0 0 0 0 0 0 0 0
  94205. - 0 0 0 0 0 0 0 0 0 0 0 0
  94206. - 0 0 0 0 0 0 0 0 0 0 0 0
  94207. - 0 0 0 0 0 0 0 0 0 0 0 0
  94208. - 0 0 0 0 0 0 0 0 0 0 0 0
  94209. - 0 0 0 0 0 0 0 0 0 0 0 0
  94210. - 0 0 0 0 0 0 0 0 0 10 10 10
  94211. - 38 38 38 86 86 86 14 14 14 10 10 10
  94212. -195 195 195 188 164 115 192 133 9 225 175 15
  94213. -239 182 13 234 190 10 232 195 16 232 200 30
  94214. -245 207 45 241 208 19 232 195 16 184 144 12
  94215. -218 194 134 211 206 186 42 42 42 2 2 6
  94216. - 2 2 6 2 2 6 2 2 6 2 2 6
  94217. - 50 50 50 74 74 74 30 30 30 6 6 6
  94218. - 0 0 0 0 0 0 0 0 0 0 0 0
  94219. - 0 0 0 0 0 0 0 0 0 0 0 0
  94220. - 0 0 0 0 0 0 0 0 0 0 0 0
  94221. - 0 0 0 0 0 0 0 0 0 0 0 0
  94222. - 0 0 0 0 0 0 0 0 0 0 0 0
  94223. - 0 0 0 0 0 0 0 0 0 0 0 0
  94224. - 0 0 0 0 0 0 0 0 0 0 0 0
  94225. - 0 0 0 0 0 0 0 0 0 0 0 0
  94226. - 0 0 0 0 0 0 0 0 0 0 0 0
  94227. - 0 0 0 0 0 0 0 0 0 0 0 0
  94228. - 0 0 0 0 0 0 0 0 0 0 0 0
  94229. - 0 0 0 0 0 0 0 0 0 0 0 0
  94230. - 0 0 0 0 0 0 0 0 0 10 10 10
  94231. - 34 34 34 86 86 86 14 14 14 2 2 6
  94232. -121 87 25 192 133 9 219 162 10 239 182 13
  94233. -236 186 11 232 195 16 241 208 19 244 214 54
  94234. -246 218 60 246 218 38 246 215 20 241 208 19
  94235. -241 208 19 226 184 13 121 87 25 2 2 6
  94236. - 2 2 6 2 2 6 2 2 6 2 2 6
  94237. - 50 50 50 82 82 82 34 34 34 10 10 10
  94238. - 0 0 0 0 0 0 0 0 0 0 0 0
  94239. - 0 0 0 0 0 0 0 0 0 0 0 0
  94240. - 0 0 0 0 0 0 0 0 0 0 0 0
  94241. - 0 0 0 0 0 0 0 0 0 0 0 0
  94242. - 0 0 0 0 0 0 0 0 0 0 0 0
  94243. - 0 0 0 0 0 0 0 0 0 0 0 0
  94244. - 0 0 0 0 0 0 0 0 0 0 0 0
  94245. - 0 0 0 0 0 0 0 0 0 0 0 0
  94246. - 0 0 0 0 0 0 0 0 0 0 0 0
  94247. - 0 0 0 0 0 0 0 0 0 0 0 0
  94248. - 0 0 0 0 0 0 0 0 0 0 0 0
  94249. - 0 0 0 0 0 0 0 0 0 0 0 0
  94250. - 0 0 0 0 0 0 0 0 0 10 10 10
  94251. - 34 34 34 82 82 82 30 30 30 61 42 6
  94252. -180 123 7 206 145 10 230 174 11 239 182 13
  94253. -234 190 10 238 202 15 241 208 19 246 218 74
  94254. -246 218 38 246 215 20 246 215 20 246 215 20
  94255. -226 184 13 215 174 15 184 144 12 6 6 6
  94256. - 2 2 6 2 2 6 2 2 6 2 2 6
  94257. - 26 26 26 94 94 94 42 42 42 14 14 14
  94258. - 0 0 0 0 0 0 0 0 0 0 0 0
  94259. - 0 0 0 0 0 0 0 0 0 0 0 0
  94260. - 0 0 0 0 0 0 0 0 0 0 0 0
  94261. - 0 0 0 0 0 0 0 0 0 0 0 0
  94262. - 0 0 0 0 0 0 0 0 0 0 0 0
  94263. - 0 0 0 0 0 0 0 0 0 0 0 0
  94264. - 0 0 0 0 0 0 0 0 0 0 0 0
  94265. - 0 0 0 0 0 0 0 0 0 0 0 0
  94266. - 0 0 0 0 0 0 0 0 0 0 0 0
  94267. - 0 0 0 0 0 0 0 0 0 0 0 0
  94268. - 0 0 0 0 0 0 0 0 0 0 0 0
  94269. - 0 0 0 0 0 0 0 0 0 0 0 0
  94270. - 0 0 0 0 0 0 0 0 0 10 10 10
  94271. - 30 30 30 78 78 78 50 50 50 104 69 6
  94272. -192 133 9 216 158 10 236 178 12 236 186 11
  94273. -232 195 16 241 208 19 244 214 54 245 215 43
  94274. -246 215 20 246 215 20 241 208 19 198 155 10
  94275. -200 144 11 216 158 10 156 118 10 2 2 6
  94276. - 2 2 6 2 2 6 2 2 6 2 2 6
  94277. - 6 6 6 90 90 90 54 54 54 18 18 18
  94278. - 6 6 6 0 0 0 0 0 0 0 0 0
  94279. - 0 0 0 0 0 0 0 0 0 0 0 0
  94280. - 0 0 0 0 0 0 0 0 0 0 0 0
  94281. - 0 0 0 0 0 0 0 0 0 0 0 0
  94282. - 0 0 0 0 0 0 0 0 0 0 0 0
  94283. - 0 0 0 0 0 0 0 0 0 0 0 0
  94284. - 0 0 0 0 0 0 0 0 0 0 0 0
  94285. - 0 0 0 0 0 0 0 0 0 0 0 0
  94286. - 0 0 0 0 0 0 0 0 0 0 0 0
  94287. - 0 0 0 0 0 0 0 0 0 0 0 0
  94288. - 0 0 0 0 0 0 0 0 0 0 0 0
  94289. - 0 0 0 0 0 0 0 0 0 0 0 0
  94290. - 0 0 0 0 0 0 0 0 0 10 10 10
  94291. - 30 30 30 78 78 78 46 46 46 22 22 22
  94292. -137 92 6 210 162 10 239 182 13 238 190 10
  94293. -238 202 15 241 208 19 246 215 20 246 215 20
  94294. -241 208 19 203 166 17 185 133 11 210 150 10
  94295. -216 158 10 210 150 10 102 78 10 2 2 6
  94296. - 6 6 6 54 54 54 14 14 14 2 2 6
  94297. - 2 2 6 62 62 62 74 74 74 30 30 30
  94298. - 10 10 10 0 0 0 0 0 0 0 0 0
  94299. - 0 0 0 0 0 0 0 0 0 0 0 0
  94300. - 0 0 0 0 0 0 0 0 0 0 0 0
  94301. - 0 0 0 0 0 0 0 0 0 0 0 0
  94302. - 0 0 0 0 0 0 0 0 0 0 0 0
  94303. - 0 0 0 0 0 0 0 0 0 0 0 0
  94304. - 0 0 0 0 0 0 0 0 0 0 0 0
  94305. - 0 0 0 0 0 0 0 0 0 0 0 0
  94306. - 0 0 0 0 0 0 0 0 0 0 0 0
  94307. - 0 0 0 0 0 0 0 0 0 0 0 0
  94308. - 0 0 0 0 0 0 0 0 0 0 0 0
  94309. - 0 0 0 0 0 0 0 0 0 0 0 0
  94310. - 0 0 0 0 0 0 0 0 0 10 10 10
  94311. - 34 34 34 78 78 78 50 50 50 6 6 6
  94312. - 94 70 30 139 102 15 190 146 13 226 184 13
  94313. -232 200 30 232 195 16 215 174 15 190 146 13
  94314. -168 122 10 192 133 9 210 150 10 213 154 11
  94315. -202 150 34 182 157 106 101 98 89 2 2 6
  94316. - 2 2 6 78 78 78 116 116 116 58 58 58
  94317. - 2 2 6 22 22 22 90 90 90 46 46 46
  94318. - 18 18 18 6 6 6 0 0 0 0 0 0
  94319. - 0 0 0 0 0 0 0 0 0 0 0 0
  94320. - 0 0 0 0 0 0 0 0 0 0 0 0
  94321. - 0 0 0 0 0 0 0 0 0 0 0 0
  94322. - 0 0 0 0 0 0 0 0 0 0 0 0
  94323. - 0 0 0 0 0 0 0 0 0 0 0 0
  94324. - 0 0 0 0 0 0 0 0 0 0 0 0
  94325. - 0 0 0 0 0 0 0 0 0 0 0 0
  94326. - 0 0 0 0 0 0 0 0 0 0 0 0
  94327. - 0 0 0 0 0 0 0 0 0 0 0 0
  94328. - 0 0 0 0 0 0 0 0 0 0 0 0
  94329. - 0 0 0 0 0 0 0 0 0 0 0 0
  94330. - 0 0 0 0 0 0 0 0 0 10 10 10
  94331. - 38 38 38 86 86 86 50 50 50 6 6 6
  94332. -128 128 128 174 154 114 156 107 11 168 122 10
  94333. -198 155 10 184 144 12 197 138 11 200 144 11
  94334. -206 145 10 206 145 10 197 138 11 188 164 115
  94335. -195 195 195 198 198 198 174 174 174 14 14 14
  94336. - 2 2 6 22 22 22 116 116 116 116 116 116
  94337. - 22 22 22 2 2 6 74 74 74 70 70 70
  94338. - 30 30 30 10 10 10 0 0 0 0 0 0
  94339. - 0 0 0 0 0 0 0 0 0 0 0 0
  94340. - 0 0 0 0 0 0 0 0 0 0 0 0
  94341. - 0 0 0 0 0 0 0 0 0 0 0 0
  94342. - 0 0 0 0 0 0 0 0 0 0 0 0
  94343. - 0 0 0 0 0 0 0 0 0 0 0 0
  94344. - 0 0 0 0 0 0 0 0 0 0 0 0
  94345. - 0 0 0 0 0 0 0 0 0 0 0 0
  94346. - 0 0 0 0 0 0 0 0 0 0 0 0
  94347. - 0 0 0 0 0 0 0 0 0 0 0 0
  94348. - 0 0 0 0 0 0 0 0 0 0 0 0
  94349. - 0 0 0 0 0 0 0 0 0 0 0 0
  94350. - 0 0 0 0 0 0 6 6 6 18 18 18
  94351. - 50 50 50 101 101 101 26 26 26 10 10 10
  94352. -138 138 138 190 190 190 174 154 114 156 107 11
  94353. -197 138 11 200 144 11 197 138 11 192 133 9
  94354. -180 123 7 190 142 34 190 178 144 187 187 187
  94355. -202 202 202 221 221 221 214 214 214 66 66 66
  94356. - 2 2 6 2 2 6 50 50 50 62 62 62
  94357. - 6 6 6 2 2 6 10 10 10 90 90 90
  94358. - 50 50 50 18 18 18 6 6 6 0 0 0
  94359. - 0 0 0 0 0 0 0 0 0 0 0 0
  94360. - 0 0 0 0 0 0 0 0 0 0 0 0
  94361. - 0 0 0 0 0 0 0 0 0 0 0 0
  94362. - 0 0 0 0 0 0 0 0 0 0 0 0
  94363. - 0 0 0 0 0 0 0 0 0 0 0 0
  94364. - 0 0 0 0 0 0 0 0 0 0 0 0
  94365. - 0 0 0 0 0 0 0 0 0 0 0 0
  94366. - 0 0 0 0 0 0 0 0 0 0 0 0
  94367. - 0 0 0 0 0 0 0 0 0 0 0 0
  94368. - 0 0 0 0 0 0 0 0 0 0 0 0
  94369. - 0 0 0 0 0 0 0 0 0 0 0 0
  94370. - 0 0 0 0 0 0 10 10 10 34 34 34
  94371. - 74 74 74 74 74 74 2 2 6 6 6 6
  94372. -144 144 144 198 198 198 190 190 190 178 166 146
  94373. -154 121 60 156 107 11 156 107 11 168 124 44
  94374. -174 154 114 187 187 187 190 190 190 210 210 210
  94375. -246 246 246 253 253 253 253 253 253 182 182 182
  94376. - 6 6 6 2 2 6 2 2 6 2 2 6
  94377. - 2 2 6 2 2 6 2 2 6 62 62 62
  94378. - 74 74 74 34 34 34 14 14 14 0 0 0
  94379. - 0 0 0 0 0 0 0 0 0 0 0 0
  94380. - 0 0 0 0 0 0 0 0 0 0 0 0
  94381. - 0 0 0 0 0 0 0 0 0 0 0 0
  94382. - 0 0 0 0 0 0 0 0 0 0 0 0
  94383. - 0 0 0 0 0 0 0 0 0 0 0 0
  94384. - 0 0 0 0 0 0 0 0 0 0 0 0
  94385. - 0 0 0 0 0 0 0 0 0 0 0 0
  94386. - 0 0 0 0 0 0 0 0 0 0 0 0
  94387. - 0 0 0 0 0 0 0 0 0 0 0 0
  94388. - 0 0 0 0 0 0 0 0 0 0 0 0
  94389. - 0 0 0 0 0 0 0 0 0 0 0 0
  94390. - 0 0 0 10 10 10 22 22 22 54 54 54
  94391. - 94 94 94 18 18 18 2 2 6 46 46 46
  94392. -234 234 234 221 221 221 190 190 190 190 190 190
  94393. -190 190 190 187 187 187 187 187 187 190 190 190
  94394. -190 190 190 195 195 195 214 214 214 242 242 242
  94395. -253 253 253 253 253 253 253 253 253 253 253 253
  94396. - 82 82 82 2 2 6 2 2 6 2 2 6
  94397. - 2 2 6 2 2 6 2 2 6 14 14 14
  94398. - 86 86 86 54 54 54 22 22 22 6 6 6
  94399. - 0 0 0 0 0 0 0 0 0 0 0 0
  94400. - 0 0 0 0 0 0 0 0 0 0 0 0
  94401. - 0 0 0 0 0 0 0 0 0 0 0 0
  94402. - 0 0 0 0 0 0 0 0 0 0 0 0
  94403. - 0 0 0 0 0 0 0 0 0 0 0 0
  94404. - 0 0 0 0 0 0 0 0 0 0 0 0
  94405. - 0 0 0 0 0 0 0 0 0 0 0 0
  94406. - 0 0 0 0 0 0 0 0 0 0 0 0
  94407. - 0 0 0 0 0 0 0 0 0 0 0 0
  94408. - 0 0 0 0 0 0 0 0 0 0 0 0
  94409. - 0 0 0 0 0 0 0 0 0 0 0 0
  94410. - 6 6 6 18 18 18 46 46 46 90 90 90
  94411. - 46 46 46 18 18 18 6 6 6 182 182 182
  94412. -253 253 253 246 246 246 206 206 206 190 190 190
  94413. -190 190 190 190 190 190 190 190 190 190 190 190
  94414. -206 206 206 231 231 231 250 250 250 253 253 253
  94415. -253 253 253 253 253 253 253 253 253 253 253 253
  94416. -202 202 202 14 14 14 2 2 6 2 2 6
  94417. - 2 2 6 2 2 6 2 2 6 2 2 6
  94418. - 42 42 42 86 86 86 42 42 42 18 18 18
  94419. - 6 6 6 0 0 0 0 0 0 0 0 0
  94420. - 0 0 0 0 0 0 0 0 0 0 0 0
  94421. - 0 0 0 0 0 0 0 0 0 0 0 0
  94422. - 0 0 0 0 0 0 0 0 0 0 0 0
  94423. - 0 0 0 0 0 0 0 0 0 0 0 0
  94424. - 0 0 0 0 0 0 0 0 0 0 0 0
  94425. - 0 0 0 0 0 0 0 0 0 0 0 0
  94426. - 0 0 0 0 0 0 0 0 0 0 0 0
  94427. - 0 0 0 0 0 0 0 0 0 0 0 0
  94428. - 0 0 0 0 0 0 0 0 0 0 0 0
  94429. - 0 0 0 0 0 0 0 0 0 6 6 6
  94430. - 14 14 14 38 38 38 74 74 74 66 66 66
  94431. - 2 2 6 6 6 6 90 90 90 250 250 250
  94432. -253 253 253 253 253 253 238 238 238 198 198 198
  94433. -190 190 190 190 190 190 195 195 195 221 221 221
  94434. -246 246 246 253 253 253 253 253 253 253 253 253
  94435. -253 253 253 253 253 253 253 253 253 253 253 253
  94436. -253 253 253 82 82 82 2 2 6 2 2 6
  94437. - 2 2 6 2 2 6 2 2 6 2 2 6
  94438. - 2 2 6 78 78 78 70 70 70 34 34 34
  94439. - 14 14 14 6 6 6 0 0 0 0 0 0
  94440. - 0 0 0 0 0 0 0 0 0 0 0 0
  94441. - 0 0 0 0 0 0 0 0 0 0 0 0
  94442. - 0 0 0 0 0 0 0 0 0 0 0 0
  94443. - 0 0 0 0 0 0 0 0 0 0 0 0
  94444. - 0 0 0 0 0 0 0 0 0 0 0 0
  94445. - 0 0 0 0 0 0 0 0 0 0 0 0
  94446. - 0 0 0 0 0 0 0 0 0 0 0 0
  94447. - 0 0 0 0 0 0 0 0 0 0 0 0
  94448. - 0 0 0 0 0 0 0 0 0 0 0 0
  94449. - 0 0 0 0 0 0 0 0 0 14 14 14
  94450. - 34 34 34 66 66 66 78 78 78 6 6 6
  94451. - 2 2 6 18 18 18 218 218 218 253 253 253
  94452. -253 253 253 253 253 253 253 253 253 246 246 246
  94453. -226 226 226 231 231 231 246 246 246 253 253 253
  94454. -253 253 253 253 253 253 253 253 253 253 253 253
  94455. -253 253 253 253 253 253 253 253 253 253 253 253
  94456. -253 253 253 178 178 178 2 2 6 2 2 6
  94457. - 2 2 6 2 2 6 2 2 6 2 2 6
  94458. - 2 2 6 18 18 18 90 90 90 62 62 62
  94459. - 30 30 30 10 10 10 0 0 0 0 0 0
  94460. - 0 0 0 0 0 0 0 0 0 0 0 0
  94461. - 0 0 0 0 0 0 0 0 0 0 0 0
  94462. - 0 0 0 0 0 0 0 0 0 0 0 0
  94463. - 0 0 0 0 0 0 0 0 0 0 0 0
  94464. - 0 0 0 0 0 0 0 0 0 0 0 0
  94465. - 0 0 0 0 0 0 0 0 0 0 0 0
  94466. - 0 0 0 0 0 0 0 0 0 0 0 0
  94467. - 0 0 0 0 0 0 0 0 0 0 0 0
  94468. - 0 0 0 0 0 0 0 0 0 0 0 0
  94469. - 0 0 0 0 0 0 10 10 10 26 26 26
  94470. - 58 58 58 90 90 90 18 18 18 2 2 6
  94471. - 2 2 6 110 110 110 253 253 253 253 253 253
  94472. -253 253 253 253 253 253 253 253 253 253 253 253
  94473. -250 250 250 253 253 253 253 253 253 253 253 253
  94474. -253 253 253 253 253 253 253 253 253 253 253 253
  94475. -253 253 253 253 253 253 253 253 253 253 253 253
  94476. -253 253 253 231 231 231 18 18 18 2 2 6
  94477. - 2 2 6 2 2 6 2 2 6 2 2 6
  94478. - 2 2 6 2 2 6 18 18 18 94 94 94
  94479. - 54 54 54 26 26 26 10 10 10 0 0 0
  94480. - 0 0 0 0 0 0 0 0 0 0 0 0
  94481. - 0 0 0 0 0 0 0 0 0 0 0 0
  94482. - 0 0 0 0 0 0 0 0 0 0 0 0
  94483. - 0 0 0 0 0 0 0 0 0 0 0 0
  94484. - 0 0 0 0 0 0 0 0 0 0 0 0
  94485. - 0 0 0 0 0 0 0 0 0 0 0 0
  94486. - 0 0 0 0 0 0 0 0 0 0 0 0
  94487. - 0 0 0 0 0 0 0 0 0 0 0 0
  94488. - 0 0 0 0 0 0 0 0 0 0 0 0
  94489. - 0 0 0 6 6 6 22 22 22 50 50 50
  94490. - 90 90 90 26 26 26 2 2 6 2 2 6
  94491. - 14 14 14 195 195 195 250 250 250 253 253 253
  94492. -253 253 253 253 253 253 253 253 253 253 253 253
  94493. -253 253 253 253 253 253 253 253 253 253 253 253
  94494. -253 253 253 253 253 253 253 253 253 253 253 253
  94495. -253 253 253 253 253 253 253 253 253 253 253 253
  94496. -250 250 250 242 242 242 54 54 54 2 2 6
  94497. - 2 2 6 2 2 6 2 2 6 2 2 6
  94498. - 2 2 6 2 2 6 2 2 6 38 38 38
  94499. - 86 86 86 50 50 50 22 22 22 6 6 6
  94500. - 0 0 0 0 0 0 0 0 0 0 0 0
  94501. - 0 0 0 0 0 0 0 0 0 0 0 0
  94502. - 0 0 0 0 0 0 0 0 0 0 0 0
  94503. - 0 0 0 0 0 0 0 0 0 0 0 0
  94504. - 0 0 0 0 0 0 0 0 0 0 0 0
  94505. - 0 0 0 0 0 0 0 0 0 0 0 0
  94506. - 0 0 0 0 0 0 0 0 0 0 0 0
  94507. - 0 0 0 0 0 0 0 0 0 0 0 0
  94508. - 0 0 0 0 0 0 0 0 0 0 0 0
  94509. - 6 6 6 14 14 14 38 38 38 82 82 82
  94510. - 34 34 34 2 2 6 2 2 6 2 2 6
  94511. - 42 42 42 195 195 195 246 246 246 253 253 253
  94512. -253 253 253 253 253 253 253 253 253 250 250 250
  94513. -242 242 242 242 242 242 250 250 250 253 253 253
  94514. -253 253 253 253 253 253 253 253 253 253 253 253
  94515. -253 253 253 250 250 250 246 246 246 238 238 238
  94516. -226 226 226 231 231 231 101 101 101 6 6 6
  94517. - 2 2 6 2 2 6 2 2 6 2 2 6
  94518. - 2 2 6 2 2 6 2 2 6 2 2 6
  94519. - 38 38 38 82 82 82 42 42 42 14 14 14
  94520. - 6 6 6 0 0 0 0 0 0 0 0 0
  94521. - 0 0 0 0 0 0 0 0 0 0 0 0
  94522. - 0 0 0 0 0 0 0 0 0 0 0 0
  94523. - 0 0 0 0 0 0 0 0 0 0 0 0
  94524. - 0 0 0 0 0 0 0 0 0 0 0 0
  94525. - 0 0 0 0 0 0 0 0 0 0 0 0
  94526. - 0 0 0 0 0 0 0 0 0 0 0 0
  94527. - 0 0 0 0 0 0 0 0 0 0 0 0
  94528. - 0 0 0 0 0 0 0 0 0 0 0 0
  94529. - 10 10 10 26 26 26 62 62 62 66 66 66
  94530. - 2 2 6 2 2 6 2 2 6 6 6 6
  94531. - 70 70 70 170 170 170 206 206 206 234 234 234
  94532. -246 246 246 250 250 250 250 250 250 238 238 238
  94533. -226 226 226 231 231 231 238 238 238 250 250 250
  94534. -250 250 250 250 250 250 246 246 246 231 231 231
  94535. -214 214 214 206 206 206 202 202 202 202 202 202
  94536. -198 198 198 202 202 202 182 182 182 18 18 18
  94537. - 2 2 6 2 2 6 2 2 6 2 2 6
  94538. - 2 2 6 2 2 6 2 2 6 2 2 6
  94539. - 2 2 6 62 62 62 66 66 66 30 30 30
  94540. - 10 10 10 0 0 0 0 0 0 0 0 0
  94541. - 0 0 0 0 0 0 0 0 0 0 0 0
  94542. - 0 0 0 0 0 0 0 0 0 0 0 0
  94543. - 0 0 0 0 0 0 0 0 0 0 0 0
  94544. - 0 0 0 0 0 0 0 0 0 0 0 0
  94545. - 0 0 0 0 0 0 0 0 0 0 0 0
  94546. - 0 0 0 0 0 0 0 0 0 0 0 0
  94547. - 0 0 0 0 0 0 0 0 0 0 0 0
  94548. - 0 0 0 0 0 0 0 0 0 0 0 0
  94549. - 14 14 14 42 42 42 82 82 82 18 18 18
  94550. - 2 2 6 2 2 6 2 2 6 10 10 10
  94551. - 94 94 94 182 182 182 218 218 218 242 242 242
  94552. -250 250 250 253 253 253 253 253 253 250 250 250
  94553. -234 234 234 253 253 253 253 253 253 253 253 253
  94554. -253 253 253 253 253 253 253 253 253 246 246 246
  94555. -238 238 238 226 226 226 210 210 210 202 202 202
  94556. -195 195 195 195 195 195 210 210 210 158 158 158
  94557. - 6 6 6 14 14 14 50 50 50 14 14 14
  94558. - 2 2 6 2 2 6 2 2 6 2 2 6
  94559. - 2 2 6 6 6 6 86 86 86 46 46 46
  94560. - 18 18 18 6 6 6 0 0 0 0 0 0
  94561. - 0 0 0 0 0 0 0 0 0 0 0 0
  94562. - 0 0 0 0 0 0 0 0 0 0 0 0
  94563. - 0 0 0 0 0 0 0 0 0 0 0 0
  94564. - 0 0 0 0 0 0 0 0 0 0 0 0
  94565. - 0 0 0 0 0 0 0 0 0 0 0 0
  94566. - 0 0 0 0 0 0 0 0 0 0 0 0
  94567. - 0 0 0 0 0 0 0 0 0 0 0 0
  94568. - 0 0 0 0 0 0 0 0 0 6 6 6
  94569. - 22 22 22 54 54 54 70 70 70 2 2 6
  94570. - 2 2 6 10 10 10 2 2 6 22 22 22
  94571. -166 166 166 231 231 231 250 250 250 253 253 253
  94572. -253 253 253 253 253 253 253 253 253 250 250 250
  94573. -242 242 242 253 253 253 253 253 253 253 253 253
  94574. -253 253 253 253 253 253 253 253 253 253 253 253
  94575. -253 253 253 253 253 253 253 253 253 246 246 246
  94576. -231 231 231 206 206 206 198 198 198 226 226 226
  94577. - 94 94 94 2 2 6 6 6 6 38 38 38
  94578. - 30 30 30 2 2 6 2 2 6 2 2 6
  94579. - 2 2 6 2 2 6 62 62 62 66 66 66
  94580. - 26 26 26 10 10 10 0 0 0 0 0 0
  94581. - 0 0 0 0 0 0 0 0 0 0 0 0
  94582. - 0 0 0 0 0 0 0 0 0 0 0 0
  94583. - 0 0 0 0 0 0 0 0 0 0 0 0
  94584. - 0 0 0 0 0 0 0 0 0 0 0 0
  94585. - 0 0 0 0 0 0 0 0 0 0 0 0
  94586. - 0 0 0 0 0 0 0 0 0 0 0 0
  94587. - 0 0 0 0 0 0 0 0 0 0 0 0
  94588. - 0 0 0 0 0 0 0 0 0 10 10 10
  94589. - 30 30 30 74 74 74 50 50 50 2 2 6
  94590. - 26 26 26 26 26 26 2 2 6 106 106 106
  94591. -238 238 238 253 253 253 253 253 253 253 253 253
  94592. -253 253 253 253 253 253 253 253 253 253 253 253
  94593. -253 253 253 253 253 253 253 253 253 253 253 253
  94594. -253 253 253 253 253 253 253 253 253 253 253 253
  94595. -253 253 253 253 253 253 253 253 253 253 253 253
  94596. -253 253 253 246 246 246 218 218 218 202 202 202
  94597. -210 210 210 14 14 14 2 2 6 2 2 6
  94598. - 30 30 30 22 22 22 2 2 6 2 2 6
  94599. - 2 2 6 2 2 6 18 18 18 86 86 86
  94600. - 42 42 42 14 14 14 0 0 0 0 0 0
  94601. - 0 0 0 0 0 0 0 0 0 0 0 0
  94602. - 0 0 0 0 0 0 0 0 0 0 0 0
  94603. - 0 0 0 0 0 0 0 0 0 0 0 0
  94604. - 0 0 0 0 0 0 0 0 0 0 0 0
  94605. - 0 0 0 0 0 0 0 0 0 0 0 0
  94606. - 0 0 0 0 0 0 0 0 0 0 0 0
  94607. - 0 0 0 0 0 0 0 0 0 0 0 0
  94608. - 0 0 0 0 0 0 0 0 0 14 14 14
  94609. - 42 42 42 90 90 90 22 22 22 2 2 6
  94610. - 42 42 42 2 2 6 18 18 18 218 218 218
  94611. -253 253 253 253 253 253 253 253 253 253 253 253
  94612. -253 253 253 253 253 253 253 253 253 253 253 253
  94613. -253 253 253 253 253 253 253 253 253 253 253 253
  94614. -253 253 253 253 253 253 253 253 253 253 253 253
  94615. -253 253 253 253 253 253 253 253 253 253 253 253
  94616. -253 253 253 253 253 253 250 250 250 221 221 221
  94617. -218 218 218 101 101 101 2 2 6 14 14 14
  94618. - 18 18 18 38 38 38 10 10 10 2 2 6
  94619. - 2 2 6 2 2 6 2 2 6 78 78 78
  94620. - 58 58 58 22 22 22 6 6 6 0 0 0
  94621. - 0 0 0 0 0 0 0 0 0 0 0 0
  94622. - 0 0 0 0 0 0 0 0 0 0 0 0
  94623. - 0 0 0 0 0 0 0 0 0 0 0 0
  94624. - 0 0 0 0 0 0 0 0 0 0 0 0
  94625. - 0 0 0 0 0 0 0 0 0 0 0 0
  94626. - 0 0 0 0 0 0 0 0 0 0 0 0
  94627. - 0 0 0 0 0 0 0 0 0 0 0 0
  94628. - 0 0 0 0 0 0 6 6 6 18 18 18
  94629. - 54 54 54 82 82 82 2 2 6 26 26 26
  94630. - 22 22 22 2 2 6 123 123 123 253 253 253
  94631. -253 253 253 253 253 253 253 253 253 253 253 253
  94632. -253 253 253 253 253 253 253 253 253 253 253 253
  94633. -253 253 253 253 253 253 253 253 253 253 253 253
  94634. -253 253 253 253 253 253 253 253 253 253 253 253
  94635. -253 253 253 253 253 253 253 253 253 253 253 253
  94636. -253 253 253 253 253 253 253 253 253 250 250 250
  94637. -238 238 238 198 198 198 6 6 6 38 38 38
  94638. - 58 58 58 26 26 26 38 38 38 2 2 6
  94639. - 2 2 6 2 2 6 2 2 6 46 46 46
  94640. - 78 78 78 30 30 30 10 10 10 0 0 0
  94641. - 0 0 0 0 0 0 0 0 0 0 0 0
  94642. - 0 0 0 0 0 0 0 0 0 0 0 0
  94643. - 0 0 0 0 0 0 0 0 0 0 0 0
  94644. - 0 0 0 0 0 0 0 0 0 0 0 0
  94645. - 0 0 0 0 0 0 0 0 0 0 0 0
  94646. - 0 0 0 0 0 0 0 0 0 0 0 0
  94647. - 0 0 0 0 0 0 0 0 0 0 0 0
  94648. - 0 0 0 0 0 0 10 10 10 30 30 30
  94649. - 74 74 74 58 58 58 2 2 6 42 42 42
  94650. - 2 2 6 22 22 22 231 231 231 253 253 253
  94651. -253 253 253 253 253 253 253 253 253 253 253 253
  94652. -253 253 253 253 253 253 253 253 253 250 250 250
  94653. -253 253 253 253 253 253 253 253 253 253 253 253
  94654. -253 253 253 253 253 253 253 253 253 253 253 253
  94655. -253 253 253 253 253 253 253 253 253 253 253 253
  94656. -253 253 253 253 253 253 253 253 253 253 253 253
  94657. -253 253 253 246 246 246 46 46 46 38 38 38
  94658. - 42 42 42 14 14 14 38 38 38 14 14 14
  94659. - 2 2 6 2 2 6 2 2 6 6 6 6
  94660. - 86 86 86 46 46 46 14 14 14 0 0 0
  94661. - 0 0 0 0 0 0 0 0 0 0 0 0
  94662. - 0 0 0 0 0 0 0 0 0 0 0 0
  94663. - 0 0 0 0 0 0 0 0 0 0 0 0
  94664. - 0 0 0 0 0 0 0 0 0 0 0 0
  94665. - 0 0 0 0 0 0 0 0 0 0 0 0
  94666. - 0 0 0 0 0 0 0 0 0 0 0 0
  94667. - 0 0 0 0 0 0 0 0 0 0 0 0
  94668. - 0 0 0 6 6 6 14 14 14 42 42 42
  94669. - 90 90 90 18 18 18 18 18 18 26 26 26
  94670. - 2 2 6 116 116 116 253 253 253 253 253 253
  94671. -253 253 253 253 253 253 253 253 253 253 253 253
  94672. -253 253 253 253 253 253 250 250 250 238 238 238
  94673. -253 253 253 253 253 253 253 253 253 253 253 253
  94674. -253 253 253 253 253 253 253 253 253 253 253 253
  94675. -253 253 253 253 253 253 253 253 253 253 253 253
  94676. -253 253 253 253 253 253 253 253 253 253 253 253
  94677. -253 253 253 253 253 253 94 94 94 6 6 6
  94678. - 2 2 6 2 2 6 10 10 10 34 34 34
  94679. - 2 2 6 2 2 6 2 2 6 2 2 6
  94680. - 74 74 74 58 58 58 22 22 22 6 6 6
  94681. - 0 0 0 0 0 0 0 0 0 0 0 0
  94682. - 0 0 0 0 0 0 0 0 0 0 0 0
  94683. - 0 0 0 0 0 0 0 0 0 0 0 0
  94684. - 0 0 0 0 0 0 0 0 0 0 0 0
  94685. - 0 0 0 0 0 0 0 0 0 0 0 0
  94686. - 0 0 0 0 0 0 0 0 0 0 0 0
  94687. - 0 0 0 0 0 0 0 0 0 0 0 0
  94688. - 0 0 0 10 10 10 26 26 26 66 66 66
  94689. - 82 82 82 2 2 6 38 38 38 6 6 6
  94690. - 14 14 14 210 210 210 253 253 253 253 253 253
  94691. -253 253 253 253 253 253 253 253 253 253 253 253
  94692. -253 253 253 253 253 253 246 246 246 242 242 242
  94693. -253 253 253 253 253 253 253 253 253 253 253 253
  94694. -253 253 253 253 253 253 253 253 253 253 253 253
  94695. -253 253 253 253 253 253 253 253 253 253 253 253
  94696. -253 253 253 253 253 253 253 253 253 253 253 253
  94697. -253 253 253 253 253 253 144 144 144 2 2 6
  94698. - 2 2 6 2 2 6 2 2 6 46 46 46
  94699. - 2 2 6 2 2 6 2 2 6 2 2 6
  94700. - 42 42 42 74 74 74 30 30 30 10 10 10
  94701. - 0 0 0 0 0 0 0 0 0 0 0 0
  94702. - 0 0 0 0 0 0 0 0 0 0 0 0
  94703. - 0 0 0 0 0 0 0 0 0 0 0 0
  94704. - 0 0 0 0 0 0 0 0 0 0 0 0
  94705. - 0 0 0 0 0 0 0 0 0 0 0 0
  94706. - 0 0 0 0 0 0 0 0 0 0 0 0
  94707. - 0 0 0 0 0 0 0 0 0 0 0 0
  94708. - 6 6 6 14 14 14 42 42 42 90 90 90
  94709. - 26 26 26 6 6 6 42 42 42 2 2 6
  94710. - 74 74 74 250 250 250 253 253 253 253 253 253
  94711. -253 253 253 253 253 253 253 253 253 253 253 253
  94712. -253 253 253 253 253 253 242 242 242 242 242 242
  94713. -253 253 253 253 253 253 253 253 253 253 253 253
  94714. -253 253 253 253 253 253 253 253 253 253 253 253
  94715. -253 253 253 253 253 253 253 253 253 253 253 253
  94716. -253 253 253 253 253 253 253 253 253 253 253 253
  94717. -253 253 253 253 253 253 182 182 182 2 2 6
  94718. - 2 2 6 2 2 6 2 2 6 46 46 46
  94719. - 2 2 6 2 2 6 2 2 6 2 2 6
  94720. - 10 10 10 86 86 86 38 38 38 10 10 10
  94721. - 0 0 0 0 0 0 0 0 0 0 0 0
  94722. - 0 0 0 0 0 0 0 0 0 0 0 0
  94723. - 0 0 0 0 0 0 0 0 0 0 0 0
  94724. - 0 0 0 0 0 0 0 0 0 0 0 0
  94725. - 0 0 0 0 0 0 0 0 0 0 0 0
  94726. - 0 0 0 0 0 0 0 0 0 0 0 0
  94727. - 0 0 0 0 0 0 0 0 0 0 0 0
  94728. - 10 10 10 26 26 26 66 66 66 82 82 82
  94729. - 2 2 6 22 22 22 18 18 18 2 2 6
  94730. -149 149 149 253 253 253 253 253 253 253 253 253
  94731. -253 253 253 253 253 253 253 253 253 253 253 253
  94732. -253 253 253 253 253 253 234 234 234 242 242 242
  94733. -253 253 253 253 253 253 253 253 253 253 253 253
  94734. -253 253 253 253 253 253 253 253 253 253 253 253
  94735. -253 253 253 253 253 253 253 253 253 253 253 253
  94736. -253 253 253 253 253 253 253 253 253 253 253 253
  94737. -253 253 253 253 253 253 206 206 206 2 2 6
  94738. - 2 2 6 2 2 6 2 2 6 38 38 38
  94739. - 2 2 6 2 2 6 2 2 6 2 2 6
  94740. - 6 6 6 86 86 86 46 46 46 14 14 14
  94741. - 0 0 0 0 0 0 0 0 0 0 0 0
  94742. - 0 0 0 0 0 0 0 0 0 0 0 0
  94743. - 0 0 0 0 0 0 0 0 0 0 0 0
  94744. - 0 0 0 0 0 0 0 0 0 0 0 0
  94745. - 0 0 0 0 0 0 0 0 0 0 0 0
  94746. - 0 0 0 0 0 0 0 0 0 0 0 0
  94747. - 0 0 0 0 0 0 0 0 0 6 6 6
  94748. - 18 18 18 46 46 46 86 86 86 18 18 18
  94749. - 2 2 6 34 34 34 10 10 10 6 6 6
  94750. -210 210 210 253 253 253 253 253 253 253 253 253
  94751. -253 253 253 253 253 253 253 253 253 253 253 253
  94752. -253 253 253 253 253 253 234 234 234 242 242 242
  94753. -253 253 253 253 253 253 253 253 253 253 253 253
  94754. -253 253 253 253 253 253 253 253 253 253 253 253
  94755. -253 253 253 253 253 253 253 253 253 253 253 253
  94756. -253 253 253 253 253 253 253 253 253 253 253 253
  94757. -253 253 253 253 253 253 221 221 221 6 6 6
  94758. - 2 2 6 2 2 6 6 6 6 30 30 30
  94759. - 2 2 6 2 2 6 2 2 6 2 2 6
  94760. - 2 2 6 82 82 82 54 54 54 18 18 18
  94761. - 6 6 6 0 0 0 0 0 0 0 0 0
  94762. - 0 0 0 0 0 0 0 0 0 0 0 0
  94763. - 0 0 0 0 0 0 0 0 0 0 0 0
  94764. - 0 0 0 0 0 0 0 0 0 0 0 0
  94765. - 0 0 0 0 0 0 0 0 0 0 0 0
  94766. - 0 0 0 0 0 0 0 0 0 0 0 0
  94767. - 0 0 0 0 0 0 0 0 0 10 10 10
  94768. - 26 26 26 66 66 66 62 62 62 2 2 6
  94769. - 2 2 6 38 38 38 10 10 10 26 26 26
  94770. -238 238 238 253 253 253 253 253 253 253 253 253
  94771. -253 253 253 253 253 253 253 253 253 253 253 253
  94772. -253 253 253 253 253 253 231 231 231 238 238 238
  94773. -253 253 253 253 253 253 253 253 253 253 253 253
  94774. -253 253 253 253 253 253 253 253 253 253 253 253
  94775. -253 253 253 253 253 253 253 253 253 253 253 253
  94776. -253 253 253 253 253 253 253 253 253 253 253 253
  94777. -253 253 253 253 253 253 231 231 231 6 6 6
  94778. - 2 2 6 2 2 6 10 10 10 30 30 30
  94779. - 2 2 6 2 2 6 2 2 6 2 2 6
  94780. - 2 2 6 66 66 66 58 58 58 22 22 22
  94781. - 6 6 6 0 0 0 0 0 0 0 0 0
  94782. - 0 0 0 0 0 0 0 0 0 0 0 0
  94783. - 0 0 0 0 0 0 0 0 0 0 0 0
  94784. - 0 0 0 0 0 0 0 0 0 0 0 0
  94785. - 0 0 0 0 0 0 0 0 0 0 0 0
  94786. - 0 0 0 0 0 0 0 0 0 0 0 0
  94787. - 0 0 0 0 0 0 0 0 0 10 10 10
  94788. - 38 38 38 78 78 78 6 6 6 2 2 6
  94789. - 2 2 6 46 46 46 14 14 14 42 42 42
  94790. -246 246 246 253 253 253 253 253 253 253 253 253
  94791. -253 253 253 253 253 253 253 253 253 253 253 253
  94792. -253 253 253 253 253 253 231 231 231 242 242 242
  94793. -253 253 253 253 253 253 253 253 253 253 253 253
  94794. -253 253 253 253 253 253 253 253 253 253 253 253
  94795. -253 253 253 253 253 253 253 253 253 253 253 253
  94796. -253 253 253 253 253 253 253 253 253 253 253 253
  94797. -253 253 253 253 253 253 234 234 234 10 10 10
  94798. - 2 2 6 2 2 6 22 22 22 14 14 14
  94799. - 2 2 6 2 2 6 2 2 6 2 2 6
  94800. - 2 2 6 66 66 66 62 62 62 22 22 22
  94801. - 6 6 6 0 0 0 0 0 0 0 0 0
  94802. - 0 0 0 0 0 0 0 0 0 0 0 0
  94803. - 0 0 0 0 0 0 0 0 0 0 0 0
  94804. - 0 0 0 0 0 0 0 0 0 0 0 0
  94805. - 0 0 0 0 0 0 0 0 0 0 0 0
  94806. - 0 0 0 0 0 0 0 0 0 0 0 0
  94807. - 0 0 0 0 0 0 6 6 6 18 18 18
  94808. - 50 50 50 74 74 74 2 2 6 2 2 6
  94809. - 14 14 14 70 70 70 34 34 34 62 62 62
  94810. -250 250 250 253 253 253 253 253 253 253 253 253
  94811. -253 253 253 253 253 253 253 253 253 253 253 253
  94812. -253 253 253 253 253 253 231 231 231 246 246 246
  94813. -253 253 253 253 253 253 253 253 253 253 253 253
  94814. -253 253 253 253 253 253 253 253 253 253 253 253
  94815. -253 253 253 253 253 253 253 253 253 253 253 253
  94816. -253 253 253 253 253 253 253 253 253 253 253 253
  94817. -253 253 253 253 253 253 234 234 234 14 14 14
  94818. - 2 2 6 2 2 6 30 30 30 2 2 6
  94819. - 2 2 6 2 2 6 2 2 6 2 2 6
  94820. - 2 2 6 66 66 66 62 62 62 22 22 22
  94821. - 6 6 6 0 0 0 0 0 0 0 0 0
  94822. - 0 0 0 0 0 0 0 0 0 0 0 0
  94823. - 0 0 0 0 0 0 0 0 0 0 0 0
  94824. - 0 0 0 0 0 0 0 0 0 0 0 0
  94825. - 0 0 0 0 0 0 0 0 0 0 0 0
  94826. - 0 0 0 0 0 0 0 0 0 0 0 0
  94827. - 0 0 0 0 0 0 6 6 6 18 18 18
  94828. - 54 54 54 62 62 62 2 2 6 2 2 6
  94829. - 2 2 6 30 30 30 46 46 46 70 70 70
  94830. -250 250 250 253 253 253 253 253 253 253 253 253
  94831. -253 253 253 253 253 253 253 253 253 253 253 253
  94832. -253 253 253 253 253 253 231 231 231 246 246 246
  94833. -253 253 253 253 253 253 253 253 253 253 253 253
  94834. -253 253 253 253 253 253 253 253 253 253 253 253
  94835. -253 253 253 253 253 253 253 253 253 253 253 253
  94836. -253 253 253 253 253 253 253 253 253 253 253 253
  94837. -253 253 253 253 253 253 226 226 226 10 10 10
  94838. - 2 2 6 6 6 6 30 30 30 2 2 6
  94839. - 2 2 6 2 2 6 2 2 6 2 2 6
  94840. - 2 2 6 66 66 66 58 58 58 22 22 22
  94841. - 6 6 6 0 0 0 0 0 0 0 0 0
  94842. - 0 0 0 0 0 0 0 0 0 0 0 0
  94843. - 0 0 0 0 0 0 0 0 0 0 0 0
  94844. - 0 0 0 0 0 0 0 0 0 0 0 0
  94845. - 0 0 0 0 0 0 0 0 0 0 0 0
  94846. - 0 0 0 0 0 0 0 0 0 0 0 0
  94847. - 0 0 0 0 0 0 6 6 6 22 22 22
  94848. - 58 58 58 62 62 62 2 2 6 2 2 6
  94849. - 2 2 6 2 2 6 30 30 30 78 78 78
  94850. -250 250 250 253 253 253 253 253 253 253 253 253
  94851. -253 253 253 253 253 253 253 253 253 253 253 253
  94852. -253 253 253 253 253 253 231 231 231 246 246 246
  94853. -253 253 253 253 253 253 253 253 253 253 253 253
  94854. -253 253 253 253 253 253 253 253 253 253 253 253
  94855. -253 253 253 253 253 253 253 253 253 253 253 253
  94856. -253 253 253 253 253 253 253 253 253 253 253 253
  94857. -253 253 253 253 253 253 206 206 206 2 2 6
  94858. - 22 22 22 34 34 34 18 14 6 22 22 22
  94859. - 26 26 26 18 18 18 6 6 6 2 2 6
  94860. - 2 2 6 82 82 82 54 54 54 18 18 18
  94861. - 6 6 6 0 0 0 0 0 0 0 0 0
  94862. - 0 0 0 0 0 0 0 0 0 0 0 0
  94863. - 0 0 0 0 0 0 0 0 0 0 0 0
  94864. - 0 0 0 0 0 0 0 0 0 0 0 0
  94865. - 0 0 0 0 0 0 0 0 0 0 0 0
  94866. - 0 0 0 0 0 0 0 0 0 0 0 0
  94867. - 0 0 0 0 0 0 6 6 6 26 26 26
  94868. - 62 62 62 106 106 106 74 54 14 185 133 11
  94869. -210 162 10 121 92 8 6 6 6 62 62 62
  94870. -238 238 238 253 253 253 253 253 253 253 253 253
  94871. -253 253 253 253 253 253 253 253 253 253 253 253
  94872. -253 253 253 253 253 253 231 231 231 246 246 246
  94873. -253 253 253 253 253 253 253 253 253 253 253 253
  94874. -253 253 253 253 253 253 253 253 253 253 253 253
  94875. -253 253 253 253 253 253 253 253 253 253 253 253
  94876. -253 253 253 253 253 253 253 253 253 253 253 253
  94877. -253 253 253 253 253 253 158 158 158 18 18 18
  94878. - 14 14 14 2 2 6 2 2 6 2 2 6
  94879. - 6 6 6 18 18 18 66 66 66 38 38 38
  94880. - 6 6 6 94 94 94 50 50 50 18 18 18
  94881. - 6 6 6 0 0 0 0 0 0 0 0 0
  94882. - 0 0 0 0 0 0 0 0 0 0 0 0
  94883. - 0 0 0 0 0 0 0 0 0 0 0 0
  94884. - 0 0 0 0 0 0 0 0 0 0 0 0
  94885. - 0 0 0 0 0 0 0 0 0 0 0 0
  94886. - 0 0 0 0 0 0 0 0 0 6 6 6
  94887. - 10 10 10 10 10 10 18 18 18 38 38 38
  94888. - 78 78 78 142 134 106 216 158 10 242 186 14
  94889. -246 190 14 246 190 14 156 118 10 10 10 10
  94890. - 90 90 90 238 238 238 253 253 253 253 253 253
  94891. -253 253 253 253 253 253 253 253 253 253 253 253
  94892. -253 253 253 253 253 253 231 231 231 250 250 250
  94893. -253 253 253 253 253 253 253 253 253 253 253 253
  94894. -253 253 253 253 253 253 253 253 253 253 253 253
  94895. -253 253 253 253 253 253 253 253 253 253 253 253
  94896. -253 253 253 253 253 253 253 253 253 246 230 190
  94897. -238 204 91 238 204 91 181 142 44 37 26 9
  94898. - 2 2 6 2 2 6 2 2 6 2 2 6
  94899. - 2 2 6 2 2 6 38 38 38 46 46 46
  94900. - 26 26 26 106 106 106 54 54 54 18 18 18
  94901. - 6 6 6 0 0 0 0 0 0 0 0 0
  94902. - 0 0 0 0 0 0 0 0 0 0 0 0
  94903. - 0 0 0 0 0 0 0 0 0 0 0 0
  94904. - 0 0 0 0 0 0 0 0 0 0 0 0
  94905. - 0 0 0 0 0 0 0 0 0 0 0 0
  94906. - 0 0 0 6 6 6 14 14 14 22 22 22
  94907. - 30 30 30 38 38 38 50 50 50 70 70 70
  94908. -106 106 106 190 142 34 226 170 11 242 186 14
  94909. -246 190 14 246 190 14 246 190 14 154 114 10
  94910. - 6 6 6 74 74 74 226 226 226 253 253 253
  94911. -253 253 253 253 253 253 253 253 253 253 253 253
  94912. -253 253 253 253 253 253 231 231 231 250 250 250
  94913. -253 253 253 253 253 253 253 253 253 253 253 253
  94914. -253 253 253 253 253 253 253 253 253 253 253 253
  94915. -253 253 253 253 253 253 253 253 253 253 253 253
  94916. -253 253 253 253 253 253 253 253 253 228 184 62
  94917. -241 196 14 241 208 19 232 195 16 38 30 10
  94918. - 2 2 6 2 2 6 2 2 6 2 2 6
  94919. - 2 2 6 6 6 6 30 30 30 26 26 26
  94920. -203 166 17 154 142 90 66 66 66 26 26 26
  94921. - 6 6 6 0 0 0 0 0 0 0 0 0
  94922. - 0 0 0 0 0 0 0 0 0 0 0 0
  94923. - 0 0 0 0 0 0 0 0 0 0 0 0
  94924. - 0 0 0 0 0 0 0 0 0 0 0 0
  94925. - 0 0 0 0 0 0 0 0 0 0 0 0
  94926. - 6 6 6 18 18 18 38 38 38 58 58 58
  94927. - 78 78 78 86 86 86 101 101 101 123 123 123
  94928. -175 146 61 210 150 10 234 174 13 246 186 14
  94929. -246 190 14 246 190 14 246 190 14 238 190 10
  94930. -102 78 10 2 2 6 46 46 46 198 198 198
  94931. -253 253 253 253 253 253 253 253 253 253 253 253
  94932. -253 253 253 253 253 253 234 234 234 242 242 242
  94933. -253 253 253 253 253 253 253 253 253 253 253 253
  94934. -253 253 253 253 253 253 253 253 253 253 253 253
  94935. -253 253 253 253 253 253 253 253 253 253 253 253
  94936. -253 253 253 253 253 253 253 253 253 224 178 62
  94937. -242 186 14 241 196 14 210 166 10 22 18 6
  94938. - 2 2 6 2 2 6 2 2 6 2 2 6
  94939. - 2 2 6 2 2 6 6 6 6 121 92 8
  94940. -238 202 15 232 195 16 82 82 82 34 34 34
  94941. - 10 10 10 0 0 0 0 0 0 0 0 0
  94942. - 0 0 0 0 0 0 0 0 0 0 0 0
  94943. - 0 0 0 0 0 0 0 0 0 0 0 0
  94944. - 0 0 0 0 0 0 0 0 0 0 0 0
  94945. - 0 0 0 0 0 0 0 0 0 0 0 0
  94946. - 14 14 14 38 38 38 70 70 70 154 122 46
  94947. -190 142 34 200 144 11 197 138 11 197 138 11
  94948. -213 154 11 226 170 11 242 186 14 246 190 14
  94949. -246 190 14 246 190 14 246 190 14 246 190 14
  94950. -225 175 15 46 32 6 2 2 6 22 22 22
  94951. -158 158 158 250 250 250 253 253 253 253 253 253
  94952. -253 253 253 253 253 253 253 253 253 253 253 253
  94953. -253 253 253 253 253 253 253 253 253 253 253 253
  94954. -253 253 253 253 253 253 253 253 253 253 253 253
  94955. -253 253 253 253 253 253 253 253 253 253 253 253
  94956. -253 253 253 250 250 250 242 242 242 224 178 62
  94957. -239 182 13 236 186 11 213 154 11 46 32 6
  94958. - 2 2 6 2 2 6 2 2 6 2 2 6
  94959. - 2 2 6 2 2 6 61 42 6 225 175 15
  94960. -238 190 10 236 186 11 112 100 78 42 42 42
  94961. - 14 14 14 0 0 0 0 0 0 0 0 0
  94962. - 0 0 0 0 0 0 0 0 0 0 0 0
  94963. - 0 0 0 0 0 0 0 0 0 0 0 0
  94964. - 0 0 0 0 0 0 0 0 0 0 0 0
  94965. - 0 0 0 0 0 0 0 0 0 6 6 6
  94966. - 22 22 22 54 54 54 154 122 46 213 154 11
  94967. -226 170 11 230 174 11 226 170 11 226 170 11
  94968. -236 178 12 242 186 14 246 190 14 246 190 14
  94969. -246 190 14 246 190 14 246 190 14 246 190 14
  94970. -241 196 14 184 144 12 10 10 10 2 2 6
  94971. - 6 6 6 116 116 116 242 242 242 253 253 253
  94972. -253 253 253 253 253 253 253 253 253 253 253 253
  94973. -253 253 253 253 253 253 253 253 253 253 253 253
  94974. -253 253 253 253 253 253 253 253 253 253 253 253
  94975. -253 253 253 253 253 253 253 253 253 253 253 253
  94976. -253 253 253 231 231 231 198 198 198 214 170 54
  94977. -236 178 12 236 178 12 210 150 10 137 92 6
  94978. - 18 14 6 2 2 6 2 2 6 2 2 6
  94979. - 6 6 6 70 47 6 200 144 11 236 178 12
  94980. -239 182 13 239 182 13 124 112 88 58 58 58
  94981. - 22 22 22 6 6 6 0 0 0 0 0 0
  94982. - 0 0 0 0 0 0 0 0 0 0 0 0
  94983. - 0 0 0 0 0 0 0 0 0 0 0 0
  94984. - 0 0 0 0 0 0 0 0 0 0 0 0
  94985. - 0 0 0 0 0 0 0 0 0 10 10 10
  94986. - 30 30 30 70 70 70 180 133 36 226 170 11
  94987. -239 182 13 242 186 14 242 186 14 246 186 14
  94988. -246 190 14 246 190 14 246 190 14 246 190 14
  94989. -246 190 14 246 190 14 246 190 14 246 190 14
  94990. -246 190 14 232 195 16 98 70 6 2 2 6
  94991. - 2 2 6 2 2 6 66 66 66 221 221 221
  94992. -253 253 253 253 253 253 253 253 253 253 253 253
  94993. -253 253 253 253 253 253 253 253 253 253 253 253
  94994. -253 253 253 253 253 253 253 253 253 253 253 253
  94995. -253 253 253 253 253 253 253 253 253 253 253 253
  94996. -253 253 253 206 206 206 198 198 198 214 166 58
  94997. -230 174 11 230 174 11 216 158 10 192 133 9
  94998. -163 110 8 116 81 8 102 78 10 116 81 8
  94999. -167 114 7 197 138 11 226 170 11 239 182 13
  95000. -242 186 14 242 186 14 162 146 94 78 78 78
  95001. - 34 34 34 14 14 14 6 6 6 0 0 0
  95002. - 0 0 0 0 0 0 0 0 0 0 0 0
  95003. - 0 0 0 0 0 0 0 0 0 0 0 0
  95004. - 0 0 0 0 0 0 0 0 0 0 0 0
  95005. - 0 0 0 0 0 0 0 0 0 6 6 6
  95006. - 30 30 30 78 78 78 190 142 34 226 170 11
  95007. -239 182 13 246 190 14 246 190 14 246 190 14
  95008. -246 190 14 246 190 14 246 190 14 246 190 14
  95009. -246 190 14 246 190 14 246 190 14 246 190 14
  95010. -246 190 14 241 196 14 203 166 17 22 18 6
  95011. - 2 2 6 2 2 6 2 2 6 38 38 38
  95012. -218 218 218 253 253 253 253 253 253 253 253 253
  95013. -253 253 253 253 253 253 253 253 253 253 253 253
  95014. -253 253 253 253 253 253 253 253 253 253 253 253
  95015. -253 253 253 253 253 253 253 253 253 253 253 253
  95016. -250 250 250 206 206 206 198 198 198 202 162 69
  95017. -226 170 11 236 178 12 224 166 10 210 150 10
  95018. -200 144 11 197 138 11 192 133 9 197 138 11
  95019. -210 150 10 226 170 11 242 186 14 246 190 14
  95020. -246 190 14 246 186 14 225 175 15 124 112 88
  95021. - 62 62 62 30 30 30 14 14 14 6 6 6
  95022. - 0 0 0 0 0 0 0 0 0 0 0 0
  95023. - 0 0 0 0 0 0 0 0 0 0 0 0
  95024. - 0 0 0 0 0 0 0 0 0 0 0 0
  95025. - 0 0 0 0 0 0 0 0 0 10 10 10
  95026. - 30 30 30 78 78 78 174 135 50 224 166 10
  95027. -239 182 13 246 190 14 246 190 14 246 190 14
  95028. -246 190 14 246 190 14 246 190 14 246 190 14
  95029. -246 190 14 246 190 14 246 190 14 246 190 14
  95030. -246 190 14 246 190 14 241 196 14 139 102 15
  95031. - 2 2 6 2 2 6 2 2 6 2 2 6
  95032. - 78 78 78 250 250 250 253 253 253 253 253 253
  95033. -253 253 253 253 253 253 253 253 253 253 253 253
  95034. -253 253 253 253 253 253 253 253 253 253 253 253
  95035. -253 253 253 253 253 253 253 253 253 253 253 253
  95036. -250 250 250 214 214 214 198 198 198 190 150 46
  95037. -219 162 10 236 178 12 234 174 13 224 166 10
  95038. -216 158 10 213 154 11 213 154 11 216 158 10
  95039. -226 170 11 239 182 13 246 190 14 246 190 14
  95040. -246 190 14 246 190 14 242 186 14 206 162 42
  95041. -101 101 101 58 58 58 30 30 30 14 14 14
  95042. - 6 6 6 0 0 0 0 0 0 0 0 0
  95043. - 0 0 0 0 0 0 0 0 0 0 0 0
  95044. - 0 0 0 0 0 0 0 0 0 0 0 0
  95045. - 0 0 0 0 0 0 0 0 0 10 10 10
  95046. - 30 30 30 74 74 74 174 135 50 216 158 10
  95047. -236 178 12 246 190 14 246 190 14 246 190 14
  95048. -246 190 14 246 190 14 246 190 14 246 190 14
  95049. -246 190 14 246 190 14 246 190 14 246 190 14
  95050. -246 190 14 246 190 14 241 196 14 226 184 13
  95051. - 61 42 6 2 2 6 2 2 6 2 2 6
  95052. - 22 22 22 238 238 238 253 253 253 253 253 253
  95053. -253 253 253 253 253 253 253 253 253 253 253 253
  95054. -253 253 253 253 253 253 253 253 253 253 253 253
  95055. -253 253 253 253 253 253 253 253 253 253 253 253
  95056. -253 253 253 226 226 226 187 187 187 180 133 36
  95057. -216 158 10 236 178 12 239 182 13 236 178 12
  95058. -230 174 11 226 170 11 226 170 11 230 174 11
  95059. -236 178 12 242 186 14 246 190 14 246 190 14
  95060. -246 190 14 246 190 14 246 186 14 239 182 13
  95061. -206 162 42 106 106 106 66 66 66 34 34 34
  95062. - 14 14 14 6 6 6 0 0 0 0 0 0
  95063. - 0 0 0 0 0 0 0 0 0 0 0 0
  95064. - 0 0 0 0 0 0 0 0 0 0 0 0
  95065. - 0 0 0 0 0 0 0 0 0 6 6 6
  95066. - 26 26 26 70 70 70 163 133 67 213 154 11
  95067. -236 178 12 246 190 14 246 190 14 246 190 14
  95068. -246 190 14 246 190 14 246 190 14 246 190 14
  95069. -246 190 14 246 190 14 246 190 14 246 190 14
  95070. -246 190 14 246 190 14 246 190 14 241 196 14
  95071. -190 146 13 18 14 6 2 2 6 2 2 6
  95072. - 46 46 46 246 246 246 253 253 253 253 253 253
  95073. -253 253 253 253 253 253 253 253 253 253 253 253
  95074. -253 253 253 253 253 253 253 253 253 253 253 253
  95075. -253 253 253 253 253 253 253 253 253 253 253 253
  95076. -253 253 253 221 221 221 86 86 86 156 107 11
  95077. -216 158 10 236 178 12 242 186 14 246 186 14
  95078. -242 186 14 239 182 13 239 182 13 242 186 14
  95079. -242 186 14 246 186 14 246 190 14 246 190 14
  95080. -246 190 14 246 190 14 246 190 14 246 190 14
  95081. -242 186 14 225 175 15 142 122 72 66 66 66
  95082. - 30 30 30 10 10 10 0 0 0 0 0 0
  95083. - 0 0 0 0 0 0 0 0 0 0 0 0
  95084. - 0 0 0 0 0 0 0 0 0 0 0 0
  95085. - 0 0 0 0 0 0 0 0 0 6 6 6
  95086. - 26 26 26 70 70 70 163 133 67 210 150 10
  95087. -236 178 12 246 190 14 246 190 14 246 190 14
  95088. -246 190 14 246 190 14 246 190 14 246 190 14
  95089. -246 190 14 246 190 14 246 190 14 246 190 14
  95090. -246 190 14 246 190 14 246 190 14 246 190 14
  95091. -232 195 16 121 92 8 34 34 34 106 106 106
  95092. -221 221 221 253 253 253 253 253 253 253 253 253
  95093. -253 253 253 253 253 253 253 253 253 253 253 253
  95094. -253 253 253 253 253 253 253 253 253 253 253 253
  95095. -253 253 253 253 253 253 253 253 253 253 253 253
  95096. -242 242 242 82 82 82 18 14 6 163 110 8
  95097. -216 158 10 236 178 12 242 186 14 246 190 14
  95098. -246 190 14 246 190 14 246 190 14 246 190 14
  95099. -246 190 14 246 190 14 246 190 14 246 190 14
  95100. -246 190 14 246 190 14 246 190 14 246 190 14
  95101. -246 190 14 246 190 14 242 186 14 163 133 67
  95102. - 46 46 46 18 18 18 6 6 6 0 0 0
  95103. - 0 0 0 0 0 0 0 0 0 0 0 0
  95104. - 0 0 0 0 0 0 0 0 0 0 0 0
  95105. - 0 0 0 0 0 0 0 0 0 10 10 10
  95106. - 30 30 30 78 78 78 163 133 67 210 150 10
  95107. -236 178 12 246 186 14 246 190 14 246 190 14
  95108. -246 190 14 246 190 14 246 190 14 246 190 14
  95109. -246 190 14 246 190 14 246 190 14 246 190 14
  95110. -246 190 14 246 190 14 246 190 14 246 190 14
  95111. -241 196 14 215 174 15 190 178 144 253 253 253
  95112. -253 253 253 253 253 253 253 253 253 253 253 253
  95113. -253 253 253 253 253 253 253 253 253 253 253 253
  95114. -253 253 253 253 253 253 253 253 253 253 253 253
  95115. -253 253 253 253 253 253 253 253 253 218 218 218
  95116. - 58 58 58 2 2 6 22 18 6 167 114 7
  95117. -216 158 10 236 178 12 246 186 14 246 190 14
  95118. -246 190 14 246 190 14 246 190 14 246 190 14
  95119. -246 190 14 246 190 14 246 190 14 246 190 14
  95120. -246 190 14 246 190 14 246 190 14 246 190 14
  95121. -246 190 14 246 186 14 242 186 14 190 150 46
  95122. - 54 54 54 22 22 22 6 6 6 0 0 0
  95123. - 0 0 0 0 0 0 0 0 0 0 0 0
  95124. - 0 0 0 0 0 0 0 0 0 0 0 0
  95125. - 0 0 0 0 0 0 0 0 0 14 14 14
  95126. - 38 38 38 86 86 86 180 133 36 213 154 11
  95127. -236 178 12 246 186 14 246 190 14 246 190 14
  95128. -246 190 14 246 190 14 246 190 14 246 190 14
  95129. -246 190 14 246 190 14 246 190 14 246 190 14
  95130. -246 190 14 246 190 14 246 190 14 246 190 14
  95131. -246 190 14 232 195 16 190 146 13 214 214 214
  95132. -253 253 253 253 253 253 253 253 253 253 253 253
  95133. -253 253 253 253 253 253 253 253 253 253 253 253
  95134. -253 253 253 253 253 253 253 253 253 253 253 253
  95135. -253 253 253 250 250 250 170 170 170 26 26 26
  95136. - 2 2 6 2 2 6 37 26 9 163 110 8
  95137. -219 162 10 239 182 13 246 186 14 246 190 14
  95138. -246 190 14 246 190 14 246 190 14 246 190 14
  95139. -246 190 14 246 190 14 246 190 14 246 190 14
  95140. -246 190 14 246 190 14 246 190 14 246 190 14
  95141. -246 186 14 236 178 12 224 166 10 142 122 72
  95142. - 46 46 46 18 18 18 6 6 6 0 0 0
  95143. - 0 0 0 0 0 0 0 0 0 0 0 0
  95144. - 0 0 0 0 0 0 0 0 0 0 0 0
  95145. - 0 0 0 0 0 0 6 6 6 18 18 18
  95146. - 50 50 50 109 106 95 192 133 9 224 166 10
  95147. -242 186 14 246 190 14 246 190 14 246 190 14
  95148. -246 190 14 246 190 14 246 190 14 246 190 14
  95149. -246 190 14 246 190 14 246 190 14 246 190 14
  95150. -246 190 14 246 190 14 246 190 14 246 190 14
  95151. -242 186 14 226 184 13 210 162 10 142 110 46
  95152. -226 226 226 253 253 253 253 253 253 253 253 253
  95153. -253 253 253 253 253 253 253 253 253 253 253 253
  95154. -253 253 253 253 253 253 253 253 253 253 253 253
  95155. -198 198 198 66 66 66 2 2 6 2 2 6
  95156. - 2 2 6 2 2 6 50 34 6 156 107 11
  95157. -219 162 10 239 182 13 246 186 14 246 190 14
  95158. -246 190 14 246 190 14 246 190 14 246 190 14
  95159. -246 190 14 246 190 14 246 190 14 246 190 14
  95160. -246 190 14 246 190 14 246 190 14 242 186 14
  95161. -234 174 13 213 154 11 154 122 46 66 66 66
  95162. - 30 30 30 10 10 10 0 0 0 0 0 0
  95163. - 0 0 0 0 0 0 0 0 0 0 0 0
  95164. - 0 0 0 0 0 0 0 0 0 0 0 0
  95165. - 0 0 0 0 0 0 6 6 6 22 22 22
  95166. - 58 58 58 154 121 60 206 145 10 234 174 13
  95167. -242 186 14 246 186 14 246 190 14 246 190 14
  95168. -246 190 14 246 190 14 246 190 14 246 190 14
  95169. -246 190 14 246 190 14 246 190 14 246 190 14
  95170. -246 190 14 246 190 14 246 190 14 246 190 14
  95171. -246 186 14 236 178 12 210 162 10 163 110 8
  95172. - 61 42 6 138 138 138 218 218 218 250 250 250
  95173. -253 253 253 253 253 253 253 253 253 250 250 250
  95174. -242 242 242 210 210 210 144 144 144 66 66 66
  95175. - 6 6 6 2 2 6 2 2 6 2 2 6
  95176. - 2 2 6 2 2 6 61 42 6 163 110 8
  95177. -216 158 10 236 178 12 246 190 14 246 190 14
  95178. -246 190 14 246 190 14 246 190 14 246 190 14
  95179. -246 190 14 246 190 14 246 190 14 246 190 14
  95180. -246 190 14 239 182 13 230 174 11 216 158 10
  95181. -190 142 34 124 112 88 70 70 70 38 38 38
  95182. - 18 18 18 6 6 6 0 0 0 0 0 0
  95183. - 0 0 0 0 0 0 0 0 0 0 0 0
  95184. - 0 0 0 0 0 0 0 0 0 0 0 0
  95185. - 0 0 0 0 0 0 6 6 6 22 22 22
  95186. - 62 62 62 168 124 44 206 145 10 224 166 10
  95187. -236 178 12 239 182 13 242 186 14 242 186 14
  95188. -246 186 14 246 190 14 246 190 14 246 190 14
  95189. -246 190 14 246 190 14 246 190 14 246 190 14
  95190. -246 190 14 246 190 14 246 190 14 246 190 14
  95191. -246 190 14 236 178 12 216 158 10 175 118 6
  95192. - 80 54 7 2 2 6 6 6 6 30 30 30
  95193. - 54 54 54 62 62 62 50 50 50 38 38 38
  95194. - 14 14 14 2 2 6 2 2 6 2 2 6
  95195. - 2 2 6 2 2 6 2 2 6 2 2 6
  95196. - 2 2 6 6 6 6 80 54 7 167 114 7
  95197. -213 154 11 236 178 12 246 190 14 246 190 14
  95198. -246 190 14 246 190 14 246 190 14 246 190 14
  95199. -246 190 14 242 186 14 239 182 13 239 182 13
  95200. -230 174 11 210 150 10 174 135 50 124 112 88
  95201. - 82 82 82 54 54 54 34 34 34 18 18 18
  95202. - 6 6 6 0 0 0 0 0 0 0 0 0
  95203. - 0 0 0 0 0 0 0 0 0 0 0 0
  95204. - 0 0 0 0 0 0 0 0 0 0 0 0
  95205. - 0 0 0 0 0 0 6 6 6 18 18 18
  95206. - 50 50 50 158 118 36 192 133 9 200 144 11
  95207. -216 158 10 219 162 10 224 166 10 226 170 11
  95208. -230 174 11 236 178 12 239 182 13 239 182 13
  95209. -242 186 14 246 186 14 246 190 14 246 190 14
  95210. -246 190 14 246 190 14 246 190 14 246 190 14
  95211. -246 186 14 230 174 11 210 150 10 163 110 8
  95212. -104 69 6 10 10 10 2 2 6 2 2 6
  95213. - 2 2 6 2 2 6 2 2 6 2 2 6
  95214. - 2 2 6 2 2 6 2 2 6 2 2 6
  95215. - 2 2 6 2 2 6 2 2 6 2 2 6
  95216. - 2 2 6 6 6 6 91 60 6 167 114 7
  95217. -206 145 10 230 174 11 242 186 14 246 190 14
  95218. -246 190 14 246 190 14 246 186 14 242 186 14
  95219. -239 182 13 230 174 11 224 166 10 213 154 11
  95220. -180 133 36 124 112 88 86 86 86 58 58 58
  95221. - 38 38 38 22 22 22 10 10 10 6 6 6
  95222. - 0 0 0 0 0 0 0 0 0 0 0 0
  95223. - 0 0 0 0 0 0 0 0 0 0 0 0
  95224. - 0 0 0 0 0 0 0 0 0 0 0 0
  95225. - 0 0 0 0 0 0 0 0 0 14 14 14
  95226. - 34 34 34 70 70 70 138 110 50 158 118 36
  95227. -167 114 7 180 123 7 192 133 9 197 138 11
  95228. -200 144 11 206 145 10 213 154 11 219 162 10
  95229. -224 166 10 230 174 11 239 182 13 242 186 14
  95230. -246 186 14 246 186 14 246 186 14 246 186 14
  95231. -239 182 13 216 158 10 185 133 11 152 99 6
  95232. -104 69 6 18 14 6 2 2 6 2 2 6
  95233. - 2 2 6 2 2 6 2 2 6 2 2 6
  95234. - 2 2 6 2 2 6 2 2 6 2 2 6
  95235. - 2 2 6 2 2 6 2 2 6 2 2 6
  95236. - 2 2 6 6 6 6 80 54 7 152 99 6
  95237. -192 133 9 219 162 10 236 178 12 239 182 13
  95238. -246 186 14 242 186 14 239 182 13 236 178 12
  95239. -224 166 10 206 145 10 192 133 9 154 121 60
  95240. - 94 94 94 62 62 62 42 42 42 22 22 22
  95241. - 14 14 14 6 6 6 0 0 0 0 0 0
  95242. - 0 0 0 0 0 0 0 0 0 0 0 0
  95243. - 0 0 0 0 0 0 0 0 0 0 0 0
  95244. - 0 0 0 0 0 0 0 0 0 0 0 0
  95245. - 0 0 0 0 0 0 0 0 0 6 6 6
  95246. - 18 18 18 34 34 34 58 58 58 78 78 78
  95247. -101 98 89 124 112 88 142 110 46 156 107 11
  95248. -163 110 8 167 114 7 175 118 6 180 123 7
  95249. -185 133 11 197 138 11 210 150 10 219 162 10
  95250. -226 170 11 236 178 12 236 178 12 234 174 13
  95251. -219 162 10 197 138 11 163 110 8 130 83 6
  95252. - 91 60 6 10 10 10 2 2 6 2 2 6
  95253. - 18 18 18 38 38 38 38 38 38 38 38 38
  95254. - 38 38 38 38 38 38 38 38 38 38 38 38
  95255. - 38 38 38 38 38 38 26 26 26 2 2 6
  95256. - 2 2 6 6 6 6 70 47 6 137 92 6
  95257. -175 118 6 200 144 11 219 162 10 230 174 11
  95258. -234 174 13 230 174 11 219 162 10 210 150 10
  95259. -192 133 9 163 110 8 124 112 88 82 82 82
  95260. - 50 50 50 30 30 30 14 14 14 6 6 6
  95261. - 0 0 0 0 0 0 0 0 0 0 0 0
  95262. - 0 0 0 0 0 0 0 0 0 0 0 0
  95263. - 0 0 0 0 0 0 0 0 0 0 0 0
  95264. - 0 0 0 0 0 0 0 0 0 0 0 0
  95265. - 0 0 0 0 0 0 0 0 0 0 0 0
  95266. - 6 6 6 14 14 14 22 22 22 34 34 34
  95267. - 42 42 42 58 58 58 74 74 74 86 86 86
  95268. -101 98 89 122 102 70 130 98 46 121 87 25
  95269. -137 92 6 152 99 6 163 110 8 180 123 7
  95270. -185 133 11 197 138 11 206 145 10 200 144 11
  95271. -180 123 7 156 107 11 130 83 6 104 69 6
  95272. - 50 34 6 54 54 54 110 110 110 101 98 89
  95273. - 86 86 86 82 82 82 78 78 78 78 78 78
  95274. - 78 78 78 78 78 78 78 78 78 78 78 78
  95275. - 78 78 78 82 82 82 86 86 86 94 94 94
  95276. -106 106 106 101 101 101 86 66 34 124 80 6
  95277. -156 107 11 180 123 7 192 133 9 200 144 11
  95278. -206 145 10 200 144 11 192 133 9 175 118 6
  95279. -139 102 15 109 106 95 70 70 70 42 42 42
  95280. - 22 22 22 10 10 10 0 0 0 0 0 0
  95281. - 0 0 0 0 0 0 0 0 0 0 0 0
  95282. - 0 0 0 0 0 0 0 0 0 0 0 0
  95283. - 0 0 0 0 0 0 0 0 0 0 0 0
  95284. - 0 0 0 0 0 0 0 0 0 0 0 0
  95285. - 0 0 0 0 0 0 0 0 0 0 0 0
  95286. - 0 0 0 0 0 0 6 6 6 10 10 10
  95287. - 14 14 14 22 22 22 30 30 30 38 38 38
  95288. - 50 50 50 62 62 62 74 74 74 90 90 90
  95289. -101 98 89 112 100 78 121 87 25 124 80 6
  95290. -137 92 6 152 99 6 152 99 6 152 99 6
  95291. -138 86 6 124 80 6 98 70 6 86 66 30
  95292. -101 98 89 82 82 82 58 58 58 46 46 46
  95293. - 38 38 38 34 34 34 34 34 34 34 34 34
  95294. - 34 34 34 34 34 34 34 34 34 34 34 34
  95295. - 34 34 34 34 34 34 38 38 38 42 42 42
  95296. - 54 54 54 82 82 82 94 86 76 91 60 6
  95297. -134 86 6 156 107 11 167 114 7 175 118 6
  95298. -175 118 6 167 114 7 152 99 6 121 87 25
  95299. -101 98 89 62 62 62 34 34 34 18 18 18
  95300. - 6 6 6 0 0 0 0 0 0 0 0 0
  95301. - 0 0 0 0 0 0 0 0 0 0 0 0
  95302. - 0 0 0 0 0 0 0 0 0 0 0 0
  95303. - 0 0 0 0 0 0 0 0 0 0 0 0
  95304. - 0 0 0 0 0 0 0 0 0 0 0 0
  95305. - 0 0 0 0 0 0 0 0 0 0 0 0
  95306. - 0 0 0 0 0 0 0 0 0 0 0 0
  95307. - 0 0 0 6 6 6 6 6 6 10 10 10
  95308. - 18 18 18 22 22 22 30 30 30 42 42 42
  95309. - 50 50 50 66 66 66 86 86 86 101 98 89
  95310. -106 86 58 98 70 6 104 69 6 104 69 6
  95311. -104 69 6 91 60 6 82 62 34 90 90 90
  95312. - 62 62 62 38 38 38 22 22 22 14 14 14
  95313. - 10 10 10 10 10 10 10 10 10 10 10 10
  95314. - 10 10 10 10 10 10 6 6 6 10 10 10
  95315. - 10 10 10 10 10 10 10 10 10 14 14 14
  95316. - 22 22 22 42 42 42 70 70 70 89 81 66
  95317. - 80 54 7 104 69 6 124 80 6 137 92 6
  95318. -134 86 6 116 81 8 100 82 52 86 86 86
  95319. - 58 58 58 30 30 30 14 14 14 6 6 6
  95320. - 0 0 0 0 0 0 0 0 0 0 0 0
  95321. - 0 0 0 0 0 0 0 0 0 0 0 0
  95322. - 0 0 0 0 0 0 0 0 0 0 0 0
  95323. - 0 0 0 0 0 0 0 0 0 0 0 0
  95324. - 0 0 0 0 0 0 0 0 0 0 0 0
  95325. - 0 0 0 0 0 0 0 0 0 0 0 0
  95326. - 0 0 0 0 0 0 0 0 0 0 0 0
  95327. - 0 0 0 0 0 0 0 0 0 0 0 0
  95328. - 0 0 0 6 6 6 10 10 10 14 14 14
  95329. - 18 18 18 26 26 26 38 38 38 54 54 54
  95330. - 70 70 70 86 86 86 94 86 76 89 81 66
  95331. - 89 81 66 86 86 86 74 74 74 50 50 50
  95332. - 30 30 30 14 14 14 6 6 6 0 0 0
  95333. - 0 0 0 0 0 0 0 0 0 0 0 0
  95334. - 0 0 0 0 0 0 0 0 0 0 0 0
  95335. - 0 0 0 0 0 0 0 0 0 0 0 0
  95336. - 6 6 6 18 18 18 34 34 34 58 58 58
  95337. - 82 82 82 89 81 66 89 81 66 89 81 66
  95338. - 94 86 66 94 86 76 74 74 74 50 50 50
  95339. - 26 26 26 14 14 14 6 6 6 0 0 0
  95340. - 0 0 0 0 0 0 0 0 0 0 0 0
  95341. - 0 0 0 0 0 0 0 0 0 0 0 0
  95342. - 0 0 0 0 0 0 0 0 0 0 0 0
  95343. - 0 0 0 0 0 0 0 0 0 0 0 0
  95344. - 0 0 0 0 0 0 0 0 0 0 0 0
  95345. - 0 0 0 0 0 0 0 0 0 0 0 0
  95346. - 0 0 0 0 0 0 0 0 0 0 0 0
  95347. - 0 0 0 0 0 0 0 0 0 0 0 0
  95348. - 0 0 0 0 0 0 0 0 0 0 0 0
  95349. - 6 6 6 6 6 6 14 14 14 18 18 18
  95350. - 30 30 30 38 38 38 46 46 46 54 54 54
  95351. - 50 50 50 42 42 42 30 30 30 18 18 18
  95352. - 10 10 10 0 0 0 0 0 0 0 0 0
  95353. - 0 0 0 0 0 0 0 0 0 0 0 0
  95354. - 0 0 0 0 0 0 0 0 0 0 0 0
  95355. - 0 0 0 0 0 0 0 0 0 0 0 0
  95356. - 0 0 0 6 6 6 14 14 14 26 26 26
  95357. - 38 38 38 50 50 50 58 58 58 58 58 58
  95358. - 54 54 54 42 42 42 30 30 30 18 18 18
  95359. - 10 10 10 0 0 0 0 0 0 0 0 0
  95360. - 0 0 0 0 0 0 0 0 0 0 0 0
  95361. - 0 0 0 0 0 0 0 0 0 0 0 0
  95362. - 0 0 0 0 0 0 0 0 0 0 0 0
  95363. - 0 0 0 0 0 0 0 0 0 0 0 0
  95364. - 0 0 0 0 0 0 0 0 0 0 0 0
  95365. - 0 0 0 0 0 0 0 0 0 0 0 0
  95366. - 0 0 0 0 0 0 0 0 0 0 0 0
  95367. - 0 0 0 0 0 0 0 0 0 0 0 0
  95368. - 0 0 0 0 0 0 0 0 0 0 0 0
  95369. - 0 0 0 0 0 0 0 0 0 6 6 6
  95370. - 6 6 6 10 10 10 14 14 14 18 18 18
  95371. - 18 18 18 14 14 14 10 10 10 6 6 6
  95372. - 0 0 0 0 0 0 0 0 0 0 0 0
  95373. - 0 0 0 0 0 0 0 0 0 0 0 0
  95374. - 0 0 0 0 0 0 0 0 0 0 0 0
  95375. - 0 0 0 0 0 0 0 0 0 0 0 0
  95376. - 0 0 0 0 0 0 0 0 0 6 6 6
  95377. - 14 14 14 18 18 18 22 22 22 22 22 22
  95378. - 18 18 18 14 14 14 10 10 10 6 6 6
  95379. - 0 0 0 0 0 0 0 0 0 0 0 0
  95380. - 0 0 0 0 0 0 0 0 0 0 0 0
  95381. - 0 0 0 0 0 0 0 0 0 0 0 0
  95382. - 0 0 0 0 0 0 0 0 0 0 0 0
  95383. - 0 0 0 0 0 0 0 0 0 0 0 0
  95384. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95385. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95386. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95387. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95388. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95389. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95390. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95391. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95392. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95393. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95394. +0 0 0 0 0 0 0 0 0
  95395. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95396. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95397. +0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
  95398. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95399. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95400. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95401. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95402. +0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
  95403. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95404. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95405. +0 0 0 0 0 0 0 0 0
  95406. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95407. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  95408. +10 15 3 2 3 1 12 18 4 42 61 14 19 27 6 11 16 4
  95409. +38 55 13 10 15 3 3 4 1 10 15 3 0 0 0 0 0 0
  95410. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95411. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95412. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1
  95413. +12 18 4 1 1 0 23 34 8 31 45 11 10 15 3 32 47 11
  95414. +34 49 12 3 4 1 3 4 1 3 4 1 0 0 0 0 0 0
  95415. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95416. +0 0 0 0 0 0 0 0 0
  95417. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95418. +0 0 0 0 0 0 10 15 3 29 42 10 26 37 9 12 18 4
  95419. +55 80 19 81 118 28 55 80 19 92 132 31 106 153 36 69 100 23
  95420. +100 144 34 80 116 27 42 61 14 81 118 28 23 34 8 27 40 9
  95421. +15 21 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95422. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95423. +0 0 0 0 0 0 1 1 0 29 42 10 15 21 5 50 72 17
  95424. +74 107 25 45 64 15 102 148 35 80 116 27 84 121 28 111 160 38
  95425. +69 100 23 65 94 22 81 118 28 29 42 10 17 25 6 29 42 10
  95426. +23 34 8 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  95427. +0 0 0 0 0 0 0 0 0
  95428. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  95429. +15 21 5 15 21 5 34 49 12 101 146 34 111 161 38 97 141 33
  95430. +97 141 33 119 172 41 117 170 40 116 167 40 118 170 40 118 171 40
  95431. +117 169 40 118 170 40 111 160 38 118 170 40 96 138 32 89 128 30
  95432. +81 118 28 11 16 4 10 15 3 1 1 0 0 0 0 0 0 0
  95433. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95434. +3 4 1 3 4 1 34 49 12 101 146 34 79 115 27 111 160 38
  95435. +114 165 39 113 163 39 118 170 40 117 169 40 118 171 40 117 169 40
  95436. +116 167 40 119 172 41 113 163 39 92 132 31 105 151 36 113 163 39
  95437. +75 109 26 19 27 6 16 23 5 11 16 4 0 1 0 0 0 0
  95438. +0 0 0 0 0 0 0 0 0
  95439. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  95440. +80 116 27 106 153 36 105 151 36 114 165 39 118 170 40 118 171 40
  95441. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95442. +117 169 40 117 169 40 117 170 40 117 169 40 118 170 40 118 170 40
  95443. +117 170 40 75 109 26 75 109 26 34 49 12 0 0 0 0 0 0
  95444. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  95445. +64 92 22 65 94 22 100 144 34 118 171 40 118 170 40 117 169 40
  95446. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95447. +117 169 40 117 169 40 117 169 40 118 171 41 118 170 40 117 169 40
  95448. +109 158 37 105 151 36 104 150 35 47 69 16 0 0 0 0 0 0
  95449. +0 0 0 0 0 0 0 0 0
  95450. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95451. +42 61 14 115 167 39 118 170 40 117 169 40 117 169 40 117 169 40
  95452. +117 170 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  95453. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95454. +117 169 40 117 169 40 118 170 40 96 138 32 17 25 6 0 0 0
  95455. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47 69 16
  95456. +114 165 39 117 168 40 117 170 40 117 169 40 117 169 40 117 169 40
  95457. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95458. +117 169 40 117 169 40 118 170 40 117 169 40 117 169 40 117 169 40
  95459. +117 170 40 119 172 41 96 138 32 12 18 4 0 0 0 0 0 0
  95460. +0 0 0 0 0 0 0 0 0
  95461. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  95462. +32 47 11 105 151 36 118 170 40 117 169 40 117 169 40 116 168 40
  95463. +109 157 37 111 160 38 117 169 40 118 171 40 117 169 40 117 169 40
  95464. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95465. +117 169 40 117 169 40 117 169 40 118 171 40 69 100 23 2 3 1
  95466. +0 0 0 0 0 0 0 0 0 0 0 0 19 27 6 101 146 34
  95467. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95468. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 170 40
  95469. +118 171 40 115 166 39 107 154 36 111 161 38 117 169 40 117 169 40
  95470. +117 169 40 118 171 40 75 109 26 19 27 6 2 3 1 0 0 0
  95471. +0 0 0 0 0 0 0 0 0
  95472. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 23 5
  95473. +89 128 30 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95474. +111 160 38 92 132 31 79 115 27 96 138 32 115 166 39 119 171 41
  95475. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95476. +117 169 40 117 169 40 117 169 40 118 170 40 109 157 37 26 37 9
  95477. +0 0 0 0 0 0 0 0 0 0 0 0 64 92 22 118 171 40
  95478. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95479. +117 169 40 117 169 40 117 169 40 118 170 40 118 171 40 109 157 37
  95480. +89 128 30 81 118 28 100 144 34 115 166 39 117 169 40 117 169 40
  95481. +117 169 40 117 170 40 113 163 39 60 86 20 1 1 0 0 0 0
  95482. +0 0 0 0 0 0 0 0 0
  95483. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95484. +27 40 9 96 138 32 118 170 40 117 169 40 117 169 40 117 169 40
  95485. +117 170 40 117 169 40 101 146 34 67 96 23 55 80 19 84 121 28
  95486. +113 163 39 119 171 41 117 169 40 117 169 40 117 169 40 117 169 40
  95487. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 65 94 22
  95488. +0 0 0 0 0 0 0 0 0 15 21 5 101 146 34 118 171 40
  95489. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95490. +117 169 40 118 170 40 118 171 40 104 150 35 69 100 23 53 76 18
  95491. +81 118 28 111 160 38 118 170 40 117 169 40 117 169 40 117 169 40
  95492. +117 169 40 114 165 39 69 100 23 10 15 3 0 0 0 0 0 0
  95493. +0 0 0 0 0 0 0 0 0
  95494. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  95495. +31 45 11 77 111 26 117 169 40 117 169 40 117 169 40 117 169 40
  95496. +117 169 40 117 169 40 118 170 40 116 168 40 92 132 31 47 69 16
  95497. +38 55 13 81 118 28 113 163 39 119 171 41 117 169 40 117 169 40
  95498. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 41 92 132 31
  95499. +10 15 3 0 0 0 0 0 0 36 52 12 115 166 39 117 169 40
  95500. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  95501. +118 171 40 102 148 35 64 92 22 34 49 12 65 94 22 106 153 36
  95502. +118 171 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  95503. +118 170 40 107 154 36 55 80 19 15 21 5 0 0 0 0 0 0
  95504. +0 0 0 0 0 0 0 0 0
  95505. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95506. +29 42 10 101 146 34 118 171 40 117 169 40 117 169 40 117 169 40
  95507. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 113 163 39
  95508. +75 109 26 27 40 9 36 52 12 89 128 30 116 167 40 118 171 40
  95509. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 104 150 35
  95510. +16 23 5 0 0 0 0 0 0 53 76 18 118 171 40 117 169 40
  95511. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 109 157 37
  95512. +67 96 23 23 34 8 42 61 14 96 138 32 118 170 40 118 170 40
  95513. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95514. +117 169 40 117 169 40 74 107 25 10 15 3 0 0 0 0 0 0
  95515. +0 0 0 0 0 0 0 0 0
  95516. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95517. +0 0 0 31 45 11 101 146 34 118 170 40 117 169 40 117 169 40
  95518. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95519. +119 171 41 102 148 35 47 69 16 14 20 5 50 72 17 102 148 35
  95520. +118 171 40 117 169 40 117 169 40 117 169 40 118 170 40 102 148 35
  95521. +15 21 5 0 0 0 0 0 0 50 72 17 118 170 40 117 169 40
  95522. +117 169 40 117 169 40 118 170 40 116 167 40 84 121 28 27 40 9
  95523. +19 27 6 74 107 25 114 165 39 118 171 40 117 169 40 117 169 40
  95524. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95525. +117 169 40 75 109 26 10 15 4 0 0 0 0 0 0 0 0 0
  95526. +0 0 0 0 0 0 0 0 0
  95527. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95528. +0 0 0 38 55 13 102 148 35 118 171 40 117 169 40 117 169 40
  95529. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95530. +117 169 40 118 170 40 115 167 39 77 111 26 17 25 6 19 27 6
  95531. +77 111 26 115 166 39 118 170 40 117 169 40 119 172 41 81 118 28
  95532. +3 4 1 0 0 0 0 0 0 27 40 9 111 160 38 118 170 40
  95533. +117 169 40 118 171 40 105 151 36 50 72 17 10 15 3 38 55 13
  95534. +100 144 34 118 171 40 117 169 40 117 169 40 117 169 40 117 169 40
  95535. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95536. +117 169 40 79 115 27 15 21 5 0 0 0 0 0 0 0 0 0
  95537. +0 0 0 0 0 0 0 0 0
  95538. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95539. +0 0 0 10 15 3 64 92 22 111 160 38 117 169 40 117 169 40
  95540. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95541. +117 169 40 117 169 40 117 169 40 118 171 40 96 138 32 32 47 11
  95542. +3 4 1 50 72 17 107 154 36 120 173 41 105 151 36 31 45 11
  95543. +0 0 0 0 0 0 0 0 0 3 4 1 65 94 22 117 169 40
  95544. +118 170 40 89 128 30 26 37 9 3 4 1 60 86 20 111 161 38
  95545. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95546. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95547. +97 141 33 36 52 12 1 1 0 0 0 0 0 0 0 0 0 0
  95548. +0 0 0 0 0 0 0 0 0
  95549. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95550. +0 0 0 0 0 0 14 20 5 75 109 26 117 168 40 117 169 40
  95551. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95552. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 107 154 36
  95553. +45 64 15 2 3 1 31 45 11 75 109 26 32 47 11 0 1 0
  95554. +0 0 0 0 0 0 0 0 0 0 0 0 10 15 3 55 80 19
  95555. +65 94 22 11 16 4 11 16 4 75 109 26 116 168 40 118 170 40
  95556. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95557. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 107 154 36
  95558. +47 69 16 3 4 1 0 0 0 0 0 0 0 0 0 0 0 0
  95559. +0 0 0 0 0 0 0 0 0
  95560. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95561. +0 0 0 0 0 0 12 18 4 69 100 23 111 161 38 118 171 40
  95562. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95563. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  95564. +111 160 38 50 72 17 2 3 1 2 3 1 0 0 0 0 0 0
  95565. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  95566. +1 1 0 12 18 4 81 118 28 118 170 40 117 169 40 117 169 40
  95567. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95568. +117 169 40 117 169 40 117 169 40 117 170 40 118 171 40 101 146 34
  95569. +42 61 14 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  95570. +0 0 0 0 0 0 0 0 0
  95571. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95572. +0 0 0 0 0 0 0 0 0 3 4 1 36 52 12 89 128 30
  95573. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95574. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95575. +118 171 41 101 146 34 14 20 5 0 0 0 0 0 0 0 0 0
  95576. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95577. +0 0 0 47 69 16 118 170 40 117 169 40 117 169 40 117 169 40
  95578. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95579. +117 169 40 117 169 40 117 170 40 111 160 38 69 100 23 19 27 6
  95580. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95581. +0 0 0 0 0 0 0 0 0
  95582. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95583. +0 0 0 0 0 0 0 0 0 0 0 0 11 16 4 69 100 23
  95584. +115 167 39 119 172 41 117 169 40 117 169 40 117 169 40 117 169 40
  95585. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95586. +119 172 41 75 109 26 3 4 1 0 0 0 0 0 0 0 0 0
  95587. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95588. +0 0 0 23 34 8 106 153 36 118 170 40 117 169 40 117 169 40
  95589. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95590. +117 169 40 118 170 40 119 172 41 105 151 36 42 61 14 2 3 1
  95591. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95592. +0 0 0 0 0 0 0 0 0
  95593. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95594. +0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 15 21 5
  95595. +45 64 15 80 116 27 114 165 39 118 170 40 117 169 40 117 169 40
  95596. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 119 172 41
  95597. +97 141 33 20 30 7 0 0 0 0 0 0 0 0 0 0 0 0
  95598. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95599. +0 0 0 1 1 0 53 76 18 114 165 39 118 171 40 117 169 40
  95600. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  95601. +118 171 40 104 150 35 64 92 22 31 45 11 10 15 3 0 0 0
  95602. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95603. +0 0 0 0 0 0 0 0 0
  95604. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95605. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95606. +0 0 0 36 52 12 97 141 33 109 158 37 113 163 39 116 168 40
  95607. +117 169 40 117 170 40 118 170 40 119 172 41 115 167 39 84 121 28
  95608. +23 34 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95609. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95610. +0 0 0 0 0 0 3 4 1 50 72 17 102 148 35 118 171 40
  95611. +119 171 41 118 170 40 117 169 40 117 169 40 115 166 39 111 161 38
  95612. +109 157 37 79 115 27 12 18 4 0 0 0 0 0 0 0 0 0
  95613. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95614. +0 0 0 0 0 0 0 0 0
  95615. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95616. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95617. +0 0 0 3 4 1 15 21 5 23 34 8 45 64 15 106 153 36
  95618. +116 167 40 111 160 38 101 146 34 79 115 27 42 61 14 10 15 3
  95619. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95620. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95621. +0 0 0 0 0 0 0 0 0 1 1 0 20 30 7 60 86 20
  95622. +89 128 30 106 153 36 113 163 39 117 169 40 84 121 28 29 42 10
  95623. +19 27 6 10 15 3 2 3 1 0 0 0 0 0 0 0 0 0
  95624. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95625. +0 0 0 0 0 0 0 0 0
  95626. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95627. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95628. +0 0 0 0 0 0 0 0 0 0 0 0 16 23 5 38 55 13
  95629. +36 52 12 26 37 9 12 18 4 2 3 1 0 0 0 0 0 0
  95630. +0 0 0 0 0 0 0 0 0 1 0 0 19 2 7 52 5 18
  95631. +78 7 27 88 8 31 81 7 29 56 5 19 25 2 9 3 0 1
  95632. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95633. +3 4 1 19 27 6 31 45 11 38 55 13 32 47 11 3 4 1
  95634. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95635. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95636. +0 0 0 0 0 0 0 0 0
  95637. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95638. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95639. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  95640. +9 0 3 12 1 4 9 0 3 4 0 1 0 0 0 0 0 0
  95641. +0 0 0 0 0 0 28 3 10 99 9 35 156 14 55 182 16 64
  95642. +189 17 66 190 17 67 189 17 66 184 17 65 166 15 58 118 13 41
  95643. +45 4 16 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  95644. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95645. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95646. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95647. +0 0 0 0 0 0 0 0 0
  95648. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95649. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95650. +0 0 0 0 0 0 11 1 4 52 5 18 101 9 35 134 12 47
  95651. +151 14 53 154 14 54 151 14 53 113 10 40 11 1 4 0 0 0
  95652. +3 0 1 67 6 24 159 14 56 190 17 67 190 17 67 188 17 66
  95653. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 191 17 67
  95654. +174 16 61 101 9 35 14 1 5 0 0 0 35 3 12 108 10 38
  95655. +122 11 43 122 11 43 112 10 39 87 8 30 50 5 17 13 1 5
  95656. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95657. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95658. +0 0 0 0 0 0 0 0 0
  95659. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95660. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95661. +3 0 1 56 5 19 141 13 49 182 16 64 191 17 67 191 17 67
  95662. +190 17 67 190 17 67 191 17 67 113 10 40 3 0 1 1 0 0
  95663. +79 7 28 180 16 63 190 17 67 188 17 66 188 17 66 188 17 66
  95664. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95665. +189 17 66 188 17 66 122 11 43 11 1 4 41 4 14 176 16 62
  95666. +191 17 67 191 17 67 191 17 67 190 17 67 181 16 63 146 13 51
  95667. +75 7 26 10 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  95668. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95669. +0 0 0 0 0 0 0 0 0
  95670. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95671. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 1 2
  95672. +90 8 32 178 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  95673. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 41 4 14
  95674. +173 16 61 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95675. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95676. +188 17 66 188 17 66 188 17 66 88 8 31 1 0 0 89 8 31
  95677. +185 17 65 189 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  95678. +186 17 65 124 11 43 25 2 9 0 0 0 0 0 0 0 0 0
  95679. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95680. +0 0 0 0 0 0 0 0 0
  95681. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95682. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 89 8 31
  95683. +184 17 65 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95684. +190 17 67 151 14 53 34 3 12 0 0 0 0 0 0 79 7 28
  95685. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95686. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95687. +188 17 66 188 17 66 191 17 67 146 13 51 9 1 3 7 1 2
  95688. +108 10 38 187 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  95689. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 0 0 0
  95690. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95691. +0 0 0 0 0 0 0 0 0
  95692. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95693. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 176 16 62
  95694. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95695. +151 14 53 38 3 13 0 0 0 0 0 0 0 0 0 50 5 17
  95696. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95697. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95698. +188 17 66 188 17 66 191 17 67 141 13 49 7 1 3 0 0 0
  95699. +11 1 4 112 10 39 187 17 66 189 17 66 188 17 66 188 17 66
  95700. +188 17 66 188 17 66 190 17 67 113 10 40 5 0 2 0 0 0
  95701. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95702. +0 0 0 0 0 0 0 0 0
  95703. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95704. +0 0 0 0 0 0 0 0 0 7 1 3 132 12 46 191 17 67
  95705. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 146 13 51
  95706. +35 3 12 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  95707. +101 9 35 185 17 65 190 17 67 188 17 66 188 17 66 188 17 66
  95708. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95709. +188 17 66 190 17 67 180 16 63 67 6 24 0 0 0 0 0 0
  95710. +0 0 0 11 1 4 108 10 38 186 17 65 189 17 66 188 17 66
  95711. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  95712. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95713. +0 0 0 0 0 0 0 0 0
  95714. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95715. +0 0 0 0 0 0 0 0 0 44 4 15 177 16 62 189 17 66
  95716. +188 17 66 188 17 66 189 17 66 189 17 66 134 12 47 28 3 10
  95717. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95718. +8 1 3 79 7 28 159 14 56 188 17 66 191 17 67 190 17 67
  95719. +189 17 66 189 17 66 189 17 66 189 17 66 190 17 67 191 17 67
  95720. +188 17 66 158 14 55 72 7 25 4 0 1 0 0 0 0 0 0
  95721. +0 0 0 0 0 0 8 1 3 95 9 33 182 16 64 189 17 67
  95722. +188 17 66 188 17 66 188 17 66 191 17 67 122 11 43 3 0 1
  95723. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95724. +0 0 0 0 0 0 0 0 0
  95725. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95726. +0 0 0 0 0 0 0 0 0 88 8 31 190 17 67 188 17 66
  95727. +188 17 66 189 17 66 185 17 65 113 10 40 18 2 6 0 0 0
  95728. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95729. +0 0 0 1 0 0 24 2 8 77 7 27 124 11 43 154 14 54
  95730. +168 15 59 173 16 61 173 16 61 168 15 59 154 14 54 124 11 43
  95731. +77 7 27 22 2 8 0 0 0 0 0 0 0 0 0 0 0 0
  95732. +0 0 0 0 0 0 0 0 0 5 0 2 77 7 27 173 16 61
  95733. +190 17 67 188 17 66 188 17 66 190 17 67 164 15 57 23 2 8
  95734. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95735. +0 0 0 0 0 0 0 0 0
  95736. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95737. +0 0 0 0 0 0 1 0 0 118 13 41 191 17 67 188 17 66
  95738. +190 17 67 174 16 61 87 8 30 8 1 3 0 0 0 0 0 0
  95739. +0 0 0 0 0 0 10 1 4 29 3 10 40 4 14 36 3 13
  95740. +18 2 6 2 0 1 0 0 0 0 0 0 3 0 1 14 1 5
  95741. +26 2 9 33 3 11 32 3 11 25 2 9 13 1 5 3 0 1
  95742. +0 0 0 14 1 5 56 5 19 95 9 33 109 10 38 101 9 35
  95743. +77 7 27 35 3 12 5 0 2 0 0 0 1 0 0 56 5 19
  95744. +156 14 55 190 17 67 188 17 66 188 17 66 182 16 64 50 5 17
  95745. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95746. +0 0 0 0 0 0 0 0 0
  95747. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95748. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 189 17 66
  95749. +151 14 53 52 5 18 2 0 1 0 0 0 0 0 0 1 0 0
  95750. +28 3 10 90 8 32 146 13 51 170 15 60 178 16 62 174 16 61
  95751. +158 14 55 112 10 39 40 4 14 1 0 0 0 0 0 0 0 0
  95752. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  95753. +56 5 19 146 13 51 183 17 64 191 17 67 191 17 67 191 17 67
  95754. +188 17 66 173 16 61 122 11 43 41 4 14 1 0 0 0 0 0
  95755. +30 3 10 124 11 43 185 17 65 190 17 67 187 17 66 67 6 24
  95756. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95757. +0 0 0 0 0 0 0 0 0
  95758. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95759. +0 0 0 0 0 0 6 1 2 134 12 47 168 15 59 99 9 35
  95760. +21 2 7 0 0 0 0 0 0 0 0 0 6 1 2 77 7 27
  95761. +162 15 57 190 17 67 191 17 67 189 17 66 189 17 66 189 17 66
  95762. +190 17 67 191 17 67 169 15 59 75 7 26 3 0 1 0 0 0
  95763. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 79 7 28
  95764. +178 16 62 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95765. +188 17 66 189 17 66 191 17 67 170 15 60 79 7 28 5 0 2
  95766. +0 0 0 10 1 3 78 7 27 159 14 56 188 17 66 75 7 26
  95767. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95768. +0 0 0 0 0 0 0 0 0
  95769. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95770. +0 0 0 0 0 0 1 0 0 35 3 12 29 3 10 2 0 1
  95771. +0 0 0 0 0 0 0 0 0 9 1 3 101 9 35 183 17 64
  95772. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95773. +188 17 66 188 17 66 190 17 67 178 16 63 67 6 23 0 0 0
  95774. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 174 16 61
  95775. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95776. +188 17 66 188 17 66 188 17 66 190 17 67 182 16 64 89 8 31
  95777. +4 0 1 0 0 0 0 0 0 25 2 9 73 7 26 31 3 11
  95778. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95779. +0 0 0 0 0 0 0 0 0
  95780. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95781. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95782. +0 0 0 0 0 0 4 0 1 98 9 34 187 17 66 189 17 66
  95783. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95784. +188 17 66 188 17 66 188 17 66 190 17 67 158 14 55 25 2 9
  95785. +0 0 0 0 0 0 0 0 0 8 1 3 134 12 47 191 17 67
  95786. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95787. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 180 16 63
  95788. +68 6 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95789. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95790. +0 0 0 0 0 0 0 0 0
  95791. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95792. +0 0 0 6 1 2 19 2 7 3 0 1 0 0 0 0 0 0
  95793. +0 0 0 0 0 0 65 6 23 180 16 63 189 17 66 188 17 66
  95794. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95795. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 83 8 29
  95796. +0 0 0 0 0 0 0 0 0 41 4 14 177 16 62 189 17 66
  95797. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95798. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95799. +159 14 56 28 3 10 0 0 0 0 0 0 0 0 0 23 2 8
  95800. +41 4 14 5 0 2 0 0 0 0 0 0 0 0 0 0 0 0
  95801. +0 0 0 0 0 0 0 0 0
  95802. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95803. +23 2 8 113 10 40 159 14 56 65 6 23 0 0 0 0 0 0
  95804. +0 0 0 16 1 6 146 13 51 191 17 67 188 17 66 188 17 66
  95805. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95806. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 132 12 46
  95807. +5 0 2 0 0 0 0 0 0 77 7 27 189 17 66 188 17 66
  95808. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95809. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95810. +190 17 67 98 9 34 0 0 0 0 0 0 12 1 4 134 12 47
  95811. +178 16 63 108 10 38 16 1 6 0 0 0 0 0 0 0 0 0
  95812. +0 0 0 0 0 0 0 0 0
  95813. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 3 10
  95814. +141 13 49 190 17 67 191 17 67 134 12 47 6 1 2 0 0 0
  95815. +0 0 0 68 6 24 186 17 65 188 17 66 188 17 66 188 17 66
  95816. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95817. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 156 14 55
  95818. +14 1 5 0 0 0 0 0 0 98 9 34 191 17 67 188 17 66
  95819. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95820. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95821. +190 17 67 156 14 55 19 2 7 0 0 0 47 4 16 181 16 63
  95822. +190 17 67 189 17 66 126 14 44 17 2 6 0 0 0 0 0 0
  95823. +0 0 0 0 0 0 0 0 0
  95824. +0 0 0 0 0 0 0 0 0 0 0 0 16 1 6 134 12 47
  95825. +191 17 67 188 17 66 190 17 67 162 15 57 19 2 7 0 0 0
  95826. +3 0 1 123 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  95827. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95828. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 163 15 57
  95829. +20 2 7 0 0 0 0 0 0 101 9 35 191 17 67 188 17 66
  95830. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95831. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95832. +188 17 66 182 16 64 52 5 18 0 0 0 73 7 26 188 17 66
  95833. +188 17 66 188 17 66 189 17 66 109 10 38 5 0 2 0 0 0
  95834. +0 0 0 0 0 0 0 0 0
  95835. +0 0 0 0 0 0 0 0 0 0 0 0 95 9 33 189 17 66
  95836. +188 17 66 188 17 66 189 17 66 171 15 60 29 3 10 0 0 0
  95837. +16 1 6 156 14 55 190 17 67 188 17 66 188 17 66 188 17 66
  95838. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95839. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 158 14 55
  95840. +17 2 6 0 0 0 0 0 0 85 8 30 190 17 67 188 17 66
  95841. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95842. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95843. +188 17 66 189 17 66 81 7 29 0 0 0 85 8 30 190 17 67
  95844. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  95845. +0 0 0 0 0 0 0 0 0
  95846. +0 0 0 0 0 0 0 0 0 25 2 9 162 15 57 190 17 67
  95847. +188 17 66 188 17 66 189 17 66 173 16 61 31 3 11 0 0 0
  95848. +30 3 10 171 15 60 189 17 66 188 17 66 188 17 66 188 17 66
  95849. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95850. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 141 13 49
  95851. +7 1 2 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  95852. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95853. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95854. +188 17 66 191 17 67 98 9 34 0 0 0 88 8 31 190 17 67
  95855. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 5 0 2
  95856. +0 0 0 0 0 0 0 0 0
  95857. +0 0 0 0 0 0 0 0 0 68 6 24 187 17 66 188 17 66
  95858. +188 17 66 188 17 66 189 17 66 170 15 60 28 3 10 0 0 0
  95859. +34 3 12 174 16 61 189 17 66 188 17 66 188 17 66 188 17 66
  95860. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95861. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 101 9 35
  95862. +0 0 0 0 0 0 0 0 0 21 2 7 159 14 56 190 17 67
  95863. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95864. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95865. +188 17 66 191 17 67 98 9 34 0 0 0 81 7 29 189 17 66
  95866. +188 17 66 188 17 66 188 17 66 189 17 66 168 15 59 28 3 10
  95867. +0 0 0 0 0 0 0 0 0
  95868. +0 0 0 0 0 0 0 0 0 109 10 38 191 17 67 188 17 66
  95869. +188 17 66 188 17 66 190 17 67 163 15 57 21 2 7 0 0 0
  95870. +26 2 9 168 15 59 189 17 66 188 17 66 188 17 66 188 17 66
  95871. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95872. +188 17 66 188 17 66 188 17 66 189 17 66 180 16 63 47 4 16
  95873. +0 0 0 0 0 0 0 0 0 0 0 0 108 10 38 190 17 67
  95874. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95875. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95876. +188 17 66 189 17 66 78 7 27 0 0 0 68 6 24 187 17 66
  95877. +188 17 66 188 17 66 188 17 66 188 17 66 183 17 64 56 5 19
  95878. +0 0 0 0 0 0 0 0 0
  95879. +0 0 0 0 0 0 3 0 1 131 12 46 191 17 67 188 17 66
  95880. +188 17 66 188 17 66 190 17 67 151 14 53 12 1 4 0 0 0
  95881. +11 1 4 146 13 51 190 17 67 188 17 66 188 17 66 188 17 66
  95882. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95883. +188 17 66 188 17 66 188 17 66 191 17 67 126 14 44 7 1 2
  95884. +0 0 0 0 0 0 0 0 0 0 0 0 32 3 11 164 15 58
  95885. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95886. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95887. +189 17 66 178 16 62 44 4 15 0 0 0 50 5 17 182 16 64
  95888. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  95889. +0 0 0 0 0 0 0 0 0
  95890. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 188 17 66
  95891. +188 17 66 188 17 66 191 17 67 131 12 46 3 0 1 0 0 0
  95892. +0 0 0 101 9 35 190 17 67 188 17 66 188 17 66 188 17 66
  95893. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95894. +188 17 66 188 17 66 190 17 67 170 15 60 44 4 15 0 0 0
  95895. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 7 27
  95896. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95897. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95898. +191 17 67 134 12 47 9 1 3 0 0 0 31 3 11 171 15 60
  95899. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  95900. +0 0 0 0 0 0 0 0 0
  95901. +0 0 0 0 0 0 2 0 1 124 11 43 191 17 67 188 17 66
  95902. +188 17 66 188 17 66 191 17 67 101 9 35 0 0 0 0 0 0
  95903. +0 0 0 35 3 12 168 15 59 190 17 67 188 17 66 188 17 66
  95904. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95905. +188 17 66 189 17 66 182 16 64 77 7 27 0 0 0 0 0 0
  95906. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 2
  95907. +99 9 35 185 17 65 189 17 66 188 17 66 188 17 66 188 17 66
  95908. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  95909. +177 16 62 56 5 19 0 0 0 0 0 0 13 1 5 151 14 53
  95910. +190 17 67 188 17 66 188 17 66 188 17 66 185 17 65 56 5 19
  95911. +0 0 0 0 0 0 0 0 0
  95912. +0 0 0 0 0 0 0 0 0 99 9 35 191 17 67 188 17 66
  95913. +188 17 66 188 17 66 186 17 65 65 6 23 0 0 0 0 0 0
  95914. +0 0 0 0 0 0 79 7 28 182 16 64 190 17 67 188 17 66
  95915. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95916. +191 17 67 177 16 62 83 8 29 4 0 1 0 0 0 0 0 0
  95917. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95918. +8 1 3 89 8 31 175 16 62 191 17 67 189 17 66 188 17 66
  95919. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 181 16 63
  95920. +85 8 30 3 0 1 0 0 0 0 0 0 1 0 0 118 13 41
  95921. +191 17 67 188 17 66 188 17 66 189 17 66 173 16 61 34 3 12
  95922. +0 0 0 0 0 0 0 0 0
  95923. +0 0 0 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  95924. +188 17 66 189 17 66 169 15 59 30 3 10 0 0 0 0 0 0
  95925. +0 0 0 0 0 0 5 0 2 83 8 29 173 16 61 191 17 67
  95926. +190 17 67 189 17 66 189 17 66 190 17 67 191 17 67 187 17 66
  95927. +151 14 53 56 5 19 3 0 1 0 0 0 16 1 6 50 5 17
  95928. +79 7 28 95 9 33 95 9 33 75 7 26 41 4 14 10 1 4
  95929. +0 0 0 2 0 1 50 5 17 132 12 46 178 16 62 190 17 67
  95930. +191 17 67 191 17 67 191 17 67 186 17 65 154 14 54 68 6 24
  95931. +4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  95932. +187 17 66 188 17 66 188 17 66 191 17 67 141 13 49 9 1 3
  95933. +0 0 0 0 0 0 0 0 0
  95934. +0 0 0 0 0 0 0 0 0 14 1 5 151 14 53 190 17 67
  95935. +188 17 66 191 17 67 131 12 46 5 0 2 0 0 0 0 0 0
  95936. +0 0 0 0 0 0 0 0 0 2 0 1 44 4 15 113 10 40
  95937. +156 14 55 173 16 61 174 16 61 164 15 58 134 12 47 77 7 27
  95938. +18 2 6 0 0 0 16 1 6 85 8 30 151 14 53 182 16 64
  95939. +189 17 66 191 17 67 190 17 67 188 17 66 177 16 62 141 13 49
  95940. +68 6 24 8 1 3 0 0 0 8 1 3 44 4 15 88 8 31
  95941. +113 10 40 122 11 43 108 10 38 67 6 24 20 2 7 0 0 0
  95942. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 3 10
  95943. +166 15 58 190 17 67 188 17 66 187 17 66 79 7 28 0 0 0
  95944. +0 0 0 0 0 0 0 0 0
  95945. +0 0 0 0 0 0 0 0 0 0 0 0 73 7 26 185 17 65
  95946. +189 17 66 184 17 65 65 6 23 0 0 0 0 0 0 0 0 0
  95947. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
  95948. +17 2 6 32 3 11 34 3 12 22 2 8 6 1 2 0 0 0
  95949. +0 0 0 38 3 13 141 13 49 188 17 66 190 17 67 188 17 66
  95950. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  95951. +184 17 65 122 11 43 21 2 7 0 0 0 0 0 0 0 0 0
  95952. +0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95953. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95954. +108 10 38 191 17 67 191 17 67 141 13 49 16 1 6 0 0 0
  95955. +0 0 0 0 0 0 0 0 0
  95956. +0 0 0 0 0 0 0 0 0 0 0 0 8 1 3 112 10 39
  95957. +186 17 65 124 11 43 10 1 4 0 0 0 0 0 0 0 0 0
  95958. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95959. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95960. +36 3 13 156 14 55 191 17 67 188 17 66 188 17 66 188 17 66
  95961. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95962. +189 17 66 190 17 67 134 12 47 18 2 6 0 0 0 0 0 0
  95963. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95964. +0 0 0 7 1 2 41 4 14 75 7 26 66 5 23 19 2 7
  95965. +26 2 9 144 13 50 154 14 54 40 4 14 0 0 0 0 0 0
  95966. +0 0 0 0 0 0 0 0 0
  95967. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  95968. +56 5 19 19 2 7 0 0 0 7 1 2 29 3 10 35 3 12
  95969. +19 2 7 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  95970. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  95971. +134 12 47 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95972. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95973. +188 17 66 188 17 66 189 17 67 108 10 38 3 0 1 0 0 0
  95974. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95975. +40 4 14 124 11 43 177 16 62 188 17 66 187 17 66 144 13 50
  95976. +24 2 8 17 2 6 22 2 8 0 0 0 0 0 0 0 0 0
  95977. +0 0 0 0 0 0 0 0 0
  95978. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95979. +0 0 0 0 0 0 19 2 7 122 11 43 171 15 60 175 16 62
  95980. +159 14 56 112 10 39 40 4 14 2 0 1 0 0 0 0 0 0
  95981. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  95982. +186 17 65 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95983. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95984. +188 17 66 188 17 66 189 17 66 174 16 61 41 4 14 0 0 0
  95985. +0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 72 7 25
  95986. +168 15 59 191 17 67 189 17 66 188 17 66 188 17 66 190 17 67
  95987. +95 9 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95988. +0 0 0 0 0 0 0 0 0
  95989. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95990. +0 0 0 0 0 0 95 9 33 191 17 67 189 17 66 189 17 66
  95991. +190 17 67 191 17 67 171 15 60 90 8 32 12 1 4 0 0 0
  95992. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 132 12 46
  95993. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95994. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95995. +188 17 66 188 17 66 188 17 66 190 17 67 98 9 34 0 0 0
  95996. +0 0 0 0 0 0 0 0 0 5 0 2 88 8 31 180 16 63
  95997. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 191 17 67
  95998. +146 13 51 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  95999. +0 0 0 0 0 0 0 0 0
  96000. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96001. +0 0 0 9 1 3 144 13 50 191 17 67 188 17 66 188 17 66
  96002. +188 17 66 188 17 66 189 17 66 187 17 66 123 11 43 20 2 7
  96003. +0 0 0 0 0 0 0 0 0 0 0 0 21 2 7 163 15 57
  96004. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96005. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96006. +188 17 66 188 17 66 188 17 66 191 17 67 134 12 47 5 0 2
  96007. +0 0 0 0 0 0 3 0 1 88 8 31 182 16 64 189 17 66
  96008. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  96009. +171 15 60 31 3 11 0 0 0 0 0 0 0 0 0 0 0 0
  96010. +0 0 0 0 0 0 0 0 0
  96011. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96012. +0 0 0 20 2 7 162 15 57 190 17 67 188 17 66 188 17 66
  96013. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 132 12 46
  96014. +20 2 7 0 0 0 0 0 0 0 0 0 32 3 11 173 16 61
  96015. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96016. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96017. +188 17 66 188 17 66 188 17 66 190 17 67 151 14 53 12 1 4
  96018. +0 0 0 0 0 0 72 7 25 180 16 63 189 17 66 188 17 66
  96019. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96020. +181 16 63 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  96021. +0 0 0 0 0 0 0 0 0
  96022. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96023. +0 0 0 21 2 7 163 15 57 190 17 67 188 17 66 188 17 66
  96024. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  96025. +122 11 43 9 1 3 0 0 0 0 0 0 30 3 10 171 15 60
  96026. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96027. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96028. +188 17 66 188 17 66 188 17 66 190 17 67 146 13 51 10 1 4
  96029. +0 0 0 38 3 13 166 15 58 190 17 67 188 17 66 188 17 66
  96030. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96031. +183 17 64 52 5 18 0 0 0 0 0 0 0 0 0 0 0 0
  96032. +0 0 0 0 0 0 0 0 0
  96033. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96034. +0 0 0 13 1 5 154 14 54 190 17 67 188 17 66 188 17 66
  96035. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96036. +186 17 65 79 7 28 0 0 0 0 0 0 14 1 5 156 14 54
  96037. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96038. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96039. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 2 0 1
  96040. +5 0 2 122 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  96041. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96042. +182 16 64 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  96043. +0 0 0 0 0 0 0 0 0
  96044. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96045. +0 0 0 3 0 1 126 14 44 191 17 67 188 17 66 188 17 66
  96046. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96047. +190 17 67 158 14 55 23 2 8 0 0 0 1 0 0 113 10 40
  96048. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96049. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96050. +188 17 66 188 17 66 188 17 66 188 17 66 78 7 27 0 0 0
  96051. +47 4 16 177 16 62 189 17 66 188 17 66 188 17 66 188 17 66
  96052. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  96053. +173 16 61 34 3 12 0 0 0 0 0 0 0 0 0 0 0 0
  96054. +0 0 0 0 0 0 0 0 0
  96055. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96056. +0 0 0 0 0 0 85 8 30 189 17 66 188 17 66 188 17 66
  96057. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96058. +188 17 66 188 17 66 79 7 28 0 0 0 0 0 0 47 4 16
  96059. +175 16 62 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96060. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96061. +188 17 66 188 17 66 190 17 67 156 14 55 22 2 8 0 0 0
  96062. +109 10 38 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  96063. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  96064. +151 14 53 13 1 5 0 0 0 0 0 0 0 0 0 0 0 0
  96065. +0 0 0 0 0 0 0 0 0
  96066. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96067. +0 0 0 0 0 0 35 3 12 173 16 61 189 17 66 188 17 66
  96068. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96069. +188 17 66 191 17 67 134 12 47 7 1 2 0 0 0 3 0 1
  96070. +99 9 35 188 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  96071. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96072. +188 17 66 189 17 66 181 16 63 68 6 24 0 0 0 18 2 6
  96073. +156 14 55 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  96074. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  96075. +101 9 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96076. +0 0 0 0 0 0 0 0 0
  96077. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96078. +0 0 0 0 0 0 3 0 1 118 13 41 191 17 67 188 17 66
  96079. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96080. +188 17 66 189 17 66 168 15 59 28 3 10 0 0 0 0 0 0
  96081. +12 1 4 113 10 40 187 17 66 189 17 67 188 17 66 188 17 66
  96082. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96083. +190 17 67 180 16 63 88 8 31 4 0 1 0 0 0 47 4 16
  96084. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96085. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 168 15 59
  96086. +36 3 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96087. +0 0 0 0 0 0 0 0 0
  96088. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96089. +0 0 0 0 0 0 0 0 0 38 3 13 164 15 58 190 17 67
  96090. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96091. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  96092. +0 0 0 11 1 4 90 8 32 169 15 59 190 17 67 190 17 67
  96093. +189 17 66 189 17 66 189 17 66 189 17 66 191 17 67 189 17 66
  96094. +158 14 55 68 6 24 4 0 1 0 0 0 0 0 0 73 7 26
  96095. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96096. +188 17 66 188 17 66 188 17 66 189 17 66 185 17 65 83 8 29
  96097. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96098. +0 0 0 0 0 0 0 0 0
  96099. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96100. +0 0 0 0 0 0 0 0 0 0 0 0 65 6 23 174 16 61
  96101. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96102. +188 17 66 188 17 66 185 17 65 56 5 19 0 0 0 0 0 0
  96103. +0 0 0 0 0 0 2 0 1 35 3 12 99 9 35 146 13 51
  96104. +170 15 60 177 16 62 177 16 62 166 15 58 141 13 49 85 8 30
  96105. +24 2 8 0 0 0 0 0 0 0 0 0 0 0 0 85 8 30
  96106. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96107. +188 17 66 188 17 66 188 17 66 189 17 66 112 10 39 8 1 3
  96108. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96109. +0 0 0 0 0 0 0 0 0
  96110. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96111. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 68 6 24
  96112. +170 15 60 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  96113. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  96114. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 11 1 4
  96115. +28 3 10 40 4 14 38 3 13 25 2 9 8 1 3 0 0 0
  96116. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 7 27
  96117. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96118. +188 17 66 189 17 66 187 17 66 113 10 40 14 1 5 0 0 0
  96119. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96120. +0 0 0 0 0 0 0 0 0
  96121. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96122. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  96123. +47 4 16 141 13 49 186 17 65 191 17 67 190 17 67 189 17 66
  96124. +189 17 66 191 17 67 156 14 55 20 2 7 0 0 0 0 0 0
  96125. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96126. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96127. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 4 15
  96128. +178 16 62 190 17 67 188 17 66 188 17 66 188 17 66 190 17 67
  96129. +191 17 67 173 16 61 90 8 32 10 1 4 0 0 0 0 0 0
  96130. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96131. +0 0 0 0 0 0 0 0 0
  96132. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96133. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96134. +0 0 0 14 1 5 68 6 24 131 12 46 162 15 57 174 16 61
  96135. +171 15 60 146 13 51 56 5 19 0 0 0 0 0 0 0 0 0
  96136. +0 0 0 0 0 0 0 0 0 3 0 1 14 1 5 29 3 10
  96137. +41 4 14 47 4 16 50 5 17 45 4 16 34 3 12 18 2 6
  96138. +5 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  96139. +90 8 32 169 15 59 185 17 65 187 17 66 182 16 64 163 15 57
  96140. +113 10 40 41 4 14 2 0 1 0 0 0 0 0 0 0 0 0
  96141. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96142. +0 0 0 0 0 0 0 0 0
  96143. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96144. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96145. +0 0 0 0 0 0 0 0 0 5 0 2 21 2 7 34 3 12
  96146. +29 3 10 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  96147. +3 0 1 32 3 11 79 7 28 124 11 43 154 14 54 171 15 60
  96148. +180 16 63 182 16 64 182 16 64 180 16 63 174 16 61 159 14 56
  96149. +132 12 46 88 8 31 34 3 12 3 0 1 0 0 0 0 0 0
  96150. +3 0 1 29 3 10 56 5 19 65 6 23 50 5 17 23 2 8
  96151. +3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96152. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96153. +0 0 0 0 0 0 0 0 0
  96154. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96155. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96156. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96157. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 2 9
  96158. +109 10 38 169 15 59 189 17 66 191 17 67 190 17 67 189 17 66
  96159. +189 17 66 188 17 66 188 17 66 188 17 66 189 17 66 190 17 67
  96160. +191 17 67 190 17 67 171 15 60 98 9 34 10 1 3 0 0 0
  96161. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96162. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96163. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96164. +0 0 0 0 0 0 0 0 0
  96165. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96166. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96167. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96168. +0 0 0 0 0 0 0 0 0 0 0 0 14 1 5 141 13 49
  96169. +191 17 67 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96170. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96171. +188 17 66 188 17 66 189 17 67 186 17 65 65 6 23 0 0 0
  96172. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96173. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96174. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96175. +0 0 0 0 0 0 0 0 0
  96176. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96177. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96178. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96179. +0 0 0 0 0 0 0 0 0 0 0 0 23 2 8 166 15 58
  96180. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96181. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96182. +188 17 66 188 17 66 189 17 66 176 16 62 45 4 16 0 0 0
  96183. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96184. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96185. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96186. +0 0 0 0 0 0 0 0 0
  96187. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96188. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96189. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96190. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 83 8 29
  96191. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96192. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96193. +188 17 66 189 17 66 185 17 65 95 9 33 3 0 1 0 0 0
  96194. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96195. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96196. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96197. +0 0 0 0 0 0 0 0 0
  96198. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96199. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96200. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96201. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  96202. +85 8 30 176 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  96203. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  96204. +191 17 67 180 16 63 95 9 33 7 1 3 0 0 0 0 0 0
  96205. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96206. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96207. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96208. +0 0 0 0 0 0 0 0 0
  96209. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96210. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96211. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96212. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96213. +2 0 1 52 5 18 141 13 49 185 17 65 191 17 67 189 17 67
  96214. +189 17 66 188 17 66 188 17 66 189 17 66 191 17 67 187 17 66
  96215. +146 13 51 56 5 19 4 0 1 0 0 0 0 0 0 0 0 0
  96216. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96217. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96218. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96219. +0 0 0 0 0 0 0 0 0
  96220. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96221. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96222. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96223. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96224. +0 0 0 0 0 0 14 1 5 68 6 24 131 12 46 166 15 58
  96225. +180 16 63 183 17 64 180 16 63 168 15 59 134 12 47 75 7 26
  96226. +17 2 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96227. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96228. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96229. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96230. +0 0 0 0 0 0 0 0 0
  96231. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96232. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96233. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96234. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96235. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 24 2 8
  96236. +44 4 15 52 5 18 45 4 16 26 2 9 6 1 2 0 0 0
  96237. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96238. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96239. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96240. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96241. +0 0 0 0 0 0 0 0 0
  96242. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96243. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96244. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96245. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96246. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96247. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96248. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96249. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96250. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96251. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96252. +0 0 0 0 0 0 0 0 0
  96253. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96254. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96255. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96256. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96257. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96258. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96259. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96260. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96261. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96262. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  96263. +0 0 0 0 0 0 0 0 0
  96264. diff -Nur linux-3.10.37/drivers/video/Makefile linux-rpi/drivers/video/Makefile
  96265. --- linux-3.10.37/drivers/video/Makefile 2014-04-14 15:42:31.000000000 +0200
  96266. +++ linux-rpi/drivers/video/Makefile 2014-04-24 15:35:04.249566623 +0200
  96267. @@ -100,6 +100,7 @@
  96268. obj-$(CONFIG_FB_VOODOO1) += sstfb.o
  96269. obj-$(CONFIG_FB_ARMCLCD) += amba-clcd.o
  96270. obj-$(CONFIG_FB_GOLDFISH) += goldfishfb.o
  96271. +obj-$(CONFIG_FB_BCM2708) += bcm2708_fb.o
  96272. obj-$(CONFIG_FB_68328) += 68328fb.o
  96273. obj-$(CONFIG_FB_GBE) += gbefb.o
  96274. obj-$(CONFIG_FB_CIRRUS) += cirrusfb.o
  96275. diff -Nur linux-3.10.37/drivers/w1/masters/w1-gpio.c linux-rpi/drivers/w1/masters/w1-gpio.c
  96276. --- linux-3.10.37/drivers/w1/masters/w1-gpio.c 2014-04-14 15:42:31.000000000 +0200
  96277. +++ linux-rpi/drivers/w1/masters/w1-gpio.c 2014-04-24 15:35:04.349567736 +0200
  96278. @@ -23,6 +23,9 @@
  96279. #include "../w1.h"
  96280. #include "../w1_int.h"
  96281. +static int w1_gpio_pullup = 0;
  96282. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  96283. +
  96284. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  96285. {
  96286. struct w1_gpio_platform_data *pdata = data;
  96287. @@ -47,6 +50,16 @@
  96288. return gpio_get_value(pdata->pin) ? 1 : 0;
  96289. }
  96290. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  96291. +{
  96292. + struct w1_gpio_platform_data *pdata = data;
  96293. +
  96294. + if (on)
  96295. + gpio_direction_output(pdata->pin, 1);
  96296. + else
  96297. + gpio_direction_input(pdata->pin);
  96298. +}
  96299. +
  96300. #if defined(CONFIG_OF)
  96301. static struct of_device_id w1_gpio_dt_ids[] = {
  96302. { .compatible = "w1-gpio" },
  96303. @@ -133,6 +146,13 @@
  96304. master->write_bit = w1_gpio_write_bit_dir;
  96305. }
  96306. + if (w1_gpio_pullup)
  96307. + if (pdata->is_open_drain)
  96308. + printk(KERN_ERR "w1-gpio 'pullup' option "
  96309. + "doesn't work with open drain GPIO\n");
  96310. + else
  96311. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  96312. +
  96313. err = w1_add_master_device(master);
  96314. if (err) {
  96315. dev_err(&pdev->dev, "w1_add_master device failed\n");
  96316. diff -Nur linux-3.10.37/drivers/w1/w1.h linux-rpi/drivers/w1/w1.h
  96317. --- linux-3.10.37/drivers/w1/w1.h 2014-04-14 15:42:31.000000000 +0200
  96318. +++ linux-rpi/drivers/w1/w1.h 2014-04-24 15:35:04.349567736 +0200
  96319. @@ -148,6 +148,12 @@
  96320. */
  96321. u8 (*set_pullup)(void *, int);
  96322. + /**
  96323. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  96324. + * @return -1=Error, 0=completed
  96325. + */
  96326. + void (*bitbang_pullup) (void *, u8);
  96327. +
  96328. /** Really nice hardware can handles the different types of ROM search
  96329. * w1_master* is passed to the slave found callback.
  96330. */
  96331. diff -Nur linux-3.10.37/drivers/w1/w1_int.c linux-rpi/drivers/w1/w1_int.c
  96332. --- linux-3.10.37/drivers/w1/w1_int.c 2014-04-14 15:42:31.000000000 +0200
  96333. +++ linux-rpi/drivers/w1/w1_int.c 2014-04-24 15:35:04.349567736 +0200
  96334. @@ -117,19 +117,21 @@
  96335. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  96336. return(-EINVAL);
  96337. }
  96338. - /* While it would be electrically possible to make a device that
  96339. - * generated a strong pullup in bit bang mode, only hardware that
  96340. - * controls 1-wire time frames are even expected to support a strong
  96341. - * pullup. w1_io.c would need to support calling set_pullup before
  96342. - * the last write_bit operation of a w1_write_8 which it currently
  96343. - * doesn't.
  96344. - */
  96345. +
  96346. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  96347. + * and takes care of timing itself */
  96348. if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  96349. printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  96350. "write_byte or touch_bit, disabling\n");
  96351. master->set_pullup = NULL;
  96352. }
  96353. + if (master->set_pullup && master->bitbang_pullup) {
  96354. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  96355. + "be set when bitbang_pullup is used, disabling\n");
  96356. + master->set_pullup = NULL;
  96357. + }
  96358. +
  96359. /* Lock until the device is added (or not) to w1_masters. */
  96360. mutex_lock(&w1_mlock);
  96361. /* Search for the first available id (starting at 1). */
  96362. diff -Nur linux-3.10.37/drivers/w1/w1_io.c linux-rpi/drivers/w1/w1_io.c
  96363. --- linux-3.10.37/drivers/w1/w1_io.c 2014-04-14 15:42:31.000000000 +0200
  96364. +++ linux-rpi/drivers/w1/w1_io.c 2014-04-24 15:35:04.349567736 +0200
  96365. @@ -127,10 +127,22 @@
  96366. static void w1_post_write(struct w1_master *dev)
  96367. {
  96368. if (dev->pullup_duration) {
  96369. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  96370. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  96371. - else
  96372. + if (dev->enable_pullup) {
  96373. + if (dev->bus_master->set_pullup) {
  96374. + dev->bus_master->set_pullup(dev->
  96375. + bus_master->data,
  96376. + 0);
  96377. + } else if (dev->bus_master->bitbang_pullup) {
  96378. + dev->bus_master->
  96379. + bitbang_pullup(dev->bus_master->data, 1);
  96380. msleep(dev->pullup_duration);
  96381. + dev->bus_master->
  96382. + bitbang_pullup(dev->bus_master->data, 0);
  96383. + }
  96384. + } else {
  96385. + msleep(dev->pullup_duration);
  96386. + }
  96387. +
  96388. dev->pullup_duration = 0;
  96389. }
  96390. }
  96391. diff -Nur linux-3.10.37/drivers/watchdog/bcm2708_wdog.c linux-rpi/drivers/watchdog/bcm2708_wdog.c
  96392. --- linux-3.10.37/drivers/watchdog/bcm2708_wdog.c 1970-01-01 01:00:00.000000000 +0100
  96393. +++ linux-rpi/drivers/watchdog/bcm2708_wdog.c 2014-04-24 15:35:04.353567781 +0200
  96394. @@ -0,0 +1,385 @@
  96395. +/*
  96396. + * Broadcom BCM2708 watchdog driver.
  96397. + *
  96398. + * (c) Copyright 2010 Broadcom Europe Ltd
  96399. + *
  96400. + * This program is free software; you can redistribute it and/or
  96401. + * modify it under the terms of the GNU General Public License
  96402. + * as published by the Free Software Foundation; either version
  96403. + * 2 of the License, or (at your option) any later version.
  96404. + *
  96405. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  96406. + */
  96407. +
  96408. +#include <linux/interrupt.h>
  96409. +#include <linux/module.h>
  96410. +#include <linux/moduleparam.h>
  96411. +#include <linux/types.h>
  96412. +#include <linux/miscdevice.h>
  96413. +#include <linux/watchdog.h>
  96414. +#include <linux/fs.h>
  96415. +#include <linux/ioport.h>
  96416. +#include <linux/notifier.h>
  96417. +#include <linux/reboot.h>
  96418. +#include <linux/init.h>
  96419. +#include <linux/io.h>
  96420. +#include <linux/uaccess.h>
  96421. +#include <mach/platform.h>
  96422. +
  96423. +#include <asm/system.h>
  96424. +
  96425. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  96426. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  96427. +
  96428. +static unsigned long wdog_is_open;
  96429. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  96430. +static char expect_close;
  96431. +
  96432. +/*
  96433. + * Module parameters
  96434. + */
  96435. +
  96436. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  96437. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  96438. +
  96439. +module_param(heartbeat, int, 0);
  96440. +MODULE_PARM_DESC(heartbeat,
  96441. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  96442. + __MODULE_STRING(WD_TIMO) ")");
  96443. +
  96444. +static int nowayout = WATCHDOG_NOWAYOUT;
  96445. +module_param(nowayout, int, 0);
  96446. +MODULE_PARM_DESC(nowayout,
  96447. + "Watchdog cannot be stopped once started (default="
  96448. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  96449. +
  96450. +static DEFINE_SPINLOCK(wdog_lock);
  96451. +
  96452. +/**
  96453. + * Start the watchdog driver.
  96454. + */
  96455. +
  96456. +static int wdog_start(unsigned long timeout)
  96457. +{
  96458. + uint32_t cur;
  96459. + unsigned long flags;
  96460. + spin_lock_irqsave(&wdog_lock, flags);
  96461. +
  96462. + /* enable the watchdog */
  96463. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  96464. + __io_address(PM_WDOG));
  96465. + cur = ioread32(__io_address(PM_RSTC));
  96466. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  96467. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  96468. +
  96469. + spin_unlock_irqrestore(&wdog_lock, flags);
  96470. + return 0;
  96471. +}
  96472. +
  96473. +/**
  96474. + * Stop the watchdog driver.
  96475. + */
  96476. +
  96477. +static int wdog_stop(void)
  96478. +{
  96479. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  96480. + printk(KERN_INFO "watchdog stopped\n");
  96481. + return 0;
  96482. +}
  96483. +
  96484. +/**
  96485. + * Reload counter one with the watchdog heartbeat. We don't bother
  96486. + * reloading the cascade counter.
  96487. + */
  96488. +
  96489. +static void wdog_ping(void)
  96490. +{
  96491. + wdog_start(wdog_ticks);
  96492. +}
  96493. +
  96494. +/**
  96495. + * @t: the new heartbeat value that needs to be set.
  96496. + *
  96497. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  96498. + * value is incorrect we keep the old value and return -EINVAL. If
  96499. + * successful we return 0.
  96500. + */
  96501. +
  96502. +static int wdog_set_heartbeat(int t)
  96503. +{
  96504. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  96505. + return -EINVAL;
  96506. +
  96507. + heartbeat = t;
  96508. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  96509. + return 0;
  96510. +}
  96511. +
  96512. +/**
  96513. + * @file: file handle to the watchdog
  96514. + * @buf: buffer to write (unused as data does not matter here
  96515. + * @count: count of bytes
  96516. + * @ppos: pointer to the position to write. No seeks allowed
  96517. + *
  96518. + * A write to a watchdog device is defined as a keepalive signal.
  96519. + *
  96520. + * if 'nowayout' is set then normally a close() is ignored. But
  96521. + * if you write 'V' first then the close() will stop the timer.
  96522. + */
  96523. +
  96524. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  96525. + size_t count, loff_t *ppos)
  96526. +{
  96527. + if (count) {
  96528. + if (!nowayout) {
  96529. + size_t i;
  96530. +
  96531. + /* In case it was set long ago */
  96532. + expect_close = 0;
  96533. +
  96534. + for (i = 0; i != count; i++) {
  96535. + char c;
  96536. + if (get_user(c, buf + i))
  96537. + return -EFAULT;
  96538. + if (c == 'V')
  96539. + expect_close = 42;
  96540. + }
  96541. + }
  96542. + wdog_ping();
  96543. + }
  96544. + return count;
  96545. +}
  96546. +
  96547. +static int wdog_get_status(void)
  96548. +{
  96549. + unsigned long flags;
  96550. + int status = 0;
  96551. + spin_lock_irqsave(&wdog_lock, flags);
  96552. + /* FIXME: readback reset reason */
  96553. + spin_unlock_irqrestore(&wdog_lock, flags);
  96554. + return status;
  96555. +}
  96556. +
  96557. +static uint32_t wdog_get_remaining(void)
  96558. +{
  96559. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  96560. + return ret & PM_WDOG_TIME_SET;
  96561. +}
  96562. +
  96563. +/**
  96564. + * @file: file handle to the device
  96565. + * @cmd: watchdog command
  96566. + * @arg: argument pointer
  96567. + *
  96568. + * The watchdog API defines a common set of functions for all watchdogs
  96569. + * according to their available features. We only actually usefully support
  96570. + * querying capabilities and current status.
  96571. + */
  96572. +
  96573. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  96574. +{
  96575. + void __user *argp = (void __user *)arg;
  96576. + int __user *p = argp;
  96577. + int new_heartbeat;
  96578. + int status;
  96579. + int options;
  96580. + uint32_t remaining;
  96581. +
  96582. + struct watchdog_info ident = {
  96583. + .options = WDIOF_SETTIMEOUT|
  96584. + WDIOF_MAGICCLOSE|
  96585. + WDIOF_KEEPALIVEPING,
  96586. + .firmware_version = 1,
  96587. + .identity = "BCM2708",
  96588. + };
  96589. +
  96590. + switch (cmd) {
  96591. + case WDIOC_GETSUPPORT:
  96592. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  96593. + case WDIOC_GETSTATUS:
  96594. + status = wdog_get_status();
  96595. + return put_user(status, p);
  96596. + case WDIOC_GETBOOTSTATUS:
  96597. + return put_user(0, p);
  96598. + case WDIOC_KEEPALIVE:
  96599. + wdog_ping();
  96600. + return 0;
  96601. + case WDIOC_SETTIMEOUT:
  96602. + if (get_user(new_heartbeat, p))
  96603. + return -EFAULT;
  96604. + if (wdog_set_heartbeat(new_heartbeat))
  96605. + return -EINVAL;
  96606. + wdog_ping();
  96607. + /* Fall */
  96608. + case WDIOC_GETTIMEOUT:
  96609. + return put_user(heartbeat, p);
  96610. + case WDIOC_GETTIMELEFT:
  96611. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  96612. + return put_user(remaining, p);
  96613. + case WDIOC_SETOPTIONS:
  96614. + if (get_user(options, p))
  96615. + return -EFAULT;
  96616. + if (options & WDIOS_DISABLECARD)
  96617. + wdog_stop();
  96618. + if (options & WDIOS_ENABLECARD)
  96619. + wdog_start(wdog_ticks);
  96620. + return 0;
  96621. + default:
  96622. + return -ENOTTY;
  96623. + }
  96624. +}
  96625. +
  96626. +/**
  96627. + * @inode: inode of device
  96628. + * @file: file handle to device
  96629. + *
  96630. + * The watchdog device has been opened. The watchdog device is single
  96631. + * open and on opening we load the counters.
  96632. + */
  96633. +
  96634. +static int wdog_open(struct inode *inode, struct file *file)
  96635. +{
  96636. + if (test_and_set_bit(0, &wdog_is_open))
  96637. + return -EBUSY;
  96638. + /*
  96639. + * Activate
  96640. + */
  96641. + wdog_start(wdog_ticks);
  96642. + return nonseekable_open(inode, file);
  96643. +}
  96644. +
  96645. +/**
  96646. + * @inode: inode to board
  96647. + * @file: file handle to board
  96648. + *
  96649. + * The watchdog has a configurable API. There is a religious dispute
  96650. + * between people who want their watchdog to be able to shut down and
  96651. + * those who want to be sure if the watchdog manager dies the machine
  96652. + * reboots. In the former case we disable the counters, in the latter
  96653. + * case you have to open it again very soon.
  96654. + */
  96655. +
  96656. +static int wdog_release(struct inode *inode, struct file *file)
  96657. +{
  96658. + if (expect_close == 42) {
  96659. + wdog_stop();
  96660. + } else {
  96661. + printk(KERN_CRIT
  96662. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  96663. + wdog_ping();
  96664. + }
  96665. + clear_bit(0, &wdog_is_open);
  96666. + expect_close = 0;
  96667. + return 0;
  96668. +}
  96669. +
  96670. +/**
  96671. + * @this: our notifier block
  96672. + * @code: the event being reported
  96673. + * @unused: unused
  96674. + *
  96675. + * Our notifier is called on system shutdowns. Turn the watchdog
  96676. + * off so that it does not fire during the next reboot.
  96677. + */
  96678. +
  96679. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  96680. + void *unused)
  96681. +{
  96682. + if (code == SYS_DOWN || code == SYS_HALT)
  96683. + wdog_stop();
  96684. + return NOTIFY_DONE;
  96685. +}
  96686. +
  96687. +/*
  96688. + * Kernel Interfaces
  96689. + */
  96690. +
  96691. +
  96692. +static const struct file_operations wdog_fops = {
  96693. + .owner = THIS_MODULE,
  96694. + .llseek = no_llseek,
  96695. + .write = wdog_write,
  96696. + .unlocked_ioctl = wdog_ioctl,
  96697. + .open = wdog_open,
  96698. + .release = wdog_release,
  96699. +};
  96700. +
  96701. +static struct miscdevice wdog_miscdev = {
  96702. + .minor = WATCHDOG_MINOR,
  96703. + .name = "watchdog",
  96704. + .fops = &wdog_fops,
  96705. +};
  96706. +
  96707. +/*
  96708. + * The WDT card needs to learn about soft shutdowns in order to
  96709. + * turn the timebomb registers off.
  96710. + */
  96711. +
  96712. +static struct notifier_block wdog_notifier = {
  96713. + .notifier_call = wdog_notify_sys,
  96714. +};
  96715. +
  96716. +/**
  96717. + * cleanup_module:
  96718. + *
  96719. + * Unload the watchdog. You cannot do this with any file handles open.
  96720. + * If your watchdog is set to continue ticking on close and you unload
  96721. + * it, well it keeps ticking. We won't get the interrupt but the board
  96722. + * will not touch PC memory so all is fine. You just have to load a new
  96723. + * module in 60 seconds or reboot.
  96724. + */
  96725. +
  96726. +static void __exit wdog_exit(void)
  96727. +{
  96728. + misc_deregister(&wdog_miscdev);
  96729. + unregister_reboot_notifier(&wdog_notifier);
  96730. +}
  96731. +
  96732. +static int __init wdog_init(void)
  96733. +{
  96734. + int ret;
  96735. +
  96736. + /* Check that the heartbeat value is within it's range;
  96737. + if not reset to the default */
  96738. + if (wdog_set_heartbeat(heartbeat)) {
  96739. + wdog_set_heartbeat(WD_TIMO);
  96740. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  96741. + "0 < heartbeat < %d, using %d\n",
  96742. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  96743. + WD_TIMO);
  96744. + }
  96745. +
  96746. + ret = register_reboot_notifier(&wdog_notifier);
  96747. + if (ret) {
  96748. + printk(KERN_ERR
  96749. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  96750. + goto out_reboot;
  96751. + }
  96752. +
  96753. + ret = misc_register(&wdog_miscdev);
  96754. + if (ret) {
  96755. + printk(KERN_ERR
  96756. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  96757. + WATCHDOG_MINOR, ret);
  96758. + goto out_misc;
  96759. + }
  96760. +
  96761. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  96762. + heartbeat, nowayout);
  96763. + return 0;
  96764. +
  96765. +out_misc:
  96766. + unregister_reboot_notifier(&wdog_notifier);
  96767. +out_reboot:
  96768. + return ret;
  96769. +}
  96770. +
  96771. +module_init(wdog_init);
  96772. +module_exit(wdog_exit);
  96773. +
  96774. +MODULE_AUTHOR("Luke Diamand");
  96775. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  96776. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  96777. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  96778. +MODULE_LICENSE("GPL");
  96779. +
  96780. diff -Nur linux-3.10.37/drivers/watchdog/Kconfig linux-rpi/drivers/watchdog/Kconfig
  96781. --- linux-3.10.37/drivers/watchdog/Kconfig 2014-04-14 15:42:31.000000000 +0200
  96782. +++ linux-rpi/drivers/watchdog/Kconfig 2014-04-24 15:35:04.349567736 +0200
  96783. @@ -391,6 +391,12 @@
  96784. To compile this driver as a module, choose M here: the
  96785. module will be called retu_wdt.
  96786. +config BCM2708_WDT
  96787. + tristate "BCM2708 Watchdog"
  96788. + depends on ARCH_BCM2708
  96789. + help
  96790. + Enables BCM2708 watchdog support.
  96791. +
  96792. # AVR32 Architecture
  96793. config AT32AP700X_WDT
  96794. diff -Nur linux-3.10.37/drivers/watchdog/Makefile linux-rpi/drivers/watchdog/Makefile
  96795. --- linux-3.10.37/drivers/watchdog/Makefile 2014-04-14 15:42:31.000000000 +0200
  96796. +++ linux-rpi/drivers/watchdog/Makefile 2014-04-24 15:35:04.349567736 +0200
  96797. @@ -54,6 +54,7 @@
  96798. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  96799. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  96800. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  96801. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  96802. # AVR32 Architecture
  96803. obj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o
  96804. diff -Nur linux-3.10.37/include/linux/broadcom/vc_cma.h linux-rpi/include/linux/broadcom/vc_cma.h
  96805. --- linux-3.10.37/include/linux/broadcom/vc_cma.h 1970-01-01 01:00:00.000000000 +0100
  96806. +++ linux-rpi/include/linux/broadcom/vc_cma.h 2014-04-24 15:35:04.729571969 +0200
  96807. @@ -0,0 +1,30 @@
  96808. +/*****************************************************************************
  96809. +* Copyright 2012 Broadcom Corporation. All rights reserved.
  96810. +*
  96811. +* Unless you and Broadcom execute a separate written software license
  96812. +* agreement governing use of this software, this software is licensed to you
  96813. +* under the terms of the GNU General Public License version 2, available at
  96814. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96815. +*
  96816. +* Notwithstanding the above, under no circumstances may you combine this
  96817. +* software in any way with any other Broadcom software provided under a
  96818. +* license other than the GPL, without Broadcom's express prior written
  96819. +* consent.
  96820. +*****************************************************************************/
  96821. +
  96822. +#if !defined( VC_CMA_H )
  96823. +#define VC_CMA_H
  96824. +
  96825. +#include <linux/ioctl.h>
  96826. +
  96827. +#define VC_CMA_IOC_MAGIC 0xc5
  96828. +
  96829. +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
  96830. +
  96831. +#ifdef __KERNEL__
  96832. +extern void __init vc_cma_early_init(void);
  96833. +extern void __init vc_cma_reserve(void);
  96834. +#endif
  96835. +
  96836. +#endif /* VC_CMA_H */
  96837. +
  96838. diff -Nur linux-3.10.37/include/linux/mmc/host.h linux-rpi/include/linux/mmc/host.h
  96839. --- linux-3.10.37/include/linux/mmc/host.h 2014-04-14 15:42:31.000000000 +0200
  96840. +++ linux-rpi/include/linux/mmc/host.h 2014-04-24 15:35:04.797572727 +0200
  96841. @@ -281,6 +281,7 @@
  96842. #define MMC_CAP2_PACKED_CMD (MMC_CAP2_PACKED_RD | \
  96843. MMC_CAP2_PACKED_WR)
  96844. #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14) /* Don't power up before scan */
  96845. +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
  96846. mmc_pm_flag_t pm_caps; /* supported pm features */
  96847. diff -Nur linux-3.10.37/include/linux/mmc/sdhci.h linux-rpi/include/linux/mmc/sdhci.h
  96848. --- linux-3.10.37/include/linux/mmc/sdhci.h 2014-04-14 15:42:31.000000000 +0200
  96849. +++ linux-rpi/include/linux/mmc/sdhci.h 2014-04-24 15:35:04.797572727 +0200
  96850. @@ -97,6 +97,7 @@
  96851. #define SDHCI_QUIRK2_PRESET_VALUE_BROKEN (1<<3)
  96852. int irq; /* Device IRQ */
  96853. + int second_irq; /* Additional IRQ to disable/enable in low-latency mode */
  96854. void __iomem *ioaddr; /* Mapped address */
  96855. const struct sdhci_ops *ops; /* Low level hw interface */
  96856. @@ -128,6 +129,7 @@
  96857. #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  96858. #define SDHCI_HS200_NEEDS_TUNING (1<<10) /* HS200 needs tuning */
  96859. #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
  96860. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  96861. unsigned int version; /* SDHCI spec. version */
  96862. @@ -142,6 +144,7 @@
  96863. struct mmc_request *mrq; /* Current request */
  96864. struct mmc_command *cmd; /* Current command */
  96865. + int last_cmdop; /* Opcode of last cmd sent */
  96866. struct mmc_data *data; /* Current data request */
  96867. unsigned int data_early:1; /* Data finished before cmd */
  96868. diff -Nur linux-3.10.37/include/sound/soc-dai.h linux-rpi/include/sound/soc-dai.h
  96869. --- linux-3.10.37/include/sound/soc-dai.h 2014-04-14 15:42:31.000000000 +0200
  96870. +++ linux-rpi/include/sound/soc-dai.h 2014-04-24 15:35:04.921574108 +0200
  96871. @@ -105,6 +105,8 @@
  96872. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  96873. int pll_id, int source, unsigned int freq_in, unsigned int freq_out);
  96874. +int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio);
  96875. +
  96876. /* Digital Audio interface formatting */
  96877. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt);
  96878. @@ -131,6 +133,7 @@
  96879. int (*set_pll)(struct snd_soc_dai *dai, int pll_id, int source,
  96880. unsigned int freq_in, unsigned int freq_out);
  96881. int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);
  96882. + int (*set_bclk_ratio)(struct snd_soc_dai *dai, unsigned int ratio);
  96883. /*
  96884. * DAI format configuration
  96885. diff -Nur linux-3.10.37/include/uapi/linux/fb.h linux-rpi/include/uapi/linux/fb.h
  96886. --- linux-3.10.37/include/uapi/linux/fb.h 2014-04-14 15:42:31.000000000 +0200
  96887. +++ linux-rpi/include/uapi/linux/fb.h 2014-04-24 15:35:04.941574330 +0200
  96888. @@ -34,6 +34,11 @@
  96889. #define FBIOPUT_MODEINFO 0x4617
  96890. #define FBIOGET_DISPINFO 0x4618
  96891. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  96892. +/*
  96893. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  96894. + * be concurrently added to the mainline kernel
  96895. + */
  96896. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  96897. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  96898. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  96899. diff -Nur linux-3.10.37/kernel/cgroup.c linux-rpi/kernel/cgroup.c
  96900. --- linux-3.10.37/kernel/cgroup.c 2014-04-14 15:42:31.000000000 +0200
  96901. +++ linux-rpi/kernel/cgroup.c 2014-04-24 15:35:05.001574999 +0200
  96902. @@ -5127,6 +5127,37 @@
  96903. }
  96904. __setup("cgroup_disable=", cgroup_disable);
  96905. +static int __init cgroup_enable(char *str)
  96906. +{
  96907. + int i;
  96908. + char *token;
  96909. +
  96910. + while ((token = strsep(&str, ",")) != NULL) {
  96911. + if (!*token)
  96912. + continue;
  96913. + for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
  96914. + struct cgroup_subsys *ss = subsys[i];
  96915. +
  96916. + /*
  96917. + * cgroup_enable, being at boot time, can't
  96918. + * know about module subsystems, so we don't
  96919. + * worry about them.
  96920. + */
  96921. + if (!ss || ss->module)
  96922. + continue;
  96923. +
  96924. + if (!strcmp(token, ss->name)) {
  96925. + ss->disabled = 0;
  96926. + printk(KERN_INFO "Enabling %s control group"
  96927. + " subsystem\n", ss->name);
  96928. + break;
  96929. + }
  96930. + }
  96931. + }
  96932. + return 1;
  96933. +}
  96934. +__setup("cgroup_enable=", cgroup_enable);
  96935. +
  96936. /*
  96937. * Functons for CSS ID.
  96938. */
  96939. diff -Nur linux-3.10.37/mm/memcontrol.c linux-rpi/mm/memcontrol.c
  96940. --- linux-3.10.37/mm/memcontrol.c 2014-04-14 15:42:31.000000000 +0200
  96941. +++ linux-rpi/mm/memcontrol.c 2014-04-24 15:35:05.077575846 +0200
  96942. @@ -6970,6 +6970,7 @@
  96943. .base_cftypes = mem_cgroup_files,
  96944. .early_init = 0,
  96945. .use_id = 1,
  96946. + .disabled = 1,
  96947. };
  96948. #ifdef CONFIG_MEMCG_SWAP
  96949. diff -Nur linux-3.10.37/sound/arm/bcm2835.c linux-rpi/sound/arm/bcm2835.c
  96950. --- linux-3.10.37/sound/arm/bcm2835.c 1970-01-01 01:00:00.000000000 +0100
  96951. +++ linux-rpi/sound/arm/bcm2835.c 2014-04-24 15:35:05.361579008 +0200
  96952. @@ -0,0 +1,413 @@
  96953. +/*****************************************************************************
  96954. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  96955. +*
  96956. +* Unless you and Broadcom execute a separate written software license
  96957. +* agreement governing use of this software, this software is licensed to you
  96958. +* under the terms of the GNU General Public License version 2, available at
  96959. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96960. +*
  96961. +* Notwithstanding the above, under no circumstances may you combine this
  96962. +* software in any way with any other Broadcom software provided under a
  96963. +* license other than the GPL, without Broadcom's express prior written
  96964. +* consent.
  96965. +*****************************************************************************/
  96966. +
  96967. +#include <linux/platform_device.h>
  96968. +
  96969. +#include <linux/init.h>
  96970. +#include <linux/slab.h>
  96971. +#include <linux/module.h>
  96972. +
  96973. +#include "bcm2835.h"
  96974. +
  96975. +/* module parameters (see "Module Parameters") */
  96976. +/* SNDRV_CARDS: maximum number of cards supported by this module */
  96977. +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
  96978. +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
  96979. +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
  96980. +
  96981. +/* HACKY global pointers needed for successive probes to work : ssp
  96982. + * But compared against the changes we will have to do in VC audio_ipc code
  96983. + * to export 8 audio_ipc devices as a single IPC device and then monitor all
  96984. + * four devices in a thread, this gets things done quickly and should be easier
  96985. + * to debug if we run into issues
  96986. + */
  96987. +
  96988. +static struct snd_card *g_card = NULL;
  96989. +static bcm2835_chip_t *g_chip = NULL;
  96990. +
  96991. +static int snd_bcm2835_free(bcm2835_chip_t * chip)
  96992. +{
  96993. + kfree(chip);
  96994. + return 0;
  96995. +}
  96996. +
  96997. +/* component-destructor
  96998. + * (see "Management of Cards and Components")
  96999. + */
  97000. +static int snd_bcm2835_dev_free(struct snd_device *device)
  97001. +{
  97002. + return snd_bcm2835_free(device->device_data);
  97003. +}
  97004. +
  97005. +/* chip-specific constructor
  97006. + * (see "Management of Cards and Components")
  97007. + */
  97008. +static int snd_bcm2835_create(struct snd_card *card,
  97009. + struct platform_device *pdev,
  97010. + bcm2835_chip_t ** rchip)
  97011. +{
  97012. + bcm2835_chip_t *chip;
  97013. + int err;
  97014. + static struct snd_device_ops ops = {
  97015. + .dev_free = snd_bcm2835_dev_free,
  97016. + };
  97017. +
  97018. + *rchip = NULL;
  97019. +
  97020. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  97021. + if (chip == NULL)
  97022. + return -ENOMEM;
  97023. +
  97024. + chip->card = card;
  97025. +
  97026. + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  97027. + if (err < 0) {
  97028. + snd_bcm2835_free(chip);
  97029. + return err;
  97030. + }
  97031. +
  97032. + *rchip = chip;
  97033. + return 0;
  97034. +}
  97035. +
  97036. +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
  97037. +{
  97038. + static int dev;
  97039. + bcm2835_chip_t *chip;
  97040. + struct snd_card *card;
  97041. + int err;
  97042. +
  97043. + if (dev >= MAX_SUBSTREAMS)
  97044. + return -ENODEV;
  97045. +
  97046. + if (!enable[dev]) {
  97047. + dev++;
  97048. + return -ENOENT;
  97049. + }
  97050. +
  97051. + if (dev > 0)
  97052. + goto add_register_map;
  97053. +
  97054. + err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &g_card);
  97055. + if (err < 0)
  97056. + goto out;
  97057. +
  97058. + snd_card_set_dev(g_card, &pdev->dev);
  97059. + strcpy(g_card->driver, "BRCM bcm2835 ALSA Driver");
  97060. + strcpy(g_card->shortname, "bcm2835 ALSA");
  97061. + sprintf(g_card->longname, "%s", g_card->shortname);
  97062. +
  97063. + err = snd_bcm2835_create(g_card, pdev, &chip);
  97064. + if (err < 0) {
  97065. + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
  97066. + goto out_bcm2835_create;
  97067. + }
  97068. +
  97069. + g_chip = chip;
  97070. + err = snd_bcm2835_new_pcm(chip);
  97071. + if (err < 0) {
  97072. + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
  97073. + goto out_bcm2835_new_pcm;
  97074. + }
  97075. +
  97076. + err = snd_bcm2835_new_ctl(chip);
  97077. + if (err < 0) {
  97078. + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
  97079. + goto out_bcm2835_new_ctl;
  97080. + }
  97081. +
  97082. +add_register_map:
  97083. + card = g_card;
  97084. + chip = g_chip;
  97085. +
  97086. + BUG_ON(!(card && chip));
  97087. +
  97088. + chip->avail_substreams |= (1 << dev);
  97089. + chip->pdev[dev] = pdev;
  97090. +
  97091. + if (dev == 0) {
  97092. + err = snd_card_register(card);
  97093. + if (err < 0) {
  97094. + dev_err(&pdev->dev,
  97095. + "Failed to register bcm2835 ALSA card \n");
  97096. + goto out_card_register;
  97097. + }
  97098. + platform_set_drvdata(pdev, card);
  97099. + audio_info("bcm2835 ALSA card created!\n");
  97100. + } else {
  97101. + audio_info("bcm2835 ALSA chip created!\n");
  97102. + platform_set_drvdata(pdev, (void *)dev);
  97103. + }
  97104. +
  97105. + dev++;
  97106. +
  97107. + return 0;
  97108. +
  97109. +out_card_register:
  97110. +out_bcm2835_new_ctl:
  97111. +out_bcm2835_new_pcm:
  97112. +out_bcm2835_create:
  97113. + BUG_ON(!g_card);
  97114. + if (snd_card_free(g_card))
  97115. + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
  97116. + g_card = NULL;
  97117. +out:
  97118. + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
  97119. + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
  97120. + return err;
  97121. +}
  97122. +
  97123. +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
  97124. +{
  97125. + uint32_t idx;
  97126. + void *drv_data;
  97127. +
  97128. + drv_data = platform_get_drvdata(pdev);
  97129. +
  97130. + if (drv_data == (void *)g_card) {
  97131. + /* This is the card device */
  97132. + snd_card_free((struct snd_card *)drv_data);
  97133. + g_card = NULL;
  97134. + g_chip = NULL;
  97135. + } else {
  97136. + idx = (uint32_t) drv_data;
  97137. + if (g_card != NULL) {
  97138. + BUG_ON(!g_chip);
  97139. + /* We pass chip device numbers in audio ipc devices
  97140. + * other than the one we registered our card with
  97141. + */
  97142. + idx = (uint32_t) drv_data;
  97143. + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
  97144. + g_chip->avail_substreams &= ~(1 << idx);
  97145. + /* There should be atleast one substream registered
  97146. + * after we are done here, as it wil be removed when
  97147. + * the *remove* is called for the card device
  97148. + */
  97149. + BUG_ON(!g_chip->avail_substreams);
  97150. + }
  97151. + }
  97152. +
  97153. + platform_set_drvdata(pdev, NULL);
  97154. +
  97155. + return 0;
  97156. +}
  97157. +
  97158. +#ifdef CONFIG_PM
  97159. +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
  97160. + pm_message_t state)
  97161. +{
  97162. + return 0;
  97163. +}
  97164. +
  97165. +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
  97166. +{
  97167. + return 0;
  97168. +}
  97169. +
  97170. +#endif
  97171. +
  97172. +static struct platform_driver bcm2835_alsa0_driver = {
  97173. + .probe = snd_bcm2835_alsa_probe,
  97174. + .remove = snd_bcm2835_alsa_remove,
  97175. +#ifdef CONFIG_PM
  97176. + .suspend = snd_bcm2835_alsa_suspend,
  97177. + .resume = snd_bcm2835_alsa_resume,
  97178. +#endif
  97179. + .driver = {
  97180. + .name = "bcm2835_AUD0",
  97181. + .owner = THIS_MODULE,
  97182. + },
  97183. +};
  97184. +
  97185. +static struct platform_driver bcm2835_alsa1_driver = {
  97186. + .probe = snd_bcm2835_alsa_probe,
  97187. + .remove = snd_bcm2835_alsa_remove,
  97188. +#ifdef CONFIG_PM
  97189. + .suspend = snd_bcm2835_alsa_suspend,
  97190. + .resume = snd_bcm2835_alsa_resume,
  97191. +#endif
  97192. + .driver = {
  97193. + .name = "bcm2835_AUD1",
  97194. + .owner = THIS_MODULE,
  97195. + },
  97196. +};
  97197. +
  97198. +static struct platform_driver bcm2835_alsa2_driver = {
  97199. + .probe = snd_bcm2835_alsa_probe,
  97200. + .remove = snd_bcm2835_alsa_remove,
  97201. +#ifdef CONFIG_PM
  97202. + .suspend = snd_bcm2835_alsa_suspend,
  97203. + .resume = snd_bcm2835_alsa_resume,
  97204. +#endif
  97205. + .driver = {
  97206. + .name = "bcm2835_AUD2",
  97207. + .owner = THIS_MODULE,
  97208. + },
  97209. +};
  97210. +
  97211. +static struct platform_driver bcm2835_alsa3_driver = {
  97212. + .probe = snd_bcm2835_alsa_probe,
  97213. + .remove = snd_bcm2835_alsa_remove,
  97214. +#ifdef CONFIG_PM
  97215. + .suspend = snd_bcm2835_alsa_suspend,
  97216. + .resume = snd_bcm2835_alsa_resume,
  97217. +#endif
  97218. + .driver = {
  97219. + .name = "bcm2835_AUD3",
  97220. + .owner = THIS_MODULE,
  97221. + },
  97222. +};
  97223. +
  97224. +static struct platform_driver bcm2835_alsa4_driver = {
  97225. + .probe = snd_bcm2835_alsa_probe,
  97226. + .remove = snd_bcm2835_alsa_remove,
  97227. +#ifdef CONFIG_PM
  97228. + .suspend = snd_bcm2835_alsa_suspend,
  97229. + .resume = snd_bcm2835_alsa_resume,
  97230. +#endif
  97231. + .driver = {
  97232. + .name = "bcm2835_AUD4",
  97233. + .owner = THIS_MODULE,
  97234. + },
  97235. +};
  97236. +
  97237. +static struct platform_driver bcm2835_alsa5_driver = {
  97238. + .probe = snd_bcm2835_alsa_probe,
  97239. + .remove = snd_bcm2835_alsa_remove,
  97240. +#ifdef CONFIG_PM
  97241. + .suspend = snd_bcm2835_alsa_suspend,
  97242. + .resume = snd_bcm2835_alsa_resume,
  97243. +#endif
  97244. + .driver = {
  97245. + .name = "bcm2835_AUD5",
  97246. + .owner = THIS_MODULE,
  97247. + },
  97248. +};
  97249. +
  97250. +static struct platform_driver bcm2835_alsa6_driver = {
  97251. + .probe = snd_bcm2835_alsa_probe,
  97252. + .remove = snd_bcm2835_alsa_remove,
  97253. +#ifdef CONFIG_PM
  97254. + .suspend = snd_bcm2835_alsa_suspend,
  97255. + .resume = snd_bcm2835_alsa_resume,
  97256. +#endif
  97257. + .driver = {
  97258. + .name = "bcm2835_AUD6",
  97259. + .owner = THIS_MODULE,
  97260. + },
  97261. +};
  97262. +
  97263. +static struct platform_driver bcm2835_alsa7_driver = {
  97264. + .probe = snd_bcm2835_alsa_probe,
  97265. + .remove = snd_bcm2835_alsa_remove,
  97266. +#ifdef CONFIG_PM
  97267. + .suspend = snd_bcm2835_alsa_suspend,
  97268. + .resume = snd_bcm2835_alsa_resume,
  97269. +#endif
  97270. + .driver = {
  97271. + .name = "bcm2835_AUD7",
  97272. + .owner = THIS_MODULE,
  97273. + },
  97274. +};
  97275. +
  97276. +static int bcm2835_alsa_device_init(void)
  97277. +{
  97278. + int err;
  97279. + err = platform_driver_register(&bcm2835_alsa0_driver);
  97280. + if (err) {
  97281. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97282. + goto out;
  97283. + }
  97284. +
  97285. + err = platform_driver_register(&bcm2835_alsa1_driver);
  97286. + if (err) {
  97287. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97288. + goto unregister_0;
  97289. + }
  97290. +
  97291. + err = platform_driver_register(&bcm2835_alsa2_driver);
  97292. + if (err) {
  97293. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97294. + goto unregister_1;
  97295. + }
  97296. +
  97297. + err = platform_driver_register(&bcm2835_alsa3_driver);
  97298. + if (err) {
  97299. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97300. + goto unregister_2;
  97301. + }
  97302. +
  97303. + err = platform_driver_register(&bcm2835_alsa4_driver);
  97304. + if (err) {
  97305. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97306. + goto unregister_3;
  97307. + }
  97308. +
  97309. + err = platform_driver_register(&bcm2835_alsa5_driver);
  97310. + if (err) {
  97311. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97312. + goto unregister_4;
  97313. + }
  97314. +
  97315. + err = platform_driver_register(&bcm2835_alsa6_driver);
  97316. + if (err) {
  97317. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97318. + goto unregister_5;
  97319. + }
  97320. +
  97321. + err = platform_driver_register(&bcm2835_alsa7_driver);
  97322. + if (err) {
  97323. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  97324. + goto unregister_6;
  97325. + }
  97326. +
  97327. + return 0;
  97328. +
  97329. +unregister_6:
  97330. + platform_driver_unregister(&bcm2835_alsa6_driver);
  97331. +unregister_5:
  97332. + platform_driver_unregister(&bcm2835_alsa5_driver);
  97333. +unregister_4:
  97334. + platform_driver_unregister(&bcm2835_alsa4_driver);
  97335. +unregister_3:
  97336. + platform_driver_unregister(&bcm2835_alsa3_driver);
  97337. +unregister_2:
  97338. + platform_driver_unregister(&bcm2835_alsa2_driver);
  97339. +unregister_1:
  97340. + platform_driver_unregister(&bcm2835_alsa1_driver);
  97341. +unregister_0:
  97342. + platform_driver_unregister(&bcm2835_alsa0_driver);
  97343. +out:
  97344. + return err;
  97345. +}
  97346. +
  97347. +static void bcm2835_alsa_device_exit(void)
  97348. +{
  97349. + platform_driver_unregister(&bcm2835_alsa0_driver);
  97350. + platform_driver_unregister(&bcm2835_alsa1_driver);
  97351. + platform_driver_unregister(&bcm2835_alsa2_driver);
  97352. + platform_driver_unregister(&bcm2835_alsa3_driver);
  97353. + platform_driver_unregister(&bcm2835_alsa4_driver);
  97354. + platform_driver_unregister(&bcm2835_alsa5_driver);
  97355. + platform_driver_unregister(&bcm2835_alsa6_driver);
  97356. + platform_driver_unregister(&bcm2835_alsa7_driver);
  97357. +}
  97358. +
  97359. +late_initcall(bcm2835_alsa_device_init);
  97360. +module_exit(bcm2835_alsa_device_exit);
  97361. +
  97362. +MODULE_AUTHOR("Dom Cobley");
  97363. +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
  97364. +MODULE_LICENSE("GPL");
  97365. +MODULE_ALIAS("platform:bcm2835_alsa");
  97366. diff -Nur linux-3.10.37/sound/arm/bcm2835-ctl.c linux-rpi/sound/arm/bcm2835-ctl.c
  97367. --- linux-3.10.37/sound/arm/bcm2835-ctl.c 1970-01-01 01:00:00.000000000 +0100
  97368. +++ linux-rpi/sound/arm/bcm2835-ctl.c 2014-04-24 15:35:05.361579008 +0200
  97369. @@ -0,0 +1,200 @@
  97370. +/*****************************************************************************
  97371. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97372. +*
  97373. +* Unless you and Broadcom execute a separate written software license
  97374. +* agreement governing use of this software, this software is licensed to you
  97375. +* under the terms of the GNU General Public License version 2, available at
  97376. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97377. +*
  97378. +* Notwithstanding the above, under no circumstances may you combine this
  97379. +* software in any way with any other Broadcom software provided under a
  97380. +* license other than the GPL, without Broadcom's express prior written
  97381. +* consent.
  97382. +*****************************************************************************/
  97383. +
  97384. +#include <linux/platform_device.h>
  97385. +#include <linux/init.h>
  97386. +#include <linux/io.h>
  97387. +#include <linux/jiffies.h>
  97388. +#include <linux/slab.h>
  97389. +#include <linux/time.h>
  97390. +#include <linux/wait.h>
  97391. +#include <linux/delay.h>
  97392. +#include <linux/moduleparam.h>
  97393. +#include <linux/sched.h>
  97394. +
  97395. +#include <sound/core.h>
  97396. +#include <sound/control.h>
  97397. +#include <sound/pcm.h>
  97398. +#include <sound/pcm_params.h>
  97399. +#include <sound/rawmidi.h>
  97400. +#include <sound/initval.h>
  97401. +#include <sound/tlv.h>
  97402. +
  97403. +#include "bcm2835.h"
  97404. +
  97405. +/* volume maximum and minimum in terms of 0.01dB */
  97406. +#define CTRL_VOL_MAX 400
  97407. +#define CTRL_VOL_MIN -10239 /* originally -10240 */
  97408. +
  97409. +
  97410. +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
  97411. + struct snd_ctl_elem_info *uinfo)
  97412. +{
  97413. + audio_info(" ... IN\n");
  97414. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  97415. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  97416. + uinfo->count = 1;
  97417. + uinfo->value.integer.min = CTRL_VOL_MIN;
  97418. + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
  97419. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  97420. + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  97421. + uinfo->count = 1;
  97422. + uinfo->value.integer.min = 0;
  97423. + uinfo->value.integer.max = 1;
  97424. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  97425. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  97426. + uinfo->count = 1;
  97427. + uinfo->value.integer.min = 0;
  97428. + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
  97429. + }
  97430. + audio_info(" ... OUT\n");
  97431. + return 0;
  97432. +}
  97433. +
  97434. +/* toggles mute on or off depending on the value of nmute, and returns
  97435. + * 1 if the mute value was changed, otherwise 0
  97436. + */
  97437. +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
  97438. +{
  97439. + /* if settings are ok, just return 0 */
  97440. + if(chip->mute == nmute)
  97441. + return 0;
  97442. +
  97443. + /* if the sound is muted then we need to unmute */
  97444. + if(chip->mute == CTRL_VOL_MUTE)
  97445. + {
  97446. + chip->volume = chip->old_volume; /* copy the old volume back */
  97447. + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  97448. + }
  97449. + else /* otherwise we mute */
  97450. + {
  97451. + chip->old_volume = chip->volume;
  97452. + chip->volume = 26214; /* set volume to minimum level AKA mute */
  97453. + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  97454. + }
  97455. +
  97456. + chip->mute = nmute;
  97457. + return 1;
  97458. +}
  97459. +
  97460. +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
  97461. + struct snd_ctl_elem_value *ucontrol)
  97462. +{
  97463. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  97464. +
  97465. + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
  97466. +
  97467. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
  97468. + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
  97469. + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
  97470. + ucontrol->value.integer.value[0] = chip->mute;
  97471. + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
  97472. + ucontrol->value.integer.value[0] = chip->dest;
  97473. +
  97474. + return 0;
  97475. +}
  97476. +
  97477. +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
  97478. + struct snd_ctl_elem_value *ucontrol)
  97479. +{
  97480. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  97481. + int changed = 0;
  97482. +
  97483. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  97484. + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
  97485. + if (chip->mute == CTRL_VOL_MUTE) {
  97486. + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
  97487. + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
  97488. + }
  97489. + if (changed
  97490. + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
  97491. +
  97492. + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
  97493. + changed = 1;
  97494. + }
  97495. +
  97496. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  97497. + /* Now implemented */
  97498. + audio_info(" Mute attempted\n");
  97499. + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
  97500. +
  97501. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  97502. + if (ucontrol->value.integer.value[0] != chip->dest) {
  97503. + chip->dest = ucontrol->value.integer.value[0];
  97504. + changed = 1;
  97505. + }
  97506. + }
  97507. +
  97508. + if (changed) {
  97509. + if (bcm2835_audio_set_ctls(chip))
  97510. + printk(KERN_ERR "Failed to set ALSA controls..\n");
  97511. + }
  97512. +
  97513. + return changed;
  97514. +}
  97515. +
  97516. +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
  97517. +
  97518. +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
  97519. + {
  97520. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  97521. + .name = "PCM Playback Volume",
  97522. + .index = 0,
  97523. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  97524. + .private_value = PCM_PLAYBACK_VOLUME,
  97525. + .info = snd_bcm2835_ctl_info,
  97526. + .get = snd_bcm2835_ctl_get,
  97527. + .put = snd_bcm2835_ctl_put,
  97528. + .count = 1,
  97529. + .tlv = {.p = snd_bcm2835_db_scale}
  97530. + },
  97531. + {
  97532. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  97533. + .name = "PCM Playback Switch",
  97534. + .index = 0,
  97535. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  97536. + .private_value = PCM_PLAYBACK_MUTE,
  97537. + .info = snd_bcm2835_ctl_info,
  97538. + .get = snd_bcm2835_ctl_get,
  97539. + .put = snd_bcm2835_ctl_put,
  97540. + .count = 1,
  97541. + },
  97542. + {
  97543. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  97544. + .name = "PCM Playback Route",
  97545. + .index = 0,
  97546. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  97547. + .private_value = PCM_PLAYBACK_DEVICE,
  97548. + .info = snd_bcm2835_ctl_info,
  97549. + .get = snd_bcm2835_ctl_get,
  97550. + .put = snd_bcm2835_ctl_put,
  97551. + .count = 1,
  97552. + },
  97553. +};
  97554. +
  97555. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
  97556. +{
  97557. + int err;
  97558. + unsigned int idx;
  97559. +
  97560. + strcpy(chip->card->mixername, "Broadcom Mixer");
  97561. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
  97562. + err =
  97563. + snd_ctl_add(chip->card,
  97564. + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
  97565. + if (err < 0)
  97566. + return err;
  97567. + }
  97568. + return 0;
  97569. +}
  97570. diff -Nur linux-3.10.37/sound/arm/bcm2835.h linux-rpi/sound/arm/bcm2835.h
  97571. --- linux-3.10.37/sound/arm/bcm2835.h 1970-01-01 01:00:00.000000000 +0100
  97572. +++ linux-rpi/sound/arm/bcm2835.h 2014-04-24 15:35:05.365579053 +0200
  97573. @@ -0,0 +1,157 @@
  97574. +/*****************************************************************************
  97575. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97576. +*
  97577. +* Unless you and Broadcom execute a separate written software license
  97578. +* agreement governing use of this software, this software is licensed to you
  97579. +* under the terms of the GNU General Public License version 2, available at
  97580. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97581. +*
  97582. +* Notwithstanding the above, under no circumstances may you combine this
  97583. +* software in any way with any other Broadcom software provided under a
  97584. +* license other than the GPL, without Broadcom's express prior written
  97585. +* consent.
  97586. +*****************************************************************************/
  97587. +
  97588. +#ifndef __SOUND_ARM_BCM2835_H
  97589. +#define __SOUND_ARM_BCM2835_H
  97590. +
  97591. +#include <linux/device.h>
  97592. +#include <linux/list.h>
  97593. +#include <linux/interrupt.h>
  97594. +#include <linux/wait.h>
  97595. +#include <sound/core.h>
  97596. +#include <sound/initval.h>
  97597. +#include <sound/pcm.h>
  97598. +#include <sound/pcm_params.h>
  97599. +#include <sound/pcm-indirect.h>
  97600. +#include <linux/workqueue.h>
  97601. +
  97602. +/*
  97603. +#define AUDIO_DEBUG_ENABLE
  97604. +#define AUDIO_VERBOSE_DEBUG_ENABLE
  97605. +*/
  97606. +
  97607. +/* Debug macros */
  97608. +
  97609. +#ifdef AUDIO_DEBUG_ENABLE
  97610. +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
  97611. +
  97612. +#define audio_debug(fmt, arg...) \
  97613. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  97614. +
  97615. +#define audio_info(fmt, arg...) \
  97616. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  97617. +
  97618. +#else
  97619. +
  97620. +#define audio_debug(fmt, arg...)
  97621. +
  97622. +#define audio_info(fmt, arg...)
  97623. +
  97624. +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
  97625. +
  97626. +#else
  97627. +
  97628. +#define audio_debug(fmt, arg...)
  97629. +
  97630. +#define audio_info(fmt, arg...)
  97631. +
  97632. +#endif /* AUDIO_DEBUG_ENABLE */
  97633. +
  97634. +#define audio_error(fmt, arg...) \
  97635. + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
  97636. +
  97637. +#define audio_warning(fmt, arg...) \
  97638. + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
  97639. +
  97640. +#define audio_alert(fmt, arg...) \
  97641. + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
  97642. +
  97643. +#define MAX_SUBSTREAMS (8)
  97644. +#define AVAIL_SUBSTREAMS_MASK (0xff)
  97645. +enum {
  97646. + CTRL_VOL_MUTE,
  97647. + CTRL_VOL_UNMUTE
  97648. +};
  97649. +
  97650. +/* macros for alsa2chip and chip2alsa, instead of functions */
  97651. +
  97652. +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
  97653. +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
  97654. +
  97655. +/* Some constants for values .. */
  97656. +typedef enum {
  97657. + AUDIO_DEST_AUTO = 0,
  97658. + AUDIO_DEST_HEADPHONES = 1,
  97659. + AUDIO_DEST_HDMI = 2,
  97660. + AUDIO_DEST_MAX,
  97661. +} SND_BCM2835_ROUTE_T;
  97662. +
  97663. +typedef enum {
  97664. + PCM_PLAYBACK_VOLUME,
  97665. + PCM_PLAYBACK_MUTE,
  97666. + PCM_PLAYBACK_DEVICE,
  97667. +} SND_BCM2835_CTRL_T;
  97668. +
  97669. +/* definition of the chip-specific record */
  97670. +typedef struct bcm2835_chip {
  97671. + struct snd_card *card;
  97672. + struct snd_pcm *pcm;
  97673. + /* Bitmat for valid reg_base and irq numbers */
  97674. + uint32_t avail_substreams;
  97675. + struct platform_device *pdev[MAX_SUBSTREAMS];
  97676. + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
  97677. +
  97678. + int volume;
  97679. + int old_volume; /* stores the volume value whist muted */
  97680. + int dest;
  97681. + int mute;
  97682. +} bcm2835_chip_t;
  97683. +
  97684. +typedef struct bcm2835_alsa_stream {
  97685. + bcm2835_chip_t *chip;
  97686. + struct snd_pcm_substream *substream;
  97687. + struct snd_pcm_indirect pcm_indirect;
  97688. +
  97689. + struct semaphore buffers_update_sem;
  97690. + struct semaphore control_sem;
  97691. + spinlock_t lock;
  97692. + volatile uint32_t control;
  97693. + volatile uint32_t status;
  97694. +
  97695. + int open;
  97696. + int running;
  97697. + int draining;
  97698. +
  97699. + unsigned int pos;
  97700. + unsigned int buffer_size;
  97701. + unsigned int period_size;
  97702. +
  97703. + uint32_t enable_fifo_irq;
  97704. + irq_handler_t fifo_irq_handler;
  97705. +
  97706. + atomic_t retrieved;
  97707. + struct opaque_AUDIO_INSTANCE_T *instance;
  97708. + struct workqueue_struct *my_wq;
  97709. + int idx;
  97710. +} bcm2835_alsa_stream_t;
  97711. +
  97712. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
  97713. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
  97714. +
  97715. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
  97716. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
  97717. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  97718. + uint32_t channels, uint32_t samplerate,
  97719. + uint32_t bps);
  97720. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
  97721. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
  97722. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
  97723. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
  97724. +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
  97725. + void *src);
  97726. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97727. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97728. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97729. +
  97730. +#endif /* __SOUND_ARM_BCM2835_H */
  97731. diff -Nur linux-3.10.37/sound/arm/bcm2835-pcm.c linux-rpi/sound/arm/bcm2835-pcm.c
  97732. --- linux-3.10.37/sound/arm/bcm2835-pcm.c 1970-01-01 01:00:00.000000000 +0100
  97733. +++ linux-rpi/sound/arm/bcm2835-pcm.c 2014-04-24 15:35:05.361579008 +0200
  97734. @@ -0,0 +1,426 @@
  97735. +/*****************************************************************************
  97736. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97737. +*
  97738. +* Unless you and Broadcom execute a separate written software license
  97739. +* agreement governing use of this software, this software is licensed to you
  97740. +* under the terms of the GNU General Public License version 2, available at
  97741. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97742. +*
  97743. +* Notwithstanding the above, under no circumstances may you combine this
  97744. +* software in any way with any other Broadcom software provided under a
  97745. +* license other than the GPL, without Broadcom's express prior written
  97746. +* consent.
  97747. +*****************************************************************************/
  97748. +
  97749. +#include <linux/interrupt.h>
  97750. +#include <linux/slab.h>
  97751. +
  97752. +#include "bcm2835.h"
  97753. +
  97754. +/* hardware definition */
  97755. +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  97756. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  97757. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  97758. + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  97759. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  97760. + .rate_min = 8000,
  97761. + .rate_max = 48000,
  97762. + .channels_min = 1,
  97763. + .channels_max = 2,
  97764. + .buffer_bytes_max = 128 * 1024,
  97765. + .period_bytes_min = 1 * 1024,
  97766. + .period_bytes_max = 128 * 1024,
  97767. + .periods_min = 1,
  97768. + .periods_max = 128,
  97769. +};
  97770. +
  97771. +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
  97772. +{
  97773. + audio_info("Freeing up alsa stream here ..\n");
  97774. + if (runtime->private_data)
  97775. + kfree(runtime->private_data);
  97776. + runtime->private_data = NULL;
  97777. +}
  97778. +
  97779. +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
  97780. +{
  97781. + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
  97782. + uint32_t consumed = 0;
  97783. + int new_period = 0;
  97784. +
  97785. + audio_info(" .. IN\n");
  97786. +
  97787. + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
  97788. + alsa_stream ? alsa_stream->substream : 0);
  97789. +
  97790. + if (alsa_stream->open)
  97791. + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
  97792. +
  97793. + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
  97794. + * each iteration are the buffers that have been played out already
  97795. + */
  97796. +
  97797. + if (alsa_stream->period_size) {
  97798. + if ((alsa_stream->pos / alsa_stream->period_size) !=
  97799. + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
  97800. + new_period = 1;
  97801. + }
  97802. + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
  97803. + alsa_stream->pos,
  97804. + consumed,
  97805. + alsa_stream->buffer_size,
  97806. + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
  97807. + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
  97808. + new_period);
  97809. + if (alsa_stream->buffer_size) {
  97810. + alsa_stream->pos += consumed &~ (1<<30);
  97811. + alsa_stream->pos %= alsa_stream->buffer_size;
  97812. + }
  97813. +
  97814. + if (alsa_stream->substream) {
  97815. + if (new_period)
  97816. + snd_pcm_period_elapsed(alsa_stream->substream);
  97817. + } else {
  97818. + audio_warning(" unexpected NULL substream\n");
  97819. + }
  97820. + audio_info(" .. OUT\n");
  97821. +
  97822. + return IRQ_HANDLED;
  97823. +}
  97824. +
  97825. +/* open callback */
  97826. +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
  97827. +{
  97828. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  97829. + struct snd_pcm_runtime *runtime = substream->runtime;
  97830. + bcm2835_alsa_stream_t *alsa_stream;
  97831. + int idx;
  97832. + int err;
  97833. +
  97834. + audio_info(" .. IN (%d)\n", substream->number);
  97835. +
  97836. + audio_info("Alsa open (%d)\n", substream->number);
  97837. + idx = substream->number;
  97838. +
  97839. + if (idx > MAX_SUBSTREAMS) {
  97840. + audio_error
  97841. + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
  97842. + idx, MAX_SUBSTREAMS);
  97843. + err = -ENODEV;
  97844. + goto out;
  97845. + }
  97846. +
  97847. + /* Check if we are ready */
  97848. + if (!(chip->avail_substreams & (1 << idx))) {
  97849. + /* We are not ready yet */
  97850. + audio_error("substream(%d) device is not ready yet\n", idx);
  97851. + err = -EAGAIN;
  97852. + goto out;
  97853. + }
  97854. +
  97855. + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
  97856. + if (alsa_stream == NULL) {
  97857. + return -ENOMEM;
  97858. + }
  97859. +
  97860. + /* Initialise alsa_stream */
  97861. + alsa_stream->chip = chip;
  97862. + alsa_stream->substream = substream;
  97863. + alsa_stream->idx = idx;
  97864. +
  97865. + sema_init(&alsa_stream->buffers_update_sem, 0);
  97866. + sema_init(&alsa_stream->control_sem, 0);
  97867. + spin_lock_init(&alsa_stream->lock);
  97868. +
  97869. + /* Enabled in start trigger, called on each "fifo irq" after that */
  97870. + alsa_stream->enable_fifo_irq = 0;
  97871. + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
  97872. +
  97873. + runtime->private_data = alsa_stream;
  97874. + runtime->private_free = snd_bcm2835_playback_free;
  97875. + runtime->hw = snd_bcm2835_playback_hw;
  97876. + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
  97877. + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  97878. + 16);
  97879. +
  97880. + err = bcm2835_audio_open(alsa_stream);
  97881. + if (err != 0) {
  97882. + kfree(alsa_stream);
  97883. + return err;
  97884. + }
  97885. + chip->alsa_stream[idx] = alsa_stream;
  97886. +
  97887. + alsa_stream->open = 1;
  97888. + alsa_stream->draining = 1;
  97889. +
  97890. +out:
  97891. + audio_info(" .. OUT =%d\n", err);
  97892. +
  97893. + return err;
  97894. +}
  97895. +
  97896. +/* close callback */
  97897. +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
  97898. +{
  97899. + /* the hardware-specific codes will be here */
  97900. +
  97901. + struct snd_pcm_runtime *runtime = substream->runtime;
  97902. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97903. +
  97904. + audio_info(" .. IN\n");
  97905. + audio_info("Alsa close\n");
  97906. +
  97907. + /*
  97908. + * Call stop if it's still running. This happens when app
  97909. + * is force killed and we don't get a stop trigger.
  97910. + */
  97911. + if (alsa_stream->running) {
  97912. + int err;
  97913. + err = bcm2835_audio_stop(alsa_stream);
  97914. + alsa_stream->running = 0;
  97915. + if (err != 0)
  97916. + audio_error(" Failed to STOP alsa device\n");
  97917. + }
  97918. +
  97919. + alsa_stream->period_size = 0;
  97920. + alsa_stream->buffer_size = 0;
  97921. +
  97922. + if (alsa_stream->open) {
  97923. + alsa_stream->open = 0;
  97924. + bcm2835_audio_close(alsa_stream);
  97925. + }
  97926. + if (alsa_stream->chip)
  97927. + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
  97928. + /*
  97929. + * Do not free up alsa_stream here, it will be freed up by
  97930. + * runtime->private_free callback we registered in *_open above
  97931. + */
  97932. +
  97933. + audio_info(" .. OUT\n");
  97934. +
  97935. + return 0;
  97936. +}
  97937. +
  97938. +/* hw_params callback */
  97939. +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
  97940. + struct snd_pcm_hw_params *params)
  97941. +{
  97942. + int err;
  97943. + struct snd_pcm_runtime *runtime = substream->runtime;
  97944. + bcm2835_alsa_stream_t *alsa_stream =
  97945. + (bcm2835_alsa_stream_t *) runtime->private_data;
  97946. +
  97947. + audio_info(" .. IN\n");
  97948. +
  97949. + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  97950. + if (err < 0) {
  97951. + audio_error
  97952. + (" pcm_lib_malloc failed to allocated pages for buffers\n");
  97953. + return err;
  97954. + }
  97955. +
  97956. + err = bcm2835_audio_set_params(alsa_stream, params_channels(params),
  97957. + params_rate(params),
  97958. + snd_pcm_format_width(params_format
  97959. + (params)));
  97960. + if (err < 0) {
  97961. + audio_error(" error setting hw params\n");
  97962. + }
  97963. +
  97964. + bcm2835_audio_setup(alsa_stream);
  97965. +
  97966. + /* in preparation of the stream, set the controls (volume level) of the stream */
  97967. + bcm2835_audio_set_ctls(alsa_stream->chip);
  97968. +
  97969. + audio_info(" .. OUT\n");
  97970. +
  97971. + return err;
  97972. +}
  97973. +
  97974. +/* hw_free callback */
  97975. +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
  97976. +{
  97977. + audio_info(" .. IN\n");
  97978. + return snd_pcm_lib_free_pages(substream);
  97979. +}
  97980. +
  97981. +/* prepare callback */
  97982. +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
  97983. +{
  97984. + struct snd_pcm_runtime *runtime = substream->runtime;
  97985. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97986. +
  97987. + audio_info(" .. IN\n");
  97988. +
  97989. + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
  97990. +
  97991. + alsa_stream->pcm_indirect.hw_buffer_size =
  97992. + alsa_stream->pcm_indirect.sw_buffer_size =
  97993. + snd_pcm_lib_buffer_bytes(substream);
  97994. +
  97995. + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  97996. + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  97997. + alsa_stream->pos = 0;
  97998. +
  97999. + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  98000. + alsa_stream->buffer_size, alsa_stream->period_size,
  98001. + alsa_stream->pos, runtime->frame_bits);
  98002. +
  98003. + audio_info(" .. OUT\n");
  98004. + return 0;
  98005. +}
  98006. +
  98007. +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
  98008. + struct snd_pcm_indirect *rec, size_t bytes)
  98009. +{
  98010. + struct snd_pcm_runtime *runtime = substream->runtime;
  98011. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  98012. + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
  98013. + int err;
  98014. +
  98015. + err = bcm2835_audio_write(alsa_stream, bytes, src);
  98016. + if (err)
  98017. + audio_error(" Failed to transfer to alsa device (%d)\n", err);
  98018. +
  98019. +}
  98020. +
  98021. +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
  98022. +{
  98023. + struct snd_pcm_runtime *runtime = substream->runtime;
  98024. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  98025. + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
  98026. +
  98027. + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
  98028. + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
  98029. + snd_bcm2835_pcm_transfer);
  98030. + return 0;
  98031. +}
  98032. +
  98033. +/* trigger callback */
  98034. +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  98035. +{
  98036. + struct snd_pcm_runtime *runtime = substream->runtime;
  98037. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  98038. + int err = 0;
  98039. +
  98040. + audio_info(" .. IN\n");
  98041. +
  98042. + switch (cmd) {
  98043. + case SNDRV_PCM_TRIGGER_START:
  98044. + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
  98045. + alsa_stream->running);
  98046. + if (!alsa_stream->running) {
  98047. + err = bcm2835_audio_start(alsa_stream);
  98048. + if (err == 0) {
  98049. + alsa_stream->pcm_indirect.hw_io =
  98050. + alsa_stream->pcm_indirect.hw_data =
  98051. + bytes_to_frames(runtime,
  98052. + alsa_stream->pos);
  98053. + substream->ops->ack(substream);
  98054. + alsa_stream->running = 1;
  98055. + alsa_stream->draining = 1;
  98056. + } else {
  98057. + audio_error(" Failed to START alsa device (%d)\n", err);
  98058. + }
  98059. + }
  98060. + break;
  98061. + case SNDRV_PCM_TRIGGER_STOP:
  98062. + audio_debug
  98063. + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
  98064. + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
  98065. + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  98066. + audio_info("DRAINING\n");
  98067. + alsa_stream->draining = 1;
  98068. + } else {
  98069. + audio_info("DROPPING\n");
  98070. + alsa_stream->draining = 0;
  98071. + }
  98072. + if (alsa_stream->running) {
  98073. + err = bcm2835_audio_stop(alsa_stream);
  98074. + if (err != 0)
  98075. + audio_error(" Failed to STOP alsa device (%d)\n", err);
  98076. + alsa_stream->running = 0;
  98077. + }
  98078. + break;
  98079. + default:
  98080. + err = -EINVAL;
  98081. + }
  98082. +
  98083. + audio_info(" .. OUT\n");
  98084. + return err;
  98085. +}
  98086. +
  98087. +/* pointer callback */
  98088. +static snd_pcm_uframes_t
  98089. +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
  98090. +{
  98091. + struct snd_pcm_runtime *runtime = substream->runtime;
  98092. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  98093. +
  98094. + audio_info(" .. IN\n");
  98095. +
  98096. + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
  98097. + frames_to_bytes(runtime, runtime->status->hw_ptr),
  98098. + frames_to_bytes(runtime, runtime->control->appl_ptr),
  98099. + alsa_stream->pos);
  98100. +
  98101. + audio_info(" .. OUT\n");
  98102. + return snd_pcm_indirect_playback_pointer(substream,
  98103. + &alsa_stream->pcm_indirect,
  98104. + alsa_stream->pos);
  98105. +}
  98106. +
  98107. +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  98108. + unsigned int cmd, void *arg)
  98109. +{
  98110. + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  98111. + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
  98112. + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
  98113. + return ret;
  98114. +}
  98115. +
  98116. +/* operators */
  98117. +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
  98118. + .open = snd_bcm2835_playback_open,
  98119. + .close = snd_bcm2835_playback_close,
  98120. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  98121. + .hw_params = snd_bcm2835_pcm_hw_params,
  98122. + .hw_free = snd_bcm2835_pcm_hw_free,
  98123. + .prepare = snd_bcm2835_pcm_prepare,
  98124. + .trigger = snd_bcm2835_pcm_trigger,
  98125. + .pointer = snd_bcm2835_pcm_pointer,
  98126. + .ack = snd_bcm2835_pcm_ack,
  98127. +};
  98128. +
  98129. +/* create a pcm device */
  98130. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
  98131. +{
  98132. + struct snd_pcm *pcm;
  98133. + int err;
  98134. +
  98135. + audio_info(" .. IN\n");
  98136. + err =
  98137. + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
  98138. + if (err < 0)
  98139. + return err;
  98140. + pcm->private_data = chip;
  98141. + strcpy(pcm->name, "bcm2835 ALSA");
  98142. + chip->pcm = pcm;
  98143. + chip->dest = AUDIO_DEST_AUTO;
  98144. + chip->volume = alsa2chip(0);
  98145. + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
  98146. + /* set operators */
  98147. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  98148. + &snd_bcm2835_playback_ops);
  98149. +
  98150. + /* pre-allocation of buffers */
  98151. + /* NOTE: this may fail */
  98152. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  98153. + snd_dma_continuous_data
  98154. + (GFP_KERNEL), 64 * 1024,
  98155. + 64 * 1024);
  98156. +
  98157. + audio_info(" .. OUT\n");
  98158. +
  98159. + return 0;
  98160. +}
  98161. diff -Nur linux-3.10.37/sound/arm/bcm2835-vchiq.c linux-rpi/sound/arm/bcm2835-vchiq.c
  98162. --- linux-3.10.37/sound/arm/bcm2835-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  98163. +++ linux-rpi/sound/arm/bcm2835-vchiq.c 2014-04-24 15:35:05.361579008 +0200
  98164. @@ -0,0 +1,879 @@
  98165. +/*****************************************************************************
  98166. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  98167. +*
  98168. +* Unless you and Broadcom execute a separate written software license
  98169. +* agreement governing use of this software, this software is licensed to you
  98170. +* under the terms of the GNU General Public License version 2, available at
  98171. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  98172. +*
  98173. +* Notwithstanding the above, under no circumstances may you combine this
  98174. +* software in any way with any other Broadcom software provided under a
  98175. +* license other than the GPL, without Broadcom's express prior written
  98176. +* consent.
  98177. +*****************************************************************************/
  98178. +
  98179. +#include <linux/device.h>
  98180. +#include <sound/core.h>
  98181. +#include <sound/initval.h>
  98182. +#include <sound/pcm.h>
  98183. +#include <linux/io.h>
  98184. +#include <linux/interrupt.h>
  98185. +#include <linux/fs.h>
  98186. +#include <linux/file.h>
  98187. +#include <linux/mm.h>
  98188. +#include <linux/syscalls.h>
  98189. +#include <asm/uaccess.h>
  98190. +#include <linux/slab.h>
  98191. +#include <linux/delay.h>
  98192. +#include <linux/atomic.h>
  98193. +#include <linux/module.h>
  98194. +#include <linux/completion.h>
  98195. +
  98196. +#include "bcm2835.h"
  98197. +
  98198. +/* ---- Include Files -------------------------------------------------------- */
  98199. +
  98200. +#include "interface/vchi/vchi.h"
  98201. +#include "vc_vchi_audioserv_defs.h"
  98202. +
  98203. +/* ---- Private Constants and Types ------------------------------------------ */
  98204. +
  98205. +#define BCM2835_AUDIO_STOP 0
  98206. +#define BCM2835_AUDIO_START 1
  98207. +#define BCM2835_AUDIO_WRITE 2
  98208. +
  98209. +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
  98210. +#ifdef AUDIO_DEBUG_ENABLE
  98211. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98212. + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98213. + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98214. + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98215. +#else
  98216. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  98217. + #define LOG_WARN( fmt, arg... )
  98218. + #define LOG_INFO( fmt, arg... )
  98219. + #define LOG_DBG( fmt, arg... )
  98220. +#endif
  98221. +
  98222. +typedef struct opaque_AUDIO_INSTANCE_T {
  98223. + uint32_t num_connections;
  98224. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  98225. + struct completion msg_avail_comp;
  98226. + struct mutex vchi_mutex;
  98227. + bcm2835_alsa_stream_t *alsa_stream;
  98228. + int32_t result;
  98229. + short peer_version;
  98230. +} AUDIO_INSTANCE_T;
  98231. +
  98232. +bool force_bulk = false;
  98233. +
  98234. +/* ---- Private Variables ---------------------------------------------------- */
  98235. +
  98236. +/* ---- Private Function Prototypes ------------------------------------------ */
  98237. +
  98238. +/* ---- Private Functions ---------------------------------------------------- */
  98239. +
  98240. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
  98241. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
  98242. +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  98243. + uint32_t count, void *src);
  98244. +
  98245. +typedef struct {
  98246. + struct work_struct my_work;
  98247. + bcm2835_alsa_stream_t *alsa_stream;
  98248. + int cmd;
  98249. + void *src;
  98250. + uint32_t count;
  98251. +} my_work_t;
  98252. +
  98253. +static void my_wq_function(struct work_struct *work)
  98254. +{
  98255. + my_work_t *w = (my_work_t *) work;
  98256. + int ret = -9;
  98257. + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
  98258. + switch (w->cmd) {
  98259. + case BCM2835_AUDIO_START:
  98260. + ret = bcm2835_audio_start_worker(w->alsa_stream);
  98261. + break;
  98262. + case BCM2835_AUDIO_STOP:
  98263. + ret = bcm2835_audio_stop_worker(w->alsa_stream);
  98264. + break;
  98265. + case BCM2835_AUDIO_WRITE:
  98266. + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
  98267. + w->src);
  98268. + break;
  98269. + default:
  98270. + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
  98271. + break;
  98272. + }
  98273. + kfree((void *)work);
  98274. + LOG_DBG(" .. OUT %d\n", ret);
  98275. +}
  98276. +
  98277. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
  98278. +{
  98279. + int ret = -1;
  98280. + LOG_DBG(" .. IN\n");
  98281. + if (alsa_stream->my_wq) {
  98282. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  98283. + /*--- Queue some work (item 1) ---*/
  98284. + if (work) {
  98285. + INIT_WORK((struct work_struct *)work, my_wq_function);
  98286. + work->alsa_stream = alsa_stream;
  98287. + work->cmd = BCM2835_AUDIO_START;
  98288. + if (queue_work
  98289. + (alsa_stream->my_wq, (struct work_struct *)work))
  98290. + ret = 0;
  98291. + } else
  98292. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  98293. + }
  98294. + LOG_DBG(" .. OUT %d\n", ret);
  98295. + return ret;
  98296. +}
  98297. +
  98298. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
  98299. +{
  98300. + int ret = -1;
  98301. + LOG_DBG(" .. IN\n");
  98302. + if (alsa_stream->my_wq) {
  98303. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  98304. + /*--- Queue some work (item 1) ---*/
  98305. + if (work) {
  98306. + INIT_WORK((struct work_struct *)work, my_wq_function);
  98307. + work->alsa_stream = alsa_stream;
  98308. + work->cmd = BCM2835_AUDIO_STOP;
  98309. + if (queue_work
  98310. + (alsa_stream->my_wq, (struct work_struct *)work))
  98311. + ret = 0;
  98312. + } else
  98313. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  98314. + }
  98315. + LOG_DBG(" .. OUT %d\n", ret);
  98316. + return ret;
  98317. +}
  98318. +
  98319. +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
  98320. + uint32_t count, void *src)
  98321. +{
  98322. + int ret = -1;
  98323. + LOG_DBG(" .. IN\n");
  98324. + if (alsa_stream->my_wq) {
  98325. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  98326. + /*--- Queue some work (item 1) ---*/
  98327. + if (work) {
  98328. + INIT_WORK((struct work_struct *)work, my_wq_function);
  98329. + work->alsa_stream = alsa_stream;
  98330. + work->cmd = BCM2835_AUDIO_WRITE;
  98331. + work->src = src;
  98332. + work->count = count;
  98333. + if (queue_work
  98334. + (alsa_stream->my_wq, (struct work_struct *)work))
  98335. + ret = 0;
  98336. + } else
  98337. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  98338. + }
  98339. + LOG_DBG(" .. OUT %d\n", ret);
  98340. + return ret;
  98341. +}
  98342. +
  98343. +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
  98344. +{
  98345. + alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
  98346. + return;
  98347. +}
  98348. +
  98349. +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
  98350. +{
  98351. + if (alsa_stream->my_wq) {
  98352. + flush_workqueue(alsa_stream->my_wq);
  98353. + destroy_workqueue(alsa_stream->my_wq);
  98354. + alsa_stream->my_wq = NULL;
  98355. + }
  98356. + return;
  98357. +}
  98358. +
  98359. +static void audio_vchi_callback(void *param,
  98360. + const VCHI_CALLBACK_REASON_T reason,
  98361. + void *msg_handle)
  98362. +{
  98363. + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
  98364. + int32_t status;
  98365. + int32_t msg_len;
  98366. + VC_AUDIO_MSG_T m;
  98367. + bcm2835_alsa_stream_t *alsa_stream = 0;
  98368. + LOG_DBG(" .. IN instance=%p, param=%p, reason=%d, handle=%p\n",
  98369. + instance, param, reason, msg_handle);
  98370. +
  98371. + if (!instance || reason != VCHI_CALLBACK_MSG_AVAILABLE) {
  98372. + return;
  98373. + }
  98374. + alsa_stream = instance->alsa_stream;
  98375. + status = vchi_msg_dequeue(instance->vchi_handle[0],
  98376. + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
  98377. + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
  98378. + LOG_DBG
  98379. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
  98380. + instance, m.u.result.success);
  98381. + instance->result = m.u.result.success;
  98382. + complete(&instance->msg_avail_comp);
  98383. + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
  98384. + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
  98385. + LOG_DBG
  98386. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
  98387. + instance, m.u.complete.count);
  98388. + if (alsa_stream && callback) {
  98389. + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
  98390. + callback(0, alsa_stream);
  98391. + } else {
  98392. + LOG_DBG(" .. unexpected alsa_stream=%p, callback=%p\n",
  98393. + alsa_stream, callback);
  98394. + }
  98395. + } else {
  98396. + LOG_DBG(" .. unexpected m.type=%d\n", m.type);
  98397. + }
  98398. + LOG_DBG(" .. OUT\n");
  98399. +}
  98400. +
  98401. +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
  98402. + VCHI_CONNECTION_T **
  98403. + vchi_connections,
  98404. + uint32_t num_connections)
  98405. +{
  98406. + uint32_t i;
  98407. + AUDIO_INSTANCE_T *instance;
  98408. + int status;
  98409. +
  98410. + LOG_DBG("%s: start", __func__);
  98411. +
  98412. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  98413. + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
  98414. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  98415. +
  98416. + return NULL;
  98417. + }
  98418. + /* Allocate memory for this instance */
  98419. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  98420. +
  98421. + memset(instance, 0, sizeof(*instance));
  98422. + instance->num_connections = num_connections;
  98423. +
  98424. + /* Create a lock for exclusive, serialized VCHI connection access */
  98425. + mutex_init(&instance->vchi_mutex);
  98426. + /* Open the VCHI service connections */
  98427. + for (i = 0; i < num_connections; i++) {
  98428. + SERVICE_CREATION_T params = {
  98429. + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
  98430. + VC_AUDIO_SERVER_NAME, // 4cc service code
  98431. + vchi_connections[i], // passed in fn pointers
  98432. + 0, // rx fifo size (unused)
  98433. + 0, // tx fifo size (unused)
  98434. + audio_vchi_callback, // service callback
  98435. + instance, // service callback parameter
  98436. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
  98437. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
  98438. + 0 // want crc check on bulk transfers
  98439. + };
  98440. +
  98441. + status = vchi_service_open(vchi_instance, &params,
  98442. + &instance->vchi_handle[i]);
  98443. + if (status) {
  98444. + LOG_ERR
  98445. + ("%s: failed to open VCHI service connection (status=%d)\n",
  98446. + __func__, status);
  98447. +
  98448. + goto err_close_services;
  98449. + }
  98450. + /* Finished with the service for now */
  98451. + vchi_service_release(instance->vchi_handle[i]);
  98452. + }
  98453. +
  98454. + return instance;
  98455. +
  98456. +err_close_services:
  98457. + for (i = 0; i < instance->num_connections; i++) {
  98458. + vchi_service_close(instance->vchi_handle[i]);
  98459. + }
  98460. +
  98461. + kfree(instance);
  98462. +
  98463. + return NULL;
  98464. +}
  98465. +
  98466. +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
  98467. +{
  98468. + uint32_t i;
  98469. +
  98470. + LOG_DBG(" .. IN\n");
  98471. +
  98472. + if (instance == NULL) {
  98473. + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
  98474. +
  98475. + return -1;
  98476. + }
  98477. +
  98478. + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
  98479. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98480. + {
  98481. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98482. + return -EINTR;
  98483. + }
  98484. +
  98485. + /* Close all VCHI service connections */
  98486. + for (i = 0; i < instance->num_connections; i++) {
  98487. + int32_t success;
  98488. + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
  98489. + vchi_service_use(instance->vchi_handle[i]);
  98490. +
  98491. + success = vchi_service_close(instance->vchi_handle[i]);
  98492. + if (success != 0) {
  98493. + LOG_ERR
  98494. + ("%s: failed to close VCHI service connection (status=%d)\n",
  98495. + __func__, success);
  98496. + }
  98497. + }
  98498. +
  98499. + mutex_unlock(&instance->vchi_mutex);
  98500. +
  98501. + kfree(instance);
  98502. +
  98503. + LOG_DBG(" .. OUT\n");
  98504. +
  98505. + return 0;
  98506. +}
  98507. +
  98508. +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
  98509. +{
  98510. + static VCHI_INSTANCE_T vchi_instance;
  98511. + static VCHI_CONNECTION_T *vchi_connection;
  98512. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98513. + int ret;
  98514. + LOG_DBG(" .. IN\n");
  98515. +
  98516. + LOG_INFO("%s: start", __func__);
  98517. + //BUG_ON(instance);
  98518. + if (instance) {
  98519. + LOG_ERR("%s: VCHI instance already open (%p)\n",
  98520. + __func__, instance);
  98521. + instance->alsa_stream = alsa_stream;
  98522. + alsa_stream->instance = instance;
  98523. + ret = 0; // xxx todo -1;
  98524. + goto err_free_mem;
  98525. + }
  98526. +
  98527. + /* Initialize and create a VCHI connection */
  98528. + ret = vchi_initialise(&vchi_instance);
  98529. + if (ret != 0) {
  98530. + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
  98531. + __func__, ret);
  98532. +
  98533. + ret = -EIO;
  98534. + goto err_free_mem;
  98535. + }
  98536. + ret = vchi_connect(NULL, 0, vchi_instance);
  98537. + if (ret != 0) {
  98538. + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
  98539. + __func__, ret);
  98540. +
  98541. + ret = -EIO;
  98542. + goto err_free_mem;
  98543. + }
  98544. +
  98545. + /* Initialize an instance of the audio service */
  98546. + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
  98547. +
  98548. + if (instance == NULL /*|| audio_handle != instance */ ) {
  98549. + LOG_ERR("%s: failed to initialize audio service\n", __func__);
  98550. +
  98551. + ret = -EPERM;
  98552. + goto err_free_mem;
  98553. + }
  98554. +
  98555. + instance->alsa_stream = alsa_stream;
  98556. + alsa_stream->instance = instance;
  98557. +
  98558. + LOG_DBG(" success !\n");
  98559. +err_free_mem:
  98560. + LOG_DBG(" .. OUT\n");
  98561. +
  98562. + return ret;
  98563. +}
  98564. +
  98565. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
  98566. +{
  98567. + AUDIO_INSTANCE_T *instance;
  98568. + VC_AUDIO_MSG_T m;
  98569. + int32_t success;
  98570. + int ret;
  98571. + LOG_DBG(" .. IN\n");
  98572. +
  98573. + my_workqueue_init(alsa_stream);
  98574. +
  98575. + ret = bcm2835_audio_open_connection(alsa_stream);
  98576. + if (ret != 0) {
  98577. + ret = -1;
  98578. + goto exit;
  98579. + }
  98580. + instance = alsa_stream->instance;
  98581. +
  98582. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98583. + {
  98584. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98585. + return -EINTR;
  98586. + }
  98587. + vchi_service_use(instance->vchi_handle[0]);
  98588. +
  98589. + m.type = VC_AUDIO_MSG_TYPE_OPEN;
  98590. +
  98591. + /* Send the message to the videocore */
  98592. + success = vchi_msg_queue(instance->vchi_handle[0],
  98593. + &m, sizeof m,
  98594. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98595. +
  98596. + if (success != 0) {
  98597. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98598. + __func__, success);
  98599. +
  98600. + ret = -1;
  98601. + goto unlock;
  98602. + }
  98603. +
  98604. + ret = 0;
  98605. +
  98606. +unlock:
  98607. + vchi_service_release(instance->vchi_handle[0]);
  98608. + mutex_unlock(&instance->vchi_mutex);
  98609. +exit:
  98610. + LOG_DBG(" .. OUT\n");
  98611. + return ret;
  98612. +}
  98613. +
  98614. +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
  98615. + bcm2835_chip_t * chip)
  98616. +{
  98617. + VC_AUDIO_MSG_T m;
  98618. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98619. + int32_t success;
  98620. + int ret;
  98621. + LOG_DBG(" .. IN\n");
  98622. +
  98623. + LOG_INFO
  98624. + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  98625. +
  98626. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98627. + {
  98628. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98629. + return -EINTR;
  98630. + }
  98631. + vchi_service_use(instance->vchi_handle[0]);
  98632. +
  98633. + instance->result = -1;
  98634. +
  98635. + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
  98636. + m.u.control.dest = chip->dest;
  98637. + m.u.control.volume = chip->volume;
  98638. +
  98639. + /* Create the message available completion */
  98640. + init_completion(&instance->msg_avail_comp);
  98641. +
  98642. + /* Send the message to the videocore */
  98643. + success = vchi_msg_queue(instance->vchi_handle[0],
  98644. + &m, sizeof m,
  98645. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98646. +
  98647. + if (success != 0) {
  98648. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98649. + __func__, success);
  98650. +
  98651. + ret = -1;
  98652. + goto unlock;
  98653. + }
  98654. +
  98655. + /* We are expecting a reply from the videocore */
  98656. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98657. + if (ret) {
  98658. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  98659. + __func__, success);
  98660. + goto unlock;
  98661. + }
  98662. +
  98663. + if (instance->result != 0) {
  98664. + LOG_ERR("%s: result=%d\n", __func__, instance->result);
  98665. +
  98666. + ret = -1;
  98667. + goto unlock;
  98668. + }
  98669. +
  98670. + ret = 0;
  98671. +
  98672. +unlock:
  98673. + vchi_service_release(instance->vchi_handle[0]);
  98674. + mutex_unlock(&instance->vchi_mutex);
  98675. +
  98676. + LOG_DBG(" .. OUT\n");
  98677. + return ret;
  98678. +}
  98679. +
  98680. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
  98681. +{
  98682. + int i;
  98683. + int ret = 0;
  98684. + LOG_DBG(" .. IN\n");
  98685. +
  98686. + /* change ctls for all substreams */
  98687. + for (i = 0; i < MAX_SUBSTREAMS; i++) {
  98688. + if (chip->avail_substreams & (1 << i)) {
  98689. + if (!chip->alsa_stream[i])
  98690. + {
  98691. + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
  98692. + ret = 0;
  98693. + }
  98694. + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
  98695. + (chip->alsa_stream[i], chip) != 0)
  98696. + {
  98697. + LOG_DBG("Couldn't set the controls for stream %d\n", i);
  98698. + ret = -1;
  98699. + }
  98700. + else LOG_DBG(" Controls set for stream %d\n", i);
  98701. + }
  98702. + }
  98703. + LOG_DBG(" .. OUT ret=%d\n", ret);
  98704. + return ret;
  98705. +}
  98706. +
  98707. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  98708. + uint32_t channels, uint32_t samplerate,
  98709. + uint32_t bps)
  98710. +{
  98711. + VC_AUDIO_MSG_T m;
  98712. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98713. + int32_t success;
  98714. + int ret;
  98715. + LOG_DBG(" .. IN\n");
  98716. +
  98717. + LOG_INFO
  98718. + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
  98719. + channels, samplerate, bps);
  98720. +
  98721. + /* resend ctls - alsa_stream may not have been open when first send */
  98722. + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
  98723. + if (ret != 0) {
  98724. + LOG_ERR(" Alsa controls not supported\n");
  98725. + return -EINVAL;
  98726. + }
  98727. +
  98728. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98729. + {
  98730. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98731. + return -EINTR;
  98732. + }
  98733. + vchi_service_use(instance->vchi_handle[0]);
  98734. +
  98735. + instance->result = -1;
  98736. +
  98737. + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
  98738. + m.u.config.channels = channels;
  98739. + m.u.config.samplerate = samplerate;
  98740. + m.u.config.bps = bps;
  98741. +
  98742. + /* Create the message available completion */
  98743. + init_completion(&instance->msg_avail_comp);
  98744. +
  98745. + /* Send the message to the videocore */
  98746. + success = vchi_msg_queue(instance->vchi_handle[0],
  98747. + &m, sizeof m,
  98748. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98749. +
  98750. + if (success != 0) {
  98751. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98752. + __func__, success);
  98753. +
  98754. + ret = -1;
  98755. + goto unlock;
  98756. + }
  98757. +
  98758. + /* We are expecting a reply from the videocore */
  98759. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98760. + if (ret) {
  98761. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  98762. + __func__, success);
  98763. + goto unlock;
  98764. + }
  98765. +
  98766. + if (instance->result != 0) {
  98767. + LOG_ERR("%s: result=%d", __func__, instance->result);
  98768. +
  98769. + ret = -1;
  98770. + goto unlock;
  98771. + }
  98772. +
  98773. + ret = 0;
  98774. +
  98775. +unlock:
  98776. + vchi_service_release(instance->vchi_handle[0]);
  98777. + mutex_unlock(&instance->vchi_mutex);
  98778. +
  98779. + LOG_DBG(" .. OUT\n");
  98780. + return ret;
  98781. +}
  98782. +
  98783. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
  98784. +{
  98785. + LOG_DBG(" .. IN\n");
  98786. +
  98787. + LOG_DBG(" .. OUT\n");
  98788. +
  98789. + return 0;
  98790. +}
  98791. +
  98792. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
  98793. +{
  98794. + VC_AUDIO_MSG_T m;
  98795. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98796. + int32_t success;
  98797. + int ret;
  98798. + LOG_DBG(" .. IN\n");
  98799. +
  98800. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98801. + {
  98802. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98803. + return -EINTR;
  98804. + }
  98805. + vchi_service_use(instance->vchi_handle[0]);
  98806. +
  98807. + m.type = VC_AUDIO_MSG_TYPE_START;
  98808. +
  98809. + /* Send the message to the videocore */
  98810. + success = vchi_msg_queue(instance->vchi_handle[0],
  98811. + &m, sizeof m,
  98812. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98813. +
  98814. + if (success != 0) {
  98815. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98816. + __func__, success);
  98817. +
  98818. + ret = -1;
  98819. + goto unlock;
  98820. + }
  98821. +
  98822. + ret = 0;
  98823. +
  98824. +unlock:
  98825. + vchi_service_release(instance->vchi_handle[0]);
  98826. + mutex_unlock(&instance->vchi_mutex);
  98827. + LOG_DBG(" .. OUT\n");
  98828. + return ret;
  98829. +}
  98830. +
  98831. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
  98832. +{
  98833. + VC_AUDIO_MSG_T m;
  98834. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98835. + int32_t success;
  98836. + int ret;
  98837. + LOG_DBG(" .. IN\n");
  98838. +
  98839. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98840. + {
  98841. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98842. + return -EINTR;
  98843. + }
  98844. + vchi_service_use(instance->vchi_handle[0]);
  98845. +
  98846. + m.type = VC_AUDIO_MSG_TYPE_STOP;
  98847. + m.u.stop.draining = alsa_stream->draining;
  98848. +
  98849. + /* Send the message to the videocore */
  98850. + success = vchi_msg_queue(instance->vchi_handle[0],
  98851. + &m, sizeof m,
  98852. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98853. +
  98854. + if (success != 0) {
  98855. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98856. + __func__, success);
  98857. +
  98858. + ret = -1;
  98859. + goto unlock;
  98860. + }
  98861. +
  98862. + ret = 0;
  98863. +
  98864. +unlock:
  98865. + vchi_service_release(instance->vchi_handle[0]);
  98866. + mutex_unlock(&instance->vchi_mutex);
  98867. + LOG_DBG(" .. OUT\n");
  98868. + return ret;
  98869. +}
  98870. +
  98871. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
  98872. +{
  98873. + VC_AUDIO_MSG_T m;
  98874. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98875. + int32_t success;
  98876. + int ret;
  98877. + LOG_DBG(" .. IN\n");
  98878. +
  98879. + my_workqueue_quit(alsa_stream);
  98880. +
  98881. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98882. + {
  98883. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98884. + return -EINTR;
  98885. + }
  98886. + vchi_service_use(instance->vchi_handle[0]);
  98887. +
  98888. + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
  98889. +
  98890. + /* Create the message available completion */
  98891. + init_completion(&instance->msg_avail_comp);
  98892. +
  98893. + /* Send the message to the videocore */
  98894. + success = vchi_msg_queue(instance->vchi_handle[0],
  98895. + &m, sizeof m,
  98896. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98897. +
  98898. + if (success != 0) {
  98899. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98900. + __func__, success);
  98901. + ret = -1;
  98902. + goto unlock;
  98903. + }
  98904. +
  98905. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98906. + if (ret) {
  98907. + LOG_ERR("%s: failed on waiting for event (status=%d)",
  98908. + __func__, success);
  98909. + goto unlock;
  98910. + }
  98911. + if (instance->result != 0) {
  98912. + LOG_ERR("%s: failed result (status=%d)",
  98913. + __func__, instance->result);
  98914. +
  98915. + ret = -1;
  98916. + goto unlock;
  98917. + }
  98918. +
  98919. + ret = 0;
  98920. +
  98921. +unlock:
  98922. + vchi_service_release(instance->vchi_handle[0]);
  98923. + mutex_unlock(&instance->vchi_mutex);
  98924. +
  98925. + /* Stop the audio service */
  98926. + if (instance) {
  98927. + vc_vchi_audio_deinit(instance);
  98928. + alsa_stream->instance = NULL;
  98929. + }
  98930. + LOG_DBG(" .. OUT\n");
  98931. + return ret;
  98932. +}
  98933. +
  98934. +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  98935. + uint32_t count, void *src)
  98936. +{
  98937. + VC_AUDIO_MSG_T m;
  98938. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98939. + int32_t success;
  98940. + int ret;
  98941. +
  98942. + LOG_DBG(" .. IN\n");
  98943. +
  98944. + LOG_INFO(" Writing %d bytes from %p\n", count, src);
  98945. +
  98946. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98947. + {
  98948. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98949. + return -EINTR;
  98950. + }
  98951. + vchi_service_use(instance->vchi_handle[0]);
  98952. +
  98953. + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
  98954. + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
  98955. + }
  98956. + m.type = VC_AUDIO_MSG_TYPE_WRITE;
  98957. + m.u.write.count = count;
  98958. + // old version uses bulk, new version uses control
  98959. + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
  98960. + m.u.write.callback = alsa_stream->fifo_irq_handler;
  98961. + m.u.write.cookie = alsa_stream;
  98962. + m.u.write.silence = src == NULL;
  98963. +
  98964. + /* Send the message to the videocore */
  98965. + success = vchi_msg_queue(instance->vchi_handle[0],
  98966. + &m, sizeof m,
  98967. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98968. +
  98969. + if (success != 0) {
  98970. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98971. + __func__, success);
  98972. +
  98973. + ret = -1;
  98974. + goto unlock;
  98975. + }
  98976. + if (!m.u.write.silence) {
  98977. + if (m.u.write.max_packet == 0) {
  98978. + /* Send the message to the videocore */
  98979. + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
  98980. + src, count,
  98981. + 0 *
  98982. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
  98983. + +
  98984. + 1 *
  98985. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
  98986. + NULL);
  98987. + } else {
  98988. + while (count > 0) {
  98989. + int bytes = min((int)m.u.write.max_packet, (int)count);
  98990. + success = vchi_msg_queue(instance->vchi_handle[0],
  98991. + src, bytes,
  98992. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98993. + src = (char *)src + bytes;
  98994. + count -= bytes;
  98995. + }
  98996. + }
  98997. + if (success != 0) {
  98998. + LOG_ERR
  98999. + ("%s: failed on vchi_bulk_queue_transmit (status=%d)",
  99000. + __func__, success);
  99001. +
  99002. + ret = -1;
  99003. + goto unlock;
  99004. + }
  99005. + }
  99006. + ret = 0;
  99007. +
  99008. +unlock:
  99009. + vchi_service_release(instance->vchi_handle[0]);
  99010. + mutex_unlock(&instance->vchi_mutex);
  99011. + LOG_DBG(" .. OUT\n");
  99012. + return ret;
  99013. +}
  99014. +
  99015. +/**
  99016. + * Returns all buffers from arm->vc
  99017. + */
  99018. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
  99019. +{
  99020. + LOG_DBG(" .. IN\n");
  99021. + LOG_DBG(" .. OUT\n");
  99022. + return;
  99023. +}
  99024. +
  99025. +/**
  99026. + * Forces VC to flush(drop) its filled playback buffers and
  99027. + * return them the us. (VC->ARM)
  99028. + */
  99029. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
  99030. +{
  99031. + LOG_DBG(" .. IN\n");
  99032. + LOG_DBG(" .. OUT\n");
  99033. +}
  99034. +
  99035. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
  99036. +{
  99037. + uint32_t count = atomic_read(&alsa_stream->retrieved);
  99038. + atomic_sub(count, &alsa_stream->retrieved);
  99039. + return count;
  99040. +}
  99041. +
  99042. +module_param(force_bulk, bool, 0444);
  99043. +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
  99044. diff -Nur linux-3.10.37/sound/arm/Kconfig linux-rpi/sound/arm/Kconfig
  99045. --- linux-3.10.37/sound/arm/Kconfig 2014-04-14 15:42:31.000000000 +0200
  99046. +++ linux-rpi/sound/arm/Kconfig 2014-04-24 15:35:05.357578964 +0200
  99047. @@ -39,5 +39,12 @@
  99048. Say Y or M if you want to support any AC97 codec attached to
  99049. the PXA2xx AC97 interface.
  99050. +config SND_BCM2835
  99051. + tristate "BCM2835 ALSA driver"
  99052. + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
  99053. + select SND_PCM
  99054. + help
  99055. + Say Y or M if you want to support BCM2835 Alsa pcm card driver
  99056. +
  99057. endif # SND_ARM
  99058. diff -Nur linux-3.10.37/sound/arm/Makefile linux-rpi/sound/arm/Makefile
  99059. --- linux-3.10.37/sound/arm/Makefile 2014-04-14 15:42:31.000000000 +0200
  99060. +++ linux-rpi/sound/arm/Makefile 2014-04-24 15:35:05.357578964 +0200
  99061. @@ -14,3 +14,9 @@
  99062. obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
  99063. snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
  99064. +
  99065. +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
  99066. +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
  99067. +
  99068. +ccflags-y += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  99069. +
  99070. diff -Nur linux-3.10.37/sound/arm/vc_vchi_audioserv_defs.h linux-rpi/sound/arm/vc_vchi_audioserv_defs.h
  99071. --- linux-3.10.37/sound/arm/vc_vchi_audioserv_defs.h 1970-01-01 01:00:00.000000000 +0100
  99072. +++ linux-rpi/sound/arm/vc_vchi_audioserv_defs.h 2014-04-24 15:35:05.365579053 +0200
  99073. @@ -0,0 +1,116 @@
  99074. +/*****************************************************************************
  99075. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  99076. +*
  99077. +* Unless you and Broadcom execute a separate written software license
  99078. +* agreement governing use of this software, this software is licensed to you
  99079. +* under the terms of the GNU General Public License version 2, available at
  99080. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  99081. +*
  99082. +* Notwithstanding the above, under no circumstances may you combine this
  99083. +* software in any way with any other Broadcom software provided under a
  99084. +* license other than the GPL, without Broadcom's express prior written
  99085. +* consent.
  99086. +*****************************************************************************/
  99087. +
  99088. +#ifndef _VC_AUDIO_DEFS_H_
  99089. +#define _VC_AUDIO_DEFS_H_
  99090. +
  99091. +#define VC_AUDIOSERV_MIN_VER 1
  99092. +#define VC_AUDIOSERV_VER 2
  99093. +
  99094. +// FourCC code used for VCHI connection
  99095. +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
  99096. +
  99097. +// Maximum message length
  99098. +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
  99099. +
  99100. +// List of screens that are currently supported
  99101. +// All message types supported for HOST->VC direction
  99102. +typedef enum {
  99103. + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
  99104. + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
  99105. + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
  99106. + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
  99107. + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
  99108. + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
  99109. + VC_AUDIO_MSG_TYPE_START, // Configure audio
  99110. + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
  99111. + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
  99112. + VC_AUDIO_MSG_TYPE_MAX
  99113. +} VC_AUDIO_MSG_TYPE;
  99114. +
  99115. +// configure the audio
  99116. +typedef struct {
  99117. + uint32_t channels;
  99118. + uint32_t samplerate;
  99119. + uint32_t bps;
  99120. +
  99121. +} VC_AUDIO_CONFIG_T;
  99122. +
  99123. +typedef struct {
  99124. + uint32_t volume;
  99125. + uint32_t dest;
  99126. +
  99127. +} VC_AUDIO_CONTROL_T;
  99128. +
  99129. +// audio
  99130. +typedef struct {
  99131. + uint32_t dummy;
  99132. +
  99133. +} VC_AUDIO_OPEN_T;
  99134. +
  99135. +// audio
  99136. +typedef struct {
  99137. + uint32_t dummy;
  99138. +
  99139. +} VC_AUDIO_CLOSE_T;
  99140. +// audio
  99141. +typedef struct {
  99142. + uint32_t dummy;
  99143. +
  99144. +} VC_AUDIO_START_T;
  99145. +// audio
  99146. +typedef struct {
  99147. + uint32_t draining;
  99148. +
  99149. +} VC_AUDIO_STOP_T;
  99150. +
  99151. +// configure the write audio samples
  99152. +typedef struct {
  99153. + uint32_t count; // in bytes
  99154. + void *callback;
  99155. + void *cookie;
  99156. + uint16_t silence;
  99157. + uint16_t max_packet;
  99158. +} VC_AUDIO_WRITE_T;
  99159. +
  99160. +// Generic result for a request (VC->HOST)
  99161. +typedef struct {
  99162. + int32_t success; // Success value
  99163. +
  99164. +} VC_AUDIO_RESULT_T;
  99165. +
  99166. +// Generic result for a request (VC->HOST)
  99167. +typedef struct {
  99168. + int32_t count; // Success value
  99169. + void *callback;
  99170. + void *cookie;
  99171. +} VC_AUDIO_COMPLETE_T;
  99172. +
  99173. +// Message header for all messages in HOST->VC direction
  99174. +typedef struct {
  99175. + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
  99176. + union {
  99177. + VC_AUDIO_CONFIG_T config;
  99178. + VC_AUDIO_CONTROL_T control;
  99179. + VC_AUDIO_OPEN_T open;
  99180. + VC_AUDIO_CLOSE_T close;
  99181. + VC_AUDIO_START_T start;
  99182. + VC_AUDIO_STOP_T stop;
  99183. + VC_AUDIO_WRITE_T write;
  99184. + VC_AUDIO_RESULT_T result;
  99185. + VC_AUDIO_COMPLETE_T complete;
  99186. + } u;
  99187. +} VC_AUDIO_MSG_T;
  99188. +
  99189. +#endif // _VC_AUDIO_DEFS_H_
  99190. diff -Nur linux-3.10.37/sound/soc/bcm/bcm2708-i2s.c linux-rpi/sound/soc/bcm/bcm2708-i2s.c
  99191. --- linux-3.10.37/sound/soc/bcm/bcm2708-i2s.c 1970-01-01 01:00:00.000000000 +0100
  99192. +++ linux-rpi/sound/soc/bcm/bcm2708-i2s.c 2014-04-24 15:35:05.481580345 +0200
  99193. @@ -0,0 +1,945 @@
  99194. +/*
  99195. + * ALSA SoC I2S Audio Layer for Broadcom BCM2708 SoC
  99196. + *
  99197. + * Author: Florian Meier <florian.meier@koalo.de>
  99198. + * Copyright 2013
  99199. + *
  99200. + * Based on
  99201. + * Raspberry Pi PCM I2S ALSA Driver
  99202. + * Copyright (c) by Phil Poole 2013
  99203. + *
  99204. + * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  99205. + * Vladimir Barinov, <vbarinov@embeddedalley.com>
  99206. + * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  99207. + *
  99208. + * OMAP ALSA SoC DAI driver using McBSP port
  99209. + * Copyright (C) 2008 Nokia Corporation
  99210. + * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  99211. + * Peter Ujfalusi <peter.ujfalusi@ti.com>
  99212. + *
  99213. + * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  99214. + * Author: Timur Tabi <timur@freescale.com>
  99215. + * Copyright 2007-2010 Freescale Semiconductor, Inc.
  99216. + *
  99217. + * This program is free software; you can redistribute it and/or
  99218. + * modify it under the terms of the GNU General Public License
  99219. + * version 2 as published by the Free Software Foundation.
  99220. + *
  99221. + * This program is distributed in the hope that it will be useful, but
  99222. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  99223. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  99224. + * General Public License for more details.
  99225. + */
  99226. +
  99227. +#include <linux/init.h>
  99228. +#include <linux/module.h>
  99229. +#include <linux/device.h>
  99230. +#include <linux/slab.h>
  99231. +#include <linux/delay.h>
  99232. +#include <linux/io.h>
  99233. +#include <linux/clk.h>
  99234. +
  99235. +#include <sound/core.h>
  99236. +#include <sound/pcm.h>
  99237. +#include <sound/pcm_params.h>
  99238. +#include <sound/initval.h>
  99239. +#include <sound/soc.h>
  99240. +#include <sound/dmaengine_pcm.h>
  99241. +
  99242. +/* Clock registers */
  99243. +#define BCM2708_CLK_PCMCTL_REG 0x00
  99244. +#define BCM2708_CLK_PCMDIV_REG 0x04
  99245. +
  99246. +/* Clock register settings */
  99247. +#define BCM2708_CLK_PASSWD (0x5a000000)
  99248. +#define BCM2708_CLK_PASSWD_MASK (0xff000000)
  99249. +#define BCM2708_CLK_MASH(v) ((v) << 9)
  99250. +#define BCM2708_CLK_FLIP BIT(8)
  99251. +#define BCM2708_CLK_BUSY BIT(7)
  99252. +#define BCM2708_CLK_KILL BIT(5)
  99253. +#define BCM2708_CLK_ENAB BIT(4)
  99254. +#define BCM2708_CLK_SRC(v) (v)
  99255. +
  99256. +#define BCM2708_CLK_SHIFT (12)
  99257. +#define BCM2708_CLK_DIVI(v) ((v) << BCM2708_CLK_SHIFT)
  99258. +#define BCM2708_CLK_DIVF(v) (v)
  99259. +#define BCM2708_CLK_DIVF_MASK (0xFFF)
  99260. +
  99261. +enum {
  99262. + BCM2708_CLK_MASH_0 = 0,
  99263. + BCM2708_CLK_MASH_1,
  99264. + BCM2708_CLK_MASH_2,
  99265. + BCM2708_CLK_MASH_3,
  99266. +};
  99267. +
  99268. +enum {
  99269. + BCM2708_CLK_SRC_GND = 0,
  99270. + BCM2708_CLK_SRC_OSC,
  99271. + BCM2708_CLK_SRC_DBG0,
  99272. + BCM2708_CLK_SRC_DBG1,
  99273. + BCM2708_CLK_SRC_PLLA,
  99274. + BCM2708_CLK_SRC_PLLC,
  99275. + BCM2708_CLK_SRC_PLLD,
  99276. + BCM2708_CLK_SRC_HDMI,
  99277. +};
  99278. +
  99279. +/* Most clocks are not useable (freq = 0) */
  99280. +static const unsigned int bcm2708_clk_freq[BCM2708_CLK_SRC_HDMI+1] = {
  99281. + [BCM2708_CLK_SRC_GND] = 0,
  99282. + [BCM2708_CLK_SRC_OSC] = 19200000,
  99283. + [BCM2708_CLK_SRC_DBG0] = 0,
  99284. + [BCM2708_CLK_SRC_DBG1] = 0,
  99285. + [BCM2708_CLK_SRC_PLLA] = 0,
  99286. + [BCM2708_CLK_SRC_PLLC] = 0,
  99287. + [BCM2708_CLK_SRC_PLLD] = 500000000,
  99288. + [BCM2708_CLK_SRC_HDMI] = 0,
  99289. +};
  99290. +
  99291. +/* I2S registers */
  99292. +#define BCM2708_I2S_CS_A_REG 0x00
  99293. +#define BCM2708_I2S_FIFO_A_REG 0x04
  99294. +#define BCM2708_I2S_MODE_A_REG 0x08
  99295. +#define BCM2708_I2S_RXC_A_REG 0x0c
  99296. +#define BCM2708_I2S_TXC_A_REG 0x10
  99297. +#define BCM2708_I2S_DREQ_A_REG 0x14
  99298. +#define BCM2708_I2S_INTEN_A_REG 0x18
  99299. +#define BCM2708_I2S_INTSTC_A_REG 0x1c
  99300. +#define BCM2708_I2S_GRAY_REG 0x20
  99301. +
  99302. +/* I2S register settings */
  99303. +#define BCM2708_I2S_STBY BIT(25)
  99304. +#define BCM2708_I2S_SYNC BIT(24)
  99305. +#define BCM2708_I2S_RXSEX BIT(23)
  99306. +#define BCM2708_I2S_RXF BIT(22)
  99307. +#define BCM2708_I2S_TXE BIT(21)
  99308. +#define BCM2708_I2S_RXD BIT(20)
  99309. +#define BCM2708_I2S_TXD BIT(19)
  99310. +#define BCM2708_I2S_RXR BIT(18)
  99311. +#define BCM2708_I2S_TXW BIT(17)
  99312. +#define BCM2708_I2S_CS_RXERR BIT(16)
  99313. +#define BCM2708_I2S_CS_TXERR BIT(15)
  99314. +#define BCM2708_I2S_RXSYNC BIT(14)
  99315. +#define BCM2708_I2S_TXSYNC BIT(13)
  99316. +#define BCM2708_I2S_DMAEN BIT(9)
  99317. +#define BCM2708_I2S_RXTHR(v) ((v) << 7)
  99318. +#define BCM2708_I2S_TXTHR(v) ((v) << 5)
  99319. +#define BCM2708_I2S_RXCLR BIT(4)
  99320. +#define BCM2708_I2S_TXCLR BIT(3)
  99321. +#define BCM2708_I2S_TXON BIT(2)
  99322. +#define BCM2708_I2S_RXON BIT(1)
  99323. +#define BCM2708_I2S_EN (1)
  99324. +
  99325. +#define BCM2708_I2S_CLKDIS BIT(28)
  99326. +#define BCM2708_I2S_PDMN BIT(27)
  99327. +#define BCM2708_I2S_PDME BIT(26)
  99328. +#define BCM2708_I2S_FRXP BIT(25)
  99329. +#define BCM2708_I2S_FTXP BIT(24)
  99330. +#define BCM2708_I2S_CLKM BIT(23)
  99331. +#define BCM2708_I2S_CLKI BIT(22)
  99332. +#define BCM2708_I2S_FSM BIT(21)
  99333. +#define BCM2708_I2S_FSI BIT(20)
  99334. +#define BCM2708_I2S_FLEN(v) ((v) << 10)
  99335. +#define BCM2708_I2S_FSLEN(v) (v)
  99336. +
  99337. +#define BCM2708_I2S_CHWEX BIT(15)
  99338. +#define BCM2708_I2S_CHEN BIT(14)
  99339. +#define BCM2708_I2S_CHPOS(v) ((v) << 4)
  99340. +#define BCM2708_I2S_CHWID(v) (v)
  99341. +#define BCM2708_I2S_CH1(v) ((v) << 16)
  99342. +#define BCM2708_I2S_CH2(v) (v)
  99343. +
  99344. +#define BCM2708_I2S_TX_PANIC(v) ((v) << 24)
  99345. +#define BCM2708_I2S_RX_PANIC(v) ((v) << 16)
  99346. +#define BCM2708_I2S_TX(v) ((v) << 8)
  99347. +#define BCM2708_I2S_RX(v) (v)
  99348. +
  99349. +#define BCM2708_I2S_INT_RXERR BIT(3)
  99350. +#define BCM2708_I2S_INT_TXERR BIT(2)
  99351. +#define BCM2708_I2S_INT_RXR BIT(1)
  99352. +#define BCM2708_I2S_INT_TXW BIT(0)
  99353. +
  99354. +/* I2S DMA interface */
  99355. +#define BCM2708_I2S_FIFO_PHYSICAL_ADDR 0x7E203004
  99356. +#define BCM2708_DMA_DREQ_PCM_TX 2
  99357. +#define BCM2708_DMA_DREQ_PCM_RX 3
  99358. +
  99359. +/* General device struct */
  99360. +struct bcm2708_i2s_dev {
  99361. + struct device *dev;
  99362. + struct snd_dmaengine_dai_dma_data dma_data[2];
  99363. + unsigned int fmt;
  99364. + unsigned int bclk_ratio;
  99365. +
  99366. + struct regmap *i2s_regmap;
  99367. + struct regmap *clk_regmap;
  99368. +};
  99369. +
  99370. +static void bcm2708_i2s_start_clock(struct bcm2708_i2s_dev *dev)
  99371. +{
  99372. + /* Start the clock if in master mode */
  99373. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  99374. +
  99375. + switch (master) {
  99376. + case SND_SOC_DAIFMT_CBS_CFS:
  99377. + case SND_SOC_DAIFMT_CBS_CFM:
  99378. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99379. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  99380. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  99381. + break;
  99382. + default:
  99383. + break;
  99384. + }
  99385. +}
  99386. +
  99387. +static void bcm2708_i2s_stop_clock(struct bcm2708_i2s_dev *dev)
  99388. +{
  99389. + uint32_t clkreg;
  99390. + int timeout = 1000;
  99391. +
  99392. + /* Stop clock */
  99393. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99394. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  99395. + BCM2708_CLK_PASSWD);
  99396. +
  99397. + /* Wait for the BUSY flag going down */
  99398. + while (--timeout) {
  99399. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  99400. + if (!(clkreg & BCM2708_CLK_BUSY))
  99401. + break;
  99402. + }
  99403. +
  99404. + if (!timeout) {
  99405. + /* KILL the clock */
  99406. + dev_err(dev->dev, "I2S clock didn't stop. Kill the clock!\n");
  99407. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99408. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD_MASK,
  99409. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD);
  99410. + }
  99411. +}
  99412. +
  99413. +static void bcm2708_i2s_clear_fifos(struct bcm2708_i2s_dev *dev,
  99414. + bool tx, bool rx)
  99415. +{
  99416. + int timeout = 1000;
  99417. + uint32_t syncval;
  99418. + uint32_t csreg;
  99419. + uint32_t i2s_active_state;
  99420. + uint32_t clkreg;
  99421. + uint32_t clk_active_state;
  99422. + uint32_t off;
  99423. + uint32_t clr;
  99424. +
  99425. + off = tx ? BCM2708_I2S_TXON : 0;
  99426. + off |= rx ? BCM2708_I2S_RXON : 0;
  99427. +
  99428. + clr = tx ? BCM2708_I2S_TXCLR : 0;
  99429. + clr |= rx ? BCM2708_I2S_RXCLR : 0;
  99430. +
  99431. + /* Backup the current state */
  99432. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  99433. + i2s_active_state = csreg & (BCM2708_I2S_RXON | BCM2708_I2S_TXON);
  99434. +
  99435. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  99436. + clk_active_state = clkreg & BCM2708_CLK_ENAB;
  99437. +
  99438. + /* Start clock if not running */
  99439. + if (!clk_active_state) {
  99440. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  99441. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  99442. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  99443. + }
  99444. +
  99445. + /* Stop I2S module */
  99446. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, off, 0);
  99447. +
  99448. + /*
  99449. + * Clear the FIFOs
  99450. + * Requires at least 2 PCM clock cycles to take effect
  99451. + */
  99452. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, clr, clr);
  99453. +
  99454. + /* Wait for 2 PCM clock cycles */
  99455. +
  99456. + /*
  99457. + * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  99458. + * FIXME: This does not seem to work for slave mode!
  99459. + */
  99460. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &syncval);
  99461. + syncval &= BCM2708_I2S_SYNC;
  99462. +
  99463. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99464. + BCM2708_I2S_SYNC, ~syncval);
  99465. +
  99466. + /* Wait for the SYNC flag changing it's state */
  99467. + while (--timeout) {
  99468. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  99469. + if ((csreg & BCM2708_I2S_SYNC) != syncval)
  99470. + break;
  99471. + }
  99472. +
  99473. + if (!timeout)
  99474. + dev_err(dev->dev, "I2S SYNC error!\n");
  99475. +
  99476. + /* Stop clock if it was not running before */
  99477. + if (!clk_active_state)
  99478. + bcm2708_i2s_stop_clock(dev);
  99479. +
  99480. + /* Restore I2S state */
  99481. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99482. + BCM2708_I2S_RXON | BCM2708_I2S_TXON, i2s_active_state);
  99483. +}
  99484. +
  99485. +static int bcm2708_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  99486. + unsigned int fmt)
  99487. +{
  99488. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99489. + dev->fmt = fmt;
  99490. + return 0;
  99491. +}
  99492. +
  99493. +static int bcm2708_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  99494. + unsigned int ratio)
  99495. +{
  99496. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99497. + dev->bclk_ratio = ratio;
  99498. + return 0;
  99499. +}
  99500. +
  99501. +static int bcm2708_i2s_hw_params(struct snd_pcm_substream *substream,
  99502. + struct snd_pcm_hw_params *params,
  99503. + struct snd_soc_dai *dai)
  99504. +{
  99505. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99506. +
  99507. + unsigned int sampling_rate = params_rate(params);
  99508. + unsigned int data_length, data_delay, bclk_ratio;
  99509. + unsigned int ch1pos, ch2pos, mode, format;
  99510. + unsigned int mash = BCM2708_CLK_MASH_1;
  99511. + unsigned int divi, divf, target_frequency;
  99512. + int clk_src = -1;
  99513. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  99514. + bool bit_master = (master == SND_SOC_DAIFMT_CBS_CFS
  99515. + || master == SND_SOC_DAIFMT_CBS_CFM);
  99516. +
  99517. + bool frame_master = (master == SND_SOC_DAIFMT_CBS_CFS
  99518. + || master == SND_SOC_DAIFMT_CBM_CFS);
  99519. + uint32_t csreg;
  99520. +
  99521. + /*
  99522. + * If a stream is already enabled,
  99523. + * the registers are already set properly.
  99524. + */
  99525. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  99526. +
  99527. + if (csreg & (BCM2708_I2S_TXON | BCM2708_I2S_RXON))
  99528. + return 0;
  99529. +
  99530. + /*
  99531. + * Adjust the data length according to the format.
  99532. + * We prefill the half frame length with an integer
  99533. + * divider of 2400 as explained at the clock settings.
  99534. + * Maybe it is overwritten there, if the Integer mode
  99535. + * does not apply.
  99536. + */
  99537. + switch (params_format(params)) {
  99538. + case SNDRV_PCM_FORMAT_S16_LE:
  99539. + data_length = 16;
  99540. + bclk_ratio = 40;
  99541. + break;
  99542. + case SNDRV_PCM_FORMAT_S24_LE:
  99543. + data_length = 24;
  99544. + bclk_ratio = 40;
  99545. + break;
  99546. + case SNDRV_PCM_FORMAT_S32_LE:
  99547. + data_length = 32;
  99548. + bclk_ratio = 80;
  99549. + break;
  99550. + default:
  99551. + return -EINVAL;
  99552. + }
  99553. +
  99554. + /* If bclk_ratio already set, use that one. */
  99555. + if (dev->bclk_ratio)
  99556. + bclk_ratio = dev->bclk_ratio;
  99557. +
  99558. + /*
  99559. + * Clock Settings
  99560. + *
  99561. + * The target frequency of the bit clock is
  99562. + * sampling rate * frame length
  99563. + *
  99564. + * Integer mode:
  99565. + * Sampling rates that are multiples of 8000 kHz
  99566. + * can be driven by the oscillator of 19.2 MHz
  99567. + * with an integer divider as long as the frame length
  99568. + * is an integer divider of 19200000/8000=2400 as set up above.
  99569. + * This is no longer possible if the sampling rate
  99570. + * is too high (e.g. 192 kHz), because the oscillator is too slow.
  99571. + *
  99572. + * MASH mode:
  99573. + * For all other sampling rates, it is not possible to
  99574. + * have an integer divider. Approximate the clock
  99575. + * with the MASH module that induces a slight frequency
  99576. + * variance. To minimize that it is best to have the fastest
  99577. + * clock here. That is PLLD with 500 MHz.
  99578. + */
  99579. + target_frequency = sampling_rate * bclk_ratio;
  99580. + clk_src = BCM2708_CLK_SRC_OSC;
  99581. + mash = BCM2708_CLK_MASH_0;
  99582. +
  99583. + if (bcm2708_clk_freq[clk_src] % target_frequency == 0
  99584. + && bit_master && frame_master) {
  99585. + divi = bcm2708_clk_freq[clk_src] / target_frequency;
  99586. + divf = 0;
  99587. + } else {
  99588. + uint64_t dividend;
  99589. +
  99590. + if (!dev->bclk_ratio) {
  99591. + /*
  99592. + * Overwrite bclk_ratio, because the
  99593. + * above trick is not needed or can
  99594. + * not be used.
  99595. + */
  99596. + bclk_ratio = 2 * data_length;
  99597. + }
  99598. +
  99599. + target_frequency = sampling_rate * bclk_ratio;
  99600. +
  99601. + clk_src = BCM2708_CLK_SRC_PLLD;
  99602. + mash = BCM2708_CLK_MASH_1;
  99603. +
  99604. + dividend = bcm2708_clk_freq[clk_src];
  99605. + dividend <<= BCM2708_CLK_SHIFT;
  99606. + do_div(dividend, target_frequency);
  99607. + divi = dividend >> BCM2708_CLK_SHIFT;
  99608. + divf = dividend & BCM2708_CLK_DIVF_MASK;
  99609. + }
  99610. +
  99611. + /* Set clock divider */
  99612. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMDIV_REG, BCM2708_CLK_PASSWD
  99613. + | BCM2708_CLK_DIVI(divi)
  99614. + | BCM2708_CLK_DIVF(divf));
  99615. +
  99616. + /* Setup clock, but don't start it yet */
  99617. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, BCM2708_CLK_PASSWD
  99618. + | BCM2708_CLK_MASH(mash)
  99619. + | BCM2708_CLK_SRC(clk_src));
  99620. +
  99621. + /* Setup the frame format */
  99622. + format = BCM2708_I2S_CHEN;
  99623. +
  99624. + if (data_length >= 24)
  99625. + format |= BCM2708_I2S_CHWEX;
  99626. +
  99627. + format |= BCM2708_I2S_CHWID((data_length-8)&0xf);
  99628. +
  99629. + switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  99630. + case SND_SOC_DAIFMT_I2S:
  99631. + data_delay = 1;
  99632. + break;
  99633. + default:
  99634. + /*
  99635. + * TODO
  99636. + * Others are possible but are not implemented at the moment.
  99637. + */
  99638. + dev_err(dev->dev, "%s:bad format\n", __func__);
  99639. + return -EINVAL;
  99640. + }
  99641. +
  99642. + ch1pos = data_delay;
  99643. + ch2pos = bclk_ratio / 2 + data_delay;
  99644. +
  99645. + switch (params_channels(params)) {
  99646. + case 2:
  99647. + format = BCM2708_I2S_CH1(format) | BCM2708_I2S_CH2(format);
  99648. + format |= BCM2708_I2S_CH1(BCM2708_I2S_CHPOS(ch1pos));
  99649. + format |= BCM2708_I2S_CH2(BCM2708_I2S_CHPOS(ch2pos));
  99650. + break;
  99651. + default:
  99652. + return -EINVAL;
  99653. + }
  99654. +
  99655. + /*
  99656. + * Set format for both streams.
  99657. + * We cannot set another frame length
  99658. + * (and therefore word length) anyway,
  99659. + * so the format will be the same.
  99660. + */
  99661. + regmap_write(dev->i2s_regmap, BCM2708_I2S_RXC_A_REG, format);
  99662. + regmap_write(dev->i2s_regmap, BCM2708_I2S_TXC_A_REG, format);
  99663. +
  99664. + /* Setup the I2S mode */
  99665. + mode = 0;
  99666. +
  99667. + if (data_length <= 16) {
  99668. + /*
  99669. + * Use frame packed mode (2 channels per 32 bit word)
  99670. + * We cannot set another frame length in the second stream
  99671. + * (and therefore word length) anyway,
  99672. + * so the format will be the same.
  99673. + */
  99674. + mode |= BCM2708_I2S_FTXP | BCM2708_I2S_FRXP;
  99675. + }
  99676. +
  99677. + mode |= BCM2708_I2S_FLEN(bclk_ratio - 1);
  99678. + mode |= BCM2708_I2S_FSLEN(bclk_ratio / 2);
  99679. +
  99680. + /* Master or slave? */
  99681. + switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  99682. + case SND_SOC_DAIFMT_CBS_CFS:
  99683. + /* CPU is master */
  99684. + break;
  99685. + case SND_SOC_DAIFMT_CBM_CFS:
  99686. + /*
  99687. + * CODEC is bit clock master
  99688. + * CPU is frame master
  99689. + */
  99690. + mode |= BCM2708_I2S_CLKM;
  99691. + break;
  99692. + case SND_SOC_DAIFMT_CBS_CFM:
  99693. + /*
  99694. + * CODEC is frame master
  99695. + * CPU is bit clock master
  99696. + */
  99697. + mode |= BCM2708_I2S_FSM;
  99698. + break;
  99699. + case SND_SOC_DAIFMT_CBM_CFM:
  99700. + /* CODEC is master */
  99701. + mode |= BCM2708_I2S_CLKM;
  99702. + mode |= BCM2708_I2S_FSM;
  99703. + break;
  99704. + default:
  99705. + dev_err(dev->dev, "%s:bad master\n", __func__);
  99706. + return -EINVAL;
  99707. + }
  99708. +
  99709. + /*
  99710. + * Invert clocks?
  99711. + *
  99712. + * The BCM approach seems to be inverted to the classical I2S approach.
  99713. + */
  99714. + switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  99715. + case SND_SOC_DAIFMT_NB_NF:
  99716. + /* None. Therefore, both for BCM */
  99717. + mode |= BCM2708_I2S_CLKI;
  99718. + mode |= BCM2708_I2S_FSI;
  99719. + break;
  99720. + case SND_SOC_DAIFMT_IB_IF:
  99721. + /* Both. Therefore, none for BCM */
  99722. + break;
  99723. + case SND_SOC_DAIFMT_NB_IF:
  99724. + /*
  99725. + * Invert only frame sync. Therefore,
  99726. + * invert only bit clock for BCM
  99727. + */
  99728. + mode |= BCM2708_I2S_CLKI;
  99729. + break;
  99730. + case SND_SOC_DAIFMT_IB_NF:
  99731. + /*
  99732. + * Invert only bit clock. Therefore,
  99733. + * invert only frame sync for BCM
  99734. + */
  99735. + mode |= BCM2708_I2S_FSI;
  99736. + break;
  99737. + default:
  99738. + return -EINVAL;
  99739. + }
  99740. +
  99741. + regmap_write(dev->i2s_regmap, BCM2708_I2S_MODE_A_REG, mode);
  99742. +
  99743. + /* Setup the DMA parameters */
  99744. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99745. + BCM2708_I2S_RXTHR(1)
  99746. + | BCM2708_I2S_TXTHR(1)
  99747. + | BCM2708_I2S_DMAEN, 0xffffffff);
  99748. +
  99749. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_DREQ_A_REG,
  99750. + BCM2708_I2S_TX_PANIC(0x10)
  99751. + | BCM2708_I2S_RX_PANIC(0x30)
  99752. + | BCM2708_I2S_TX(0x30)
  99753. + | BCM2708_I2S_RX(0x20), 0xffffffff);
  99754. +
  99755. + /* Clear FIFOs */
  99756. + bcm2708_i2s_clear_fifos(dev, true, true);
  99757. +
  99758. + return 0;
  99759. +}
  99760. +
  99761. +static int bcm2708_i2s_prepare(struct snd_pcm_substream *substream,
  99762. + struct snd_soc_dai *dai)
  99763. +{
  99764. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99765. + uint32_t cs_reg;
  99766. +
  99767. + bcm2708_i2s_start_clock(dev);
  99768. +
  99769. + /*
  99770. + * Clear both FIFOs if the one that should be started
  99771. + * is not empty at the moment. This should only happen
  99772. + * after overrun. Otherwise, hw_params would have cleared
  99773. + * the FIFO.
  99774. + */
  99775. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &cs_reg);
  99776. +
  99777. + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  99778. + && !(cs_reg & BCM2708_I2S_TXE))
  99779. + bcm2708_i2s_clear_fifos(dev, true, false);
  99780. + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  99781. + && (cs_reg & BCM2708_I2S_RXD))
  99782. + bcm2708_i2s_clear_fifos(dev, false, true);
  99783. +
  99784. + return 0;
  99785. +}
  99786. +
  99787. +static void bcm2708_i2s_stop(struct bcm2708_i2s_dev *dev,
  99788. + struct snd_pcm_substream *substream,
  99789. + struct snd_soc_dai *dai)
  99790. +{
  99791. + uint32_t mask;
  99792. +
  99793. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  99794. + mask = BCM2708_I2S_RXON;
  99795. + else
  99796. + mask = BCM2708_I2S_TXON;
  99797. +
  99798. + regmap_update_bits(dev->i2s_regmap,
  99799. + BCM2708_I2S_CS_A_REG, mask, 0);
  99800. +
  99801. + /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  99802. + if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  99803. + bcm2708_i2s_stop_clock(dev);
  99804. +}
  99805. +
  99806. +static int bcm2708_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  99807. + struct snd_soc_dai *dai)
  99808. +{
  99809. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99810. + uint32_t mask;
  99811. +
  99812. + switch (cmd) {
  99813. + case SNDRV_PCM_TRIGGER_START:
  99814. + case SNDRV_PCM_TRIGGER_RESUME:
  99815. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  99816. + bcm2708_i2s_start_clock(dev);
  99817. +
  99818. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  99819. + mask = BCM2708_I2S_RXON;
  99820. + else
  99821. + mask = BCM2708_I2S_TXON;
  99822. +
  99823. + regmap_update_bits(dev->i2s_regmap,
  99824. + BCM2708_I2S_CS_A_REG, mask, mask);
  99825. + break;
  99826. +
  99827. + case SNDRV_PCM_TRIGGER_STOP:
  99828. + case SNDRV_PCM_TRIGGER_SUSPEND:
  99829. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  99830. + bcm2708_i2s_stop(dev, substream, dai);
  99831. + break;
  99832. + default:
  99833. + return -EINVAL;
  99834. + }
  99835. +
  99836. + return 0;
  99837. +}
  99838. +
  99839. +static int bcm2708_i2s_startup(struct snd_pcm_substream *substream,
  99840. + struct snd_soc_dai *dai)
  99841. +{
  99842. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99843. +
  99844. + if (dai->active)
  99845. + return 0;
  99846. +
  99847. + /* Should this still be running stop it */
  99848. + bcm2708_i2s_stop_clock(dev);
  99849. +
  99850. + /* Enable PCM block */
  99851. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99852. + BCM2708_I2S_EN, BCM2708_I2S_EN);
  99853. +
  99854. + /*
  99855. + * Disable STBY.
  99856. + * Requires at least 4 PCM clock cycles to take effect.
  99857. + */
  99858. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99859. + BCM2708_I2S_STBY, BCM2708_I2S_STBY);
  99860. +
  99861. + return 0;
  99862. +}
  99863. +
  99864. +static void bcm2708_i2s_shutdown(struct snd_pcm_substream *substream,
  99865. + struct snd_soc_dai *dai)
  99866. +{
  99867. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99868. +
  99869. + bcm2708_i2s_stop(dev, substream, dai);
  99870. +
  99871. + /* If both streams are stopped, disable module and clock */
  99872. + if (dai->active)
  99873. + return;
  99874. +
  99875. + /* Disable the module */
  99876. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99877. + BCM2708_I2S_EN, 0);
  99878. +
  99879. + /*
  99880. + * Stopping clock is necessary, because stop does
  99881. + * not stop the clock when SND_SOC_DAIFMT_CONT
  99882. + */
  99883. + bcm2708_i2s_stop_clock(dev);
  99884. +}
  99885. +
  99886. +static const struct snd_soc_dai_ops bcm2708_i2s_dai_ops = {
  99887. + .startup = bcm2708_i2s_startup,
  99888. + .shutdown = bcm2708_i2s_shutdown,
  99889. + .prepare = bcm2708_i2s_prepare,
  99890. + .trigger = bcm2708_i2s_trigger,
  99891. + .hw_params = bcm2708_i2s_hw_params,
  99892. + .set_fmt = bcm2708_i2s_set_dai_fmt,
  99893. + .set_bclk_ratio = bcm2708_i2s_set_dai_bclk_ratio
  99894. +};
  99895. +
  99896. +static int bcm2708_i2s_dai_probe(struct snd_soc_dai *dai)
  99897. +{
  99898. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99899. +
  99900. + dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  99901. + dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  99902. +
  99903. + return 0;
  99904. +}
  99905. +
  99906. +static struct snd_soc_dai_driver bcm2708_i2s_dai = {
  99907. + .name = "bcm2708-i2s",
  99908. + .probe = bcm2708_i2s_dai_probe,
  99909. + .playback = {
  99910. + .channels_min = 2,
  99911. + .channels_max = 2,
  99912. + .rates = SNDRV_PCM_RATE_8000_192000,
  99913. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  99914. + | SNDRV_PCM_FMTBIT_S24_LE
  99915. + | SNDRV_PCM_FMTBIT_S32_LE
  99916. + },
  99917. + .capture = {
  99918. + .channels_min = 2,
  99919. + .channels_max = 2,
  99920. + .rates = SNDRV_PCM_RATE_8000_192000,
  99921. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  99922. + | SNDRV_PCM_FMTBIT_S24_LE
  99923. + | SNDRV_PCM_FMTBIT_S32_LE
  99924. + },
  99925. + .ops = &bcm2708_i2s_dai_ops,
  99926. + .symmetric_rates = 1
  99927. +};
  99928. +
  99929. +static bool bcm2708_i2s_volatile_reg(struct device *dev, unsigned int reg)
  99930. +{
  99931. + switch (reg) {
  99932. + case BCM2708_I2S_CS_A_REG:
  99933. + case BCM2708_I2S_FIFO_A_REG:
  99934. + case BCM2708_I2S_INTSTC_A_REG:
  99935. + case BCM2708_I2S_GRAY_REG:
  99936. + return true;
  99937. + default:
  99938. + return false;
  99939. + };
  99940. +}
  99941. +
  99942. +static bool bcm2708_i2s_precious_reg(struct device *dev, unsigned int reg)
  99943. +{
  99944. + switch (reg) {
  99945. + case BCM2708_I2S_FIFO_A_REG:
  99946. + return true;
  99947. + default:
  99948. + return false;
  99949. + };
  99950. +}
  99951. +
  99952. +static bool bcm2708_clk_volatile_reg(struct device *dev, unsigned int reg)
  99953. +{
  99954. + switch (reg) {
  99955. + case BCM2708_CLK_PCMCTL_REG:
  99956. + return true;
  99957. + default:
  99958. + return false;
  99959. + };
  99960. +}
  99961. +
  99962. +static const struct regmap_config bcm2708_regmap_config[] = {
  99963. + {
  99964. + .reg_bits = 32,
  99965. + .reg_stride = 4,
  99966. + .val_bits = 32,
  99967. + .max_register = BCM2708_I2S_GRAY_REG,
  99968. + .precious_reg = bcm2708_i2s_precious_reg,
  99969. + .volatile_reg = bcm2708_i2s_volatile_reg,
  99970. + .cache_type = REGCACHE_RBTREE,
  99971. + },
  99972. + {
  99973. + .reg_bits = 32,
  99974. + .reg_stride = 4,
  99975. + .val_bits = 32,
  99976. + .max_register = BCM2708_CLK_PCMDIV_REG,
  99977. + .volatile_reg = bcm2708_clk_volatile_reg,
  99978. + .cache_type = REGCACHE_RBTREE,
  99979. + },
  99980. +};
  99981. +
  99982. +static const struct snd_soc_component_driver bcm2708_i2s_component = {
  99983. + .name = "bcm2708-i2s-comp",
  99984. +};
  99985. +
  99986. +
  99987. +static void bcm2708_i2s_setup_gpio(void)
  99988. +{
  99989. + /*
  99990. + * This is the common way to handle the GPIO pins for
  99991. + * the Raspberry Pi.
  99992. + * TODO Better way would be to handle
  99993. + * this in the device tree!
  99994. + */
  99995. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  99996. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  99997. +
  99998. + unsigned int *gpio;
  99999. + int pin;
  100000. + gpio = ioremap(GPIO_BASE, SZ_16K);
  100001. +
  100002. + /* SPI is on GPIO 7..11 */
  100003. + for (pin = 28; pin <= 31; pin++) {
  100004. + INP_GPIO(pin); /* set mode to GPIO input first */
  100005. + SET_GPIO_ALT(pin, 2); /* set mode to ALT 0 */
  100006. + }
  100007. +#undef INP_GPIO
  100008. +#undef SET_GPIO_ALT
  100009. +}
  100010. +
  100011. +static const struct snd_pcm_hardware bcm2708_pcm_hardware = {
  100012. + .info = SNDRV_PCM_INFO_INTERLEAVED |
  100013. + SNDRV_PCM_INFO_JOINT_DUPLEX,
  100014. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100015. + SNDRV_PCM_FMTBIT_S24_LE |
  100016. + SNDRV_PCM_FMTBIT_S32_LE,
  100017. + .period_bytes_min = 32,
  100018. + .period_bytes_max = 64 * PAGE_SIZE,
  100019. + .periods_min = 2,
  100020. + .periods_max = 255,
  100021. + .buffer_bytes_max = 128 * PAGE_SIZE,
  100022. +};
  100023. +
  100024. +static const struct snd_dmaengine_pcm_config bcm2708_dmaengine_pcm_config = {
  100025. + .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  100026. + .pcm_hardware = &bcm2708_pcm_hardware,
  100027. + .prealloc_buffer_size = 256 * PAGE_SIZE,
  100028. +};
  100029. +
  100030. +
  100031. +static int bcm2708_i2s_probe(struct platform_device *pdev)
  100032. +{
  100033. + struct bcm2708_i2s_dev *dev;
  100034. + int i;
  100035. + int ret;
  100036. + struct regmap *regmap[2];
  100037. + struct resource *mem[2];
  100038. +
  100039. + /* Request both ioareas */
  100040. + for (i = 0; i <= 1; i++) {
  100041. + void __iomem *base;
  100042. +
  100043. + mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
  100044. + base = devm_ioremap_resource(&pdev->dev, mem[i]);
  100045. + if (IS_ERR(base))
  100046. + return PTR_ERR(base);
  100047. +
  100048. + regmap[i] = devm_regmap_init_mmio(&pdev->dev, base,
  100049. + &bcm2708_regmap_config[i]);
  100050. + if (IS_ERR(regmap[i])) {
  100051. + dev_err(&pdev->dev, "I2S probe: regmap init failed\n");
  100052. + return PTR_ERR(regmap[i]);
  100053. + }
  100054. + }
  100055. +
  100056. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  100057. + GFP_KERNEL);
  100058. + if (IS_ERR(dev))
  100059. + return PTR_ERR(dev);
  100060. +
  100061. + bcm2708_i2s_setup_gpio();
  100062. +
  100063. + dev->i2s_regmap = regmap[0];
  100064. + dev->clk_regmap = regmap[1];
  100065. +
  100066. + /* Set the DMA address */
  100067. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  100068. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  100069. +
  100070. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  100071. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  100072. +
  100073. + /* Set the DREQ */
  100074. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
  100075. + BCM2708_DMA_DREQ_PCM_TX;
  100076. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
  100077. + BCM2708_DMA_DREQ_PCM_RX;
  100078. +
  100079. + /* Set the bus width */
  100080. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  100081. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  100082. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  100083. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  100084. +
  100085. + /* Set burst */
  100086. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  100087. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  100088. +
  100089. + /* BCLK ratio - use default */
  100090. + dev->bclk_ratio = 0;
  100091. +
  100092. + /* Store the pdev */
  100093. + dev->dev = &pdev->dev;
  100094. + dev_set_drvdata(&pdev->dev, dev);
  100095. +
  100096. + ret = snd_soc_register_component(&pdev->dev,
  100097. + &bcm2708_i2s_component, &bcm2708_i2s_dai, 1);
  100098. +
  100099. + if (ret) {
  100100. + dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  100101. + ret = -ENOMEM;
  100102. + return ret;
  100103. + }
  100104. +
  100105. + ret = snd_dmaengine_pcm_register(&pdev->dev,
  100106. + &bcm2708_dmaengine_pcm_config,
  100107. + SND_DMAENGINE_PCM_FLAG_COMPAT);
  100108. + if (ret) {
  100109. + dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  100110. + snd_soc_unregister_component(&pdev->dev);
  100111. + return ret;
  100112. + }
  100113. +
  100114. + return 0;
  100115. +}
  100116. +
  100117. +static int bcm2708_i2s_remove(struct platform_device *pdev)
  100118. +{
  100119. + snd_dmaengine_pcm_unregister(&pdev->dev);
  100120. + snd_soc_unregister_component(&pdev->dev);
  100121. + return 0;
  100122. +}
  100123. +
  100124. +static struct platform_driver bcm2708_i2s_driver = {
  100125. + .probe = bcm2708_i2s_probe,
  100126. + .remove = bcm2708_i2s_remove,
  100127. + .driver = {
  100128. + .name = "bcm2708-i2s",
  100129. + .owner = THIS_MODULE,
  100130. + },
  100131. +};
  100132. +
  100133. +module_platform_driver(bcm2708_i2s_driver);
  100134. +
  100135. +MODULE_ALIAS("platform:bcm2708-i2s");
  100136. +MODULE_DESCRIPTION("BCM2708 I2S interface");
  100137. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100138. +MODULE_LICENSE("GPL v2");
  100139. diff -Nur linux-3.10.37/sound/soc/bcm/hifiberry_dac.c linux-rpi/sound/soc/bcm/hifiberry_dac.c
  100140. --- linux-3.10.37/sound/soc/bcm/hifiberry_dac.c 1970-01-01 01:00:00.000000000 +0100
  100141. +++ linux-rpi/sound/soc/bcm/hifiberry_dac.c 2014-04-24 15:35:05.481580345 +0200
  100142. @@ -0,0 +1,100 @@
  100143. +/*
  100144. + * ASoC Driver for HifiBerry DAC
  100145. + *
  100146. + * Author: Florian Meier <florian.meier@koalo.de>
  100147. + * Copyright 2013
  100148. + *
  100149. + * This program is free software; you can redistribute it and/or
  100150. + * modify it under the terms of the GNU General Public License
  100151. + * version 2 as published by the Free Software Foundation.
  100152. + *
  100153. + * This program is distributed in the hope that it will be useful, but
  100154. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100155. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100156. + * General Public License for more details.
  100157. + */
  100158. +
  100159. +#include <linux/module.h>
  100160. +#include <linux/platform_device.h>
  100161. +
  100162. +#include <sound/core.h>
  100163. +#include <sound/pcm.h>
  100164. +#include <sound/pcm_params.h>
  100165. +#include <sound/soc.h>
  100166. +#include <sound/jack.h>
  100167. +
  100168. +static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
  100169. +{
  100170. + return 0;
  100171. +}
  100172. +
  100173. +static int snd_rpi_hifiberry_dac_hw_params(struct snd_pcm_substream *substream,
  100174. + struct snd_pcm_hw_params *params)
  100175. +{
  100176. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100177. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100178. +
  100179. + unsigned int sample_bits =
  100180. + snd_pcm_format_physical_width(params_format(params));
  100181. +
  100182. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  100183. +}
  100184. +
  100185. +/* machine stream operations */
  100186. +static struct snd_soc_ops snd_rpi_hifiberry_dac_ops = {
  100187. + .hw_params = snd_rpi_hifiberry_dac_hw_params,
  100188. +};
  100189. +
  100190. +static struct snd_soc_dai_link snd_rpi_hifiberry_dac_dai[] = {
  100191. +{
  100192. + .name = "HifiBerry DAC",
  100193. + .stream_name = "HifiBerry DAC HiFi",
  100194. + .cpu_dai_name = "bcm2708-i2s.0",
  100195. + .codec_dai_name = "pcm5102a-hifi",
  100196. + .platform_name = "bcm2708-i2s.0",
  100197. + .codec_name = "pcm5102a-codec",
  100198. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100199. + SND_SOC_DAIFMT_CBS_CFS,
  100200. + .ops = &snd_rpi_hifiberry_dac_ops,
  100201. + .init = snd_rpi_hifiberry_dac_init,
  100202. +},
  100203. +};
  100204. +
  100205. +/* audio machine driver */
  100206. +static struct snd_soc_card snd_rpi_hifiberry_dac = {
  100207. + .name = "snd_rpi_hifiberry_dac",
  100208. + .dai_link = snd_rpi_hifiberry_dac_dai,
  100209. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dac_dai),
  100210. +};
  100211. +
  100212. +static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
  100213. +{
  100214. + int ret = 0;
  100215. +
  100216. + snd_rpi_hifiberry_dac.dev = &pdev->dev;
  100217. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);
  100218. + if (ret)
  100219. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100220. +
  100221. + return ret;
  100222. +}
  100223. +
  100224. +static int snd_rpi_hifiberry_dac_remove(struct platform_device *pdev)
  100225. +{
  100226. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dac);
  100227. +}
  100228. +
  100229. +static struct platform_driver snd_rpi_hifiberry_dac_driver = {
  100230. + .driver = {
  100231. + .name = "snd-hifiberry-dac",
  100232. + .owner = THIS_MODULE,
  100233. + },
  100234. + .probe = snd_rpi_hifiberry_dac_probe,
  100235. + .remove = snd_rpi_hifiberry_dac_remove,
  100236. +};
  100237. +
  100238. +module_platform_driver(snd_rpi_hifiberry_dac_driver);
  100239. +
  100240. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100241. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry DAC");
  100242. +MODULE_LICENSE("GPL v2");
  100243. diff -Nur linux-3.10.37/sound/soc/bcm/hifiberry_digi.c linux-rpi/sound/soc/bcm/hifiberry_digi.c
  100244. --- linux-3.10.37/sound/soc/bcm/hifiberry_digi.c 1970-01-01 01:00:00.000000000 +0100
  100245. +++ linux-rpi/sound/soc/bcm/hifiberry_digi.c 2014-04-24 15:35:05.481580345 +0200
  100246. @@ -0,0 +1,153 @@
  100247. +/*
  100248. + * ASoC Driver for HifiBerry Digi
  100249. + *
  100250. + * Author: Daniel Matuschek <info@crazy-audio.com>
  100251. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  100252. + * Copyright 2013
  100253. + *
  100254. + * This program is free software; you can redistribute it and/or
  100255. + * modify it under the terms of the GNU General Public License
  100256. + * version 2 as published by the Free Software Foundation.
  100257. + *
  100258. + * This program is distributed in the hope that it will be useful, but
  100259. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100260. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100261. + * General Public License for more details.
  100262. + */
  100263. +
  100264. +#include <linux/module.h>
  100265. +#include <linux/platform_device.h>
  100266. +
  100267. +#include <sound/core.h>
  100268. +#include <sound/pcm.h>
  100269. +#include <sound/pcm_params.h>
  100270. +#include <sound/soc.h>
  100271. +#include <sound/jack.h>
  100272. +
  100273. +#include "../codecs/wm8804.h"
  100274. +
  100275. +static int samplerate=44100;
  100276. +
  100277. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  100278. +{
  100279. + struct snd_soc_codec *codec = rtd->codec;
  100280. +
  100281. + /* enable TX output */
  100282. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  100283. +
  100284. + return 0;
  100285. +}
  100286. +
  100287. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  100288. + struct snd_pcm_hw_params *params)
  100289. +{
  100290. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100291. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  100292. + struct snd_soc_codec *codec = rtd->codec;
  100293. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100294. +
  100295. + int sysclk = 27000000; /* This is fixed on this board */
  100296. +
  100297. + long mclk_freq=0;
  100298. + int mclk_div=1;
  100299. +
  100300. + int ret;
  100301. +
  100302. + samplerate = params_rate(params);
  100303. +
  100304. + switch (samplerate) {
  100305. + case 44100:
  100306. + case 48000:
  100307. + case 88200:
  100308. + case 96000:
  100309. + mclk_freq=samplerate*256;
  100310. + mclk_div=WM8804_MCLKDIV_256FS;
  100311. + break;
  100312. + case 176400:
  100313. + case 192000:
  100314. + mclk_freq=samplerate*128;
  100315. + mclk_div=WM8804_MCLKDIV_128FS;
  100316. + break;
  100317. + default:
  100318. + dev_err(substream->pcm->dev,
  100319. + "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
  100320. + }
  100321. +
  100322. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  100323. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  100324. +
  100325. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  100326. + sysclk, SND_SOC_CLOCK_OUT);
  100327. + if (ret < 0) {
  100328. + dev_err(substream->pcm->dev,
  100329. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  100330. + return ret;
  100331. + }
  100332. +
  100333. + /* Enable TX output */
  100334. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  100335. +
  100336. + /* Power on */
  100337. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  100338. +
  100339. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  100340. +}
  100341. +
  100342. +/* machine stream operations */
  100343. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  100344. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  100345. +};
  100346. +
  100347. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  100348. +{
  100349. + .name = "HifiBerry Digi",
  100350. + .stream_name = "HifiBerry Digi HiFi",
  100351. + .cpu_dai_name = "bcm2708-i2s.0",
  100352. + .codec_dai_name = "wm8804-spdif",
  100353. + .platform_name = "bcm2708-i2s.0",
  100354. + .codec_name = "wm8804.1-003b",
  100355. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100356. + SND_SOC_DAIFMT_CBM_CFM,
  100357. + .ops = &snd_rpi_hifiberry_digi_ops,
  100358. + .init = snd_rpi_hifiberry_digi_init,
  100359. +},
  100360. +};
  100361. +
  100362. +/* audio machine driver */
  100363. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  100364. + .name = "snd_rpi_hifiberry_digi",
  100365. + .dai_link = snd_rpi_hifiberry_digi_dai,
  100366. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  100367. +};
  100368. +
  100369. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  100370. +{
  100371. + int ret = 0;
  100372. +
  100373. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  100374. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  100375. + if (ret)
  100376. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100377. +
  100378. + return ret;
  100379. +}
  100380. +
  100381. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  100382. +{
  100383. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  100384. +}
  100385. +
  100386. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  100387. + .driver = {
  100388. + .name = "snd-hifiberry-digi",
  100389. + .owner = THIS_MODULE,
  100390. + },
  100391. + .probe = snd_rpi_hifiberry_digi_probe,
  100392. + .remove = snd_rpi_hifiberry_digi_remove,
  100393. +};
  100394. +
  100395. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  100396. +
  100397. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  100398. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  100399. +MODULE_LICENSE("GPL v2");
  100400. diff -Nur linux-3.10.37/sound/soc/bcm/iqaudio-dac.c linux-rpi/sound/soc/bcm/iqaudio-dac.c
  100401. --- linux-3.10.37/sound/soc/bcm/iqaudio-dac.c 1970-01-01 01:00:00.000000000 +0100
  100402. +++ linux-rpi/sound/soc/bcm/iqaudio-dac.c 2014-04-24 15:35:05.481580345 +0200
  100403. @@ -0,0 +1,111 @@
  100404. +/*
  100405. + * ASoC Driver for IQaudIO DAC
  100406. + *
  100407. + * Author: Florian Meier <florian.meier@koalo.de>
  100408. + * Copyright 2013
  100409. + *
  100410. + * This program is free software; you can redistribute it and/or
  100411. + * modify it under the terms of the GNU General Public License
  100412. + * version 2 as published by the Free Software Foundation.
  100413. + *
  100414. + * This program is distributed in the hope that it will be useful, but
  100415. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100416. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100417. + * General Public License for more details.
  100418. + */
  100419. +
  100420. +#include <linux/module.h>
  100421. +#include <linux/platform_device.h>
  100422. +
  100423. +#include <sound/core.h>
  100424. +#include <sound/pcm.h>
  100425. +#include <sound/pcm_params.h>
  100426. +#include <sound/soc.h>
  100427. +#include <sound/jack.h>
  100428. +
  100429. +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
  100430. +{
  100431. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  100432. +
  100433. + return 0;
  100434. +}
  100435. +
  100436. +static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
  100437. + struct snd_pcm_hw_params *params)
  100438. +{
  100439. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100440. +// NOT USED struct snd_soc_dai *codec_dai = rtd->codec_dai;
  100441. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  100442. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100443. +
  100444. + unsigned int sample_bits =
  100445. + snd_pcm_format_physical_width(params_format(params));
  100446. +
  100447. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  100448. +}
  100449. +
  100450. +/* machine stream operations */
  100451. +static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
  100452. + .hw_params = snd_rpi_iqaudio_dac_hw_params,
  100453. +};
  100454. +
  100455. +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
  100456. +{
  100457. + .name = "IQaudIO DAC",
  100458. + .stream_name = "IQaudIO DAC HiFi",
  100459. + .cpu_dai_name = "bcm2708-i2s.0",
  100460. + .codec_dai_name = "pcm512x-hifi",
  100461. + .platform_name = "bcm2708-i2s.0",
  100462. + .codec_name = "pcm512x.1-004c",
  100463. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100464. + SND_SOC_DAIFMT_CBS_CFS,
  100465. + .ops = &snd_rpi_iqaudio_dac_ops,
  100466. + .init = snd_rpi_iqaudio_dac_init,
  100467. +},
  100468. +};
  100469. +
  100470. +/* audio machine driver */
  100471. +static struct snd_soc_card snd_rpi_iqaudio_dac = {
  100472. + .name = "snd_rpi_iqaudio_dac",
  100473. + .dai_link = snd_rpi_iqaudio_dac_dai,
  100474. + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
  100475. +};
  100476. +
  100477. +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
  100478. +{
  100479. + int ret = 0;
  100480. +
  100481. + snd_rpi_iqaudio_dac.dev = &pdev->dev;
  100482. + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
  100483. + if (ret)
  100484. + dev_err(&pdev->dev,
  100485. + "snd_soc_register_card() failed: %d\n", ret);
  100486. +
  100487. + return ret;
  100488. +}
  100489. +
  100490. +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
  100491. +{
  100492. + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
  100493. +}
  100494. +
  100495. +static const struct of_device_id iqaudio_of_match[] = {
  100496. + { .compatible = "iqaudio,iqaudio-dac", },
  100497. + {},
  100498. +};
  100499. +
  100500. +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
  100501. + .driver = {
  100502. + .name = "snd-rpi-iqaudio-dac",
  100503. + .owner = THIS_MODULE,
  100504. + .of_match_table = iqaudio_of_match,
  100505. + },
  100506. + .probe = snd_rpi_iqaudio_dac_probe,
  100507. + .remove = snd_rpi_iqaudio_dac_remove,
  100508. +};
  100509. +
  100510. +module_platform_driver(snd_rpi_iqaudio_dac_driver);
  100511. +
  100512. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100513. +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
  100514. +MODULE_LICENSE("GPL v2");
  100515. diff -Nur linux-3.10.37/sound/soc/bcm/Kconfig linux-rpi/sound/soc/bcm/Kconfig
  100516. --- linux-3.10.37/sound/soc/bcm/Kconfig 1970-01-01 01:00:00.000000000 +0100
  100517. +++ linux-rpi/sound/soc/bcm/Kconfig 2014-04-24 15:35:05.481580345 +0200
  100518. @@ -0,0 +1,38 @@
  100519. +config SND_BCM2708_SOC_I2S
  100520. + tristate "SoC Audio support for the Broadcom BCM2708 I2S module"
  100521. + depends on MACH_BCM2708
  100522. + select REGMAP_MMIO
  100523. + select SND_SOC_DMAENGINE_PCM
  100524. + select SND_SOC_GENERIC_DMAENGINE_PCM
  100525. + help
  100526. + Say Y or M if you want to add support for codecs attached to
  100527. + the BCM2708 I2S interface. You will also need
  100528. + to select the audio interfaces to support below.
  100529. +
  100530. +config SND_BCM2708_SOC_HIFIBERRY_DAC
  100531. + tristate "Support for HifiBerry DAC"
  100532. + depends on SND_BCM2708_SOC_I2S
  100533. + select SND_SOC_PCM5102A
  100534. + help
  100535. + Say Y or M if you want to add support for HifiBerry DAC.
  100536. +
  100537. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  100538. + tristate "Support for HifiBerry Digi"
  100539. + depends on SND_BCM2708_SOC_I2S
  100540. + select SND_SOC_WM8804
  100541. + help
  100542. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  100543. +
  100544. +config SND_BCM2708_SOC_RPI_DAC
  100545. + tristate "Support for RPi-DAC"
  100546. + depends on SND_BCM2708_SOC_I2S
  100547. + select SND_SOC_PCM1794A
  100548. + help
  100549. + Say Y or M if you want to add support for RPi-DAC.
  100550. +
  100551. +config SND_BCM2708_SOC_IQAUDIO_DAC
  100552. + tristate "Support for IQaudIO-DAC"
  100553. + depends on SND_BCM2708_SOC_I2S
  100554. + select SND_SOC_PCM512x
  100555. + help
  100556. + Say Y or M if you want to add support for IQaudIO-DAC.
  100557. diff -Nur linux-3.10.37/sound/soc/bcm/Makefile linux-rpi/sound/soc/bcm/Makefile
  100558. --- linux-3.10.37/sound/soc/bcm/Makefile 1970-01-01 01:00:00.000000000 +0100
  100559. +++ linux-rpi/sound/soc/bcm/Makefile 2014-04-24 15:35:05.481580345 +0200
  100560. @@ -0,0 +1,16 @@
  100561. +# BCM2708 Platform Support
  100562. +snd-soc-bcm2708-i2s-objs := bcm2708-i2s.o
  100563. +
  100564. +obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
  100565. +
  100566. +# BCM2708 Machine Support
  100567. +snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  100568. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  100569. +snd-soc-rpi-dac-objs := rpi-dac.o
  100570. +snd-soc-iqaudio-dac-objs := iqaudio-dac.o
  100571. +
  100572. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  100573. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  100574. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  100575. +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
  100576. +
  100577. diff -Nur linux-3.10.37/sound/soc/bcm/rpi-dac.c linux-rpi/sound/soc/bcm/rpi-dac.c
  100578. --- linux-3.10.37/sound/soc/bcm/rpi-dac.c 1970-01-01 01:00:00.000000000 +0100
  100579. +++ linux-rpi/sound/soc/bcm/rpi-dac.c 2014-04-24 15:35:05.481580345 +0200
  100580. @@ -0,0 +1,97 @@
  100581. +/*
  100582. + * ASoC Driver for RPi-DAC.
  100583. + *
  100584. + * Author: Florian Meier <florian.meier@koalo.de>
  100585. + * Copyright 2013
  100586. + *
  100587. + * This program is free software; you can redistribute it and/or
  100588. + * modify it under the terms of the GNU General Public License
  100589. + * version 2 as published by the Free Software Foundation.
  100590. + *
  100591. + * This program is distributed in the hope that it will be useful, but
  100592. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100593. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100594. + * General Public License for more details.
  100595. + */
  100596. +
  100597. +#include <linux/module.h>
  100598. +#include <linux/platform_device.h>
  100599. +
  100600. +#include <sound/core.h>
  100601. +#include <sound/pcm.h>
  100602. +#include <sound/pcm_params.h>
  100603. +#include <sound/soc.h>
  100604. +#include <sound/jack.h>
  100605. +
  100606. +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd)
  100607. +{
  100608. + return 0;
  100609. +}
  100610. +
  100611. +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream,
  100612. + struct snd_pcm_hw_params *params)
  100613. +{
  100614. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100615. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100616. +
  100617. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 32*2);
  100618. +}
  100619. +
  100620. +/* machine stream operations */
  100621. +static struct snd_soc_ops snd_rpi_rpi_dac_ops = {
  100622. + .hw_params = snd_rpi_rpi_dac_hw_params,
  100623. +};
  100624. +
  100625. +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = {
  100626. +{
  100627. + .name = "HifiBerry Mini",
  100628. + .stream_name = "HifiBerry Mini HiFi",
  100629. + .cpu_dai_name = "bcm2708-i2s.0",
  100630. + .codec_dai_name = "pcm1794a-hifi",
  100631. + .platform_name = "bcm2708-i2s.0",
  100632. + .codec_name = "pcm1794a-codec",
  100633. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100634. + SND_SOC_DAIFMT_CBS_CFS,
  100635. + .ops = &snd_rpi_rpi_dac_ops,
  100636. + .init = snd_rpi_rpi_dac_init,
  100637. +},
  100638. +};
  100639. +
  100640. +/* audio machine driver */
  100641. +static struct snd_soc_card snd_rpi_rpi_dac = {
  100642. + .name = "snd_rpi_rpi_dac",
  100643. + .dai_link = snd_rpi_rpi_dac_dai,
  100644. + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai),
  100645. +};
  100646. +
  100647. +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev)
  100648. +{
  100649. + int ret = 0;
  100650. +
  100651. + snd_rpi_rpi_dac.dev = &pdev->dev;
  100652. + ret = snd_soc_register_card(&snd_rpi_rpi_dac);
  100653. + if (ret)
  100654. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100655. +
  100656. + return ret;
  100657. +}
  100658. +
  100659. +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev)
  100660. +{
  100661. + return snd_soc_unregister_card(&snd_rpi_rpi_dac);
  100662. +}
  100663. +
  100664. +static struct platform_driver snd_rpi_rpi_dac_driver = {
  100665. + .driver = {
  100666. + .name = "snd-rpi-dac",
  100667. + .owner = THIS_MODULE,
  100668. + },
  100669. + .probe = snd_rpi_rpi_dac_probe,
  100670. + .remove = snd_rpi_rpi_dac_remove,
  100671. +};
  100672. +
  100673. +module_platform_driver(snd_rpi_rpi_dac_driver);
  100674. +
  100675. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100676. +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC");
  100677. +MODULE_LICENSE("GPL v2");
  100678. diff -Nur linux-3.10.37/sound/soc/codecs/Kconfig linux-rpi/sound/soc/codecs/Kconfig
  100679. --- linux-3.10.37/sound/soc/codecs/Kconfig 2014-04-14 15:42:31.000000000 +0200
  100680. +++ linux-rpi/sound/soc/codecs/Kconfig 2014-04-24 15:35:05.481580345 +0200
  100681. @@ -55,6 +55,9 @@
  100682. select SND_SOC_ML26124 if I2C
  100683. select SND_SOC_OMAP_HDMI_CODEC if OMAP4_DSS_HDMI
  100684. select SND_SOC_PCM3008
  100685. + select SND_SOC_PCM1794A
  100686. + select SND_SOC_PCM5102A
  100687. + select SND_SOC_PCM512x if SND_SOC_I2C_AND_SPI
  100688. select SND_SOC_RT5631 if I2C
  100689. select SND_SOC_SGTL5000 if I2C
  100690. select SND_SOC_SI476X if MFD_SI476X_CORE
  100691. @@ -293,6 +296,15 @@
  100692. config SND_SOC_PCM3008
  100693. tristate
  100694. +config SND_SOC_PCM1794A
  100695. + tristate
  100696. +
  100697. +config SND_SOC_PCM5102A
  100698. + tristate
  100699. +
  100700. +config SND_SOC_PCM512x
  100701. + tristate
  100702. +
  100703. config SND_SOC_RT5631
  100704. tristate
  100705. diff -Nur linux-3.10.37/sound/soc/codecs/Makefile linux-rpi/sound/soc/codecs/Makefile
  100706. --- linux-3.10.37/sound/soc/codecs/Makefile 2014-04-14 15:42:31.000000000 +0200
  100707. +++ linux-rpi/sound/soc/codecs/Makefile 2014-04-24 15:35:05.481580345 +0200
  100708. @@ -43,6 +43,9 @@
  100709. snd-soc-ml26124-objs := ml26124.o
  100710. snd-soc-omap-hdmi-codec-objs := omap-hdmi.o
  100711. snd-soc-pcm3008-objs := pcm3008.o
  100712. +snd-soc-pcm1794a-objs := pcm1794a.o
  100713. +snd-soc-pcm5102a-objs := pcm5102a.o
  100714. +snd-soc-pcm512x-objs := pcm512x.o
  100715. snd-soc-rt5631-objs := rt5631.o
  100716. snd-soc-sgtl5000-objs := sgtl5000.o
  100717. snd-soc-alc5623-objs := alc5623.o
  100718. @@ -170,6 +173,9 @@
  100719. obj-$(CONFIG_SND_SOC_ML26124) += snd-soc-ml26124.o
  100720. obj-$(CONFIG_SND_SOC_OMAP_HDMI_CODEC) += snd-soc-omap-hdmi-codec.o
  100721. obj-$(CONFIG_SND_SOC_PCM3008) += snd-soc-pcm3008.o
  100722. +obj-$(CONFIG_SND_SOC_PCM1794A) += snd-soc-pcm1794a.o
  100723. +obj-$(CONFIG_SND_SOC_PCM5102A) += snd-soc-pcm5102a.o
  100724. +obj-$(CONFIG_SND_SOC_PCM512x) += snd-soc-pcm512x.o
  100725. obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o
  100726. obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
  100727. obj-$(CONFIG_SND_SOC_SIGMADSP) += snd-soc-sigmadsp.o
  100728. diff -Nur linux-3.10.37/sound/soc/codecs/pcm1794a.c linux-rpi/sound/soc/codecs/pcm1794a.c
  100729. --- linux-3.10.37/sound/soc/codecs/pcm1794a.c 1970-01-01 01:00:00.000000000 +0100
  100730. +++ linux-rpi/sound/soc/codecs/pcm1794a.c 2014-04-24 15:35:05.493580478 +0200
  100731. @@ -0,0 +1,62 @@
  100732. +/*
  100733. + * Driver for the PCM1794A codec
  100734. + *
  100735. + * Author: Florian Meier <florian.meier@koalo.de>
  100736. + * Copyright 2013
  100737. + *
  100738. + * This program is free software; you can redistribute it and/or
  100739. + * modify it under the terms of the GNU General Public License
  100740. + * version 2 as published by the Free Software Foundation.
  100741. + *
  100742. + * This program is distributed in the hope that it will be useful, but
  100743. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100744. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100745. + * General Public License for more details.
  100746. + */
  100747. +
  100748. +
  100749. +#include <linux/init.h>
  100750. +#include <linux/module.h>
  100751. +#include <linux/platform_device.h>
  100752. +
  100753. +#include <sound/soc.h>
  100754. +
  100755. +static struct snd_soc_dai_driver pcm1794a_dai = {
  100756. + .name = "pcm1794a-hifi",
  100757. + .playback = {
  100758. + .channels_min = 2,
  100759. + .channels_max = 2,
  100760. + .rates = SNDRV_PCM_RATE_8000_192000,
  100761. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100762. + SNDRV_PCM_FMTBIT_S24_LE
  100763. + },
  100764. +};
  100765. +
  100766. +static struct snd_soc_codec_driver soc_codec_dev_pcm1794a;
  100767. +
  100768. +static int pcm1794a_probe(struct platform_device *pdev)
  100769. +{
  100770. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm1794a,
  100771. + &pcm1794a_dai, 1);
  100772. +}
  100773. +
  100774. +static int pcm1794a_remove(struct platform_device *pdev)
  100775. +{
  100776. + snd_soc_unregister_codec(&pdev->dev);
  100777. + return 0;
  100778. +}
  100779. +
  100780. +static struct platform_driver pcm1794a_codec_driver = {
  100781. + .probe = pcm1794a_probe,
  100782. + .remove = pcm1794a_remove,
  100783. + .driver = {
  100784. + .name = "pcm1794a-codec",
  100785. + .owner = THIS_MODULE,
  100786. + },
  100787. +};
  100788. +
  100789. +module_platform_driver(pcm1794a_codec_driver);
  100790. +
  100791. +MODULE_DESCRIPTION("ASoC PCM1794A codec driver");
  100792. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100793. +MODULE_LICENSE("GPL v2");
  100794. diff -Nur linux-3.10.37/sound/soc/codecs/pcm5102a.c linux-rpi/sound/soc/codecs/pcm5102a.c
  100795. --- linux-3.10.37/sound/soc/codecs/pcm5102a.c 1970-01-01 01:00:00.000000000 +0100
  100796. +++ linux-rpi/sound/soc/codecs/pcm5102a.c 2014-04-24 15:35:05.493580478 +0200
  100797. @@ -0,0 +1,63 @@
  100798. +/*
  100799. + * Driver for the PCM5102A codec
  100800. + *
  100801. + * Author: Florian Meier <florian.meier@koalo.de>
  100802. + * Copyright 2013
  100803. + *
  100804. + * This program is free software; you can redistribute it and/or
  100805. + * modify it under the terms of the GNU General Public License
  100806. + * version 2 as published by the Free Software Foundation.
  100807. + *
  100808. + * This program is distributed in the hope that it will be useful, but
  100809. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100810. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100811. + * General Public License for more details.
  100812. + */
  100813. +
  100814. +
  100815. +#include <linux/init.h>
  100816. +#include <linux/module.h>
  100817. +#include <linux/platform_device.h>
  100818. +
  100819. +#include <sound/soc.h>
  100820. +
  100821. +static struct snd_soc_dai_driver pcm5102a_dai = {
  100822. + .name = "pcm5102a-hifi",
  100823. + .playback = {
  100824. + .channels_min = 2,
  100825. + .channels_max = 2,
  100826. + .rates = SNDRV_PCM_RATE_8000_192000,
  100827. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100828. + // SNDRV_PCM_FMTBIT_S24_LE | : disable for now, it causes white noise with xbmc
  100829. + SNDRV_PCM_FMTBIT_S32_LE
  100830. + },
  100831. +};
  100832. +
  100833. +static struct snd_soc_codec_driver soc_codec_dev_pcm5102a;
  100834. +
  100835. +static int pcm5102a_probe(struct platform_device *pdev)
  100836. +{
  100837. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm5102a,
  100838. + &pcm5102a_dai, 1);
  100839. +}
  100840. +
  100841. +static int pcm5102a_remove(struct platform_device *pdev)
  100842. +{
  100843. + snd_soc_unregister_codec(&pdev->dev);
  100844. + return 0;
  100845. +}
  100846. +
  100847. +static struct platform_driver pcm5102a_codec_driver = {
  100848. + .probe = pcm5102a_probe,
  100849. + .remove = pcm5102a_remove,
  100850. + .driver = {
  100851. + .name = "pcm5102a-codec",
  100852. + .owner = THIS_MODULE,
  100853. + },
  100854. +};
  100855. +
  100856. +module_platform_driver(pcm5102a_codec_driver);
  100857. +
  100858. +MODULE_DESCRIPTION("ASoC PCM5102A codec driver");
  100859. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100860. +MODULE_LICENSE("GPL v2");
  100861. diff -Nur linux-3.10.37/sound/soc/codecs/pcm512x.c linux-rpi/sound/soc/codecs/pcm512x.c
  100862. --- linux-3.10.37/sound/soc/codecs/pcm512x.c 1970-01-01 01:00:00.000000000 +0100
  100863. +++ linux-rpi/sound/soc/codecs/pcm512x.c 2014-04-24 15:35:05.493580478 +0200
  100864. @@ -0,0 +1,678 @@
  100865. +/*
  100866. + * Driver for the PCM512x CODECs
  100867. + *
  100868. + * Author: Mark Brown <broonie@linaro.org>
  100869. + * Copyright 2014 Linaro Ltd
  100870. + *
  100871. + * This program is free software; you can redistribute it and/or
  100872. + * modify it under the terms of the GNU General Public License
  100873. + * version 2 as published by the Free Software Foundation.
  100874. + *
  100875. + * This program is distributed in the hope that it will be useful, but
  100876. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100877. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100878. + * General Public License for more details.
  100879. + */
  100880. +
  100881. +
  100882. +#include <linux/init.h>
  100883. +#include <linux/module.h>
  100884. +#include <linux/clk.h>
  100885. +#include <linux/i2c.h>
  100886. +#include <linux/pm_runtime.h>
  100887. +#include <linux/regmap.h>
  100888. +#include <linux/regulator/consumer.h>
  100889. +#include <linux/spi/spi.h>
  100890. +#include <sound/soc.h>
  100891. +#include <sound/soc-dapm.h>
  100892. +#include <sound/tlv.h>
  100893. +
  100894. +#include "pcm512x.h"
  100895. +
  100896. +#define PCM512x_NUM_SUPPLIES 3
  100897. +static const char *pcm512x_supply_names[PCM512x_NUM_SUPPLIES] = {
  100898. + "AVDD",
  100899. + "DVDD",
  100900. + "CPVDD",
  100901. +};
  100902. +
  100903. +struct pcm512x_priv {
  100904. + struct regmap *regmap;
  100905. + struct clk *sclk;
  100906. + struct regulator_bulk_data supplies[PCM512x_NUM_SUPPLIES];
  100907. + struct notifier_block supply_nb[PCM512x_NUM_SUPPLIES];
  100908. +};
  100909. +
  100910. +/*
  100911. + * We can't use the same notifier block for more than one supply and
  100912. + * there's no way I can see to get from a callback to the caller
  100913. + * except container_of().
  100914. + */
  100915. +#define PCM512x_REGULATOR_EVENT(n) \
  100916. +static int pcm512x_regulator_event_##n(struct notifier_block *nb, \
  100917. + unsigned long event, void *data) \
  100918. +{ \
  100919. + struct pcm512x_priv *pcm512x = container_of(nb, struct pcm512x_priv, \
  100920. + supply_nb[n]); \
  100921. + if (event & REGULATOR_EVENT_DISABLE) { \
  100922. + regcache_mark_dirty(pcm512x->regmap); \
  100923. + regcache_cache_only(pcm512x->regmap, true); \
  100924. + } \
  100925. + return 0; \
  100926. +}
  100927. +
  100928. +PCM512x_REGULATOR_EVENT(0)
  100929. +PCM512x_REGULATOR_EVENT(1)
  100930. +PCM512x_REGULATOR_EVENT(2)
  100931. +
  100932. +static const struct reg_default pcm512x_reg_defaults[] = {
  100933. + { PCM512x_RESET, 0x00 },
  100934. + { PCM512x_POWER, 0x00 },
  100935. + { PCM512x_MUTE, 0x00 },
  100936. + { PCM512x_DSP, 0x00 },
  100937. + { PCM512x_PLL_REF, 0x00 },
  100938. + { PCM512x_DAC_ROUTING, 0x11 },
  100939. + { PCM512x_DSP_PROGRAM, 0x01 },
  100940. + { PCM512x_CLKDET, 0x00 },
  100941. + { PCM512x_AUTO_MUTE, 0x00 },
  100942. + { PCM512x_ERROR_DETECT, 0x00 },
  100943. + { PCM512x_DIGITAL_VOLUME_1, 0x00 },
  100944. + { PCM512x_DIGITAL_VOLUME_2, 0x30 },
  100945. + { PCM512x_DIGITAL_VOLUME_3, 0x30 },
  100946. + { PCM512x_DIGITAL_MUTE_1, 0x22 },
  100947. + { PCM512x_DIGITAL_MUTE_2, 0x00 },
  100948. + { PCM512x_DIGITAL_MUTE_3, 0x07 },
  100949. +};
  100950. +
  100951. +static bool pcm512x_readable(struct device *dev, unsigned int reg)
  100952. +{
  100953. + switch (reg) {
  100954. + case PCM512x_RESET:
  100955. + case PCM512x_POWER:
  100956. + case PCM512x_MUTE:
  100957. + case PCM512x_PLL_EN:
  100958. + case PCM512x_SPI_MISO_FUNCTION:
  100959. + case PCM512x_DSP:
  100960. + case PCM512x_GPIO_EN:
  100961. + case PCM512x_BCLK_LRCLK_CFG:
  100962. + case PCM512x_DSP_GPIO_INPUT:
  100963. + case PCM512x_MASTER_MODE:
  100964. + case PCM512x_PLL_REF:
  100965. + case PCM512x_PLL_COEFF_0:
  100966. + case PCM512x_PLL_COEFF_1:
  100967. + case PCM512x_PLL_COEFF_2:
  100968. + case PCM512x_PLL_COEFF_3:
  100969. + case PCM512x_PLL_COEFF_4:
  100970. + case PCM512x_DSP_CLKDIV:
  100971. + case PCM512x_DAC_CLKDIV:
  100972. + case PCM512x_NCP_CLKDIV:
  100973. + case PCM512x_OSR_CLKDIV:
  100974. + case PCM512x_MASTER_CLKDIV_1:
  100975. + case PCM512x_MASTER_CLKDIV_2:
  100976. + case PCM512x_FS_SPEED_MODE:
  100977. + case PCM512x_IDAC_1:
  100978. + case PCM512x_IDAC_2:
  100979. + case PCM512x_ERROR_DETECT:
  100980. + case PCM512x_I2S_1:
  100981. + case PCM512x_I2S_2:
  100982. + case PCM512x_DAC_ROUTING:
  100983. + case PCM512x_DSP_PROGRAM:
  100984. + case PCM512x_CLKDET:
  100985. + case PCM512x_AUTO_MUTE:
  100986. + case PCM512x_DIGITAL_VOLUME_1:
  100987. + case PCM512x_DIGITAL_VOLUME_2:
  100988. + case PCM512x_DIGITAL_VOLUME_3:
  100989. + case PCM512x_DIGITAL_MUTE_1:
  100990. + case PCM512x_DIGITAL_MUTE_2:
  100991. + case PCM512x_DIGITAL_MUTE_3:
  100992. + case PCM512x_GPIO_OUTPUT_1:
  100993. + case PCM512x_GPIO_OUTPUT_2:
  100994. + case PCM512x_GPIO_OUTPUT_3:
  100995. + case PCM512x_GPIO_OUTPUT_4:
  100996. + case PCM512x_GPIO_OUTPUT_5:
  100997. + case PCM512x_GPIO_OUTPUT_6:
  100998. + case PCM512x_GPIO_CONTROL_1:
  100999. + case PCM512x_GPIO_CONTROL_2:
  101000. + case PCM512x_OVERFLOW:
  101001. + case PCM512x_RATE_DET_1:
  101002. + case PCM512x_RATE_DET_2:
  101003. + case PCM512x_RATE_DET_3:
  101004. + case PCM512x_RATE_DET_4:
  101005. + case PCM512x_ANALOG_MUTE_DET:
  101006. + case PCM512x_GPIN:
  101007. + case PCM512x_DIGITAL_MUTE_DET:
  101008. + return true;
  101009. + default:
  101010. + return false;
  101011. + }
  101012. +}
  101013. +
  101014. +static bool pcm512x_volatile(struct device *dev, unsigned int reg)
  101015. +{
  101016. + switch (reg) {
  101017. + case PCM512x_PLL_EN:
  101018. + case PCM512x_OVERFLOW:
  101019. + case PCM512x_RATE_DET_1:
  101020. + case PCM512x_RATE_DET_2:
  101021. + case PCM512x_RATE_DET_3:
  101022. + case PCM512x_RATE_DET_4:
  101023. + case PCM512x_ANALOG_MUTE_DET:
  101024. + case PCM512x_GPIN:
  101025. + case PCM512x_DIGITAL_MUTE_DET:
  101026. + return true;
  101027. + default:
  101028. + return false;
  101029. + }
  101030. +}
  101031. +
  101032. +static const DECLARE_TLV_DB_SCALE(digital_tlv, -10350, 50, 1);
  101033. +
  101034. +static const char *pcm512x_dsp_program_texts[] = {
  101035. + "FIR interpolation with de-emphasis",
  101036. + "Low latency IIR with de-emphasis",
  101037. + "High attenuation with de-emphasis",
  101038. + "Ringing-less low latency FIR",
  101039. +};
  101040. +
  101041. +static const unsigned int pcm512x_dsp_program_values[] = {
  101042. + 1,
  101043. + 2,
  101044. + 3,
  101045. + 5,
  101046. + 7,
  101047. +};
  101048. +
  101049. +static const SOC_VALUE_ENUM_SINGLE_DECL(pcm512x_dsp_program,
  101050. + PCM512x_DSP_PROGRAM, 0, 0x1f,
  101051. + pcm512x_dsp_program_texts,
  101052. + pcm512x_dsp_program_values);
  101053. +
  101054. +static const char *pcm512x_clk_missing_text[] = {
  101055. + "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s"
  101056. +};
  101057. +
  101058. +static const struct soc_enum pcm512x_clk_missing =
  101059. + SOC_ENUM_SINGLE(PCM512x_CLKDET, 0, 7, pcm512x_clk_missing_text);
  101060. +
  101061. +static const char *pcm512x_autom_text[] = {
  101062. + "21ms", "106ms", "213ms", "533ms", "1.07s", "2.13s", "5.33s", "10.66s"
  101063. +};
  101064. +
  101065. +static const struct soc_enum pcm512x_autom_l =
  101066. + SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATML_SHIFT, 7,
  101067. + pcm512x_autom_text);
  101068. +
  101069. +static const struct soc_enum pcm512x_autom_r =
  101070. + SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATMR_SHIFT, 7,
  101071. + pcm512x_autom_text);
  101072. +
  101073. +static const char *pcm512x_ramp_rate_text[] = {
  101074. + "1 sample/update", "2 samples/update", "4 samples/update",
  101075. + "Immediate"
  101076. +};
  101077. +
  101078. +static const struct soc_enum pcm512x_vndf =
  101079. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDF_SHIFT, 4,
  101080. + pcm512x_ramp_rate_text);
  101081. +
  101082. +static const struct soc_enum pcm512x_vnuf =
  101083. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUF_SHIFT, 4,
  101084. + pcm512x_ramp_rate_text);
  101085. +
  101086. +static const struct soc_enum pcm512x_vedf =
  101087. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDF_SHIFT, 4,
  101088. + pcm512x_ramp_rate_text);
  101089. +
  101090. +static const char *pcm512x_ramp_step_text[] = {
  101091. + "4dB/step", "2dB/step", "1dB/step", "0.5dB/step"
  101092. +};
  101093. +
  101094. +static const struct soc_enum pcm512x_vnds =
  101095. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDS_SHIFT, 4,
  101096. + pcm512x_ramp_step_text);
  101097. +
  101098. +static const struct soc_enum pcm512x_vnus =
  101099. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUS_SHIFT, 4,
  101100. + pcm512x_ramp_step_text);
  101101. +
  101102. +static const struct soc_enum pcm512x_veds =
  101103. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDS_SHIFT, 4,
  101104. + pcm512x_ramp_step_text);
  101105. +
  101106. +/* Don't let the DAC go into clipping by limiting the alsa volume control range */
  101107. +static const struct snd_kcontrol_new pcm512x_controls[] = {
  101108. +SOC_DOUBLE_R_RANGE_TLV("Playback Digital Volume", PCM512x_DIGITAL_VOLUME_2,
  101109. + PCM512x_DIGITAL_VOLUME_3, 0, 40, 255, 1, digital_tlv),
  101110. +SOC_DOUBLE("Playback Digital Switch", PCM512x_MUTE, PCM512x_RQML_SHIFT,
  101111. + PCM512x_RQMR_SHIFT, 1, 1),
  101112. +
  101113. +SOC_SINGLE("Deemphasis Switch", PCM512x_DSP, PCM512x_DEMP_SHIFT, 1, 1),
  101114. +SOC_VALUE_ENUM("DSP Program", pcm512x_dsp_program),
  101115. +
  101116. +SOC_ENUM("Clock Missing Period", pcm512x_clk_missing),
  101117. +SOC_ENUM("Auto Mute Time Left", pcm512x_autom_l),
  101118. +SOC_ENUM("Auto Mute Time Right", pcm512x_autom_r),
  101119. +SOC_SINGLE("Auto Mute Mono Switch", PCM512x_DIGITAL_MUTE_3,
  101120. + PCM512x_ACTL_SHIFT, 1, 0),
  101121. +SOC_DOUBLE("Auto Mute Switch", PCM512x_DIGITAL_MUTE_3, PCM512x_AMLE_SHIFT,
  101122. + PCM512x_AMLR_SHIFT, 1, 0),
  101123. +
  101124. +SOC_ENUM("Volume Ramp Down Rate", pcm512x_vndf),
  101125. +SOC_ENUM("Volume Ramp Down Step", pcm512x_vnds),
  101126. +SOC_ENUM("Volume Ramp Up Rate", pcm512x_vnuf),
  101127. +SOC_ENUM("Volume Ramp Up Step", pcm512x_vnus),
  101128. +SOC_ENUM("Volume Ramp Down Emergency Rate", pcm512x_vedf),
  101129. +SOC_ENUM("Volume Ramp Down Emergency Step", pcm512x_veds),
  101130. +};
  101131. +
  101132. +static const struct snd_soc_dapm_widget pcm512x_dapm_widgets[] = {
  101133. +SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
  101134. +SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
  101135. +
  101136. +SND_SOC_DAPM_OUTPUT("OUTL"),
  101137. +SND_SOC_DAPM_OUTPUT("OUTR"),
  101138. +};
  101139. +
  101140. +static const struct snd_soc_dapm_route pcm512x_dapm_routes[] = {
  101141. + { "DACL", NULL, "Playback" },
  101142. + { "DACR", NULL, "Playback" },
  101143. +
  101144. + { "OUTL", NULL, "DACL" },
  101145. + { "OUTR", NULL, "DACR" },
  101146. +};
  101147. +
  101148. +static int pcm512x_set_bias_level(struct snd_soc_codec *codec,
  101149. + enum snd_soc_bias_level level)
  101150. +{
  101151. + struct pcm512x_priv *pcm512x = dev_get_drvdata(codec->dev);
  101152. + int ret;
  101153. +
  101154. + switch (level) {
  101155. + case SND_SOC_BIAS_ON:
  101156. + case SND_SOC_BIAS_PREPARE:
  101157. + break;
  101158. +
  101159. + case SND_SOC_BIAS_STANDBY:
  101160. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  101161. + PCM512x_RQST, 0);
  101162. + if (ret != 0) {
  101163. + dev_err(codec->dev, "Failed to remove standby: %d\n",
  101164. + ret);
  101165. + return ret;
  101166. + }
  101167. + break;
  101168. +
  101169. + case SND_SOC_BIAS_OFF:
  101170. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  101171. + PCM512x_RQST, PCM512x_RQST);
  101172. + if (ret != 0) {
  101173. + dev_err(codec->dev, "Failed to request standby: %d\n",
  101174. + ret);
  101175. + return ret;
  101176. + }
  101177. + break;
  101178. + }
  101179. +
  101180. + codec->dapm.bias_level = level;
  101181. +
  101182. + return 0;
  101183. +}
  101184. +
  101185. +static struct snd_soc_dai_driver pcm512x_dai = {
  101186. + .name = "pcm512x-hifi",
  101187. + .playback = {
  101188. + .stream_name = "Playback",
  101189. + .channels_min = 2,
  101190. + .channels_max = 2,
  101191. + .rates = SNDRV_PCM_RATE_8000_192000,
  101192. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  101193. + SNDRV_PCM_FMTBIT_S24_LE |
  101194. + SNDRV_PCM_FMTBIT_S32_LE
  101195. + },
  101196. +};
  101197. +
  101198. +static struct snd_soc_codec_driver pcm512x_codec_driver = {
  101199. + .set_bias_level = pcm512x_set_bias_level,
  101200. + .idle_bias_off = true,
  101201. +
  101202. + .controls = pcm512x_controls,
  101203. + .num_controls = ARRAY_SIZE(pcm512x_controls),
  101204. + .dapm_widgets = pcm512x_dapm_widgets,
  101205. + .num_dapm_widgets = ARRAY_SIZE(pcm512x_dapm_widgets),
  101206. + .dapm_routes = pcm512x_dapm_routes,
  101207. + .num_dapm_routes = ARRAY_SIZE(pcm512x_dapm_routes),
  101208. +};
  101209. +
  101210. +static const struct regmap_config pcm512x_regmap = {
  101211. + .reg_bits = 8,
  101212. + .val_bits = 8,
  101213. +
  101214. + .readable_reg = pcm512x_readable,
  101215. + .volatile_reg = pcm512x_volatile,
  101216. +
  101217. + .max_register = PCM512x_MAX_REGISTER,
  101218. + .reg_defaults = pcm512x_reg_defaults,
  101219. + .num_reg_defaults = ARRAY_SIZE(pcm512x_reg_defaults),
  101220. + .cache_type = REGCACHE_RBTREE,
  101221. +};
  101222. +
  101223. +static const struct of_device_id pcm512x_of_match[] = {
  101224. + { .compatible = "ti,pcm5121", },
  101225. + { .compatible = "ti,pcm5122", },
  101226. + { }
  101227. +};
  101228. +MODULE_DEVICE_TABLE(of, pcm512x_of_match);
  101229. +
  101230. +static int pcm512x_probe(struct device *dev, struct regmap *regmap)
  101231. +{
  101232. + struct pcm512x_priv *pcm512x;
  101233. + int i, ret;
  101234. +
  101235. + pcm512x = devm_kzalloc(dev, sizeof(struct pcm512x_priv), GFP_KERNEL);
  101236. + if (!pcm512x)
  101237. + return -ENOMEM;
  101238. +
  101239. + dev_set_drvdata(dev, pcm512x);
  101240. + pcm512x->regmap = regmap;
  101241. +
  101242. + for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++)
  101243. + pcm512x->supplies[i].supply = pcm512x_supply_names[i];
  101244. +
  101245. + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(pcm512x->supplies),
  101246. + pcm512x->supplies);
  101247. + if (ret != 0) {
  101248. + dev_err(dev, "Failed to get supplies: %d\n", ret);
  101249. + return ret;
  101250. + }
  101251. +
  101252. + pcm512x->supply_nb[0].notifier_call = pcm512x_regulator_event_0;
  101253. + pcm512x->supply_nb[1].notifier_call = pcm512x_regulator_event_1;
  101254. + pcm512x->supply_nb[2].notifier_call = pcm512x_regulator_event_2;
  101255. +
  101256. + for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++) {
  101257. + ret = regulator_register_notifier(pcm512x->supplies[i].consumer,
  101258. + &pcm512x->supply_nb[i]);
  101259. + if (ret != 0) {
  101260. + dev_err(dev,
  101261. + "Failed to register regulator notifier: %d\n",
  101262. + ret);
  101263. + }
  101264. + }
  101265. +
  101266. + ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
  101267. + pcm512x->supplies);
  101268. + if (ret != 0) {
  101269. + dev_err(dev, "Failed to enable supplies: %d\n", ret);
  101270. + return ret;
  101271. + }
  101272. +
  101273. + /* Reset the device, verifying I/O in the process for I2C */
  101274. + ret = regmap_write(regmap, PCM512x_RESET,
  101275. + PCM512x_RSTM | PCM512x_RSTR);
  101276. + if (ret != 0) {
  101277. + dev_err(dev, "Failed to reset device: %d\n", ret);
  101278. + goto err;
  101279. + }
  101280. +
  101281. + ret = regmap_write(regmap, PCM512x_RESET, 0);
  101282. + if (ret != 0) {
  101283. + dev_err(dev, "Failed to reset device: %d\n", ret);
  101284. + goto err;
  101285. + }
  101286. +
  101287. + pcm512x->sclk = devm_clk_get(dev, NULL);
  101288. + if (IS_ERR(pcm512x->sclk)) {
  101289. + if (PTR_ERR(pcm512x->sclk) == -EPROBE_DEFER)
  101290. + return -EPROBE_DEFER;
  101291. +
  101292. + dev_info(dev, "No SCLK, using BCLK: %ld\n",
  101293. + PTR_ERR(pcm512x->sclk));
  101294. +
  101295. + /* Disable reporting of missing SCLK as an error */
  101296. + regmap_update_bits(regmap, PCM512x_ERROR_DETECT,
  101297. + PCM512x_IDCH, PCM512x_IDCH);
  101298. +
  101299. + /* Switch PLL input to BCLK */
  101300. + regmap_update_bits(regmap, PCM512x_PLL_REF,
  101301. + PCM512x_SREF, PCM512x_SREF);
  101302. + } else {
  101303. + ret = clk_prepare_enable(pcm512x->sclk);
  101304. + if (ret != 0) {
  101305. + dev_err(dev, "Failed to enable SCLK: %d\n", ret);
  101306. + return ret;
  101307. + }
  101308. + }
  101309. +
  101310. + /* Default to standby mode */
  101311. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  101312. + PCM512x_RQST, PCM512x_RQST);
  101313. + if (ret != 0) {
  101314. + dev_err(dev, "Failed to request standby: %d\n",
  101315. + ret);
  101316. + goto err_clk;
  101317. + }
  101318. +
  101319. + pm_runtime_set_active(dev);
  101320. + pm_runtime_enable(dev);
  101321. + pm_runtime_idle(dev);
  101322. +
  101323. + ret = snd_soc_register_codec(dev, &pcm512x_codec_driver,
  101324. + &pcm512x_dai, 1);
  101325. + if (ret != 0) {
  101326. + dev_err(dev, "Failed to register CODEC: %d\n", ret);
  101327. + goto err_pm;
  101328. + }
  101329. +
  101330. + dev_info(dev, "Completed initialisation - pcm512x_probe");
  101331. +
  101332. + return 0;
  101333. +
  101334. +err_pm:
  101335. + pm_runtime_disable(dev);
  101336. +err_clk:
  101337. + if (!IS_ERR(pcm512x->sclk))
  101338. + clk_disable_unprepare(pcm512x->sclk);
  101339. +err:
  101340. + regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  101341. + pcm512x->supplies);
  101342. + return ret;
  101343. +}
  101344. +
  101345. +static void pcm512x_remove(struct device *dev)
  101346. +{
  101347. + struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  101348. +
  101349. + snd_soc_unregister_codec(dev);
  101350. + pm_runtime_disable(dev);
  101351. + if (!IS_ERR(pcm512x->sclk))
  101352. + clk_disable_unprepare(pcm512x->sclk);
  101353. + regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  101354. + pcm512x->supplies);
  101355. +}
  101356. +
  101357. +/* TODO
  101358. +static int pcm512x_suspend(struct device *dev)
  101359. +{
  101360. + struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  101361. + int ret;
  101362. +
  101363. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  101364. + PCM512x_RQPD, PCM512x_RQPD);
  101365. + if (ret != 0) {
  101366. + dev_err(dev, "Failed to request power down: %d\n", ret);
  101367. + return ret;
  101368. + }
  101369. +
  101370. + ret = regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  101371. + pcm512x->supplies);
  101372. + if (ret != 0) {
  101373. + dev_err(dev, "Failed to disable supplies: %d\n", ret);
  101374. + return ret;
  101375. + }
  101376. +
  101377. + if (!IS_ERR(pcm512x->sclk))
  101378. + clk_disable_unprepare(pcm512x->sclk);
  101379. +
  101380. + return 0;
  101381. +}
  101382. +
  101383. +static int pcm512x_resume(struct device *dev)
  101384. +{
  101385. + struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  101386. + int ret;
  101387. +
  101388. + if (!IS_ERR(pcm512x->sclk)) {
  101389. + ret = clk_prepare_enable(pcm512x->sclk);
  101390. + if (ret != 0) {
  101391. + dev_err(dev, "Failed to enable SCLK: %d\n", ret);
  101392. + return ret;
  101393. + }
  101394. + }
  101395. +
  101396. + ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
  101397. + pcm512x->supplies);
  101398. + if (ret != 0) {
  101399. + dev_err(dev, "Failed to enable supplies: %d\n", ret);
  101400. + return ret;
  101401. + }
  101402. +
  101403. + regcache_cache_only(pcm512x->regmap, false);
  101404. + ret = regcache_sync(pcm512x->regmap);
  101405. + if (ret != 0) {
  101406. + dev_err(dev, "Failed to sync cache: %d\n", ret);
  101407. + return ret;
  101408. + }
  101409. +
  101410. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  101411. + PCM512x_RQPD, 0);
  101412. + if (ret != 0) {
  101413. + dev_err(dev, "Failed to remove power down: %d\n", ret);
  101414. + return ret;
  101415. + }
  101416. +
  101417. + return 0;
  101418. +}
  101419. +
  101420. +// END OF PCM512x_suspend and resume calls TODO
  101421. +*/
  101422. +
  101423. +static const struct dev_pm_ops pcm512x_pm_ops = {
  101424. + SET_RUNTIME_PM_OPS(pcm512x_suspend, pcm512x_resume, NULL)
  101425. +};
  101426. +
  101427. +#if IS_ENABLED(CONFIG_I2C)
  101428. +static int pcm512x_i2c_probe(struct i2c_client *i2c,
  101429. + const struct i2c_device_id *id)
  101430. +{
  101431. + struct regmap *regmap;
  101432. +
  101433. + regmap = devm_regmap_init_i2c(i2c, &pcm512x_regmap);
  101434. + if (IS_ERR(regmap))
  101435. + return PTR_ERR(regmap);
  101436. +
  101437. + return pcm512x_probe(&i2c->dev, regmap);
  101438. +}
  101439. +
  101440. +static int pcm512x_i2c_remove(struct i2c_client *i2c)
  101441. +{
  101442. + pcm512x_remove(&i2c->dev);
  101443. + return 0;
  101444. +}
  101445. +
  101446. +static const struct i2c_device_id pcm512x_i2c_id[] = {
  101447. + { "pcm5121", },
  101448. + { "pcm5122", },
  101449. + { }
  101450. +};
  101451. +MODULE_DEVICE_TABLE(i2c, pcm512x_i2c_id);
  101452. +
  101453. +static struct i2c_driver pcm512x_i2c_driver = {
  101454. + .probe = pcm512x_i2c_probe,
  101455. + .remove = pcm512x_i2c_remove,
  101456. + .id_table = pcm512x_i2c_id,
  101457. + .driver = {
  101458. + .name = "pcm512x",
  101459. + .owner = THIS_MODULE,
  101460. + .of_match_table = pcm512x_of_match,
  101461. + .pm = &pcm512x_pm_ops,
  101462. + },
  101463. +};
  101464. +#endif
  101465. +
  101466. +#if defined(CONFIG_SPI_MASTER)
  101467. +static int pcm512x_spi_probe(struct spi_device *spi)
  101468. +{
  101469. + struct regmap *regmap;
  101470. + int ret;
  101471. +
  101472. + regmap = devm_regmap_init_spi(spi, &pcm512x_regmap);
  101473. + if (IS_ERR(regmap)) {
  101474. + ret = PTR_ERR(regmap);
  101475. + return ret;
  101476. + }
  101477. +
  101478. + return pcm512x_probe(&spi->dev, regmap);
  101479. +}
  101480. +
  101481. +static int pcm512x_spi_remove(struct spi_device *spi)
  101482. +{
  101483. + pcm512x_remove(&spi->dev);
  101484. + return 0;
  101485. +}
  101486. +
  101487. +static const struct spi_device_id pcm512x_spi_id[] = {
  101488. + { "pcm5121", },
  101489. + { "pcm5122", },
  101490. + { },
  101491. +};
  101492. +MODULE_DEVICE_TABLE(spi, pcm512x_spi_id);
  101493. +
  101494. +static struct spi_driver pcm512x_spi_driver = {
  101495. + .probe = pcm512x_spi_probe,
  101496. + .remove = pcm512x_spi_remove,
  101497. + .id_table = pcm512x_spi_id,
  101498. + .driver = {
  101499. + .name = "pcm512x",
  101500. + .owner = THIS_MODULE,
  101501. + .of_match_table = pcm512x_of_match,
  101502. + .pm = &pcm512x_pm_ops,
  101503. + },
  101504. +};
  101505. +#endif
  101506. +
  101507. +static int __init pcm512x_modinit(void)
  101508. +{
  101509. + int ret = 0;
  101510. +
  101511. +#if IS_ENABLED(CONFIG_I2C)
  101512. + ret = i2c_add_driver(&pcm512x_i2c_driver);
  101513. + if (ret) {
  101514. + printk(KERN_ERR "Failed to register pcm512x I2C driver: %d\n",
  101515. + ret);
  101516. + }
  101517. +#endif
  101518. +#if defined(CONFIG_SPI_MASTER)
  101519. + ret = spi_register_driver(&pcm512x_spi_driver);
  101520. + if (ret != 0) {
  101521. + printk(KERN_ERR "Failed to register pcm512x SPI driver: %d\n",
  101522. + ret);
  101523. + }
  101524. +#endif
  101525. + return ret;
  101526. +}
  101527. +module_init(pcm512x_modinit);
  101528. +
  101529. +static void __exit pcm512x_exit(void)
  101530. +{
  101531. +#if IS_ENABLED(CONFIG_I2C)
  101532. + i2c_del_driver(&pcm512x_i2c_driver);
  101533. +#endif
  101534. +#if defined(CONFIG_SPI_MASTER)
  101535. + spi_unregister_driver(&pcm512x_spi_driver);
  101536. +#endif
  101537. +}
  101538. +module_exit(pcm512x_exit);
  101539. +
  101540. +MODULE_DESCRIPTION("ASoC PCM512x codec driver");
  101541. +MODULE_AUTHOR("Mark Brown <broonie@linaro.org>");
  101542. +MODULE_LICENSE("GPL v2");
  101543. diff -Nur linux-3.10.37/sound/soc/codecs/pcm512x.h linux-rpi/sound/soc/codecs/pcm512x.h
  101544. --- linux-3.10.37/sound/soc/codecs/pcm512x.h 1970-01-01 01:00:00.000000000 +0100
  101545. +++ linux-rpi/sound/soc/codecs/pcm512x.h 2014-04-24 15:35:05.493580478 +0200
  101546. @@ -0,0 +1,142 @@
  101547. +/*
  101548. + * Driver for the PCM512x CODECs
  101549. + *
  101550. + * Author: Mark Brown <broonie@linaro.org>
  101551. + * Copyright 2014 Linaro Ltd
  101552. + *
  101553. + * This program is free software; you can redistribute it and/or
  101554. + * modify it under the terms of the GNU General Public License
  101555. + * version 2 as published by the Free Software Foundation.
  101556. + *
  101557. + * This program is distributed in the hope that it will be useful, but
  101558. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  101559. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  101560. + * General Public License for more details.
  101561. + */
  101562. +
  101563. +#ifndef _SND_SOC_PCM512X
  101564. +#define _SND_SOC_PCM512X
  101565. +
  101566. +#define PCM512x_PAGE_0_BASE 0
  101567. +
  101568. +#define PCM512x_PAGE 0
  101569. +
  101570. +#define PCM512x_RESET (PCM512x_PAGE_0_BASE + 1)
  101571. +#define PCM512x_POWER (PCM512x_PAGE_0_BASE + 2)
  101572. +#define PCM512x_MUTE (PCM512x_PAGE_0_BASE + 3)
  101573. +#define PCM512x_PLL_EN (PCM512x_PAGE_0_BASE + 4)
  101574. +#define PCM512x_SPI_MISO_FUNCTION (PCM512x_PAGE_0_BASE + 6)
  101575. +#define PCM512x_DSP (PCM512x_PAGE_0_BASE + 7)
  101576. +#define PCM512x_GPIO_EN (PCM512x_PAGE_0_BASE + 8)
  101577. +#define PCM512x_BCLK_LRCLK_CFG (PCM512x_PAGE_0_BASE + 9)
  101578. +#define PCM512x_DSP_GPIO_INPUT (PCM512x_PAGE_0_BASE + 10)
  101579. +#define PCM512x_MASTER_MODE (PCM512x_PAGE_0_BASE + 12)
  101580. +#define PCM512x_PLL_REF (PCM512x_PAGE_0_BASE + 13)
  101581. +#define PCM512x_PLL_COEFF_0 (PCM512x_PAGE_0_BASE + 20)
  101582. +#define PCM512x_PLL_COEFF_1 (PCM512x_PAGE_0_BASE + 21)
  101583. +#define PCM512x_PLL_COEFF_2 (PCM512x_PAGE_0_BASE + 22)
  101584. +#define PCM512x_PLL_COEFF_3 (PCM512x_PAGE_0_BASE + 23)
  101585. +#define PCM512x_PLL_COEFF_4 (PCM512x_PAGE_0_BASE + 24)
  101586. +#define PCM512x_DSP_CLKDIV (PCM512x_PAGE_0_BASE + 27)
  101587. +#define PCM512x_DAC_CLKDIV (PCM512x_PAGE_0_BASE + 28)
  101588. +#define PCM512x_NCP_CLKDIV (PCM512x_PAGE_0_BASE + 29)
  101589. +#define PCM512x_OSR_CLKDIV (PCM512x_PAGE_0_BASE + 30)
  101590. +#define PCM512x_MASTER_CLKDIV_1 (PCM512x_PAGE_0_BASE + 32)
  101591. +#define PCM512x_MASTER_CLKDIV_2 (PCM512x_PAGE_0_BASE + 33)
  101592. +#define PCM512x_FS_SPEED_MODE (PCM512x_PAGE_0_BASE + 34)
  101593. +#define PCM512x_IDAC_1 (PCM512x_PAGE_0_BASE + 35)
  101594. +#define PCM512x_IDAC_2 (PCM512x_PAGE_0_BASE + 36)
  101595. +#define PCM512x_ERROR_DETECT (PCM512x_PAGE_0_BASE + 37)
  101596. +#define PCM512x_I2S_1 (PCM512x_PAGE_0_BASE + 40)
  101597. +#define PCM512x_I2S_2 (PCM512x_PAGE_0_BASE + 41)
  101598. +#define PCM512x_DAC_ROUTING (PCM512x_PAGE_0_BASE + 42)
  101599. +#define PCM512x_DSP_PROGRAM (PCM512x_PAGE_0_BASE + 43)
  101600. +#define PCM512x_CLKDET (PCM512x_PAGE_0_BASE + 44)
  101601. +#define PCM512x_AUTO_MUTE (PCM512x_PAGE_0_BASE + 59)
  101602. +#define PCM512x_DIGITAL_VOLUME_1 (PCM512x_PAGE_0_BASE + 60)
  101603. +#define PCM512x_DIGITAL_VOLUME_2 (PCM512x_PAGE_0_BASE + 61)
  101604. +#define PCM512x_DIGITAL_VOLUME_3 (PCM512x_PAGE_0_BASE + 62)
  101605. +#define PCM512x_DIGITAL_MUTE_1 (PCM512x_PAGE_0_BASE + 63)
  101606. +#define PCM512x_DIGITAL_MUTE_2 (PCM512x_PAGE_0_BASE + 64)
  101607. +#define PCM512x_DIGITAL_MUTE_3 (PCM512x_PAGE_0_BASE + 65)
  101608. +#define PCM512x_GPIO_OUTPUT_1 (PCM512x_PAGE_0_BASE + 80)
  101609. +#define PCM512x_GPIO_OUTPUT_2 (PCM512x_PAGE_0_BASE + 81)
  101610. +#define PCM512x_GPIO_OUTPUT_3 (PCM512x_PAGE_0_BASE + 82)
  101611. +#define PCM512x_GPIO_OUTPUT_4 (PCM512x_PAGE_0_BASE + 83)
  101612. +#define PCM512x_GPIO_OUTPUT_5 (PCM512x_PAGE_0_BASE + 84)
  101613. +#define PCM512x_GPIO_OUTPUT_6 (PCM512x_PAGE_0_BASE + 85)
  101614. +#define PCM512x_GPIO_CONTROL_1 (PCM512x_PAGE_0_BASE + 86)
  101615. +#define PCM512x_GPIO_CONTROL_2 (PCM512x_PAGE_0_BASE + 87)
  101616. +#define PCM512x_OVERFLOW (PCM512x_PAGE_0_BASE + 90)
  101617. +#define PCM512x_RATE_DET_1 (PCM512x_PAGE_0_BASE + 91)
  101618. +#define PCM512x_RATE_DET_2 (PCM512x_PAGE_0_BASE + 92)
  101619. +#define PCM512x_RATE_DET_3 (PCM512x_PAGE_0_BASE + 93)
  101620. +#define PCM512x_RATE_DET_4 (PCM512x_PAGE_0_BASE + 94)
  101621. +#define PCM512x_ANALOG_MUTE_DET (PCM512x_PAGE_0_BASE + 108)
  101622. +#define PCM512x_GPIN (PCM512x_PAGE_0_BASE + 119)
  101623. +#define PCM512x_DIGITAL_MUTE_DET (PCM512x_PAGE_0_BASE + 120)
  101624. +
  101625. +#define PCM512x_MAX_REGISTER (PCM512x_PAGE_0_BASE + 120)
  101626. +
  101627. +/* Page 0, Register 1 - reset */
  101628. +#define PCM512x_RSTR (1 << 0)
  101629. +#define PCM512x_RSTM (1 << 4)
  101630. +
  101631. +/* Page 0, Register 2 - power */
  101632. +#define PCM512x_RQPD (1 << 0)
  101633. +#define PCM512x_RQPD_SHIFT 0
  101634. +#define PCM512x_RQST (1 << 4)
  101635. +#define PCM512x_RQST_SHIFT 4
  101636. +
  101637. +/* Page 0, Register 3 - mute */
  101638. +#define PCM512x_RQMR_SHIFT 0
  101639. +#define PCM512x_RQML_SHIFT 4
  101640. +
  101641. +/* Page 0, Register 4 - PLL */
  101642. +#define PCM512x_PLCE (1 << 0)
  101643. +#define PCM512x_RLCE_SHIFT 0
  101644. +#define PCM512x_PLCK (1 << 4)
  101645. +#define PCM512x_PLCK_SHIFT 4
  101646. +
  101647. +/* Page 0, Register 7 - DSP */
  101648. +#define PCM512x_SDSL (1 << 0)
  101649. +#define PCM512x_SDSL_SHIFT 0
  101650. +#define PCM512x_DEMP (1 << 4)
  101651. +#define PCM512x_DEMP_SHIFT 4
  101652. +
  101653. +/* Page 0, Register 13 - PLL reference */
  101654. +#define PCM512x_SREF (1 << 4)
  101655. +
  101656. +/* Page 0, Register 37 - Error detection */
  101657. +#define PCM512x_IPLK (1 << 0)
  101658. +#define PCM512x_DCAS (1 << 1)
  101659. +#define PCM512x_IDCM (1 << 2)
  101660. +#define PCM512x_IDCH (1 << 3)
  101661. +#define PCM512x_IDSK (1 << 4)
  101662. +#define PCM512x_IDBK (1 << 5)
  101663. +#define PCM512x_IDFS (1 << 6)
  101664. +
  101665. +/* Page 0, Register 42 - DAC routing */
  101666. +#define PCM512x_AUPR_SHIFT 0
  101667. +#define PCM512x_AUPL_SHIFT 4
  101668. +
  101669. +/* Page 0, Register 59 - auto mute */
  101670. +#define PCM512x_ATMR_SHIFT 0
  101671. +#define PCM512x_ATML_SHIFT 4
  101672. +
  101673. +/* Page 0, Register 63 - ramp rates */
  101674. +#define PCM512x_VNDF_SHIFT 6
  101675. +#define PCM512x_VNDS_SHIFT 4
  101676. +#define PCM512x_VNUF_SHIFT 2
  101677. +#define PCM512x_VNUS_SHIFT 0
  101678. +
  101679. +/* Page 0, Register 64 - emergency ramp rates */
  101680. +#define PCM512x_VEDF_SHIFT 6
  101681. +#define PCM512x_VEDS_SHIFT 4
  101682. +
  101683. +/* Page 0, Register 65 - Digital mute enables */
  101684. +#define PCM512x_ACTL_SHIFT 2
  101685. +#define PCM512x_AMLE_SHIFT 1
  101686. +#define PCM512x_AMLR_SHIFT 0
  101687. +
  101688. +#endif
  101689. diff -Nur linux-3.10.37/sound/soc/codecs/wm8804.c linux-rpi/sound/soc/codecs/wm8804.c
  101690. --- linux-3.10.37/sound/soc/codecs/wm8804.c 2014-04-14 15:42:31.000000000 +0200
  101691. +++ linux-rpi/sound/soc/codecs/wm8804.c 2014-04-24 15:35:05.509580657 +0200
  101692. @@ -63,6 +63,7 @@
  101693. struct regmap *regmap;
  101694. struct regulator_bulk_data supplies[WM8804_NUM_SUPPLIES];
  101695. struct notifier_block disable_nb[WM8804_NUM_SUPPLIES];
  101696. + int mclk_div;
  101697. };
  101698. static int txsrc_get(struct snd_kcontrol *kcontrol,
  101699. @@ -277,6 +278,7 @@
  101700. blen = 0x1;
  101701. break;
  101702. case SNDRV_PCM_FORMAT_S24_LE:
  101703. + case SNDRV_PCM_FORMAT_S32_LE:
  101704. blen = 0x2;
  101705. break;
  101706. default:
  101707. @@ -318,7 +320,7 @@
  101708. #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
  101709. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  101710. - unsigned int source)
  101711. + unsigned int source, unsigned int mclk_div)
  101712. {
  101713. u64 Kpart;
  101714. unsigned long int K, Ndiv, Nmod, tmp;
  101715. @@ -330,7 +332,8 @@
  101716. */
  101717. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  101718. tmp = target * post_table[i].div;
  101719. - if (tmp >= 90000000 && tmp <= 100000000) {
  101720. + if ((tmp >= 90000000 && tmp <= 100000000) &&
  101721. + (mclk_div == post_table[i].mclkdiv)) {
  101722. pll_div->freqmode = post_table[i].freqmode;
  101723. pll_div->mclkdiv = post_table[i].mclkdiv;
  101724. target *= post_table[i].div;
  101725. @@ -387,8 +390,11 @@
  101726. } else {
  101727. int ret;
  101728. struct pll_div pll_div;
  101729. + struct wm8804_priv *wm8804;
  101730. - ret = pll_factors(&pll_div, freq_out, freq_in);
  101731. + wm8804 = snd_soc_codec_get_drvdata(codec);
  101732. +
  101733. + ret = pll_factors(&pll_div, freq_out, freq_in, wm8804->mclk_div);
  101734. if (ret)
  101735. return ret;
  101736. @@ -452,6 +458,7 @@
  101737. int div_id, int div)
  101738. {
  101739. struct snd_soc_codec *codec;
  101740. + struct wm8804_priv *wm8804;
  101741. codec = dai->codec;
  101742. switch (div_id) {
  101743. @@ -459,6 +466,10 @@
  101744. snd_soc_update_bits(codec, WM8804_PLL5, 0x30,
  101745. (div & 0x3) << 4);
  101746. break;
  101747. + case WM8804_MCLK_DIV:
  101748. + wm8804 = snd_soc_codec_get_drvdata(codec);
  101749. + wm8804->mclk_div = div;
  101750. + break;
  101751. default:
  101752. dev_err(dai->dev, "Unknown clock divider: %d\n", div_id);
  101753. return -EINVAL;
  101754. @@ -641,7 +652,7 @@
  101755. };
  101756. #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  101757. - SNDRV_PCM_FMTBIT_S24_LE)
  101758. + SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  101759. #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  101760. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  101761. @@ -674,7 +685,7 @@
  101762. .suspend = wm8804_suspend,
  101763. .resume = wm8804_resume,
  101764. .set_bias_level = wm8804_set_bias_level,
  101765. - .idle_bias_off = true,
  101766. + .idle_bias_off = false,
  101767. .controls = wm8804_snd_controls,
  101768. .num_controls = ARRAY_SIZE(wm8804_snd_controls),
  101769. diff -Nur linux-3.10.37/sound/soc/codecs/wm8804.h linux-rpi/sound/soc/codecs/wm8804.h
  101770. --- linux-3.10.37/sound/soc/codecs/wm8804.h 2014-04-14 15:42:31.000000000 +0200
  101771. +++ linux-rpi/sound/soc/codecs/wm8804.h 2014-04-24 15:35:05.509580657 +0200
  101772. @@ -57,5 +57,9 @@
  101773. #define WM8804_CLKOUT_SRC_OSCCLK 4
  101774. #define WM8804_CLKOUT_DIV 1
  101775. +#define WM8804_MCLK_DIV 2
  101776. +
  101777. +#define WM8804_MCLKDIV_256FS 0
  101778. +#define WM8804_MCLKDIV_128FS 1
  101779. #endif /* _WM8804_H */
  101780. diff -Nur linux-3.10.37/sound/soc/Kconfig linux-rpi/sound/soc/Kconfig
  101781. --- linux-3.10.37/sound/soc/Kconfig 2014-04-14 15:42:31.000000000 +0200
  101782. +++ linux-rpi/sound/soc/Kconfig 2014-04-24 15:35:05.477580300 +0200
  101783. @@ -36,6 +36,7 @@
  101784. # All the supported SoCs
  101785. source "sound/soc/atmel/Kconfig"
  101786. source "sound/soc/au1x/Kconfig"
  101787. +source "sound/soc/bcm/Kconfig"
  101788. source "sound/soc/blackfin/Kconfig"
  101789. source "sound/soc/cirrus/Kconfig"
  101790. source "sound/soc/davinci/Kconfig"
  101791. diff -Nur linux-3.10.37/sound/soc/Makefile linux-rpi/sound/soc/Makefile
  101792. --- linux-3.10.37/sound/soc/Makefile 2014-04-14 15:42:31.000000000 +0200
  101793. +++ linux-rpi/sound/soc/Makefile 2014-04-24 15:35:05.477580300 +0200
  101794. @@ -14,6 +14,7 @@
  101795. obj-$(CONFIG_SND_SOC) += generic/
  101796. obj-$(CONFIG_SND_SOC) += atmel/
  101797. obj-$(CONFIG_SND_SOC) += au1x/
  101798. +obj-$(CONFIG_SND_SOC) += bcm/
  101799. obj-$(CONFIG_SND_SOC) += blackfin/
  101800. obj-$(CONFIG_SND_SOC) += cirrus/
  101801. obj-$(CONFIG_SND_SOC) += davinci/
  101802. diff -Nur linux-3.10.37/sound/soc/soc-core.c linux-rpi/sound/soc/soc-core.c
  101803. --- linux-3.10.37/sound/soc/soc-core.c 2014-04-14 15:42:31.000000000 +0200
  101804. +++ linux-rpi/sound/soc/soc-core.c 2014-04-24 15:35:05.545581058 +0200
  101805. @@ -2925,8 +2925,8 @@
  101806. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  101807. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  101808. - uinfo->value.integer.min = 0;
  101809. - uinfo->value.integer.max = platform_max - min;
  101810. + uinfo->value.integer.min = min;
  101811. + uinfo->value.integer.max = platform_max;
  101812. return 0;
  101813. }
  101814. @@ -2957,9 +2957,10 @@
  101815. unsigned int val, val_mask;
  101816. int ret;
  101817. - val = ((ucontrol->value.integer.value[0] + min) & mask);
  101818. if (invert)
  101819. - val = max - val;
  101820. + val = ((max - ucontrol->value.integer.value[0] + min) & mask);
  101821. + else
  101822. + val = (ucontrol->value.integer.value[0] & mask);
  101823. val_mask = mask << shift;
  101824. val = val << shift;
  101825. @@ -2968,9 +2969,10 @@
  101826. return ret;
  101827. if (snd_soc_volsw_is_stereo(mc)) {
  101828. - val = ((ucontrol->value.integer.value[1] + min) & mask);
  101829. if (invert)
  101830. - val = max - val;
  101831. + val = ((max - ucontrol->value.integer.value[1] + min) & mask);
  101832. + else
  101833. + val = (ucontrol->value.integer.value[1] & mask);
  101834. val_mask = mask << shift;
  101835. val = val << shift;
  101836. @@ -3008,18 +3010,14 @@
  101837. (snd_soc_read(codec, reg) >> shift) & mask;
  101838. if (invert)
  101839. ucontrol->value.integer.value[0] =
  101840. - max - ucontrol->value.integer.value[0];
  101841. - ucontrol->value.integer.value[0] =
  101842. - ucontrol->value.integer.value[0] - min;
  101843. + max - ucontrol->value.integer.value[0] + min;
  101844. if (snd_soc_volsw_is_stereo(mc)) {
  101845. ucontrol->value.integer.value[1] =
  101846. (snd_soc_read(codec, rreg) >> shift) & mask;
  101847. if (invert)
  101848. ucontrol->value.integer.value[1] =
  101849. - max - ucontrol->value.integer.value[1];
  101850. - ucontrol->value.integer.value[1] =
  101851. - ucontrol->value.integer.value[1] - min;
  101852. + max - ucontrol->value.integer.value[1] + min;
  101853. }
  101854. return 0;
  101855. @@ -3463,6 +3461,22 @@
  101856. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  101857. /**
  101858. + * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
  101859. + * @dai: DAI
  101860. + * @ratio Ratio of BCLK to Sample rate.
  101861. + *
  101862. + * Configures the DAI for a preset BCLK to sample rate ratio.
  101863. + */
  101864. +int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  101865. +{
  101866. + if (dai->driver && dai->driver->ops->set_bclk_ratio)
  101867. + return dai->driver->ops->set_bclk_ratio(dai, ratio);
  101868. + else
  101869. + return -EINVAL;
  101870. +}
  101871. +EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
  101872. +
  101873. +/**
  101874. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  101875. * @dai: DAI
  101876. * @fmt: SND_SOC_DAIFMT_ format value.